Leon Be;mont
Downloads: 31
Posts: 19
Smile Score: -1
About Me
I am a Free member


Visit Stats
Last Visit: 40 days ago
Member Since: 25 Mar 2013
Profile Views: 227

Leon Be;mont's Posts
Average Rating from Leon:
(18)

sort: smiles | time

LeonBe;mont2070 rated on 03 Mar 2014
[Version 1.13.5]




LeonBe;mont2070 rated on 24 Feb 2014
[Version 1.11.10]




LeonBe;mont2070 rated on 23 Feb 2014
[Version 4.2.1.1]




LeonBe;mont2070 rated on 21 Feb 2014
[Version 1.25.0]




LeonBe;mont2070 rated on 21 Feb 2014
[Version 33.0.1750.117]



burypromote

LeonBe;mont2070 reviewed on 20 Feb 2014
I've wrote one:
|#!/usr/bin/env python2.7
|
|from bs4 import BeautifulSoup as bs
|import base64 as b64
|import urllib
|import os
|import re
|import unicodedata
|import inspect
|
|cssContainer = 'n'
|jsContainer = 'n'
|imgSrcContainer = r'data:image/{0};base64,{1}'
|cssUriContainer = r'data:image/png;base64,{0}'
|cssImgPattern = re.compile(r'''url((["']?)(.+?.png)1)''', flags=re.I)
|
|def printpath(path):
| path, lengthList = terminalWidth(path)
| #print lengthList
| if sum(lengthList) > 76:
| total = 0
| for i in xrange(len(lengthList)-1, -1, -1):
| if total < = 73:
| total+=lengthList[i]
| else:
| cutoffset = i+2
| break
| path = u'...' + path[cutoffset:]
| print path
|
|def terminalWidth(string, encoding='utf-8'):
| if str in inspect.getmro(string.__class__):
| string = string.decode(encoding)
| charWidthList = [2 if unicodedata.east_asian_width(i) in ('W', 'F') else 1
| for i in string ]
| return (string,charWidthList)
|
|def get_encoding(soup):
| encod = soup.meta.get('charset')
| if encod == None:
| encod = soup.meta.get('content-type')
| if encod == None:
| content = soup.meta.get('content')
| match = re.search('charset=(.*)', content)
| if match:
| encod = match.group(1)
| else:
| encod = 'utf-8'
| return encod
|
|def embedCSS(soup, rootpath, encoding):
| for e in soup(['style', 'link']):
| try:
| if e.name == 'style':
| if e['type'] == 'text/css':
| path = os.path.join(
| rootpath,urllib.unquote(e['src'].encode(encoding)))
| del e['src']
| #e.extract()
| else:
| continue
| elif e.name == 'link':
| if 'stylesheet' in e.get('rel', '') or
| e.get('type', '') == 'text/css':
| path = os.path.join(
| rootpath, urllib.unquote(e['href'].encode(encoding)))
| del e['href']
| #e.extract()
| else:
| continue
| except KeyError:
| continue
| try:
| cssrootpath = os.path.dirname(path)
| csslines = []
| with open(path) as cssf:
| for line in cssf:
| while True:
| urlpattern = cssImgPattern.search(line)
| if urlpattern is None:
| break
| s, e = urlpattern.regs[2]
| pngpath = os.path.join(cssrootpath,urlpattern.group(2))
| try:
| pngdata = cssUriContainer.format(
| b64.b64encode(open(pngpath, 'rb').read()))
| except IOError:
| pngdata = ''
| line = line[:s] + pngdata + line[e:]
| csslines.append(line)
| printpath(path)
| soup.head.append(bs(cssContainer.format(''.join(csslines))).style)
| except IOError:
| continue
|
|
|def embedJS(soup, rootpath, encoding):
| for e in soup('script'):
| try:
| path = os.path.join(
| rootpath, urllib.unquote(e['src'].encode(encoding)))
| del e['src']
| #e.extract()
| except KeyError:
| continue
| try:
| soup.head.append(
| bs(jsContainer.format(open(path, 'rb').read())).script)
| printpath(path)
| except IOError:
| continue
|
|def embedImage(soup, rootpath, encoding):
| for img in soup.body.findAll('img'):
| imgpath = urllib.unquote(img['src'].encode(encoding))
| imgpath = os.path.join(rootpath, imgpath)
| imgtype = os.path.splitext(imgpath)[1].lstrip('.')
| try:
| imgb64data = b64.b64encode(open(imgpath, 'rb').read())
| except IOError:
| pass
| else:
| img['src'] = imgSrcContainer.format(imgtype, imgb64data)
| printpath(imgpath)
|
|def main(pathOfFile):
| filename, fileext = os.path.splitext(pathOfFile)
| rootpath = os.path.dirname(pathOfFile)
| soup = bs(open(pathOfFile, 'rb').read())
| encoding = get_encoding(soup).lower()
| embedCSS(soup, rootpath, encoding)
| #print soup.body
| embedJS(soup, rootpath, encoding)
| embedImage(soup, rootpath, encoding)
| open(filename+'_single_'+fileext, 'wb').write(soup.prettify(encoding))
|
|if __name__ == '__main__':
| import sys
| htmlfile = sys.argv[1]
| main(htmlfile)
[Version 2.1]



burypromote

LeonBe;mont2070 reviewed on 18 Feb 2014
Here is my solution for uninstalling PKGs implemented in Python.
#!/usr/bin/env python2.7

import subprocess as subp
#import shutil
import sys, os
#import pdb

class Delpkg(object):
pkgInfoCommand = 'pkgutil --pkg-info "{pkgname}"'
pkgFilesCommand = 'pkgutil --files "{pkgname}" --only-files'
pkgDirsCommand = 'pkgutil --files "{pkgname}" --only-dirs'
def __init__(self,pkglist=[]):
self.pkgsInfo = {}
for i in pkglist:
pkgInfo = self.getPkgInfo(i)
if pkgInfo is not None:
self.pkgsInfo[i] = pkgInfo
def __call__(self):
if os.getuid() != 0:
for i in self.listFiles():
print i
for i in self.listDirs():
print i
else:
for i in self.listFiles():
try:
os.remove(i)
except OSError:
sys.stderr.write('Failed to remove "{0}"n'.format(i))
for i in self.listDirs():
try:
os.rmdir(i)
except OSError:
sys.stderr.write('Failed to remove "{0}"n'.format(i))
for i in self.pkgsInfo:
subp.call('pkgutil --forget "{0}"'.format(i),shell=True)

def getPkgInfo(self,pkgname):
runPkgInfoComm = subp.Popen(self.pkgInfoCommand.format(pkgname=pkgname),
shell=True,stdout=subp.PIPE)
out = runPkgInfoComm.communicate()[0]
if runPkgInfoComm.returncode == 0:
pkgInfo = dict(i.split(': ') for i in out.rstrip().split('n'))
pkgInfo['prefixpath'] = os.path.join(pkgInfo['volume'],pkgInfo['location'])
return pkgInfo
else:
return None

def generalListComm(self,command):
for i in self.pkgsInfo:
runComm = subp.Popen(command.format(pkgname=i),
shell=True, stdout = subp.PIPE)
out = runComm.communicate()[0].rstrip().split('n')
for j in out:
yield os.path.join(self.pkgsInfo[i]['prefixpath'],j)
def listDirs(self):
dirs = [ i for i in self.generalListComm(self.pkgDirsCommand) ]
dirs.sort(reverse=True)
for i in dirs:
yield i
def listFiles(self):
for i in self.generalListComm(self.pkgFilesCommand):
yield i

if __name__=='__main__':
delpkg = Delpkg(sys.argv[1:])
#pdb.set_trace()
delpkg()
[Version 1.0.2]




LeonBe;mont2070 rated on 18 Feb 2014
[Version 11.3.3]




LeonBe;mont2070 rated on 30 Nov 2013
[Version 0.135]




LeonBe;mont2070 rated on 30 Nov 2013
[Version 2.8.10]



There are currently no troubleshooting comments by this member.

Displaying 1-10 of 18
1 2 >
Please login or create a new
MacUpdate Member account
to use this feature


- -