Exemplo n.º 1
0
 def transform(self, x):
     if isinstance(x, Node):
         method = 'transform' + x.__class__.__name__
         if hasattr(self, method):
             return getattr(self, method)(x)
         else:
             return self._transform_children(x)
     elif is_sequence(x) and not isinstance(x, basestring):
         return x.__class__(map(self.transform, x))
     else:
         return deepcopy(x)
Exemplo n.º 2
0
 def transform(self, x):
     if isinstance(x, Node):
         method = 'transform' + x.__class__.__name__
         if hasattr(self, method):
             return getattr(self, method)(x)
         else:
             return self._transform_children(x)
     elif is_sequence(x) and not isinstance(x, basestring):
         return x.__class__(map(self.transform, x))
     else:
         return deepcopy(x)
Exemplo n.º 3
0
def walk(x, visitor, walker=None, verbose=None):
    "Wrap compiler.walk to handle 'None' and sequences."

    # (Nodes are iterable and strings cause infinite regress)
    if x is None:
        return visitor
    elif isinstance(x, Node):
        return compiler.walk(x, visitor, walker, verbose)
    elif is_sequence(x) and not isinstance(x, basestring):
        for n in x:
            visitor = walk(n, visitor, walker, verbose)
        return visitor
    else:
        raise ValueError(x)
Exemplo n.º 4
0
def walk(x, visitor, walker=None, verbose=None):
    "Wrap compiler.walk to handle 'None' and sequences."

    # (Nodes are iterable and strings cause infinite regress)
    if x is None:
        return visitor
    elif isinstance(x, Node):
        return compiler.walk(x, visitor, walker, verbose)
    elif is_sequence(x) and not isinstance(x, basestring):
        for n in x:
            visitor = walk(n, visitor, walker, verbose)
        return visitor
    else:
        raise ValueError(x)
Exemplo n.º 5
0
    def __init__(self, x=(), file=None, **kw):
        super(Block, self).__init__(**kw)

        self.on_trait_change(self._structure_changed, 'sub_blocks')
        self.on_trait_change(self._structure_changed, 'sub_blocks_items')
        self.on_trait_change(self._structure_changed, 'ast')

        # Interpret arguments:

        is_file_object = lambda o: hasattr(o, 'read') and hasattr(o, 'name')

        # 'file' -> 'x'
        if file is not None:
            # Turn 'file' into a file object and move to 'x'
            if isinstance(file, basestring):
                file = open(file)
            elif not is_file_object(file):
                raise ValueError("Expected 'file' to be a file or string, "
                                 "got %r" % f)
            x = file

        # 'x': file object -> string
        saved_filename = None
        if is_file_object(x):
            self.filename = x.name
            saved_filename = self.filename
            x = x.read()

        # 'x' -> 'self.ast' or 'self.sub_blocks'
        if isinstance(x, basestring):
            # (BlockTransformer handles things like 'import *')
            self.ast = parse(x, mode='exec', transformer=BlockTransformer())
        elif isinstance(x, Node):
            self.ast = x
        elif is_sequence(x):
            self.sub_blocks = map(to_block, x)
        else:
            raise ValueError('Expecting string, Node, or sequence. Got %r' % x)

        # We really want to keep the filename for "pristine" blocks, and
        # _structure_changed nukes it most times
        self.filename = saved_filename