util: Various Utilities for dbling

common.util.validate_crx_id(crx_id)[source]

Validate the given CRX ID.

Check that the Chrome extension ID has three important properties:

  1. It must be a string
  2. It must have alpha characters only (strictly speaking, these should be lowercase and only from a-p, but checking for this is a little overboard)
  3. It must be 32 characters long
Parameters:crx_id (str) – The ID to validate.
Raises:MalformedExtID – When the ID doesn’t meet the criteria listed above.
exception common.util.MalformedExtId[source]

Raised when an ID doesn’t have the correct form.

common.util.get_crx_version(crx_path)[source]

Extract and return the version number from the CRX’s path.

The return value from the download() function is in the form: <extension ID>_<version>.crx.

The <version> part of that format is “x_y_z” for version “x.y.z”. To convert to the latter, we need to 1) get the basename of the path, 2) take off the trailing ”.crx”, 3) remove the extension ID and ‘_’ after it, and 4) replace all occurrences of ‘_’ with ‘.’.

Parameters:crx_path (str) – The full path to the downloaded CRX, as returned by the download() function.
Returns:The version number in the form “x.y.z”.
Return type:str
common.util.get_id_version(crx_path)[source]

From the path to a CRX, extract and return the ID and version as strings.

Parameters:crx_path (str) – The full path to the downloaded CRX.
Returns:The ID and version number as a tuple: (id, num)
Return type:tuple(str, str)
common.util.separate_mode_type(mode)[source]

Separate out the values for the mode (permissions) and the file type from the given mode.

Both returned values are integers. The mode is just the permissions (usually displayed in the octal format), and the type corresponds to the standard VFS types:

  • 0: Unknown file
  • 1: Regular file
  • 2: Directory
  • 3: Character device
  • 4: Block device
  • 5: Named pipe (identified by the Python stat library as a FIFO)
  • 6: Socket
  • 7: Symbolic link
Parameters:mode (int) – The mode value to be separated.
Returns:Tuple of ints in the form: (mode, type)
Return type:tuple(int, int)
common.util.calc_chrome_version(last_version, release_date, release_period=10)[source]

Calculate the most likely version number of Chrome.

The calculation is based on the last known version number and its release date, based on the number of weeks (release_period) it usually takes to release the next major version. A list of releases and their dates is available on Wikipedia.

Parameters:
  • last_version (str) – Last known version number, e.g. “43.0”. Should only have the major and minor version numbers and exclude the build and patch numbers.
  • release_date (list) – Release date of the last known version number. Must be a list of three integers: [YYYY, MM, DD].
  • release_period (int) – Typical number of weeks between releases.
Returns:

The most likely current version number of Chrome in the same format required of the last_version parameter.

Return type:

str

common.util.make_download_headers()[source]

Return a dict of headers to use when downloading a CRX.

Returns:Set of HTTP headers as a dict, where the key is the header type and the value is the header content.
Return type:dict[str, str]
common.util.dt_dict_now()[source]

Return a dict of the current time.

Returns:A dict with the following keys:
  • year
  • month
  • day
  • hour
  • minute
  • second
  • microsecond
Return type:dict[str, int]
common.util.dict_to_dt(dt_dict)[source]

Reverse of dt_dict_now().

Parameters:dt_dict (dict) – A dict (such as dt_dict_now() returns) that correspond with the keyword parameters of the datetime constructor.
Returns:A datetime object.
Return type:datetime.datetime
class common.util.MunchyMunch(f)[source]

Wrapper class to munchify crx_obj parameters.

This wrapper converts either the kwarg crx_obj or the first positional argument (tests in that order) to a Munch object, which allows us to refer to keys in the Munch dictionary as if they were attributes. See the docs on the munch library for more information.

Example usage:

>>> @MunchyMunch
... def test_func(crx_obj)
...     # crx_obj will be converted to a Munch
...     print(crx_obj.id)
Parameters:f – The function to wrap.
common.util.byte_len(s)[source]

Return the length of s in number of bytes.

Parameters:s (str or bytes) – The string or bytes object to test.
Returns:The length of s in bytes.
Return type:int
Raises:TypeError – If s is not a str or bytes.
common.util.ttl_files_in_dir(dir_path, pat='.')[source]

Count the files in the given directory.

Will count all files except . and .., including any files whose names begin with . (using the -A option of ls).

Parameters:
  • dir_path (str) – Path to the directory.
  • pat (str) – Pattern the files should match when searching. This is passed to grep, so when the default remains (.), it will match all files and thus not filter out anything.
Returns:

The number of files in the directory.

Return type:

int

Raises:

NotADirectoryError – When dir_path is not a directory.

common.util.chunkify(iterable, chunk_size)[source]

Split an iterable into smaller iterables of a certain size (chunk size).

For example, say you have a list that, for whatever reason, you don’t want to process all at once. You can use chunkify() to easily split up the list to whatever size of chunk you want. Here’s an example of what this might look like:

>>> my_list = range(1, 6)
>>> for sublist in chunkify(my_list, 2):
...     for i in sublist:
...         print(i, end=', ')
...     print()

The output of the above code would be:

1, 2,
3, 4,
5,

Idea borrowed from http://code.activestate.com/recipes/303279-getting-items-in-batches/.

Parameters:
  • iterable – The iterable to be split into chunks.
  • chunk_size (int) – Size of each chunk. See above for an example.