text
stringlengths
0
828
try:
__import__(package)
module = sys.modules[package]
except ImportError:
logger.exception('ImportError: %s' % package)
return None
except KeyError:
logger.exception('Could not find sys.modules package: %s' % package)
return None
except StandardError:
logger.exception('Unknown error occurred not import %s' % package)
return None
return module"
2013,"def packageSplit(filepath):
""""""
Determines the python path, and package information for the inputted
filepath.
:param filepath | <str>
:return (<str> path, <str> package)
""""""
filepath = nstr(filepath).strip().strip('.')
if not filepath:
return '', ''
basepath, module = os.path.split(nstr(filepath))
module = os.path.splitext(module)[0]
pathsplit = os.path.normpath(basepath).split(os.path.sep)
packagesplit = []
if module and module != '__init__':
packagesplit.append(module)
testpath = os.path.sep.join(pathsplit + ['__init__.py'])
while os.path.exists(testpath):
packagesplit.insert(0, pathsplit[-1])
pathsplit = pathsplit[:-1]
testpath = os.path.sep.join(pathsplit + ['__init__.py'])
return os.path.sep.join(pathsplit), '.'.join(packagesplit)"
2014,"def save(keystorerc=None, keystore=None, files=[], verbose=False):
'''create a keystore, compress and encrypt to file'''
config = None
if keystorerc:
config = config_reader.read(keystorerc)
if not config:
print('No configuration found.', file=sys.stderr)
sys.exit(-1)
elif keystore and len(files) > 0:
config = {
'keystore': keystore,
'files': files
}
if 'verbose' in config and config['verbose']:
verbose = True
keystore_path = None
if 'keystore' not in config:
print('.keystorerc needs to specify a keystore file path.', file=sys.stderr)
sys.exit(-1)
keystore_path = os.path.expanduser(config['keystore'])
if os.path.isdir(keystore_path):
print('keystore cannot be a folder: {}'.format(config['keystore']), file=sys.stderr)
sys.exit(-1)
elif not os.path.isfile(keystore_path):
# If keystore file does not exist already, attempt to create one
try:
pathlib.Path(keystore_path).touch()
except OSError as err:
print('keystore cannot be accessed: {}\n{}'.format(config['keystore'], err), file=sys.stderr)
sys.exit(-1)
# iterate through keys and add them here
keystore = {}
try:
for p in config['files']:
expanded_path = os.path.expanduser(p)
path = pathlib.Path(expanded_path)
if verbose: print('Inspecting {}:'.format(expanded_path))
if not path.exists():
print('Error: File or folder does not exist: {}'.format(p), file=sys.stderr)
sys.exit(-1)
if path.is_dir():
for dirpath, dirnames, filenames in os.walk(expanded_path):
for name in filenames:
fullpath = os.path.join(dirpath, name)
if verbose: print('Adding {} ...'.format(fullpath))
with open(fullpath, 'rb') as keyfile:
b64_bytes = base64.encodebytes(keyfile.read()).decode('utf-8')
keystore[fullpath] = b64_bytes
elif path.is_file():
fullpath = expanded_path