Skip to content

Commit

Permalink
feat(ctx): utility function to context creation
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed May 18, 2016
1 parent 79b5b7c commit 72f43d9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
14 changes: 4 additions & 10 deletions pyangext/syntax_tree.py
@@ -1,7 +1,5 @@
# -*- coding: utf-8 -*-
"""Tools for programmatic generation of a YANG Abstract Syntax Tree."""
from collections import namedtuple

from six import StringIO

from pyang import statements as st
Expand All @@ -10,18 +8,14 @@

from pyangext import __version__ # noqa
from pyangext.definitions import PREFIX_SEPARATOR
from pyangext.utils import create_context

__author__ = "Anderson Bravalheri"
__copyright__ = "Copyright (C) 2016 Anderson Bravalheri"
__license__ = "mozilla"

# Mock context and CLI options objects to allow `emit_yang`usage
_YangOptions = namedtuple( # pylint: disable=invalid-name
'_YangOptions', ('yang_remove_unused_imports', 'yang_canonical'))
_SimpleContext = namedtuple( # pylint: disable=invalid-name
'_SimpleContext', ('opts',))
_ctx = _SimpleContext( # pylint: disable=invalid-name
_YangOptions(False, True))
# Mock context and CLI options objects to allow `emit_yang` usage
_ctx = create_context() # pylint: disable=invalid-name


class ValidationError(RuntimeError):
Expand Down Expand Up @@ -276,7 +270,7 @@ def unwrap(self):
"""Retrieve the inner ``pyang.statements.Statement`` object"""
return self._statement

def validate(self, ctx):
def validate(self, ctx=_ctx):
"""Validates the syntax tree.
Should be called just from ``module``, ``submodule`` statements.
Expand Down
55 changes: 55 additions & 0 deletions pyangext/utils.py
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-
"""Utility belt for working with ``pyang`` and ``pyangext``."""

from pyang import Context, FileRepository

DEFAULT_OPTIONS = {
'format': 'yang',
'verbose': True,
'list_errors': True,
'print_error_code': True,
'yang_remove_unused_imports': True,
'yang_canonical': True,
'trim_yin': False,
'keep_comments': True,
'features': [],
'deviations': [],
'path': []
}
"""Default options for pyang command line"""


class objectify(object): # pylint: disable=invalid-name
"""Utility for providing object access syntax (.attr) to dicts"""

def __init__(self, *args, **kwargs):
for entry in args:
self.__dict__.update(entry)

self.__dict__.update(kwargs)

def __getattr__(self, _):
return None

def __setattr__(self, attr, value):
self.__dict__[attr] = value


def create_context(path='.', *options, **kwargs):
"""Generates a pyang context
Arguments:
path (str): location of YANG modules.
*options: list of dicts, with options to be passed to context.
**kwargs: similar to ``options`` but have a higher precedence.
Returns:
pyang.Context: Context object for ``pyang`` usage
"""

opts = objectify(DEFAULT_OPTIONS, *options, **kwargs)
repo = FileRepository(path, no_path_recurse=opts.no_path_recurse)
ctx = Context(repo)
ctx.opts = opts

return ctx
4 changes: 1 addition & 3 deletions tests/test_builder.py
Expand Up @@ -5,7 +5,6 @@
tests for YANG builder
"""
import pytest
from mock import Mock

from pyang.statements import Statement

Expand Down Expand Up @@ -231,7 +230,6 @@ def test_wrapper_validate(builder):
validate should not allow non top-level statements
"""
leaf = builder.leaf('name', builder.type('string'))
ctx = Mock()

with pytest.raises(ValidationError):
leaf.validate(ctx)
leaf.validate()

0 comments on commit 72f43d9

Please sign in to comment.