def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
python类absolute_import()的实例源码
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:cryptogram
作者: xinmingzhang
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:Repobot
作者: Desgard
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def visit_from(self, node):
if node.modname == '__future__':
for name, _ in node.names:
if name == 'division':
self._future_division = True
elif name == 'absolute_import':
self._future_absolute_import = True
elif not self._future_absolute_import:
self.add_message('no-absolute-import', node=node)
def _init(Module, MdSpi, TraderSpi):
globals()['ApiStruct'] = __import__(__name__+'.ApiStruct', None, None, 'x')
class LazyProperty(object):
def __get__(self, obj, cls):
if obj is None: return self
value = self.fget()
name = self.fget.__name__
setattr(obj, name, value)
delattr(cls, name)
return value
def lazy_property(func):
self = LazyProperty()
setattr(Module, func.__name__, self)
self.fget = func
return self
@lazy_property
def MdApi():
from ._MdApi import _init, MdApi; _init(ApiStruct)
return type('MdApi', (MdApi,), MdSpi)
@lazy_property
def TraderApi():
from ._TraderApi import _init, TraderApi; _init(ApiStruct)
return type('TraderApi', (TraderApi,), TraderSpi)
def _init(Module, MdSpi, TraderSpi):
globals()['ApiStruct'] = __import__(__name__+'.ApiStruct', None, None, 'x')
class LazyProperty(object):
def __get__(self, obj, cls):
if obj is None: return self
value = self.fget()
name = self.fget.__name__
setattr(obj, name, value)
delattr(cls, name)
return value
def lazy_property(func):
self = LazyProperty()
setattr(Module, func.__name__, self)
self.fget = func
return self
@lazy_property
def MdApi():
from ._MdApi import _init, MdApi; _init(ApiStruct)
return type('MdApi', (MdApi,), MdSpi)
@lazy_property
def TraderApi():
from ._TraderApi import _init, TraderApi; _init(ApiStruct)
return type('TraderApi', (TraderApi,), TraderSpi)
def _init(Module, MdSpi, TraderSpi):
globals()['ApiStruct'] = __import__(__name__+'.ApiStruct', None, None, 'x')
class LazyProperty(object):
def __get__(self, obj, cls):
if obj is None: return self
value = self.fget()
name = self.fget.__name__
setattr(obj, name, value)
delattr(cls, name)
return value
def lazy_property(func):
self = LazyProperty()
setattr(Module, func.__name__, self)
self.fget = func
return self
@lazy_property
def MdApi():
from ._MdApi import _init, MdApi; _init(ApiStruct)
return type('MdApi', (MdApi,), MdSpi)
@lazy_property
def TraderApi():
from ._TraderApi import _init, TraderApi; _init(ApiStruct)
return type('TraderApi', (TraderApi,), TraderSpi)
def _init(Module, MdSpi, TraderSpi):
globals()['ApiStruct'] = __import__(__name__+'.ApiStruct', None, None, 'x')
class LazyProperty(object):
def __get__(self, obj, cls):
if obj is None: return self
value = self.fget()
name = self.fget.__name__
setattr(obj, name, value)
delattr(cls, name)
return value
def lazy_property(func):
self = LazyProperty()
setattr(Module, func.__name__, self)
self.fget = func
return self
@lazy_property
def MdApi():
from ._MdApi import _init, MdApi; _init(ApiStruct)
return type('MdApi', (MdApi,), MdSpi)
@lazy_property
def TraderApi():
from ._TraderApi import _init, TraderApi; _init(ApiStruct)
return type('TraderApi', (TraderApi,), TraderSpi)
def visit_from(self, node):
if node.modname == '__future__':
for name, _ in node.names:
if name == 'division':
self._future_division = True
elif name == 'absolute_import':
self._future_absolute_import = True
elif not self._future_absolute_import:
self.add_message('no-absolute-import', node=node)
def test_all_exports_correspond_to_plugins(self):
exports = [name for name in dir(tb.summary) if not name.startswith('_')]
futures = frozenset(('absolute_import', 'division', 'print_function'))
bad_exports = [
name for name in exports
if name not in futures and not any(
name == plugin or name.startswith('%s_' % plugin)
for plugin in STANDARD_PLUGINS)
]
if bad_exports:
self.fail(
'The following exports do not correspond to known standard '
'plugins: %r. Please mark these as private by prepending an '
'underscore to their names, or, if they correspond to a new '
'plugin that you are certain should be part of the public API '
'forever, add that plugin to the STANDARD_PLUGINS set in this '
'module.' % bad_exports)
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:UMOG
作者: hsab
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
def strip_future_imports(self, code):
"""
Strips any of these import lines:
from __future__ import <anything>
from future <anything>
from future.<anything>
from builtins <anything>
or any line containing:
install_hooks()
or:
install_aliases()
Limitation: doesn't handle imports split across multiple lines like
this:
from __future__ import (absolute_import, division, print_function,
unicode_literals)
"""
output = []
# We need .splitlines(keepends=True), which doesn't exist on Py2,
# so we use this instead:
for line in code.split('\n'):
if not (line.startswith('from __future__ import ')
or line.startswith('from future ')
or line.startswith('from builtins ')
or 'install_hooks()' in line
or 'install_aliases()' in line
# but don't match "from future_builtins" :)
or line.startswith('from future.')):
output.append(line)
return '\n'.join(output)
def transform(self, node, results):
future_import(u"unicode_literals", node)
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)
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
fix_add__future__imports_except_unicode_literals.py 文件源码
项目:blackmamba
作者: zrzka
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def transform(self, node, results):
# Reverse order:
future_import(u"print_function", node)
future_import(u"division", node)
future_import(u"absolute_import", node)