rez.utils.formatting#
Utilities related to formatting output or translating input.
- rez.utils.formatting.is_valid_package_name(name, raise_error=False)#
Test the validity of a package name string.
- class rez.utils.formatting.PackageRequest#
Bases:
Requirement
A package request parser.
Valid requests include:
Any standard request, eg ‘foo-1.2.3’, ‘!foo-1’, etc
“Ephemeral” request, eg ‘.foo-1.2.3’
Example
>>> pr = PackageRequest("foo-1.3+") >>> print(pr.name, pr.range) foo 1.3+
- __init__(s)#
- property conflict#
True if the requirement is a conflict requirement, eg “!foo”, “~foo-1”.
- Return type:
- conflicts_with(other)#
Returns True if this requirement conflicts with another
Requirement
orVersionedObject
.- Return type:
- classmethod construct(name, range=None)#
Create a requirement directly from an object name and VersionRange.
- Parameters:
name (str) – Object name string.
range (Optional[VersionRange]) – If None, an unversioned requirement is created.
- merged(other)#
Merge two requirements.
Two requirements can be in conflict and if so, this function returns None. For example, requests for
foo-4
andfoo-6
are in conflict, since both cannot be satisfied with a single version of foo.Some example successful requirements merges are:
foo-3+
and!foo-5+
==foo-3+<5
foo-1
andfoo-1.5
==foo-1.5
!foo-2
and!foo-5
==!foo-2|5
- Returns:
the merged result of two requirements.
- Return type:
- property range#
Version range of the requirement.
- Return type:
- safe_str()#
Return a string representation that is safe for the current filesystem, and guarantees that no two different Requirement objects will encode to the same value.
- Return type:
- sep_regex = re.compile('[-@#=<>]')#
- class rez.utils.formatting.StringFormatType#
Bases:
Enum
Behaviour of key expansion when using ObjectStringFormatter.
- error = 1#
- empty = 2#
- unchanged = 3#
- class rez.utils.formatting.ObjectStringFormatter#
Bases:
Formatter
String formatter for objects.
This formatter will expand any reference to an object’s attributes.
- error = 1#
- empty = 2#
- unchanged = 3#
- __init__(instance, pretty=False, expand=StringFormatType.error)#
Create a formatter.
- Parameters:
instance – The object to format with.
pretty – If True, references to non-string attributes such as lists are converted to basic form, with characters such as brackets and parentheses removed.
expand – StringFormatType.
- convert_field(value, conversion)#
- get_field(field_name, args, kwargs)#
- get_value(key, args, kwds)#
- check_unused_args(used_args, args, kwargs)#
- format(format_string, /, *args, **kwargs)#
- format_field(value, format_spec)#
- parse(format_string)#
- vformat(format_string, args, kwargs)#
- class rez.utils.formatting.StringFormatMixin#
Bases:
object
Turn any object into a string formatter.
An object inheriting this mixin will have a format function added, that is able to format using attributes of the object.
- format_expand = 1#
- format_pretty = True#
- format(s, pretty=None, expand=None)#
Format a string.
- Parameters:
s (str) – String to format, eg “hello {name}”
pretty (bool) – If True, references to non-string attributes such as lists are converted to basic form, with characters such as brackets and parenthesis removed. If None, defaults to the object’s ‘format_pretty’ attribute.
expand (StringFormatType) – Expansion mode. If None, will default to the object’s ‘format_expand’ attribute.
- Returns:
The formatting string.
- rez.utils.formatting.expand_abbreviations(txt, fields)#
Expand abbreviations in a format string.
If an abbreviation does not match a field, or matches multiple fields, it is left unchanged.
Example
>>> fields = ("hey", "there", "dude") >>> expand_abbreviations("hello {d}", fields) 'hello dude'
- rez.utils.formatting.expandvars(text, environ=None)#
Expand shell variables of form $var and ${var}.
Unknown variables are left unchanged.
- rez.utils.formatting.indent(txt)#
Indent the given text by 4 spaces.
- rez.utils.formatting.dict_to_attributes_code(dict_)#
Given a nested dict, generate a python code equivalent.
Example
>>> d = {'foo': 'bah', 'colors': {'red': 1, 'blue': 2}} >>> print(dict_to_attributes_code(d)) foo = 'bah' colors.red = 1 colors.blue = 2
- Returns:
str.
- rez.utils.formatting.columnise(rows, padding=2)#
Print rows of entries in aligned columns.
- rez.utils.formatting.print_colored_columns(printer, rows, padding=2)#
Like columnise, but with colored rows.
- Parameters:
printer (colorize.Printer) – Printer object.
Note
The last entry in each row is the row color, or None for no coloring.
- rez.utils.formatting.readable_time_duration(secs)#
Convert number of seconds into human readable form, eg ‘3.2 hours’.
- rez.utils.formatting.readable_memory_size(bytes_)#
Convert number of bytes into human readable form, eg ‘1.2 Kb’.
- rez.utils.formatting.get_epoch_time_from_str(s)#
Convert a string into epoch time. Examples of valid strings:
1418350671 # already epoch time -12s # 12 seconds ago -5.4m # 5.4 minutes ago
- rez.utils.formatting.positional_number_string(n)#
Print the position string equivalent of a positive integer. Examples:
0: zeroeth 1: first 2: second 14: 14th 21: 21st
- rez.utils.formatting.expanduser(path)#
Expand ‘~’ to home directory in the given string.
Note that this function deliberately differs from the builtin os.path.expanduser() on Linux systems, which expands strings such as ‘~sclaus’ to that user’s homedir. This is problematic in rez because the string ‘~packagename’ may inadvertently convert to a homedir, if a package happens to match a username.
- rez.utils.formatting.as_block_string(txt)#
Return a string formatted as a python block comment string, like the one you’re currently reading. Special characters are escaped if necessary.
- rez.utils.formatting.header_comment(executor, txt)#
Convenience for creating header-like comment in a rex executor.
- Parameters:
executor (RexExecutor) – Executor.
txt (str) – Comment text.
- rez.utils.formatting.minor_header_comment(executor, txt)#