Added a new fuzzy module load mechanism
This commit is contained in:
parent
ac9c68b877
commit
8e8d85a4fe
1 changed files with 25 additions and 1 deletions
|
|
@ -12,6 +12,7 @@ import imp
|
||||||
import types
|
import types
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
import importlib
|
||||||
import textwrap
|
import textwrap
|
||||||
import datetime
|
import datetime
|
||||||
import random
|
import random
|
||||||
|
|
@ -871,6 +872,30 @@ def random_string_from_module(module):
|
||||||
"""
|
"""
|
||||||
return random.choice(string_from_module(module))
|
return random.choice(string_from_module(module))
|
||||||
|
|
||||||
|
def fuzzy_import_from_module(path, variable, default=None, defaultpaths=None):
|
||||||
|
"""
|
||||||
|
Import a variable based on a fuzzy path. First the literal
|
||||||
|
path will be tried, then all given defaultdirs will be
|
||||||
|
prepended to see a match is found.
|
||||||
|
|
||||||
|
path - full or partial python path
|
||||||
|
variable - name of variable to import from module
|
||||||
|
defaultpaths - an iterable of python paths to attempt
|
||||||
|
in order if importing directly from
|
||||||
|
path does not work.
|
||||||
|
"""
|
||||||
|
paths = [path] + make_iter(defaultpaths)
|
||||||
|
for modpath in paths:
|
||||||
|
try:
|
||||||
|
mod = importlib.import_module(path)
|
||||||
|
except ImportError, ex:
|
||||||
|
if not str(ex) == "No module named %s" % path:
|
||||||
|
# this means the module was found but it
|
||||||
|
# triggers an ImportError on import.
|
||||||
|
raise ex
|
||||||
|
return getattr(mod, variable, default)
|
||||||
|
return default
|
||||||
|
|
||||||
def init_new_player(player):
|
def init_new_player(player):
|
||||||
"""
|
"""
|
||||||
Helper method to call all hooks, set flags etc on a newly created
|
Helper method to call all hooks, set flags etc on a newly created
|
||||||
|
|
@ -1013,7 +1038,6 @@ def format_table(table, extra_space=1):
|
||||||
for icol, col in enumerate(table)])
|
for icol, col in enumerate(table)])
|
||||||
return ftable
|
return ftable
|
||||||
|
|
||||||
|
|
||||||
def get_evennia_pids():
|
def get_evennia_pids():
|
||||||
"""
|
"""
|
||||||
Get the currently valids PIDs (Process IDs) of the Portal and Server
|
Get the currently valids PIDs (Process IDs) of the Portal and Server
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue