本文整理汇总了Python中lib2to3.fixer_util.FromImport.prefix方法的典型用法代码示例。如果您正苦于以下问题:Python FromImport.prefix方法的具体用法?Python FromImport.prefix怎么用?Python FromImport.prefix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib2to3.fixer_util.FromImport
的用法示例。
在下文中一共展示了FromImport.prefix方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_is_shebang_comment
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def test_is_shebang_comment(self):
"""
Tests whether the fixer_util.is_encoding_comment() function is working.
"""
shebang_comments = [u"#!/usr/bin/env python\n" u"#!/usr/bin/python2\n", u"#! /usr/bin/python3\n"]
not_shebang_comments = [u"# I saw a giant python\n", u"# I have never seen a python2\n"]
for comment in shebang_comments:
node = FromImport(u"math", [Leaf(token.NAME, u"cos", prefix=" ")])
node.prefix = comment
self.assertTrue(is_shebang_comment(node))
for comment in not_shebang_comments:
node = FromImport(u"math", [Leaf(token.NAME, u"cos", prefix=" ")])
node.prefix = comment
self.assertFalse(is_shebang_comment(node))
示例2: test_is_shebang_comment
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def test_is_shebang_comment(self):
"""
Tests whether the libfuturize.fixer_util.is_shebang_comment() function is working
"""
node = FromImport(u'math', [Leaf(token.NAME, u'cos', prefix=" ")])
node.prefix = u'#!/usr/bin/env python\n'
self.assertTrue(is_shebang_comment(node))
示例3: transform
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def transform(self, node, results):
imp = results.get('imp')
if node.type == syms.import_from:
# Some imps are top-level (e.g., 'import ham')
# some are first level (e.g., 'import ham.eggs')
# some are third level (e.g., 'import ham.eggs as spam')
# Hence, the loop
while not hasattr(imp, 'value'):
imp = imp.children[0]
if imp.value.startswith("ui_"):
imp.value = u"." + imp.value
imp.changed()
else:
first = imp
if isinstance(imp, Node):
first = next(imp.leaves())
if not isinstance(first, Leaf):
return
if not first.value.startswith("ui_"):
return
new = FromImport(".", [imp])
new.prefix = node.prefix
return new
示例4: transform
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def transform(self, node, results):
if self.skip:
return
imp = results['imp']
if node.type == syms.import_from:
# Some imps are top-level (eg: 'import ham')
# some are first level (eg: 'import ham.eggs')
# some are third level (eg: 'import ham.eggs as spam')
# Hence, the loop
while not hasattr(imp, 'value'):
imp = imp.children[0]
if self.probably_a_local_import(imp.value):
imp.value = "." + imp.value
imp.changed()
else:
have_local = False
have_absolute = False
for mod_name in traverse_imports(imp):
if self.probably_a_local_import(mod_name):
have_local = True
else:
have_absolute = True
if have_absolute:
if have_local:
# We won't handle both sibling and absolute imports in the
# same statement at the moment.
self.warning(node, "absolute and local imports together")
return
new = FromImport(".", [imp])
new.prefix = node.prefix
return new
示例5: new_future_import
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def new_future_import(self, old):
new = FromImport("__future__",
[Name("absolute_import", prefix=" "), Comma(),
Name("division", prefix=" "), Comma(),
Name("print_function", prefix=" ")])
if old is not None:
new.prefix = old.prefix
return new
示例6: test_is_encoding_comment
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def test_is_encoding_comment(self):
"""
Tests whether the fixer_util.is_encoding_comment() function is working.
"""
encoding_comments = [
u"# coding: utf-8",
u"# encoding: utf-8",
u"# -*- coding: latin-1 -*-",
u"# vim: set fileencoding=iso-8859-15 :",
]
not_encoding_comments = [u"# We use the file encoding utf-8", u"coding = 'utf-8'", u"encoding = 'utf-8'"]
for comment in encoding_comments:
node = FromImport(u"math", [Leaf(token.NAME, u"cos", prefix=" ")])
node.prefix = comment
self.assertTrue(is_encoding_comment(node))
for comment in not_encoding_comments:
node = FromImport(u"math", [Leaf(token.NAME, u"cos", prefix=" ")])
node.prefix = comment
self.assertFalse(is_encoding_comment(node))
示例7: transform
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def transform(self, node, results):
"""
Copied from FixImport.transform(), but with this line added in
any modules that had implicit relative imports changed:
from __future__ import absolute_import"
"""
if self.skip:
return
imp = results['imp']
if node.type == syms.import_from:
# Some imps are top-level (eg: 'import ham')
# some are first level (eg: 'import ham.eggs')
# some are third level (eg: 'import ham.eggs as spam')
# Hence, the loop
while not hasattr(imp, 'value'):
imp = imp.children[0]
if self.probably_a_local_import(imp.value):
imp.value = u"." + imp.value
imp.changed()
future_import(u"absolute_import", node)
else:
have_local = False
have_absolute = False
for mod_name in traverse_imports(imp):
if self.probably_a_local_import(mod_name):
have_local = True
else:
have_absolute = True
if have_absolute:
if have_local:
# We won't handle both sibling and absolute imports in the
# same statement at the moment.
self.warning(node, "absolute and local imports together")
return
new = FromImport(u".", [imp])
new.prefix = node.prefix
future_import(u"absolute_import", node)
return new
示例8: future_import
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def future_import(feature, node):
"""
This seems to work
"""
root = find_root(node)
if does_tree_import(u"__future__", feature, node):
return
# Look for a shebang or encoding line
shebang_encoding_idx = None
for idx, node in enumerate(root.children):
# Is it a shebang or encoding line?
if is_shebang_comment(node) or is_encoding_comment(node):
shebang_encoding_idx = idx
if node.type == syms.simple_stmt and \
len(node.children) > 0 and node.children[0].type == token.STRING:
# skip over docstring
continue
names = check_future_import(node)
if not names:
# not a future statement; need to insert before this
break
if feature in names:
# already imported
return
import_ = FromImport(u'__future__', [Leaf(token.NAME, feature, prefix=" ")])
if shebang_encoding_idx == 0 and idx == 0:
# If this __future__ import would go on the first line,
# detach the shebang / encoding prefix from the current first line.
# and attach it to our new __future__ import node.
import_.prefix = root.children[0].prefix
root.children[0].prefix = u''
# End the __future__ import line with a newline and add a blank line
# afterwards:
children = [import_ , Newline()]
root.insert_child(idx, Node(syms.simple_stmt, children))
示例9: transform_member
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def transform_member(self, node, results):
"""Transform for imports of specific module elements. Replaces
the module to be imported from with the appropriate new
module.
"""
mod_member = results.get('mod_member')
pref = mod_member.prefix
member = results.get('member')
if member:
if isinstance(member, list):
member = member[0]
new_name = None
for change in MAPPING[mod_member.value]:
if member.value in change[1]:
new_name = change[0]
break
if new_name:
mod_member.replace(Name(new_name, prefix=pref))
else:
self.cannot_convert(node, 'This is an invalid module element')
else:
modules = []
mod_dict = {}
members = results['members']
for member in members:
if member.type == syms.import_as_name:
as_name = member.children[2].value
member_name = member.children[0].value
else:
member_name = member.value
as_name = None
if member_name != u',':
for change in MAPPING[mod_member.value]:
if member_name in change[1]:
if change[0] not in mod_dict:
modules.append(change[0])
mod_dict.setdefault(change[0], []).append(member)
new_nodes = []
indentation = find_indentation(node)
first = True
def handle_name(name, prefix):
if name.type == syms.import_as_name:
kids = [Name(name.children[0].value, prefix=prefix), name.children[1].clone(), name.children[2].clone()]
return [Node(syms.import_as_name, kids)]
return [Name(name.value, prefix=prefix)]
for module in modules:
elts = mod_dict[module]
names = []
for elt in elts[:-1]:
names.extend(handle_name(elt, pref))
names.append(Comma())
names.extend(handle_name(elts[-1], pref))
new = FromImport(module, names)
if not first or node.parent.prefix.endswith(indentation):
new.prefix = indentation
new_nodes.append(new)
first = False
if new_nodes:
nodes = []
for new_node in new_nodes[:-1]:
nodes.extend([new_node, Newline()])
nodes.append(new_nodes[-1])
node.replace(nodes)
else:
self.cannot_convert(node, 'All module elements are invalid')
return
示例10: transform_member
# 需要导入模块: from lib2to3.fixer_util import FromImport [as 别名]
# 或者: from lib2to3.fixer_util.FromImport import prefix [as 别名]
def transform_member(self, node, results):
"""Transform for imports of specific module elements. Replaces
the module to be imported from with the appropriate new
module.
"""
mod_member = results.get("mod_member")
pref = mod_member.prefix
member = results.get("member")
# Simple case with only a single member being imported
if member:
# this may be a list of length one, or just a node
if isinstance(member, list):
member = member[0]
new_name = None
for change in MAPPING[mod_member.value]:
if member.value in change[1]:
new_name = change[0]
break
if new_name:
mod_member.replace(Name(new_name, prefix=pref))
else:
self.cannot_convert(node, "This is an invalid module element")
# Multiple members being imported
else:
# a dictionary for replacements, order matters
modules = []
mod_dict = {}
members = results["members"]
for member in members:
# we only care about the actual members
if member.type == syms.import_as_name:
as_name = member.children[2].value
member_name = member.children[0].value
else:
member_name = member.value
as_name = None
if member_name != ",":
for change in MAPPING[mod_member.value]:
if member_name in change[1]:
if change[0] not in mod_dict:
modules.append(change[0])
mod_dict.setdefault(change[0], []).append(member)
new_nodes = []
indentation = find_indentation(node)
first = True
def handle_name(name, prefix):
if name.type == syms.import_as_name:
kids = [Name(name.children[0].value, prefix=prefix),
name.children[1].clone(),
name.children[2].clone()]
return [Node(syms.import_as_name, kids)]
return [Name(name.value, prefix=prefix)]
for module in modules:
elts = mod_dict[module]
names = []
for elt in elts[:-1]:
names.extend(handle_name(elt, pref))
names.append(Comma())
names.extend(handle_name(elts[-1], pref))
new = FromImport(module, names)
if not first or node.parent.prefix.endswith(indentation):
new.prefix = indentation
new_nodes.append(new)
first = False
if new_nodes:
nodes = []
for new_node in new_nodes[:-1]:
nodes.extend([new_node, Newline()])
nodes.append(new_nodes[-1])
node.replace(nodes)
else:
self.cannot_convert(node, "All module elements are invalid")