kojismokydingo.common

Koji Smoky Dingo - Common Utils

Some simple functions used by the other modules.

author:

Christopher O'Brien <obriencj@gmail.com>

license:

GPL v3

chunkseq(seq, chunksize)[source]

Chop up a sequence into sub-sequences, each up to chunksize in length.

Parameters:
  • seq (Iterable) -- a sequence to chunk up

  • chunksize (int) -- max length for chunks

Since:

1.0

Return type:

Iterator[Iterable]

escapable_replace(orig, character, replacement)[source]

Single-character string substitutions. Doubled sentinel characters can be used to represent that exact character.

Examples:

  • escapable_replace('Hello %', '%', 'World') returns "Hello World"

  • escapable_replace('Hello %%', '%', 'World') returns "Hello %"

Parameters:
  • orig (str) -- Original text

  • character (str) -- Single-character token.

  • replacement (str) -- Replacement text

Since:

1.0

Return type:

str

find_cache_dir(usage=None)[source]

The use cache dir for koji-smoky-dingo for the given usage (eg. repoquery). Attempts to use the appdirs package of it is available.

Parameters:

usage (str | None) -- nested directory to apply to the cache path

Since:

2.1

Return type:

str

find_config_dirs()[source]

The site and user configuration dirs for koji-smoky-dingo, as a tuple. Attempts to use the appdirs package if it is available.

Since:

1.0

Return type:

Tuple[str, str]

find_config_files(dirs=None)[source]

The ordered list of configuration files to be loaded.

If dirs is specified, it must be a sequence of directory names, from which conf files will be loaded in order. If unspecified, defaults to the result of find_config_dirs

Configuration files must have the extension .conf to be considered. The files will be listed in directory order, and then in alphabetical order from within each directory.

Parameters:

dirs (Iterable[str] | None) -- list of directories to look for config files within

Since:

1.0

Return type:

List[str]

fnmatches(value, patterns, ignore_case=False)[source]

Checks value against multiple glob patterns. Returns True if any match.

Parameters:
  • value (str) -- string to be matched

  • patterns (Iterable[str]) -- list of glob-style pattern strings

  • ignore_case (bool) -- if True case is normalized, Default False

Since:

1.0

Return type:

bool

get_plugin_config(conf, plugin, profile=None)[source]

Given a loaded configuration, return the section specific to the given plugin, and optionally profile-specific as well.

Parameters:
  • conf (ConfigParser) -- loaded configuration data

  • plugin (str) -- plugin name

  • profile (str | None) -- profile name, optional

Since:

1.0

Return type:

Dict[str, Any]

globfilter(seq, patterns, key=None, invert=False, ignore_case=False)[source]

Generator yielding members of sequence seq which match any of the glob patterns specified.

Patterns must be a list of glob-style pattern strings.

If key is specified, it must be a unary callable which translates a given sequence item into a string for comparison with the patterns.

If invert is True, yields the non-matches rather than the matches.

If ignore_case is True, the pattern comparison is case normalized.

Parameters:
  • seq (Iterable[GFT]) -- series of objects to be filtered. Normally strings, but may be any type provided the key parameter is specified to provide a string for matching based on the given object.

  • patterns (Iterable[str]) -- list of glob-style pattern strings. Members of seq which match any of these patterns are yielded.

  • key (Callable[[Any], Any] | Any | None) -- A unary callable which translates individual items on seq into the value to be matched against the patterns. Default, match against values in seq directly.

  • invert (bool) -- Invert the logic, yielding the non-matches rather than the matches. Default, yields matches

  • ignore_case (bool) -- pattern comparison is case normalized if True. Default, False

Since:

1.0

Return type:

Iterable[GFT]

ichunkseq(seq, chunksize)[source]

Similar to chunkseq, but lazy. Note that each chunk must be exhausted before beginning a new chunk, as the chunks will be read from the original sequence only when they themselves are iterated over.

Parameters:
  • seq (Iterable) -- a sequence to chunk up

  • chunksize (int) -- max length for chunks

Since:

2.1

Return type:

Iterator[Iterable]

itemsgetter(key, *keys)[source]

Similar to operator.itemgetter. However, the returned unary callable always returns a tuple of results, even if there's only one key.

Parameters:
  • key (Any) -- first key to fetch with the returned getter

  • keys (Any) -- additional keys to fetch with the returned getter

Returns:

a callabler getter which when called with a value will return the result of getting all of the specified keys, in a tuple.

Since:

2.1

Return type:

Callable[[Any], Tuple]

load_full_config(config_files=None)[source]

Configuration object representing the full merged view of config files.

If config_files is None, use the results of find_config_files. Otherwise, config_files must be a sequence of filenames.

Parameters:

config_files (Iterable[str] | None) -- configuration files to be loaded, in order. If not specified, the results of find_config_files will be used.

Returns:

a configuration representing a merged view of all config files

Since:

1.0

Return type:

ConfigParser

load_plugin_config(plugin, profile=None)[source]

Configuration specific to a given plugin, and optionally specific to a given profile as well.

Profile-specific sections are denoted by a suffix on the section name, eg.

[my_plugin]
# this setting is for my_plugin on all profiles
setting = foo

[my_plugin:testing]
# this setting is for my_plugin on the testing profile
setting = bar
Parameters:
  • plugin (str) -- plugin name

  • profile (str | None) -- profile name

Since:

1.0

Return type:

Dict[str, Any]

merge_extend(*dict_additions)[source]

Similar to update_extend but creates a new dict to hold results, and new initial lists to be extended, leaving all the arguments unaltered.

Parameters:

dict_additions (Dict[KT, List[Any]]) -- The additions dict. Will not be altered.

Returns:

A new dict, whose values are new lists

Since:

1.0

Return type:

Dict[KT, List[Any]]

parse_datetime(src, strict=True)[source]

Attempts to parse a datetime string in numerous ways based on pre-defined regex mappings

Supported formats:
  • %Y-%m-%d %H:%M:%S.%f %Z

  • %Y-%m-%d %H:%M:%S.%f%z

  • %Y-%m-%d %H:%M:%S %Z

  • %Y-%m-%d %H:%M:%S%z

  • %Y-%m-%d %H:%M:%S

  • %Y-%m-%d %H:%M

  • %Y-%m-%d

  • %Y-%m

Plus integer timestamps and the string "now"

Timezone offset formats (%z) may also be specified as either +HHMM or +HH:MM (the : will be removed)

Parameters:
  • src (str) -- Date-time text to be parsed

  • strict (bool) -- Raise an exception if no matching format is known and the date-time text cannot be parsed. If False, simply return None when the value cannot be parsed.

Raises:

ValueError -- if strict and no src matches none of the pre-defined formats

Since:

1.0

Return type:

datetime

unique(sequence, key=None)[source]

Given a sequence, de-duplicate it into a new list, preserving order.

In the event that the sequence contains non-hashable objects, key must be specified as a unary callable which produces a hashable unique identifier for the individual items in the sequence. This identifier is then used to perform the de-duplication.

Parameters:
  • sequence (Iterable[UT]) -- series of hashable objects

  • key (Callable[[Any], Any] | Any | None) -- unary callable that produces a hashable identifying value. Default, use each object in sequence as its own identifier.

Since:

1.0

Return type:

List[UT]

update_extend(dict_orig, *dict_additions)[source]

Extend the list values of the original dict with the list values of the additions dict.

eg.

A = {'a': [1, 2], 'b': [7], 'c': [10]}
B = {'a': [3], 'b': [8, 9], 'd': [11]}
update_extend(A, B)

A
>> {'a': [1, 2, 3], 'b': [7, 8, 9], 'c': [10], 'd': [11]}

The values of dict_orig must support an extend method.

Parameters:
  • dict_orig (Dict[KT, List[Any]]) -- The original dict, which may be mutated and whose values may be extended

  • dict_additions (Dict[KT, List[Any]]) -- The additions dict. Will not be altered.

Returns:

The original dict instance

Since:

1.0

Return type:

Dict[KT, List[Any]]