waptutils module

class waptutils.BaseObjectClass[source]

Bases: object

class waptutils.FileChunks(filename, chunk_size=2097152, progress_hook=None)[source]

Bases: object

close()[source]
get()[source]
class waptutils.Version(version, members_count=None)[source]

Bases: object

Version object of form 0.0.0 can compare with respect to natural numbering and not alphabetical

>>> Version('0.10.2') > Version('0.2.5')
True
>>> Version('0.1.2') < Version('0.2.5')
True
>>> Version('0.1.2') == Version('0.1.2')
True
>>> Version('7') < Version('7.1')
True
waptutils.all_files(rootdir, pattern=None)[source]

Recursively return all files from rootdir and sub directories matching the (dos style) pattern (example: *.exe)

waptutils.create_recursive_zip(zipfn, source_root, target_root=u'', excludes=[u'.svn', u'.git', u'.gitignore', u'*.pyc', u'*.dbg', u'src'], excludes_full=['WAPT\\manifest.sha256', 'WAPT\\manifest.sha1', 'WAPT\\signature'])[source]

Create a zip file with filename zipf from source_root directory with target_root as new root. Don’t include file which match excludes file pattern

Args;
zipfn (str) : filename for zip file to create source_root (str) : root directory of filetree to zip target_root (str) ! root directory for all in zip file excludes (list) : list of glob pattern of files to excludes excludes_full (list) : full relative filepath of files to exclude
Returns:list of zipped filepath
Return type:list
waptutils.dateof(adatetime)[source]
waptutils.datetime2isodate(adatetime=None)[source]
waptutils.default_http_headers()[source]
waptutils.default_json(o)[source]

callback to extend handling of json.dumps

waptutils.ensure_list(csv_or_list, ignore_empty_args=True, allow_none=False)[source]

if argument is not a list, return a list from a csv string

waptutils.ensure_unicode(data)[source]

Return a unicode string from data object It is sometimes difficult to know in advance what we will get from command line application output.

This is to ensure we get a (not always accurate) representation of the data mainly for logging purpose.

Parameters:data – either str or unicode or object having a __unicode__ or WindowsError or Exception
Returns:unicode string representing the data
Return type:unicode
>>> ensure_unicode(str('éé'))
u'éé'
>>> ensure_unicode(u'éé')
u'éé'
>>> ensure_unicode(Exception("test"))
u'Exception: test'
>>> ensure_unicode(Exception())
u'Exception: '
waptutils.expand_args(args)[source]

Return list of unicode file paths expanded from wildcard list args

waptutils.fileisodate(filename)[source]
waptutils.fileisoutcdate(filename)[source]
waptutils.fileutcdate(filename)[source]
waptutils.find_all_files(rootdir, include_patterns=None, exclude_patterns=None)[source]

Generator which recursively find all files from rootdir and sub directories matching the (dos style) patterns (example: *.exe)

Args;

rootdir (str): root dir where to start looking for files include_patterns (str or list) : list of glob pattern of files to return exclude_patterns (str or list) : list of glob pattern of files to exclude

(if a file is both in include and exclude, it is excluded)
>>> for fn in find_all_files('c:\tmp','*.txt'):
        print(fn)
>>>
waptutils.force_utf8_no_bom(filename)[source]

Check if the file is encoded in utf8 readable encoding without BOM rewrite the file in place if not compliant.

waptutils.format_bytes(bytes)[source]
waptutils.generate_unique_string()[source]
waptutils.get_disk_free_space(filepath)[source]

Returns the number of free bytes on the drive that filepath is on

waptutils.hours_minutes(hours)[source]
waptutils.html_table(cur, callback=None)[source]

cur est un cursor issu d’une requete callback est une fonction qui prend (rowmap,fieldname,value) et renvoie une representation texte

waptutils.http_resource_datetime(url, proxies=None, timeout=2, auth=None, verify_cert=False, cert=None)[source]

Try to get header for the supplied URL, returns None if no answer within the specified timeout

Parameters:
  • url (str) – URL to document
  • proxies (dict) – proxies to use. eg {‘http’:’http://wpad:3128’,’https’:’http://wpad:3128’}
  • timeout (int) – seconds to wait for answer before giving up
  • auth (list) – (user,password) to authenticate wirh basic auth
  • verify_cert (bool or str) – either False, True (verify with embedded CA list), or path to a directory or PEM encoded CA bundle file to check https certificate signature against.
  • cert (list) – pair of (x509certfilename,pemkeyfilename) for authenticating the client
Returns:

last-modified date of document on server

Return type:

datetime

waptutils.http_resource_isodatetime(url, proxies=None, timeout=2, auth=None, verify_cert=False, cert=None)[source]
waptutils.httpdatetime2datetime(httpdate)[source]

convert a date string as returned in http headers or mail headers to isodate

>>> import requests
>>> last_modified = requests.head('http://wapt/wapt/Packages',headers={'cache-control':'no-cache','pragma':'no-cache'}).headers['last-modified']
>>> len(httpdatetime2isodate(last_modified)) == 19
True
waptutils.httpdatetime2isodate(httpdate)[source]

convert a date string as returned in http headers or mail headers to isodate

>>> import requests
>>> last_modified = requests.head('http://wapt/wapt/Packages',headers={'cache-control':'no-cache','pragma':'no-cache'}).headers['last-modified']
>>> len(httpdatetime2isodate(last_modified)) == 19
True
waptutils.httpdatetime2time(httpdate)[source]

convert a date string as returned in http headers or mail headers to isodate

>>> import requests
>>> last_modified = requests.head('http://wapt/wapt/Packages',headers={'cache-control':'no-cache','pragma':'no-cache'}).headers['last-modified']
>>> len(httpdatetime2isodate(last_modified)) == 19
True
waptutils.import_code(code, name='', add_to_sys_modules=0)[source]

Import dynamically generated code as a module. code is the object containing the code (a string, a file handle or an actual compiled code object, same types as accepted by an exec statement). The name is the name to give to the module, and the final argument says wheter to add it to sys.modules or not. If it is added, a subsequent import statement using name will return this module. If it is not added to sys.modules import will try to load it in the normal fashion.

import foo

is equivalent to

foofile = open(“/path/to/foo.py”) foo = import_code(foofile,”foo”,1)

Returns a newly generated module. From : http://code.activestate.com/recipes/82234-importing-a-dynamically-generated-module/

Parameters:
  • code (str) – python code to load as a module
  • name (str) – import code as module name
  • add_to_sys_modules (bool) – True if module must be globally available as a sys module
Returns:

module object

Return type:

module

waptutils.import_setup(setupfilename, modulename='')[source]

Import setupfilename as modulename, return the module object

Parameters:setupfilename (str) – path to module
Returns:loaded module
Return type:module
waptutils.isodate2datetime(isodatestr)[source]
waptutils.jsondump(o, **kwargs)[source]

Dump argument to json format, including datetime and customized classes with as_dict or as_json callables >>> class MyClass(object): … def as_dict(self): … return {‘test’:’a’,’adate2’:datetime.date(2014,03,15)} >>> jsondump({‘adate’:datetime.date(2014,03,14),’an_object’:MyClass()}) ‘{“adate”: “2014-03-14”, “an_object”: {“test”: “a”, “adate2”: “2014-03-15”}}’

waptutils.merge_dict(d1, d2)[source]

merge similar dict

waptutils.ppdicttable(alist, columns=[], callback=None)[source]

pretty print a list of dict as a table columns is an ordered list of (fieldname,width) callback is a function called for each field (fieldname,value) to format the output

waptutils.pptable(cursor, data=None, rowlens=0, callback=None)[source]

pretty print a query result as a table callback is a function called for each field (fieldname,value) to format the output

waptutils.remove_encoding_declaration(source)[source]
waptutils.setloglevel(logger, loglevel)[source]

set loglevel as string

waptutils.splitThousands(s, tSep=', ', dSep='.')[source]

Splits a general float on thousands. GIGO on general input

waptutils.time2display(adatetime)[source]
waptutils.touch(filename)[source]
waptutils.wget(url, target=None, printhook=None, proxies=None, connect_timeout=10, download_timeout=None, verify_cert=False, referer=None, user_agent=None, cert=None, resume=False, md5=None, cache_dir=None)[source]

Copy the contents of a file from a given URL to a local file.

Parameters:
  • url (str) – URL to document
  • target (str) – full file path of downloaded file. If None, put in a temporary dir with supplied url filename (final part of url)
  • proxies (dict) – proxies to use. eg {‘http’:’http://wpad:3128’,’https’:’http://wpad:3128’}
  • timeout (int) – seconds to wait for answer before giving up
  • auth (list) – (user,password) to authenticate wirh basic auth
  • verify_cert (bool or str) – either False, True (verify with embedded CA list), or path to a directory or PEM encoded CA bundle file to check https certificate signature against.
  • cert (list) – pair of (x509certfilename,pemkeyfilename) for authenticating the client
  • referer (str) –
  • user_agent
  • resume (bool) –
  • md5 (str) –
  • cache_dir (str) – if file exists here, and md5 matches, copy from here instead of downloading. If not, put a copy of the file here after downloading.
Returns:

path to downloaded file

Return type:

str

>>> respath = wget('http://wapt.tranquil.it/wapt/tis-firefox_28.0.0-1_all.wapt','c:\\tmp\\test.wapt',proxies={'http':'http://proxy:3128'})
???
>>> os.stat(respath).st_size>10000
True
>>> respath = wget('http://localhost:8088/runstatus','c:\\tmp\\test.json')
???
waptutils.wgets(url, proxies=None, verify_cert=False, referer=None, user_agent=None, timeout=None)[source]

Return the content of a remote resource as a String with a http get request.

Raise an exception if remote data can’t be retrieved.

Parameters:
  • url (str) – http(s) url
  • proxies (dict) – proxy configuration as requests requires it {‘http’: url, ‘https’:url}
Returns:

content of remote resource

Return type:

str

>>> data = wgets('https://wapt/ping')
>>> "msg" in data
True