twizy_bringup/src/twizy_bringup/twizyparam.py
def parse(path):
    """Parse a YAML file

    The following extra tags are supported:

    * !eval: Evaluates a Python expression
    * !include: Includes another YAML file by path
    * !variable: A variable to be used in !eval expressions

    :param path: Path to a YAML file
    :type path: str
    
    :return: A parsed YAML object where the tags defined above have been
        expanded
    """

    # Load the YAML file
    text = Path(path).read_text()
    # Pyyaml does its merging early on, which we don't want. We instead want
    # to e.g. evalueate !eval and !include tags before mergeing. So replace
    # all merges with a custom tag for now so Pyyaml doesn't recognize them
    text = re.sub(r'^( *)<< *:', r'\1!merge :', text, flags=re.M)
    # Parse the YAML string
    parsed = yaml.load(text, yaml.SafeLoader)

    def extract_variables(root, k, v):
        if isinstance(k, _Variable):
            # Variables have so far been stored as objects in the parsed
            # structure to distinguish them from other keys. However we need
            # variables to be accessable by their name like other keys in the
            # structure, so replace the objects keys by string keys where the
            # the string is the variable name
            del root[k]
            variables[k.name] = v
        elif isinstance(v, _Eval):
            # We delete all eval expressions here since we are in general not
            # able to evalueate expressions before the variables have been
            # expanded. Remember that entries in a YAML file are by
            # specification unordered, so we cannot traverse a YAML file line
            # by line, parsing !variables:s and !eval:ss as we go. Instead all
            # !variables are paresed first, then all !eval:s. This does however
            # have the unfortunate side effect of not beeing able to use !eval
            # inside a !variable value
            del root[k]
    variables = copy.deepcopy(parsed)
    # Extract variables
    _traverse_dict(variables, extract_variables)

    def eval_expressions(root, k, v):
        if isinstance(v, _Eval):
            # Evaluate !eval expression. Make the math module available inside
            # the expression
            root[k] = eval(v.expr, {
                'math': math,
                **variables
            })
        elif isinstance(k, _Variable):
            # Remove all variables from the final output
            del root[k]
    # Evaluate !eval expressions only after !variable:s have been parsed
    _traverse_dict(parsed, eval_expressions)

    # Finally convert back to text...
    text = yaml.safe_dump(parsed)
    # ... so we can replace all the !merge tags with << again so mergeing can
    # be taken care of by Pyyaml...
    text = re.sub(r'^( *).*?!merge.*?(\n?.*?):', r'\1<<:\2 ', text, flags=re.M)
    # ... by parsing the string another time
    parsed = yaml.safe_load(text)

    return parsed
Parse a YAML file

    The following extra tags are supported:

    * !eval: Evaluates a Python expression
    * !include: Includes another YAML file by path
    * !variable: A variable to be used in !eval expressions

    :param path: Path to a YAML file
    :type path: str
    
    :return: A parsed YAML object where the tags defined above have been
        expanded
    
Parse a YAML file
The following extra tags are supported.

!eval: Evaluates a Python expression
!include: Includes another YAML file by path
!variable: A variable to be used in !eval expressions
Parse a YAML file The following extra tags are supported . !eval : Evaluates a Python expression !include : Includes another YAML file by path !variable : A variable to be used in !eval expressions
def parse(path):
    text = Path(path).read_text()
    text = re.sub(r'^( *)<< *:', r'\1!merge :', text, flags=re.M)
    parsed = yaml.load(text, yaml.SafeLoader)
    def extract_variables(root, k, v):
        if isinstance(k, _Variable):
            del root[k]
            variables[k.name] = v
        elif isinstance(v, _Eval):
            del root[k]
    variables = copy.deepcopy(parsed)
    _traverse_dict(variables, extract_variables)
    def eval_expressions(root, k, v):
        if isinstance(v, _Eval):
            root[k] = eval(v.expr, {
                'math': math,
                **variables
            })
        elif isinstance(k, _Variable):
            del root[k]
    _traverse_dict(parsed, eval_expressions)
    text = yaml.safe_dump(parsed)
    text = re.sub(r'^( *).*?!merge.*?(\n?.*?):', r'\1<<:\2 ', text, flags=re.M)
    parsed = yaml.safe_load(text)
    return parsed
def parse ( path ) : text = Path ( path ) . read_text ( ) text = re . sub ( r'^( *)<< *:' , r'\1!merge :' , text , flags = re . M ) parsed = yaml . load ( text , yaml . SafeLoader ) def extract_variables ( root , k , v ) : if isinstance ( k , _Variable ) : del root [ k ] variables [ k . name ] = v elif isinstance ( v , _Eval ) : del root [ k ] variables = copy . deepcopy ( parsed ) _traverse_dict ( variables , extract_variables ) def eval_expressions ( root , k , v ) : if isinstance ( v , _Eval ) : root [ k ] = eval ( v . expr , { 'math' : math , ** variables } ) elif isinstance ( k , _Variable ) : del root [ k ] _traverse_dict ( parsed , eval_expressions ) text = yaml . safe_dump ( parsed ) text = re . sub ( r'^( *).*?!merge.*?(\n?.*?):' , r'\1<<:\2 ' , text , flags = re . M ) parsed = yaml . safe_load ( text ) return parsed
Parse a YAML file
The following extra tags are supported:
Parse a YAML file The following extra tags are supported :
"""Parse a YAML file

    The following extra tags are supported:

    * !eval: Evaluates a Python expression
    * !include: Includes another YAML file by path
    * !variable: A variable to be used in !eval expressions

    :param path: Path to a YAML file
    :type path: str
    
    :return: A parsed YAML object where the tags defined above have been
        expanded
    """ # Load the YAML file # Pyyaml does its merging early on, which we don't want. We instead want # to e.g. evalueate !eval and !include tags before mergeing. So replace # all merges with a custom tag for now so Pyyaml doesn't recognize them # Parse the YAML string # Variables have so far been stored as objects in the parsed # structure to distinguish them from other keys. However we need # variables to be accessable by their name like other keys in the # structure, so replace the objects keys by string keys where the # the string is the variable name # We delete all eval expressions here since we are in general not # able to evalueate expressions before the variables have been # expanded. Remember that entries in a YAML file are by # specification unordered, so we cannot traverse a YAML file line # by line, parsing !variables:s and !eval:ss as we go. Instead all # !variables are paresed first, then all !eval:s. This does however # have the unfortunate side effect of not beeing able to use !eval # inside a !variable value # Extract variables # Evaluate !eval expression. Make the math module available inside # the expression # Remove all variables from the final output # Evaluate !eval expressions only after !variable:s have been parsed # Finally convert back to text... # ... so we can replace all the !merge tags with << again so mergeing can # be taken care of by Pyyaml... # ... by parsing the string another time
A parsed YAML object where the tags defined above have been
expanded
A parsed YAML object where the tags defined above have been expanded
path
Path to a YAML file
Path to a YAML file