コード例 #1
0
def build_new_name_node(*, old_node, attach: bool, new_name: str, old_name: str = None):
    # build new node from new_name
    if '.' in new_name:
        children = []
        for part in bowler_helpers.dotted_parts(new_name):
            if part == '.':
                children.append(fixer_util.Dot())
            else:
                children.append(fixer_util.Name(part))
    else:
        children = [fixer_util.Name(new_name)]

    # attach to the new node subimports from the old module
    if attach and type(old_node) is fissix_pytree.Node:
        original_name_size = len(bowler_helpers.dotted_parts(old_name))
        for part in old_node.children[original_name_size:]:
            if part.value == '.':
                children.append(fixer_util.Dot())
            else:
                children.append(fixer_util.Name(part.value))

    return fissix_pytree.Node(
        type=fixer_util.syms.dotted_name,
        children=children,
        prefix=old_node.prefix,
    )
コード例 #2
0
    def transform_iter(self, node, results):
        """Call six.(iter|view)items() and friends."""
        # Make sure six is imported.
        libmodernize.touch_import(None, u'six', node)

        # Copy of self.transform() from fissix.fix_dict with some changes to
        # use the six.* methods.

        head = results['head']
        method = results['method'][0]  # Extract node for method name
        tail = results['tail']
        syms = self.syms
        method_name = method.value
        name = fixer_util.Name(u'six.' + method_name, prefix=node.prefix)
        assert method_name.startswith((u'iter', u'view')), repr(method)
        assert method_name[4:] in (u'keys', u'items', u'values'), repr(method)
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        new = pytree.Node(syms.power, head)
        new.prefix = u''
        new = fixer_util.Call(name, [new])
        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
コード例 #3
0
    def transform_iter(self, node, results):
        """Call six.(iter|view)items() and friends."""
        # Make sure six is imported.
        libmodernize.touch_import(None, "six", node)

        # Copy of self.transform() from fissix.fix_dict with some changes to
        # use the six.* methods.

        head = results["head"]
        method = results["method"][0]  # Extract node for method name
        tail = results["tail"]
        syms = self.syms
        method_name = method.value
        name = fixer_util.Name("six." + method_name, prefix=node.prefix)
        assert method_name.startswith(("iter", "view")), repr(method)
        assert method_name[4:] in ("keys", "items", "values"), repr(method)
        head = [n.clone() for n in head]
        tail = [n.clone() for n in tail]
        new = pytree.Node(syms.power, head)
        new.prefix = ""
        new = fixer_util.Call(name, [new])
        if tail:
            new = pytree.Node(syms.power, [new] + tail)
        new.prefix = node.prefix
        return new
コード例 #4
0
    def _modify_power(self, node):
        prefix = node.children[0].prefix

        # remove old prefix
        parts = bowler_helpers.dotted_parts(self.old_name)
        for _ in range((len(parts) + 1) // 2):
            node.children.pop(0)

        # add new prefix
        name = self.new_name.split('.', maxsplit=1)[0]
        head = fixer_util.Name(name, prefix=prefix)
        children = []
        for part in bowler_helpers.dotted_parts(self.new_name)[2::2]:
            children.append(fissix_pytree.Node(
                type=fixer_util.syms.trailer,
                children=[fixer_util.Dot(), fixer_util.Name(part)],
            ))
        node.children = [head] + children + node.children
コード例 #5
0
 def __call__(self, node: 'LN', capture: 'Capture', filename: 'Filename') -> None:
     old_node = capture['module_name']
     new_node = fissix_pytree.Node(
         type=fixer_util.syms.dotted_as_name,
         children=[
             build_new_name_node(
                 old_node=old_node,
                 new_name=self.new_name,
                 attach=False,
             ),
             fixer_util.Name('as', prefix=' '),
             old_node.clone(),
         ],
     )
     old_node.replace(new_node)
コード例 #6
0
 def transform(self, node, results):
     fixer_util.touch_import(None, "six", node)
     return fixer_util.Name("six.text_type", prefix=node.prefix)
コード例 #7
0
 def transform(self, node, results):
     touch_import(None, 'six', node)
     pair = results['pair']
     pair.replace(fixer_util.Name('six.integer_types', prefix=pair.prefix))
コード例 #8
0
 def transform(self, node, results):
     fixer_util.touch_import(None, "six", node)
     pair = results["pair"]
     pair.replace(fixer_util.Name("six.integer_types", prefix=pair.prefix))
コード例 #9
0
ファイル: fix_basestring.py プロジェクト: graingert/modernize
 def transform(self, node, results):
     libmodernize.touch_import(None, "six", node)
     return fixer_util.Name("six.string_types", prefix=node.prefix)