def load(): try: if context().get_storage_file().exists(): with context().get_storage_file().open('rb') as f: _store.update(pickle.load(f)) else: _persist() except (pickle.PickleError, IOError) as e: raise McmdError("Unable to read storage from file: {}".format( e.message))
def _install(default_config): mcmd.io.io.info("Looks like this is your first time running {}!\n " "Let's take a moment to set things up. It's OK to leave some fields empty, you can always change " "them later.".format(highlight("Molgenis Commander"))) mcmd.io.io.newline() for configurer in property_configurers().values(): configurer(default_config) config.set_config(default_config, context().get_properties_file()) mcmd.io.io.newline() mcmd.io.io.info( 'The configuration file has been created at {}'.format(highlight(str(context().get_properties_file()))))
def _upgrade(default_config, user_config): mcmd.io.io.info("Some properties haven't been configured yet. Let's take a moment to fix that.") mcmd.io.io.newline() for prop, configurer in property_configurers().items(): if prop not in user_config: configurer(default_config) config.set_config(default_config, context().get_properties_file()) mcmd.io.io.newline() mcmd.io.io.info( 'The configuration file has been updated successfully ({})'.format( highlight(str(context().get_properties_file())))) exit(0)
def set_(args): """ set sets the specified row of the specified table (or setting of specified settings table) to the specified value :param args: command line arguments containing: the settings type, the setting to set, and the value to set it to, if not a setting also the --for (which row to alter) if --from-path or --from-resource is passed the value is assumed to be a file containing the value data to be set :return: None """ value = args.value value_desc = highlight(value) if args.from_path: path = Path(args.value) value = files.read_file(path) value_desc = 'contents of {}'.format(highlight(args.value)) elif args.from_resource: path = files.select_file_from_folders(context().get_resource_folders(), args.value) value = files.read_file(path) value_desc = 'contents of {}'.format(highlight(args.value)) if args.for_: entity = args.type row = args.for_ io.start('Updating {} of {} for id {} to {}'.format( highlight(args.attribute), highlight(args.type), highlight(args.for_), value_desc)) else: entity = _get_settings_entity(args.type) io.start('Updating {} of {} settings to {}'.format( highlight(args.attribute), highlight(args.type), value_desc)) row = _get_first_row_id(entity) url = api.rest1('{}/{}/{}'.format(entity, row, args.attribute)) put(url, json.dumps(value))
def _remove_script(script_name): path = context().get_scripts_folder().joinpath(script_name) _check_script_exists(path) try: io.start('Removing script %s' % highlight(script_name)) path.unlink() except OSError as e: raise McmdError('Error removing script: %s' % str(e))
def _get_script(args): if args.from_path: script = Path(args.script) else: script = context().get_scripts_folder().joinpath(args.script) if not script.exists(): raise McmdError("The script {} doesn't exist".format(script)) return script
def _read_script(script_name): path = context().get_scripts_folder().joinpath(script_name) _check_script_exists(path) try: with path.open() as f: for line in f.readlines(): log.info(line.strip()) except OSError as e: raise McmdError('Error reading script: %s' % str(e))
def load_config(): yaml = YAML() default_config = _try_load_yaml(yaml, _DEFAULT_PROPERTIES) if _is_install_required(): _install(default_config) user_config = _try_load_yaml(yaml, context().get_properties_file()) if _is_upgrade_required(user_config): _upgrade(default_config, user_config) # merge the configs so that new properties and list items are added _merge(default_config, user_config) # pass result to the config module and save to disk config.set_config(default_config, context().get_properties_file())
def write(arg_string, success): try: history = open(str(context().get_history_file()), 'a') indicator = _INDICATOR_SUCCESS if not success: indicator = _INDICATOR_FAILURE history.write('%s %s\n' % (indicator, arg_string)) except OSError as e: raise McmdError("Error writing to history: %s" % str(e))
def _input_script_name(): file_name = '' while not file_name: name = mcmd.io.ask.input_('Supply the name of the script:') if context().get_scripts_folder().joinpath(name).exists(): overwrite = confirm('%s already exists. Overwrite?' % name) if overwrite: file_name = name else: file_name = name return file_name
def load_config(): yaml = YAML() default_config = _try_load_yaml(yaml, _DEFAULT_PROPERTIES) if _is_install_required(): if os.getenv('MCMD_INSTALL_NON_INTERACTIVE', 'False').lower() == 'true': _install_non_interactive(default_config) else: _install(default_config) exit(0) user_config = _try_load_yaml(yaml, context().get_properties_file()) if _is_upgrade_required(user_config): _upgrade(default_config, user_config) # merge the configs so that new properties and list items are added _merge(default_config, user_config) # pass result to the config module and save to disk config.set_config(default_config, context().get_properties_file())
def read(num_lines, include_fails): lines = deque() if not context().get_history_file().is_file(): return lines try: with open(str(context().get_history_file()), 'r') as history: for line in history: line = line.rstrip('\n') if line.startswith(_INDICATOR_FAILURE): if include_fails: lines.append((False, line[2:])) else: lines.append((True, line[2:])) if len(lines) > num_lines: lines.popleft() except OSError as e: raise McmdError("Error reading from history: %s" % str(e)) return lines
def _create_script(args): lines = history.read(args.number, args.show_fails) if len(lines) == 0: log.info('History is empty.') return options = [line[1] for line in lines] commands = mcmd.io.ask.checkbox('Pick the lines that will form the script:', options) file_name = _input_script_name() try: with open(str(context().get_scripts_folder().joinpath(file_name)), 'w') as script_file: for cmd in commands: script_file.write(cmd + '\n') except OSError as e: raise McmdError("Error writing to script: %s" % str(e))
def _download_attachment(attachment, issue_num): issue_folder = context().get_issues_folder().joinpath(issue_num) issue_folder.mkdir(parents=True, exist_ok=True) file_path = issue_folder.joinpath(attachment.name) if file_path.exists(): overwrite = mcmd.io.ask.confirm( 'File %s already exists. Re-download?' % file_path.name) if not overwrite: return file_path io.start('Downloading %s from GitHub issue %s' % (highlight(attachment.name), highlight('#' + issue_num))) try: r = requests.get(attachment.url) r.raise_for_status() with file_path.open('wb') as f: f.write(r.content) except (OSError, requests.RequestException, requests.HTTPError) as e: raise McmdError('Error downloading GitHub attachment: %s' % str(e)) io.succeed() return file_path
def _list_scripts(): for path in context().get_scripts_folder().iterdir(): if not path.name.startswith('.'): log.info(path.name)
def clear(): try: open(str(context().get_history_file()), 'w').close() except OSError as e: raise McmdError("Error clearing history: %s" % str(e))
def _is_install_required(): return not context().get_properties_file().exists() or context().get_properties_file().stat().st_size == 0
def _persist(): try: with context().get_storage_file().open('wb') as f: pickle.dump(_store, f) except (pickle.PickleError, IOError) as e: raise McmdError("Unable to save storage to file: {}".format(e.message))
def _get_path_from_quick_folders(file_name): file_name = os_path.splitext(file_name)[0] file_map = scan_folders_for_files(context().get_resource_folders()) path = select_path(file_map, file_name) return str(path)
def _import_from_quick_folders(args): file_name = os_path.splitext(args.resource)[0] path = files.select_file_from_folders(folders=context().get_git_folders() + context().get_dataset_folders(), file_name=file_name) _do_import(path, args.to_package, args.entity_type_id, args.import_action)
def _get_path_from_quick_folders(file_name): file_name = os_path.splitext(file_name)[0] path = file_utils.select_file_from_folders( context().get_resource_folders(), file_name) return str(path)
def _install_non_interactive(default_config): config.set_config(default_config, context().get_properties_file()) config.set_non_interactive(True) mcmd.io.io.info( 'The configuration file has been created at {}'.format(highlight(str(context().get_properties_file()))))
def _import_from_quick_folders(args): file_name = os_path.splitext(args.resource)[0] file_map = scan_folders_for_files(context().get_git_folders() + context().get_dataset_folders()) path = select_path(file_map, file_name) _do_import(path, args.to_package, args.entity_type_id)