Rules module

Adding a Rule is as simple as using the add_rule() function and passing a name and a pattern which uses the Tokens you have created.

The name should be unique in the repository.

The pattern is a simple string where you place the names of the Tokens you’d like to use inbetween curly brackets and separate them using any of the most commonly separators (hyphen, underscore, dot, etc)

Then there is also the option to use Anchoring. This means you can force the evaluation of your Rule to be from left to right (default) or right to left or both. Really useful when you have hardcorded values in your naming Rule. Options for anchoring: Rule.ANCHOR_START (default), Rule.ANCHOR_END, Rule.ANCHOR_BOTH

import vfxnaming as n

n.add_rule(
   'lights',
   '{category}_{function}_{whatAffects}_{digits}_{type}'
)

n.add_rule(
   'filename',
   '{side}-{region}_{side}-{region}_{side}-{region}'
)

n.add_rule(
   'filename',
   'crazy_hardcoded_value_{awesometoken}',
   n.Rule.ANCHOR_END
)
class vfxnaming.rules.Rule(name, pattern, anchor=1)

Each rule is managed by an instance of this class. Fields exist for each Token and Separator used in the rule definition.

Args:

name (str): Name that best describes the rule, this will be used as a way to query the Rule object.

pattern (str): The template pattern to use, which uses existing Tokens. e.g.: ‘{side}_{region}_{side}_{region}.png’

anchor: ([Rule.ANCHOR_START, Rule.ANCHOR_END, Rule.ANCHOR_BOTH], optional): For parsing, regex matching will look for a match from this Anchor. If a pattern is anchored to the start, it requires the start of a passed path to match the pattern. Defaults to ANCHOR_START.

data()

Collect all data for this object instance.

Returns:
dict: {attribute:value}
fields
Returns:
[tuple]: Tuple of all Tokens found in this Rule’s pattern
classmethod from_data(data)

Create object instance from give data. Used by Rule, Token, Separator to create object instances from disk saved data.

Args:
data (dict): {attribute:value}
Returns:
Serializable: Object instance for Rule, Token or Separator.
parse(name)

Build and return dictionary with keys as tokens and values as given names.

If your rule uses the same token more than once, the returned dictionary keys will have the token name and an incremental digit next to them so they can be differentiated.

Args:
name (str): Name string e.g.: C_helmet_001_MSH
Returns:
dict: A dictionary with keys as tokens and values as given name parts. e.g.: {‘side’:’C’, ‘part’:’helmet’, ‘number’: 1, ‘type’:’MSH’}
regex
Returns:
[str]: Regular expression used to parse from this Rule
solve(**values)

Given arguments are used to build a name.

Raises:
SolvingError: Arguments passed do not match with rule fields.
Returns:
str: A string with the resulting name.
vfxnaming.rules.add_rule(name, pattern, anchor=1)

Add rule to current naming session. If no active rule is found, it adds the created one as active by default.

Args:

name (str): Name that best describes the rule, this will be used as a way to invoke the Rule object.

pattern (str): The template pattern to use, which uses existing Tokens. e.g.: ‘{side}_{region}_{side}_{region}.png’

anchor: ([Rule.ANCHOR_START, Rule.ANCHOR_END, Rule.ANCHOR_BOTH], optional): For parsing, regex matching will look for a match from this Anchor. If a pattern is anchored to the start, it requires the start of a passed path to match the pattern. Defaults to ANCHOR_START.

Returns:
Rule: The Rule object instance created for given name and fields.
vfxnaming.rules.get_active_rule()

Get currently active rule for the session. This is the rule that will be used to parse and solve from.

Returns:
Rule: Rule object instance for currently active Rule.
vfxnaming.rules.get_rule(name)

Gets Rule object with given name.

Args:
name (str): The name of the rule to query.
Returns:
Rule: Rule object instance for given name.
vfxnaming.rules.get_rules()

Get all Rule objects for current session.

Returns:
dict: {rule_name:Rule}
vfxnaming.rules.has_rule(name)

Test if current session has a rule with given name.

Args:
name (str): The name of the rule to be looked for.
Returns:
bool: True if rule with given name exists in current session, False otherwise.
vfxnaming.rules.load_rule(filepath)

Load rule from given location and create Rule object in memory to work with it.

Args:
filepath (str): Path to existing .rule file location
Returns:
bool: True if successful, False if .rule wasn’t found.
vfxnaming.rules.remove_rule(name)

Remove Rule from current session.

Args:
name (str): The name of the rule to be removed.
Returns:
bool: True if successful, False if a rule name was not found.
vfxnaming.rules.reset_rules()

Clears all rules created for current session.

Returns:
bool: True if clearing was successful.
vfxnaming.rules.save_rule(name, directory)

Saves given rule serialized to specified location.

Args:

name (str): The name of the rule to be saved.

directory (str): Path location to save the rule.

Returns:
bool: True if successful, False if rule wasn’t found in current session.
vfxnaming.rules.set_active_rule(name)

Sets given rule as active for the session. This it the rule that’s being used to parse and solve from.

Args:
name (str): The name of the rule to be activated.
Returns:
bool: True if successful, False otherwise.