Changed variable_from_module to no longer return a random entry but all variables from a module. Added random_string_from_module to support the CONNECTION_SCREEN. Also fixed a bug in the connection_screen handling that occationally (randomly) would return an empty entry.
This commit is contained in:
parent
f9c47e7945
commit
87d621bcbc
2 changed files with 23 additions and 14 deletions
|
|
@ -21,7 +21,7 @@ MULTISESSION_MODE = settings.MULTISESSION_MODE
|
||||||
CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE
|
CONNECTION_SCREEN_MODULE = settings.CONNECTION_SCREEN_MODULE
|
||||||
CONNECTION_SCREEN = ""
|
CONNECTION_SCREEN = ""
|
||||||
try:
|
try:
|
||||||
CONNECTION_SCREEN = ansi.parse_ansi(utils.string_from_module(CONNECTION_SCREEN_MODULE))
|
CONNECTION_SCREEN = ansi.parse_ansi(utils.random_string_from_module(CONNECTION_SCREEN_MODULE))
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
if not CONNECTION_SCREEN:
|
if not CONNECTION_SCREEN:
|
||||||
|
|
|
||||||
|
|
@ -788,7 +788,7 @@ def variable_from_module(module, variable=None, default=None):
|
||||||
"""
|
"""
|
||||||
Retrieve a variable or list of variables from a module. The variable(s)
|
Retrieve a variable or list of variables from a module. The variable(s)
|
||||||
must be defined globally in the module. If no variable is given (or a
|
must be defined globally in the module. If no variable is given (or a
|
||||||
list entry is None), a random variable is extracted from the module.
|
list entry is None), all global variables are extracted from the module.
|
||||||
|
|
||||||
If module cannot be imported or given variable not found, default
|
If module cannot be imported or given variable not found, default
|
||||||
is returned.
|
is returned.
|
||||||
|
|
@ -798,7 +798,7 @@ def variable_from_module(module, variable=None, default=None):
|
||||||
variable (string or iterable) - single variable name or iterable of
|
variable (string or iterable) - single variable name or iterable of
|
||||||
variable names to extract
|
variable names to extract
|
||||||
default (string) - default value to use if a variable fails
|
default (string) - default value to use if a variable fails
|
||||||
to be extracted.
|
to be extracted. Ignored if variable is not given
|
||||||
Returns:
|
Returns:
|
||||||
a single value or a list of values depending on the type of
|
a single value or a list of values depending on the type of
|
||||||
'variable' argument. Errors in lists are replaced by the
|
'variable' argument. Errors in lists are replaced by the
|
||||||
|
|
@ -807,18 +807,20 @@ def variable_from_module(module, variable=None, default=None):
|
||||||
|
|
||||||
if not module:
|
if not module:
|
||||||
return default
|
return default
|
||||||
|
|
||||||
mod = mod_import(module)
|
mod = mod_import(module)
|
||||||
|
|
||||||
|
if variable:
|
||||||
result = []
|
result = []
|
||||||
for var in make_iter(variable):
|
for var in make_iter(variable):
|
||||||
if var:
|
if var:
|
||||||
# try to pick a named variable
|
# try to pick a named variable
|
||||||
result.append(mod.__dict__.get(var, default))
|
result.append(mod.__dict__.get(var, default))
|
||||||
else:
|
else:
|
||||||
# random selection
|
# get all
|
||||||
mvars = [val for key, val in mod.__dict__.items()
|
result = [val for key, val in mod.__dict__.items()
|
||||||
if not (key.startswith("_") or ismodule(val))]
|
if not (key.startswith("_") or ismodule(val))]
|
||||||
result.append((mvars and random.choice(mvars)) or default)
|
|
||||||
if len(result) == 1:
|
if len(result) == 1:
|
||||||
return result[0]
|
return result[0]
|
||||||
return result
|
return result
|
||||||
|
|
@ -833,9 +835,16 @@ def string_from_module(module, variable=None, default=None):
|
||||||
if isinstance(val, basestring):
|
if isinstance(val, basestring):
|
||||||
return val
|
return val
|
||||||
elif is_iter(val):
|
elif is_iter(val):
|
||||||
return [(isinstance(v, basestring) and v or default) for v in val]
|
result = [v for v in val if isinstance(v, basestring)]
|
||||||
|
return result if result else default
|
||||||
return default
|
return default
|
||||||
|
|
||||||
|
def random_string_from_module(module):
|
||||||
|
"""
|
||||||
|
Returns a random global string from a module
|
||||||
|
"""
|
||||||
|
string = random.choice(string_from_module(module))
|
||||||
|
return string
|
||||||
|
|
||||||
def init_new_player(player):
|
def init_new_player(player):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue