rez.rex
- class rez.rex.ActionInterpreter[source]
Bases:
objectAbstract base class that provides callbacks for rex Actions. This class should not be used directly. Its methods are called by the ActionManager in response to actions issued by user code written using the rex python API.
Sub-classes should override the get_output method to return implementation-specific data structure. For example, an interpreter for a shell language like bash would return a string of shell code. An interpreter for an active python session might return a dictionary of the modified environment.
Sub-classes can override the expand_env_vars class variable to instruct the ActionManager whether or not to expand the value of environment variables which reference other variables (e.g. “this-${THAT}”).
- ENV_VAR_REGEX = re.compile('\\${([^\\{\\}]+?)}|\\$([a-zA-Z_]+[a-zA-Z0-9_]*?)')
- appendenv(key, value)[source]
This is optional, but if it is not implemented, you must implement setenv.
- escape_string(value, is_path=False)[source]
Escape a string.
Escape the given string so that special characters (such as quotes and whitespace) are treated properly. If value is a string, assume that this is an expandable string in this interpreter.
Note that is_path provided because of the special case where a path-like envvar is set. In this case, path normalization, if it needs to occur, has to be part of the string escaping process.
Note
This default implementation returns the string with no escaping applied.
- expand_env_vars = False
- get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]
Returns any implementation specific data.
- Parameters
style (OutputStyle) – Style affecting output format.
- Returns
Depends on implementation, but usually a code string.
- normalize_path(path)[source]
Normalize a path.
Change path to a valid filepath representation for this interpreter.
IMPORTANT: Because var references like ${THIS} might be passed to funcs like appendvar, path might be in this form. You need to take that into account (ie, ensure normalization doesn’t break such a var reference).
- Parameters
path (str) – A filepath which may be in posix format, or windows format, or some combination of the two. For eg, a string like {root}/bin on windows will evaluate to C:.../bin - in this case, the cmd shell would want to normalize this and convert to all forward slashes.
- Returns
The normalized path.
- Return type
- normalize_paths(value)[source]
Normalize value if it’s a path(s).
Note that value may be more than one pathsep-delimited paths.
- pathsep = ':'
- class rez.rex.ActionManager(interpreter, parent_environ=None, parent_variables=None, formatter=None, verbose=False, env_sep_map=None)[source]
Bases:
objectHandles the execution book-keeping. Tracks env variable values, and triggers the callbacks of the ActionInterpreter.
- get_action_methods()[source]
return a list of methods on this class for executing actions. methods are return as a list of (name, func) tuples
- class rez.rex.Alias(*args)[source]
Bases:
rez.rex.Action- name = 'alias'
- class rez.rex.Appendenv(*args)[source]
Bases:
rez.rex.Setenv- name = 'appendenv'
- class rez.rex.Command(*args)[source]
Bases:
rez.rex.Action- name = 'command'
- class rez.rex.Comment(*args)[source]
Bases:
rez.rex.Action- name = 'comment'
- class rez.rex.EnvAction(*args)[source]
Bases:
rez.rex.Action- property key
- property value
- class rez.rex.EnvironmentDict(manager)[source]
Bases:
collections.abc.MutableMappingProvides a mapping interface to EnvironmentVariable instances, which provide an object-oriented interface for recording environment variable manipulations.
__getitem__ is always guaranteed to return an EnvironmentVariable instance: it will not raise a KeyError.
- class rez.rex.EnvironmentVariable(name, environ_map)[source]
Bases:
objectclass representing an environment variable
combined with EnvironmentDict class, records changes to the environment
- property name
- class rez.rex.Error(*args)[source]
Bases:
rez.rex.Action- name = 'error'
- class rez.rex.EscapedString(value, is_literal=False)[source]
Bases:
objectClass for constructing literal or expandable strings, or a combination of both.
This determines how a string is escaped in an interpreter. For example, the following rex commands may result in the bash code shown:
>>> env.FOO = literal('oh "noes"') >>> env.BAH = expandable('oh "noes"') export FOO='oh "noes"' export BAH="oh "noes""
You do not need to use expandable - a string by default is interpreted as expandable. However you can mix literals and expandables together, like so:
>>> env.FOO = literal("hello").expandable(" ${DUDE}") export FOO='hello'" ${DUDE}"
Shorthand methods e and l are also supplied, for better readability:
>>> env.FOO = literal("hello").e(" ${DUDE}").l(", and welcome!") export FOO='hello'" ${DUDE}"', and welcome!'
Note
you can use the literal and expandable free functions, rather than constructing a class instance directly.
- expanduser()[source]
Analogous to os.path.expanduser.
- Returns
EscapedString object with expanded ‘~’ references.
- class rez.rex.Info(*args)[source]
Bases:
rez.rex.Action- name = 'info'
- class rez.rex.NamespaceFormatter(namespace)[source]
Bases:
string.FormatterString formatter that, as well as expanding ‘{variable}’ strings, also protects environment variable references such as ${THIS} so they do not get expanded as though {THIS} is a formatting target. Also, environment variable references such as $THIS are converted to ${THIS}, which gives consistency across shells, and avoids some problems with non-curly-braced variables in some situations.
- class rez.rex.OutputStyle(value, names=None, module=None, type=None)[source]
Bases:
rez.vendor.enum.EnumEnum to represent the style of code output when using Rex.
- class rez.rex.Prependenv(*args)[source]
Bases:
rez.rex.Setenv- name = 'prependenv'
- class rez.rex.Python(target_environ=None, passive=False)[source]
Bases:
rez.rex.ActionInterpreterExecute commands in the current python session
- appendenv(key, value)[source]
This is optional, but if it is not implemented, you must implement setenv.
- expand_env_vars = True
- get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]
Returns any implementation specific data.
- Parameters
style (OutputStyle) – Style affecting output format.
- Returns
Depends on implementation, but usually a code string.
- class rez.rex.Resetenv(*args)[source]
Bases:
rez.rex.EnvAction- property friends
- name = 'resetenv'
- class rez.rex.RexExecutor(interpreter=None, globals_map=None, parent_environ=None, parent_variables=None, shebang=True, add_default_namespaces=True)[source]
Bases:
objectRuns an interpreter over code within the given namespace. You can also access namespaces and rex functions directly in the executor, like so:
RexExecutor ex() ex.setenv(‘FOO’, ‘BAH’) ex.env.FOO_SET = 1 ex.alias(‘foo’,’foo -l’)
- property actions
List of Action objects that will be executed.
- classmethod compile_code(code, filename=None, exec_namespace=None)[source]
Compile and possibly execute rex code.
- Parameters
code (str or SourceCode) – The python code to compile.
filename (str) – File to associate with the code, will default to ‘<string>’.
exec_namespace (dict) – Namespace to execute the code in. If None, the code is not executed.
- Returns
Compiled code object.
- execute_code(code, filename=None, isolate=False)[source]
Execute code within the execution context.
- Parameters
code (str or SourceCode) – Rex code to execute.
filename (str) – Filename to report if there are syntax errors.
isolate (bool) – If True, do not affect self.globals by executing this code. DEPRECATED - use self.reset_globals instead.
- execute_function(func, *nargs, **kwargs)[source]
Execute a function object within the execution context. @returns The result of the function call.
- get_output(style=<OutputStyle.file: ('Code as it would appear in a script file.', )>)[source]
Returns the result of all previous calls to execute_code.
- property interpreter
- normalize_path(path)[source]
Normalize a path.
Note that in many interpreters this will be unchanged.
- Returns
The normalized path.
- Return type
- class rez.rex.Setenv(*args)[source]
Bases:
rez.rex.EnvAction- name = 'setenv'
- class rez.rex.Shebang(*args)[source]
Bases:
rez.rex.Action- name = 'shebang'
- class rez.rex.Source(*args)[source]
Bases:
rez.rex.Action- name = 'source'
- class rez.rex.Stop(*args)[source]
Bases:
rez.rex.Action- name = 'stop'
- class rez.rex.Unsetenv(*args)[source]
Bases:
rez.rex.EnvAction- name = 'unsetenv'