rez.utils.formatting

Utilities related to formatting output or translating input.

class rez.utils.formatting.ObjectStringFormatter(instance, pretty=False, expand=<StringFormatType.error: 1>)[source]

Bases: string.Formatter

String formatter for objects.

This formatter will expand any reference to an object’s attributes.

convert_field(value, conversion)[source]
empty = <StringFormatType.empty: 2>
error = <StringFormatType.error: 1>
get_field(field_name, args, kwargs)[source]
get_value(key, args, kwds)[source]
unchanged = <StringFormatType.unchanged: 3>
class rez.utils.formatting.PackageRequest(s)[source]

Bases: rez.vendor.version.requirement.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+
class rez.utils.formatting.StringFormatMixin[source]

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(s, pretty=None, expand=None)[source]

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.

format_expand = <StringFormatType.error: 1>
format_pretty = True
class rez.utils.formatting.StringFormatType(value, names=None, module=None, type=None)[source]

Bases: rez.vendor.enum.Enum

Behaviour of key expansion when using ObjectStringFormatter.

rez.utils.formatting.as_block_string(txt)[source]

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.columnise(rows, padding=2)[source]

Print rows of entries in aligned columns.

rez.utils.formatting.dict_to_attributes_code(dict_)[source]

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.expand_abbreviations(txt, fields)[source]

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'
Parameters
  • txt (str) – Format string.

  • fields (list of str) – Fields to expand to.

Returns

Expanded string.

rez.utils.formatting.expanduser(path)[source]

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.expandvars(text, environ=None)[source]

Expand shell variables of form $var and ${var}.

Unknown variables are left unchanged.

Parameters
  • text (str) – String to expand.

  • environ (dict) – Environ dict to use for expansions, defaults to os.environ.

Returns

The expanded string.

rez.utils.formatting.get_epoch_time_from_str(s)[source]

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.header_comment(executor, txt)[source]

Convenience for creating header-like comment in a rex executor.

Parameters
  • executor (RexExecutor) – Executor.

  • txt (str) – Comment text.

rez.utils.formatting.indent(txt)[source]

Indent the given text by 4 spaces.

rez.utils.formatting.is_valid_package_name(name, raise_error=False)[source]

Test the validity of a package name string.

Parameters
  • name (str) – Name to test.

  • raise_error (bool) – If True, raise an exception on failure

Returns

bool.

rez.utils.formatting.minor_header_comment(executor, txt)[source]
rez.utils.formatting.positional_number_string(n)[source]

Print the position string equivalent of a positive integer. Examples:

0: zeroeth 1: first 2: second 14: 14th 21: 21st

rez.utils.formatting.print_colored_columns(printer, rows, padding=2)[source]

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_memory_size(bytes_)[source]

Convert number of bytes into human readable form, eg ‘1.2 Kb’.

rez.utils.formatting.readable_time_duration(secs)[source]

Convert number of seconds into human readable form, eg ‘3.2 hours’.