text
stringlengths
0
828
1247,"def to_unicode(value):
""""""Returns a unicode string from a string, using UTF-8 to decode if needed.
This function comes from `Tornado`_.
:param value:
A unicode or string to be decoded.
:returns:
The decoded string.
""""""
if isinstance(value, str):
return value.decode('utf-8')
assert isinstance(value, unicode)
return value"
1248,"def find_all_commands(management_dir):
""""""
Find all valid commands in a directory
management_dir : directory path
return - List of commands
""""""
try:
#Find all commands in the directory that are not __init__.py and end in .py. Then, remove the trailing .py
return [f[:-3] for f in os.listdir(management_dir) if f.endswith('.py') and not f.startswith(""__"")]
except OSError:
#If nothing is found, return empty
return []"
1249,"def find_commands_module(app_name):
""""""
Find the commands module in each app (if it exists) and return the path
app_name : The name of an app in the INSTALLED_APPS setting
return - path to the app
""""""
parts = app_name.split('.')
parts.append('commands')
parts.reverse()
part = parts.pop()
path = None
#Load the module if needed
try:
f, path, descr = imp.find_module(part, path)
except ImportError as e:
if os.path.basename(os.getcwd()) != part:
raise e
else:
try:
if f:
f.close()
except UnboundLocalError:
log.error(""Could not import module {0} at path {1}. Sys.path is {2}"".format(part, path, sys.path))
#Go down level by and level and try to load the module at each level
while parts:
part = parts.pop()
f, path, descr = imp.find_module(part, [path] if path else None)
if f:
f.close()
return path"
1250,"def get_commands():
""""""
Get all valid commands
return - all valid commands in dictionary form
""""""
commands = {}
#Try to load the settings file (settings can be specified on the command line) and get the INSTALLED_APPS
try:
from percept.conf.base import settings
apps = settings.INSTALLED_APPS
except KeyError:
apps = []
#For each app, try to find the command module (command folder in the app)
#Then, try to load all commands in the directory
for app_name in apps:
try:
path = find_commands_module(app_name)
commands.update(dict([(name, app_name) for name in find_all_commands(path)]))
except ImportError as e:
pass
return commands"
1251,"def execute(self):
""""""
Run the command with the command line arguments
""""""
#Initialize the option parser
parser = LaxOptionParser(
usage=""%prog subcommand [options] [args]"",
option_list=BaseCommand.option_list #This will define what is allowed input to the parser (ie --settings=)
)
#Parse the options
options, args = parser.parse_args(self.argv)
#Handle --settings and --pythonpath properly
options = handle_default_options(options)
try:
#Get the name of the subcommand