ways.parsing.resource module

Asset objects are objects that store per-instance data for Context objects.

They are necessary because Context objects are flyweights and, because of that, cannot carry instance data.

class ways.parsing.resource.Asset(info, context, parse_type='regex')[source]

Bases: object

An object that contains a Context and data about the Context.

The idea of this class is to keep Context information abstract, and let Context parse/use that information. Depending on what the Context is for, it could be used to ground the information to a filesystem or a database or some other structure that the Context knows about.

get_missing_required_tokens()[source]

Find any token that still needs to be filled for our parser.

If a token is missing but it has child tokens and all of the child tokens are defined, it is excluded from the final output. If the missing token is a child of some parent token that is defined, then the value of the token is parsed. If the parse is successful, the token is excluded from the final output.

Returns:Any tokens that have no value.
Return type:list[str]
get_str(required=True, *args, **kwargs)[source]

Get the full path to the asset, if any.

Parameters:
  • required (bool, optional) – If True and there are tokens that are required that still are not filled, raise an error. If False, return the incomplete string. Default is True.
  • *args (list) – Positional args to send to ContextParser.get_str.
  • **kwargs (list) – Keywords args to send to ContextParser.get_str.
Raises:

ValueError – If required is True (in other words, we assume that)

Returns:

The resolved string for this instance.

Return type:

str

get_token_parse(name, parse_type='')[source]

Get the parse expression for some token name.

Parameters:
  • name (str) – The name of the token to get parse details from.
  • parse_type (str, optional) – The engine type whose expression will be returned. If no parse_type is given, the stored parse_type is used.
Returns:

The parse expression used for the given token.

get_unfilled_tokens(required_only=False)[source]

Get the tokens in this instance that still don’t have values.

Parameters:required_only (bool, optional) – If True, do not return optional tokens. If False, return all tokens, required and optional. Default is False.
Returns:The tokens that still need values.
Return type:list[str]
get_value(name, real=False)[source]

Get some information about this asset, using a token-name.

If the information is directly available, we return it. If it isn’t though, it is searched for, using whatever information that we do have.

If the token name is a child of another token that is defined, we use the parent token to “build” a value for the token that was requested.

If the token name is a parent of some other tokens that all have values, we try to “build” it again, by combining all of the child tokens.

In both cases, the return value is created but not defined. But it lets you do this:

Example

>>> shot_info = {
...     'JOB': 'someJob',
...     'SCENE': 'SOMETHING',
...     'SHOT': 'sh0010'  # Pretend SHOT_NUMBER is a child of SHOT
... }
>>> shot_asset = resource.Asset(shot_info, context='job/scene/shot')
>>> shot_asset.get_value('SHOT_NUMBER')
... # Result: '0010'
Parameters:
  • name (str) – The token to get the value of.
  • real (bool, optional) – If True, the original parsed value is returned. If False and the given token has functions defined in “before_return” then those functions will process the output and then return it. Default is False.
Returns:

The value at the given token.

set_value(key, value, force=False)[source]

Store the given value to some key.

Parameters:
  • key (str) – The token that our value will be stored into.
  • value (str) – The value to store.
  • force (bool, optional) – If False, values are checked against their tokens before being set. If True, values are set for each token, even if they are not valid input for that token. Default is False.
class ways.parsing.resource.AssetFinder(finder, asset)[source]

Bases: ways.core.compat.DirMixIn, object

A class that wraps a Find class with the current asset.

Ways Action objects don’t assume anything about their input. This is normally a good thing because it keeps Actions flexible. But if we’re working with an Action that expects an Asset object, we’d have to do this all the time:

Example

>>> asset = resource.get_asset({'info': 'here'}, context='some/context')
>>> output = asset.context.actions.get_foo(action, some='other', args=4)

Gross, right?

So instead what we do is add AssetFinder as an ‘actions’ property and then forcefully pass the Asset as the first argument to Actions.

Example

>>> asset = resource.get_asset({'info': 'here'}, context='some/context')
>>> output = asset.actions.get_foo(some='other', args=4)

That’s much better.

ways.parsing.resource.expand_info(info, context=None)[source]

Get parsed information, using the given Context.

Note

This function requires regex in order to parse.

Todo

Maybe I can abstract the parser to use different parse options, like I did in get_value_from_parent. And then if that doesn’t work, I can add the option to “register” a particular parser.

Parameters:
  • info (dict[str] or str) – The info to expand. If the input is a dict, it is passed through and returned. If it is a string, the string is parsed against the given context.
  • context (<ways.api.Context, optional) – The Context that will be used to parse info. If no Context is given, the Context is automatically found. Default is None.
Raises:

NotImplementedError – If context is None. There’s no auto-find-context option yet.

Returns:

The asset info.

Return type:

dict[str]

ways.parsing.resource.get_asset(info, context=None, *args, **kwargs)[source]

Get some class object that matches the given Context and wraps some info.

Parameters:
  • info (dict[str] or str) – The info to expand. If the input is a dict, it is passed through and returned. If it is a string, the string is parsed against the given context. Generally speaking, it’s better to give a string that is an exact or partial match to a Context’s mapping than it is to give a dict. This is doubly true if no context is given.
  • context (ways.api.Context or str or tuple[str]`, optional) – The Context to use for the asset. If a string is given, it is assumed to be the Context’s hierarchy and a Context object is constructed. If nothing is given, the best possible Context is “found” and tried. This auto-find process will try to find the “best” match by looking at every known Context’s mapping. A match is not guaranteed. Default is None.
  • *args (list) – Optional position variables to pass to our found class’s constructor.
  • **kwargs (dict) – Optional keyword variables to pass to our found class’s constructor.
Raises:

NotImplementedError – If context is None. There’s no auto-find-context option yet.

Returns:

The found class object or NoneType. If no class definition was found for the given Context, return a generic Asset object.