def show_set(args): """ Shows aliases stored in sets """ show_all = args.all columns = args.columns view_format = args.format if show_all is True: names = get_files_in_path(data_dir_path()) else: if not args.name: print("Show operation needs -a/--all flag or a name") sys.exit(1) names = args.name names_count = len(names) if columns is None: columns = ALIAS_COLUMNS for index, name in enumerate(names): try: if names_count > 1: print("%s:" % name) view_data = load_json_from_file(name, data_dir_path()) if view_format == JSON_FORMAT: print_json_in_console(view_data, columns) elif view_format == TABLE_FORMAT: print_table_in_console(view_data, columns) except Exception as err: print("Operation failed: " + str(err)) if index < names_count - 1: print_divider()
def add_alias(args): """ add new alias to a set """ set_name = args.set_name name = args.name command = args.command description = args.description tags = args.tags is_active = args.is_active try: if path_exists(data_dir_path() + set_name) is False: raise Exception("'{}' set does not exist".format(set_name)) set_aliases = load_json_from_file(set_name, data_dir_path()) for alias in set_aliases: if NAME_ATTRIBUTE in alias.keys() and name == alias[NAME_ATTRIBUTE]: raise Exception("'{}' alias is already in set '{}'".format(name, set_name)) alias = { NAME_ATTRIBUTE: name, COMMAND_ATTRIBUTE: command, DESCRIPTION_ATTRIBUTE: description, TAGS_ATTRIBUTE: tags, IS_ACTIVE_ATTRIBUTE: is_active, } set_aliases.append(alias) print_json_in_file(set_aliases, ALIAS_COLUMNS, data_dir_path() + set_name) print_table_in_console(set_aliases, ALIAS_COLUMNS) except Exception as e: print("Operation failed: {}".format(str(e))) sys.exit(1)
def delete_set(args): """ Deletes an alias set """ delete_all = args.all if delete_all is True: names = get_files_in_path(data_dir_path()) else: if not args.name: print("Delete operation needs -a/--all flag or a name") sys.exit(1) names = args.name yes = args.yes if yes is False: assurance_answer = input( "Are you sure you want to continue? Enter \"yes\" or \"y\" to confirm. " ) if assurance_answer == "yes" or assurance_answer == "y": yes = True if yes is True: for name in names: try: remove_file(data_dir_path() + name) print("deleted \"%s\" successfully" % name) except FileNotFoundError: print("\"%s\" not found" % name) sys.exit(1) except Exception as e: print("Something went wrong: " + str(e)) sys.exit(1)
def remove_aliases(args): """ remove aliases from a set """ aliases_names = args.name confirmation = args.yes set_name = args.set_name try: if path_exists(data_dir_path() + set_name) is False: raise Exception("'{}' set does not exist".format(set_name)) set_aliases = load_json_from_file(set_name, data_dir_path()) set_names_list = [alias[NAME_ATTRIBUTE] for alias in set_aliases] found_aliases = [] not_found_aliases = [] for alias_name in aliases_names: if alias_name in set_names_list: found_aliases.append(alias_name) else: not_found_aliases.append(alias_name) if not_found_aliases: print("{} not found in the set".format(not_found_aliases)) if not found_aliases: raise Exception("You didn't pass a valid existing aliases' names") print("{} found in the set".format(found_aliases)) if confirmation is False: assurance_answer = input( "Are you sure you want to continue? Enter \"yes\" or \"y\" to confirm. " ) if assurance_answer == "yes" or assurance_answer == "y": confirmation = True if confirmation is True: remaining_aliases = list( filter( lambda alias: alias[NAME_ATTRIBUTE] not in found_aliases, set_aliases)) print_json_in_file(remaining_aliases, ALIAS_COLUMNS, data_dir_path() + set_name) print_table_in_console(remaining_aliases, ALIAS_COLUMNS) except Exception as e: print("Operation failed: {}".format(str(e))) sys.exit(1)
def edit_alias(args): """ edit alias in a set """ set_name = args.set_name alias_name = args.alias_name new_name = args.name new_command = args.command new_description = args.description new_tags = args.tags new_is_active = args.is_active try: if all(value is None for value in [new_name, new_command, new_description, new_tags, new_is_active]): raise Exception("Need to pass something for edit to work".format(set_name)) if path_exists(data_dir_path() + set_name) is False: raise Exception("'{}' set does not exist".format(set_name)) set_aliases = load_json_from_file(set_name, data_dir_path()) target_alias = None for alias in set_aliases: if alias_name == alias[NAME_ATTRIBUTE]: target_alias = alias break if target_alias is None: raise Exception("'{}' is not a valid alias name".format(alias_name)) if new_name != alias_name: for alias in set_aliases: if new_name == alias[NAME_ATTRIBUTE]: raise Exception("'{}' new alias name already used".format(new_name)) target_alias[NAME_ATTRIBUTE] = new_name if new_name is not None else target_alias[NAME_ATTRIBUTE] target_alias[COMMAND_ATTRIBUTE] = new_command if new_command is not None else target_alias[COMMAND_ATTRIBUTE] target_alias[DESCRIPTION_ATTRIBUTE] = new_description if new_description is not None \ else target_alias[DESCRIPTION_ATTRIBUTE] target_alias[TAGS_ATTRIBUTE] = new_tags if new_tags is not None else target_alias[TAGS_ATTRIBUTE] target_alias[IS_ACTIVE_ATTRIBUTE] = new_is_active if new_is_active is not None \ else target_alias[IS_ACTIVE_ATTRIBUTE] print_json_in_file(set_aliases, ALIAS_COLUMNS, data_dir_path() + set_name) print_table_in_console(set_aliases, ALIAS_COLUMNS) except Exception as e: print("Operation failed: {}".format(str(e))) sys.exit(1)
def create_set(args): """ Creates a new aliases set """ name = args.name if path_exists(data_dir_path() + name) is True: print("'%s' set already exists" % name) sys.exit(1) else: try: print_json_in_file([], ALIAS_COLUMNS, data_dir_path() + name) print("'%s' set created successfully" % name) args.format = "table" args.validity = False list_sets(args) except Exception as err: print("Failed to create set '%s'" % str(err)) sys.exit(1)
def import_set(args): """ Import set operation """ name = args.name source = args.source overwrite = args.overwrite replace = args.replace path, file = separate_file_from_path(source) try: imported_data = pre_process_set_data(file, path) if path_exists(data_dir_path() + name) is not True or overwrite is True: # If set is new or overwrite is true then put data directly print_json_in_file(imported_data, ALIAS_COLUMNS, data_dir_path() + name) else: try: set_data = pre_process_set_data(name, data_dir_path()) if replace is True: result = append_aliases_with_replacing_repetitive( imported_data, set_data) else: result = handle_conflict(set_data + imported_data, attach_index_to_name) print_json_in_file(result, ALIAS_COLUMNS, data_dir_path() + name) except Exception as err: raise Exception(str(err)) sys.exit(1) except Exception as err: print("Importing '{}' into '{}' failed: {}".format( file, name, str(err))) sys.exit(1)
def list_sets(args): """ Lists all aliases sets """ view_format = args.format show_validity = args.validity json_files = get_files_in_path(data_dir_path()) headers = [__NAME__] view_data = [{ __NAME__: json_file, __STATUS__: None } for json_file in json_files] if show_validity is True: headers.append(__STATUS__) for json_data in view_data: try: load_json_from_file(json_data[__NAME__], data_dir_path()) json_data[__STATUS__] = __VALID_STATUS__ except Exception: json_data[__STATUS__] = __INVALID_STATUS__ if view_format == JSON_FORMAT: print_json_in_console(view_data, headers) elif view_format == TABLE_FORMAT: print_table_in_console(view_data, headers)
def export_set(args): """ Export aliases sets """ destination = args.destination view_format = args.format names = args.name package = args.package ignore_conflict = args.ignore_conflict overwrite = args.overwrite if package is not None: # Export all sets into a single file destination_file = destination + package if path_exists(destination_file) and overwrite is not True: print( "'{}' already exists in destination".format(destination_file)) else: all_aliases = [] for name in names: try: aliases_set = load_json_from_file(name, data_dir_path()) for alias in aliases_set: temp = {} for attribute in ALIAS_ATTRIBUTES_DEFAULTS.keys(): temp[attribute] = alias[attribute] if attribute in alias \ else ALIAS_ATTRIBUTES_DEFAULTS[attribute] temp[SET_NAME_ATTRIBUTE] = name all_aliases.append(temp) except Exception as err: print("Failed to export '{}': {}".format(name, str(err))) continue try: # Remove invalid aliases from list all_aliases = remove_non_valid_aliases(all_aliases) if ignore_conflict is True: # Set alias name to set_name + alias_name all_aliases = handle_conflict(all_aliases, change_name_handler) else: # Delete conflicted aliases all_aliases = handle_conflict(all_aliases, delete_an_element_handler) if view_format == JSON_FORMAT: print_json_in_file(all_aliases, ALIAS_COLUMNS, destination_file) elif view_format == TABLE_FORMAT: print_table_in_file(all_aliases, ALIAS_COLUMNS, destination_file) print("Exported '{}' successfully".format(package)) except Exception as err: print("Failed to export package '{}': {}".format( package, str(err))) sys.exit(1) else: # Export the sets separately for name in names: try: destination_file = destination + name data = load_json_from_file(name, data_dir_path()) if path_exists(destination_file) and overwrite is not True: raise Exception("file already exits in destination") if view_format == JSON_FORMAT: output, err, rc = execute_shell_command("cp {} {}".format( data_dir_path() + name, destination_file)) if rc != 0: raise Exception(err) elif view_format == TABLE_FORMAT: print_table_in_file(data, ALIAS_COLUMNS, destination_file) print("Exported '{}' Successfully".format(name)) except Exception as err: print("Failed to export '{}': {}".format(name, str(err)))