-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eec4560
commit 3b0e66c
Showing
6 changed files
with
226 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# coding: utf-8 | ||
from manage.utils import import_string | ||
|
||
|
||
def get_name(obj, default): | ||
default = default.split('.')[0] | ||
return getattr(obj, '__name__', default) | ||
|
||
|
||
def import_objects(manage_dict): | ||
auto_import = {} | ||
auto_scripts = [] | ||
import_dict = manage_dict.get('shell', {}).get('auto_import', {}) | ||
for name, spec in import_dict.get('objects', {}).items(): | ||
_obj = import_string(name) | ||
if spec: | ||
if 'init' in spec: | ||
method_name = spec['init'].keys()[0] | ||
args = spec['init'].get(method_name, {}).get('args', []) | ||
kwargs = spec['init'].get(method_name, {}).get('kwargs', {}) | ||
getattr(_obj, method_name)(*args, **kwargs) | ||
auto_import[spec.get('as', get_name(_obj, name))] = _obj | ||
if 'init_script' in spec: | ||
auto_scripts.append(spec['init_script']) | ||
else: | ||
auto_import[get_name(_obj, name)] = _obj | ||
for script in auto_scripts: | ||
exec(script, auto_import) | ||
return auto_import | ||
|
||
|
||
def exec_init(manage_dict, context): | ||
for name, spec in manage_dict['shell'].get('init', {}).items(): | ||
_obj = context.get(name, import_string(name)) | ||
args = spec.get('args', []) if spec else [] | ||
kwargs = spec.get('kwargs', {}) if spec else {} | ||
_obj(*args, **kwargs) | ||
|
||
|
||
def exec_init_script(manage_dict, context): | ||
if 'init_script' in manage_dict['shell']: | ||
exec(manage_dict['shell']['init_script'], context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,15 @@ | ||
default_manage_dict = { | ||
'shell': { | ||
'banner': { | ||
'enabled': True, | ||
'message': 'Hello World!' | ||
'project_name': 'Project', | ||
'shell': { # Preferences for 'manage shell' | ||
'banner': { # Banner is the message printed on top of console | ||
'enabled': True, # It can be disabled | ||
'message': 'Hello World!' # Here it goes the message | ||
}, | ||
'auto_import': { # Objects to be auto imported to shell context | ||
'display': True, # Weather to print all a list of all objects | ||
'objects': { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# coding: utf-8 | ||
import sys | ||
import importlib | ||
|
||
PY2 = sys.version_info[0] == 2 | ||
WIN = sys.platform.startswith('win') | ||
|
||
|
||
if PY2: | ||
# exec('def reraise(tp, value, tb=None):\n raise tp, value, tb') | ||
def reraise(tp, value, tb=None): | ||
raise tp, value, tb # noqa | ||
else: | ||
def reraise(tp, value, tb=None): | ||
if value.__traceback__ is not tb: | ||
raise value.with_traceback(tb) | ||
raise value | ||
|
||
|
||
def import_string(import_name, silent=False): | ||
"""Imports an object based on a string. This is useful if you want to | ||
use import paths as endpoints or something similar. An import path can | ||
be specified either in dotted notation (``xml.sax.saxutils.escape``) | ||
or with a colon as object delimiter (``xml.sax.saxutils:escape``). | ||
If `silent` is True the return value will be `None` if the import fails. | ||
:param import_name: the dotted name for the object to import. | ||
:param silent: if set to `True` import errors are ignored and | ||
`None` is returned instead. | ||
:return: imported object | ||
""" | ||
# force the import name to automatically convert to strings | ||
# __import__ is not able to handle unicode strings in the fromlist | ||
# if the module is a package | ||
import_name = str(import_name).replace(':', '.') | ||
try: | ||
try: | ||
__import__(import_name) | ||
except ImportError: | ||
if '.' not in import_name: | ||
raise | ||
else: | ||
return sys.modules[import_name] | ||
|
||
module_name, obj_name = import_name.rsplit('.', 1) | ||
try: | ||
module = __import__(module_name, None, None, [obj_name]) | ||
except ImportError: | ||
# support importing modules not yet set up by the parent module | ||
# (or package for that matter) | ||
module = import_string(module_name) | ||
|
||
try: | ||
return getattr(module, obj_name) | ||
except AttributeError as e: | ||
raise ImportError(e) | ||
|
||
except ImportError as e: | ||
if not silent: | ||
reraise( | ||
ImportStringError, | ||
ImportStringError(import_name, e), | ||
sys.exc_info()[2]) | ||
|
||
|
||
class ImportStringError(ImportError): | ||
|
||
"""Provides information about a failed :func:`import_string` attempt.""" | ||
|
||
#: String in dotted notation that failed to be imported. | ||
import_name = None | ||
#: Wrapped exception. | ||
exception = None | ||
|
||
def __init__(self, import_name, exception): | ||
self.import_name = import_name | ||
self.exception = exception | ||
|
||
msg = ( | ||
'import_string() failed for %r. Possible reasons are:\n\n' | ||
'- missing __init__.py in a package;\n' | ||
'- package or module path not included in sys.path;\n' | ||
'- duplicated package or module name taking precedence in ' | ||
'sys.path;\n' | ||
'- missing module, class, function or variable;\n\n' | ||
'Debugged import:\n\n%s\n\n' | ||
'Original exception:\n\n%s: %s') | ||
|
||
name = '' | ||
tracked = [] | ||
for part in import_name.replace(':', '.').split('.'): | ||
name += (name and '.') + part | ||
imported = import_string(name, silent=True) | ||
if imported: | ||
tracked.append((name, getattr(imported, '__file__', None))) | ||
else: | ||
track = ['- %r found in %r.' % (n, i) for n, i in tracked] | ||
track.append('- %r not found.' % name) | ||
msg = msg % (import_name, '\n'.join(track), | ||
exception.__class__.__name__, str(exception)) | ||
break | ||
|
||
ImportError.__init__(self, msg) | ||
|
||
def __repr__(self): | ||
return '<%s(%r, %r)>' % (self.__class__.__name__, self.import_name, | ||
self.exception) | ||
|
||
|
||
class DynamicObjectImporter(object): | ||
def __init__(self, module_name): | ||
self.module_name = module_name | ||
|
||
def __getattr__(self, item): | ||
return importlib.import_module( | ||
'{0}.{1}'.format(self.module_name, item)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters