Utilities

Note

The documentation in this section is aimed at people wishing to contribute to rdial, and can be skipped if you are simply using the tool from the command line.

Convenience functions and classes

class rdial.utils.AttrDict[source]

Dictionary with attribute access.

See also

dict

__contains__(key)[source]

Check for item membership.

Parameters:key (object) – Key to test for
Returns:True, if item in dictionary
Return type:bool
__delattr__(key)[source]

Support item deletion via dot notation.

Parameters:key (object) – Key to delete
__getattr__(key)[source]

Support item access via dot notation.

Parameters:key (object) – Key to fetch
__setattr__(key, value)[source]

Support item assignment via dot notation.

Parameters:
  • key (object) – Key to set value for
  • value (object) – Value to set key to
rdial.utils.write_current(fun)[source]

Decorator to write current file on function exit.

Returns:Wrapped function
Return type:types.FunctionType
rdial.utils.remove_current(fun)[source]

Decorator to remove current file on function exit.

Returns:Wrapped function
Return type:types.FunctionType
rdial.utils.newer(fname, reference)[source]

Check whether given file is newer than reference file.

Parameters:
  • fname (str) – File to check
  • reference (str) – file to test against
Returns:

True if fname is newer than reference

Return type:

bool

Time handling

rdial.utils.parse_delta(string)[source]

Parse ISO-8601 duration string.

Parameters:string (str) – Duration string to parse
Returns:Parsed delta object
Return type:datetime.timedelta
rdial.utils.format_delta(timedelta_)[source]

Format ISO-8601 duration string.

Parameters:timedelta (datetime.timedelta) – Duration to process
Returns:ISO-8601 representation of duration
Return type:str
rdial.utils.parse_datetime(string)[source]

Parse datetime string.

Parameters:string (str) – Datetime string to parse
Returns:Parsed datetime object
Return type:datetime.datetime
rdial.utils.parse_datetime_user(string)[source]

Parse datetime string from user.

We accept the normal ISO-8601 formats, but kick through to the formats supported by the system’s date command if parsing fails.

Parameters:string (str) – Datetime string to parse
Returns:Parsed datetime object
Return type:datetime.datetime

XDG Base Directory support

rdial.utils.xdg_cache_location()[source]

Return a cache location honouring $XDG_CACHE_HOME.

Returns:Location of cache directory
Return type:str
rdial.utils.xdg_config_location()[source]

Return a config location honouring $XDG_CONFIG_HOME.

Note

click provides click.get_app_dir(), but it isn’t quite XDG basedir compliant. It also has no support for cache or data storage locations, so we need to implement these anyway. It does however support Windows, which this most definitely does not.

Returns:Location of config directory
Return type:str
rdial.utils.xdg_data_location()[source]

Return a data location honouring $XDG_DATA_HOME.

Returns:Location of data directory
Return type:str

Text formatting

rdial.utils._colourise(text, colour, **kwargs)[source]

Colour text, if possible.

See also

click.termui.secho()

Parameters:
  • text (str) – Text to colourise
  • colour (str) – Colour to display text in
  • kwargs (dict) – Extra arguments for secho()
rdial.utils.success(text)[source]

Pretty print a success message.

Parameters:text (str) – Text to format
rdial.utils.fail(text)[source]

Pretty print a failure message.

Parameters:text (str) – Text to format
rdial.utils.warn(text)[source]

Pretty print a warning message.

Parameters:text (str) – Text to format

Examples

Time handling

 >>> parse_delta('PT36M10S')
 datetime.timedelta(0, 2170)
 >>> parse_delta('')
 datetime.timedelta(0)
 >>> format_delta(datetime.timedelta(0))
 ''
 >>> format_delta(datetime.timedelta(minutes=30))
 'PT30M'
 >>> parse_datetime('2012-02-15T18:59:18Z')
 datetime.datetime(2012, 2, 15, 18, 59, 18)
 >>> parse_datetime('40 minutes ago')
 datetime.datetime(2012, 2, 15, 18, 59, 18

XDG Base Directory support

 >>> xdg_data_location()
 '/home/jay/.xdg/local/rdial'

Text formatting

>>> fail('Error!')
Error!
>>> success('Excellent')
Excellent
>>> warn('Ick')
Ick