コード例 #1
0
    def _eval(self, literal, context):
        """
        Evaluates a literal.

        For now, we evaluate only text (unicode) or boolean values
        (True/False).

        :param str literal:
        :return object:
        """
        try:
            result = literal_eval(literal)
            if isinstance(result, bytes):
                result = result.decode('UTF-8')
            if isinstance(result, six.text_type):
                result = result.format(**context)
            return result
        except Exception as e:
            reraise(
                e,
                'While evaluation literal: "{}" with context: {}'.format(
                    literal,
                    ', '.join(context.keys())
                )
            )
コード例 #2
0
    def TestLines(self, doc, processor):
        from zerotk.reraiseit import reraise

        lines = doc.split('\n')
        input_ = []
        expected = []
        stage = 'input'
        for i_line in lines:
            if i_line.strip() == '---':
                stage = 'output'
                continue
            if i_line.strip() == '===':
                try:
                    obtained = processor(input_)
                except Exception as exception:
                    reraise(exception, 'While processing lines::\n  %s\n' % '\n  '.join(input_))
                if obtained != expected:
                    self._Fail(obtained, expected)
                input_ = []
                expected = []
                stage = 'input'
                continue

            if stage == 'input':
                input_.append(i_line)
            else:
                expected.append(i_line)

        if input_:
            obtained = processor(input_)
            if obtained != expected:
                self._Fail(obtained, expected)
コード例 #3
0
ファイル: _terra_former.py プロジェクト: zerotk/terraformer
    def _Parse(cls, code):
        """
        Parses the given code string returning its lib2to3 AST tree.

        :return lib2to3.AST:
            Returns the lib2to3 AST.
        """

        def _GetLastLeaf(node):
            from lib2to3.pytree import Leaf

            r_leaf = node
            while not isinstance(r_leaf, Leaf):
                r_leaf = r_leaf.children[-1]
            return r_leaf

        # Prioritary import.

        # Other imports
        from zerotk.reraiseit import reraise
        from lib2to3 import pygram, pytree
        from lib2to3.pgen2 import driver
        from lib2to3.pgen2.parse import ParseError
        from lib2to3.pygram import python_symbols
        from lib2to3.pytree import Leaf, Node
        from lib2to3.refactor import _detect_future_features

        added_newline = code and not code.endswith('\n')
        if added_newline:
            code += '\n'

        # Selects the appropriate grammar depending on the usage of
        # "print_function" future feature.
        future_features = _detect_future_features(code)
        if 'print_function' in future_features:
            grammar = pygram.python_grammar_no_print_statement
        else:
            grammar = pygram.python_grammar

        try:
            drv = driver.Driver(grammar, pytree.convert)
            result = drv.parse_string(code, True)
        except ParseError as e:
            reraise(e, "Had problems parsing:\n%s\n" % cls._QuotedBlock(code))

        # Always return a Node, not a Leaf.
        if isinstance(result, Leaf):
            result = Node(python_symbols.file_input, [result])

        # Remove AST-leaf for the added newline.
        if added_newline:
            last_leaf = _GetLastLeaf(result)
            if not (last_leaf.type == 0 and last_leaf.value == ''):
                if last_leaf.prefix:
                    last_leaf.prefix = last_leaf.prefix[:-1]
                else:
                    last_leaf.remove()

        return result
コード例 #4
0
ファイル: _terra_former.py プロジェクト: zerotk/terraformer
    def _Parse(cls, code):
        """
        Parses the given code string returning its lib2to3 AST tree.

        :return lib2to3.AST:
            Returns the lib2to3 AST.
        """
        def _GetLastLeaf(node):
            from lib2to3.pytree import Leaf

            r_leaf = node
            while not isinstance(r_leaf, Leaf):
                r_leaf = r_leaf.children[-1]
            return r_leaf

        # Prioritary import.

        # Other imports
        from zerotk.reraiseit import reraise
        from lib2to3 import pygram, pytree
        from lib2to3.pgen2 import driver
        from lib2to3.pgen2.parse import ParseError
        from lib2to3.pygram import python_symbols
        from lib2to3.pytree import Leaf, Node
        from lib2to3.refactor import _detect_future_features

        added_newline = code and not code.endswith('\n')
        if added_newline:
            code += '\n'

        # Selects the appropriate grammar depending on the usage of
        # "print_function" future feature.
        future_features = _detect_future_features(code)
        if 'print_function' in future_features:
            grammar = pygram.python_grammar_no_print_statement
        else:
            grammar = pygram.python_grammar

        try:
            drv = driver.Driver(grammar, pytree.convert)
            result = drv.parse_string(code, True)
        except ParseError as e:
            reraise(e, "Had problems parsing:\n%s\n" % cls._QuotedBlock(code))

        # Always return a Node, not a Leaf.
        if isinstance(result, Leaf):
            result = Node(python_symbols.file_input, [result])

        # Remove AST-leaf for the added newline.
        if added_newline:
            last_leaf = _GetLastLeaf(result)
            if not (last_leaf.type == 0 and last_leaf.value == ''):
                if last_leaf.prefix:
                    last_leaf.prefix = last_leaf.prefix[:-1]
                else:
                    last_leaf.remove()

        return result
コード例 #5
0
    def handle_tag(self, token, after, context):

        # Parses the TAG line, extracting the id, classes, arguments and the
        # contents.
        try:
            tag = self.__parser.parse_tag(token.line)
        except Exception as e:
            reraise(e, 'While parsing tag in line %d' % token.line_no)
            raise

        result = []

        have_content = hasattr(tag, 'content') and tag.content
        have_children = len(token.children) > 0

        if have_content and have_children:
            raise RuntimeError('A tag should have contents OR children nodes.')

        if after:
            if not have_content and have_children:
                result.append(
                    token.indentation + '</{name}>'.format(
                        name=tag.name
                    )
                )
        else:
            args = getattr(tag, 'args', [])
            args = {
                i.key: self._eval(getattr(i, 'value', 'True'), context)
                for i in args
            }
            tag_text = create_tag(
                tag.name,
                args,
                klass=tag.classes,
                id_=tag.id
            )
            if have_content or have_children:
                tag_format = '<{}>'
            else:
                tag_format = '<{} />'
            line = token.indentation + tag_format.format(tag_text)

            if have_content:
                # With content, close the tag in the same line.
                end_tag = '</{}>'.format(tag.name)
                content = self._eval(tag.content, context)
                line += content + end_tag

            result.append(line)

        return result
コード例 #6
0
    def _handle_line_token(self, t, context):
        handler = self.HANDLERS.get(t.line_type)
        assert \
            handler is not None, \
            'No handler for token of type "{}"'.format(t.line_type)

        result = []
        try:
            result += handler(t, after=False, context=context)
            result += self._handle_children(t.children, context=context)
            result += handler(t, after=True, context=context)
        except Exception as e:
            reraise(e, 'While handling line-token {}'.format(six.text_type(t)))
        return result
コード例 #7
0
ファイル: test_reraiseit.py プロジェクト: zerotk/reraiseit
    def RaiseExceptionUsingReraise(self):

        def raise_exception():
            execute_python_code(self.string_statement)
            pytest.fail('Should not reach here')

        def reraise_exception():
            try:
                raise_exception()
            except self.raised_exception as e:
                reraise(e, "While doing 'bar'")

        try:
            try:
                reraise_exception()
            except self.raised_exception as e1:
                reraise(e1, "While doing x:")
        except self.raised_exception as e2:
            reraise(e2, "While doing y:")
コード例 #8
0
def ImportModule(p_import_path):
    """
        Import the module in the given import path.

        * Returns the "final" module, so importing "coilib50.subject.visu" return the "visu"
        module, not the "coilib50" as returned by __import__
    """
    from zerotk.reraiseit import reraise
    try:
        result = __import__(p_import_path)
        for i in p_import_path.split('.')[1:]:
            try:
                result = getattr(result, i)
            except AttributeError as e:
                reraise(
                    e, 'AttributeError:  p_import_path: %s, %s has no %s' %
                    (p_import_path, result, i))
        return result
    except ImportError as e:
        reraise(e, 'Error importing module %s' % p_import_path)
コード例 #9
0
def _reorganize_imports(filename, refactor={}):
    """
    Reorganizes all import statements in the given filename, optionally performing a "move"
    refactoring.

    This is the main API for TerraForming.

    :param unicode filename:
        The file to perform the reorganization/refactoring. Note that the file is always
        rewritten by this algorithm, no backups. It's assumed that you're using a version
        control system.

    :param dict refactor:
        A dictionary mapping the moved symbols path. The keys are the current path, the values
        are the new path.
        Ex:
            {
                'coilbi50.basic.Bunch' : 'etk11.foundation.bunch.Bunch',
                'coilbi50.basic.interfaces' : 'etk11.interfaces',
            }

        Note that we do not support symbol renaming, only move. This means that the last part of
        the string must be the same. In the example, "Bunch" and "interface".

    :return boolean:
        Returns True if the file was changed.
    """
    from zerotk.terraformer import TerraFormer
    from zerotk.reraiseit import reraise

    try:
        terra = TerraFormer.Factory(filename)
        terra.ReorganizeImports(refactor=refactor)
        changed = terra.Save()
        return changed
    except Exception as e:
        reraise(e, 'On TerraForming.ReorganizeImports with filename: %s' % filename)
コード例 #10
0
ファイル: test_reraiseit.py プロジェクト: zerotk/reraiseit
 def reraise_exception():
     try:
         raise_exception()
     except self.raised_exception as e:
         reraise(e, "While doing 'bar'")
コード例 #11
0
ファイル: example.py プロジェクト: zerotk/reraiseit
from zerotk.reraiseit import reraise

try:
    raise RuntimeError('ops')
except Exception as e:
    reraise(e, 'While testing reraise.')