Library

Api

class ffflash.lib.api.FFApi(content)[source]

Helper class provide some easy way to access and modify dictionaries. It only provides reading and replacing already existing keys.

Parameters:content – The initial data to work with
pretty()[source]
Return str:current content in a human readable way using pprint.pformat
pull(*fields)[source]

Retrieve contents from deep down somewhere in the dictionary.

Parameters:fields – one or more key names to retrieve
push(value, *fields)[source]

Replace contents deeply inside the dictionary, if the key already exists.

Parameters:
  • value – the actual data to be written
  • fields – one or more key names where to write value

Args

ffflash.lib.args.parsed_args(argv=None)[source]

Parse arguments from commandline

Parameters:argv – List of Arguments to parse. - If omitted sys.argv is used
Return Namespace:
 arguments from ArgumentParser for argv

Clock

ffflash.lib.clock.get_iso_timestamp(dt=None)[source]

Generate iso timestrings

Parameters:dt – custom datetime object, or now() if None
Return str:iso representation of dt

Files

ffflash.lib.files.dump_file(location, content, as_yaml=False)[source]

Pickle either json or yaml into a file

Parameters:
  • location – path where to pickle into
  • content – data to store
  • as_yaml – output as yaml instead of json
ffflash.lib.files.load_file(location, fallback=None, as_yaml=False)[source]

Unpickle either json or yaml from a file

Parameters:
  • location – path where to unpickle from
  • fallback – data to return in case of unpickle failure
  • as_yaml – read as yaml instead of json
Returns:

unpickled data from location

ffflash.lib.files.read_file(location, fallback=None)[source]

Read string data from files

Parameters:
  • location – filename where to write to
  • fallback – data to return in case of read failure
Returns:

read data from location if successful else fallback

ffflash.lib.files.write_file(location, data)[source]

Write string data into files

Parameters:
  • location – filename where to write to
  • data – content to write into filename
Returns:

data if successful

Locations

ffflash.lib.locations.check_file_extension(location, *extensions)[source]

Validate path for a selection of extensions.

Parameters:
  • location – path to check
  • extensions – one or more extensions the location should end with
Return tuple:

(basename of location, extension of location) or (None, None) if extension did not match

ffflash.lib.locations.check_file_location(location, must_exist=False)[source]

Validate path for a file.

Checks for the parent folder to exist, and that location itself is not a folder. Optionally, if location is an already existing file.

Parameters:
  • location – path to check
  • must_exist – check also if location really exists and is a file
Return str:

validated path of location if all above conditions are met or None

ffflash.lib.locations.get_basedir()[source]

Fancy helper to find project’s basedir. Use locate_file() to reach into the package folder.

Returns:full absolute path to ffflash‘s basedir
ffflash.lib.locations.locate_file(*parts, must_exist=False)[source]

Find files inside :meth:get_basedir.

Parameters:
  • parts – trail to your file. e.g. bla/fasel/blubb would be 'bla', 'fasel', 'blubb'
  • must_exist – check if located file really exists and is a file see ffflash.lib.files.check_file_location() for more
Returns:

full absolute path to desired file, or None on error

Remote

ffflash.lib.remote.fetch_www(url, fallback=None, timeout=5)[source]

Contextmanager to retrieve content from the web

Parameters:
  • url – URL to fetch
  • fallback – what to return instead in case of error
  • timeout – timeout to pass to urllib.request
Yield str:

fetched result as unicode string, or fallback

ffflash.lib.remote.fetch_www_struct(url, fallback=None, timeout=5, as_yaml=False)[source]

Helper to unpickle either json or yaml from fetched files

Parameters:
  • url – URL to fetch
  • fallback – what to return in case of (fetch or unpickle) error
  • timeout – timeout to pass down to fetch_www()
  • as_yaml – load content as yaml instead of json
Returns:

unpickled data from url

Struct

ffflash.lib.struct.dump_struct(content, as_yaml=False)[source]

Contextmanager to pickle either json or yaml into a string

Parameters:
  • content – data to pickle
  • as_yaml – output as yaml instead of json
Yield str:

pickled content

ffflash.lib.struct.load_struct(content, fallback=None, as_yaml=False)[source]

Contextmanager to unpickle either json or yaml from a string

Parameters:
  • content – string to unpickle
  • fallback – data to return in case of unpickle failure
  • as_yaml – read as yaml instead of json
Yield:

unpickled content

ffflash.lib.struct.merge_dicts(first, second)[source]

Merge nested dictionaries deeply

Parameters:
  • first – Source dictionary
  • second – Dictionary to merge into first
Return dict:

merged dictionaries

Text

ffflash.lib.text.make_pretty(data)[source]
Parameters:data – ugly data
Return str:pretty data formatted using pprint.pformat, or None if data was too ugly
ffflash.lib.text.replace_text(rx, replacement, text)[source]

Replace text if rx matches.

Parameters:
  • rx – regex to match on text
  • replacement – content to put into text on rx match
  • text – content to work on
Return str:

text with replaced parts, or unchanged text

ffflash.lib.text.search_text(rx, text)[source]

Safe search text with regex.

Parameters:
  • rx – regex to match on text
  • text – content to work on
Returns:

either None if rx is not in text or match-object of re.