def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}Info is in charge{0}\\".format(os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) assert self.context provider = self.context.provider assert isinstance(provider, AlchemyProvider) #process arguments obj_name, obj_type = self._process_arguments(args) path = self.context.prepare_path(obj_name) # more likely obj_name is path to dir or table if not obj_name: log.warning("No path or name is given. Use 'help info' for getting help.") #it is a type table if obj_type == InfoTypes.type_table: self.print_type_table(provider.get_type_table(path)) #it is a directory if obj_type == InfoTypes.directory: self.print_directory(provider.get_directory(path)) #it is a variation if obj_type == InfoTypes.variation: self.print_variation(provider.get_variation(obj_name)) #it is a file!!! if obj_type == InfoTypes.file: self.print_file(obj_name) #everything is fine! return 0
def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}List is in charge{0}\\".format(os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) assert self.context is not None provider = self.context.provider assert isinstance(provider, AlchemyProvider) self.reset() #PARSE ARGUMENTS #------------------------------- raw_path, task, is_extended = self._process_arguments(args) #dump as tree if task == ListTasks.directory_tree: self.parent_dir = provider.get_root_directory() self.print_directory_tree(self.parent_dir, False, 0) return #dump as directories if task == ListTasks.directories: self.parent_dir = provider.get_root_directory() self.print_directory_tree(self.parent_dir, True, 0) return #dump variations if task == ListTasks.variations: self.print_variations() return #dump type_tables if task == ListTasks.type_tables: self.print_tables() return if len(args) > 0: self.raw_entry = raw_path else: self.raw_entry = self.context.current_path dirs, tables = self.get_name_pathes(self.raw_entry) if (not dirs) and (not tables): log.info("Can't find the directory or tables") # it is not a wild card search, and if ("*" not in self.raw_entry) and\ ("?" not in self.raw_entry) and\ (not dirs) and\ (len(tables) == 1): self.table_info(tables[0], is_extended) return for directory in dirs: log.info(self.theme.Directories + directory.name + self.theme.Reset) for table in tables: log.info(table.name)
def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}ShowLog is in charge{0}\\".format( os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) provider = self.context.provider assert isinstance(provider, AlchemyProvider) offset = 0 limit = 10 if len(args) == 1: limit = int(args[0]) if len(args) == 2: offset = int(args[1]) log_records = provider.get_log_records(limit=limit, offset=offset) if not log_records: print("No log records found") return 0 print self.theme.Directories + "(action) (author) (date) (description)" for log_record in log_records: assert isinstance(log_record, LogRecord) print " %-13s "%log_record.action +\ " %-16s"%log_record.author.name + \ " %-18s"%log_record.created.strftime("%Y-%m-%d %H-%M-%S ") + " " +\ log_record.description
def _process_arguments(args): #solo arguments #utility argument parser is argparse which raises errors instead of exiting app parser = UtilityArgumentParser() parser.add_argument("obj_name", default="") parser.add_argument("-v", "--variation", action="store_true") parser.add_argument("-d", "--directory", action="store_true") parser.add_argument("-f", "--file", action="store_true") result = parser.parse_args(args) if result.variation: obj_type = InfoTypes.variation elif result.directory: obj_type = InfoTypes.directory elif result.file: obj_type = InfoTypes.file else: obj_type = InfoTypes.type_table log.debug( LogFmt(" |- parsed as (obj: '{0}', type: '{1}')", result.obj_name, obj_type)) return result.obj_name, obj_type
def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}ShowLog is in charge{0}\\".format( os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) provider = self.context.provider assert isinstance(provider, AlchemyProvider) result = self.process_arguments(args) log_records = self.filter(result) if len(log_records) == 0: print("No entries matching the filter criteria.") else: self.print_logs(log_records)
def _process_arguments(args): # solo arguments # utility argument parser is argparse which raises errors instead of exiting app parser = UtilityArgumentParser() parser.add_argument("raw_path", nargs='?', default="") parser.add_argument("-x", "--dtree", action="store_true") parser.add_argument("-d", "--directories", action="store_true") parser.add_argument("-t", "--tables", action="store_true") parser.add_argument("-v", "--variations", action="store_true") parser.add_argument("-l", "--extended", action="store_true") result = parser.parse_args(args) if result.variations: task = ListTasks.variations elif result.directories: task = ListTasks.directories elif result.dtree: task = ListTasks.directory_tree elif result.tables: task = ListTasks.type_tables else: task = ListTasks.default log.debug(LogFmt(" |- parsed as (obj: '{0}', type: '{1}')", result.raw_path, task)) return result.raw_path, task, result.extended
def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug( LogFmt("{0}MakeDirectory is in charge{0}\\".format( os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) if not len(args): return comment = "" raw_str = args[0] #in case there is a space between comments and name if len(args) >= 2 and args[1].startswith("#"): comment = args[1][1:] if len(args) >= 3: comment = comment + " " + " ".join(args[2:]) #try to extract comment from /path/#comment if "#" in raw_str: path = raw_str[:raw_str.index("#")] comment = raw_str[raw_str.index("#") + 1:] else: path = raw_str #prepare path path = self.context.prepare_path(path) #where is name and parent_path? (parent_path, name) = posixpath.split(path) #try to create directory log.debug( " |- creating directory. Name: {0}, parent path: {1}, comment: {2}" .format(name, parent_path, comment)) try: self.context.provider.create_directory(name, parent_path, comment) log.info("Directory " + name + self.theme.Success + " created" + self.theme.Reset) except Exception as ex: log.warning( self.theme.Fail + "Failed" + self.theme.Reset + " to create directory. Exception message: {0}".format(ex)) raise
def analyse_file(self): #reading file try: dom = read_ccdb_text_file(self.file_name) except IOError as error: log.warning( LogFmt( "Unable to read file '{0}'. The error message is: '{1}'", self.file_name, error)) raise #Is there data at all? if not dom.has_data: message = "Seems like file has no appropriate data" log.warning(message) raise ValueError(message=message) #check what we've got assert isinstance(dom, TextFileDOM) if not dom.data_is_consistent: message = "Inconsistency error. " + dom.inconsistent_reason log.warning(message) raise ValueError(message=message) if dom.column_names: columns_str = " ".join([col_name for col_name in dom.column_names]) else: columns_str = str(len(dom.rows[0])) + "col" name = self.table_path if self.table_path else "<name>" comment = self.comment if self.comment else "<comments>" log.info("Command to create a table: " + linesep) log.info( LogFmt("mktbl {0} -r {1} {2} #{3}", name, len(dom.rows), columns_str, comment)) log.info(linesep) if dom.comment_lines: log.info( LogFmt("{0}Comments in file: {0}{1}", linesep, linesep.join(ln for ln in dom.comment_lines)))
def process(self, args): """This is an entry point for each time the command is called""" if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}Copy is in charge{0}\\".format(os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args)+"'")) # get provider class which has functions for all CCDB database operation assert self.context provider = self.context.provider assert isinstance(provider, AlchemyProvider) # process arguments parsed_args = self.process_arguments(args) if not self.validate(parsed_args): return 1 # the return is like application ret. 1 means problems path = self.context.prepare_path(parsed_args.raw_path) # add current path to user input # try avoid print() and use log to print data log.info(LogFmt("{0}raw_path{1} : {2}", self.theme.Accent, self.theme.Reset, parsed_args.raw_path)) log.info(LogFmt("{0}path{1} : {2}", self.theme.Accent, self.theme.Reset, path)) log.info(LogFmt("{0}variation{1}: {2}", self.theme.Accent, self.theme.Reset, parsed_args.variation)) log.info(LogFmt("{0}run{1} : {2}", self.theme.Accent, self.theme.Reset, parsed_args.run)) # the return is like application ret. 0 means OK return 0
def process(self, args): #>oO debug log.debug(LogFmt("{0}MakeTable is in charge{0}\\".format(os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) #reset all needed variables self.reset_on_process() #process arguments self.process_arguments(args) #do we need just infer mktable from file? if self.infer_from_file: self.analyse_file() return 0 if self.interactive: self.interactive_mode() #lets parse columns self.columns = self.parse_columns(self.unparsed_columns) #set name self.table_path = self.context.prepare_path(self.table_path) (self.table_parent_path, self.table_name) = posixpath.split(self.table_path) #>oO debug if log.getEffectiveLevel() <= logging.DEBUG: self.print_settings_summary() if not self.interactive: self.print_validation() #create table self.do_create_type()
def print_file(self, file_path): #reading file try: dom = read_ccdb_text_file(file_path) except IOError as error: log.warning(LogFmt("Unable to read file '{0}'. The error message is: '{1}'", file_path, error)) raise #Is there data at all? if not dom.has_data: message = "Seems like file has no data" log.warning(message) raise ValueError(message=message) #check what we've got assert isinstance(dom, TextFileDOM) if not dom.data_is_consistent: message = "Inconsistency error. " + dom.inconsistent_reason log.warning(message) raise ValueError(message=message) log.info(LogFmt("Rows: {}{}{}", self.theme.Accent, len(dom.rows), self.theme.Reset)) log.info(LogFmt("Columns: {}{}{}", self.theme.Accent, len(dom.rows[0]), self.theme.Reset)) #column names if dom.column_names: log.info("Column names:") log.info(" " + (os.linesep + " ").join( [self.theme.Accent + col_name + self.theme.Reset for col_name in dom.column_names])) else: log.info("No column names found (column name string starts with #&)") #meta data if dom.metas: log.info("Meta data:") log.info((os.linesep + " ").join([key + " = " + val for key, val in dom.metas])) #comments if dom.comment_lines: log.info(LogFmt("{0}Comments in file: {0}{1}", os.linesep, os.linesep.join(ln for ln in dom.comment_lines))) else: log.info("No comments in file found") ccdb_prefix = "ccdb " if not self.context.is_interactive else "" log.info("") log.info(LogFmt("Type '{1}mktbl -f {0}' to see how to create a table for the file", file_path, ccdb_prefix)) log.info(LogFmt("Type '{1}add <table name> {0} #<comments>' to add the file to existing table" " (rows and columns must consist)", file_path, ccdb_prefix))
def process(self, args): if log.isEnabledFor(logging.DEBUG): log.debug(LogFmt("{0}AddData is in charge{0}\\".format( os.linesep))) log.debug(LogFmt(" |- arguments : '" + "' '".join(args) + "'")) self.reset() provider = self.context.provider assert isinstance(provider, AlchemyProvider) # process arguments if not self.process_arguments(args): log.debug( LogFmt(" |- process arguments {0}{1}{2}", self.theme.Fail, "failed", self.theme.Reset)) raise ValueError("Problem parsing arguments") # by "" user means default variation # self.variation = "default" if not bool(self.variation) else self.variation # TODO commented as self.variation is set in self.reset() need to be tested # validate what we've got if not self.validate(): log.debug( LogFmt(" |- arguments validation {0}{1}{2}", self.theme.Fail, "failed", self.theme.Reset)) raise ValueError("Arguments validation failed") # correct paths self.table_path = self.context.prepare_path(self.raw_table_path) self.file_path = self.raw_file_path # reading file try: if not self.is_namevalue_format: dom = ccdb.read_ccdb_text_file(self.file_path) else: dom = ccdb.read_namevalue_text_file(self.file_path, self.c_comments) except IOError as error: log.warning( LogFmt( "Unable to read file '{0}'. The error message is: '{1}'", self.file_path, error)) raise # check what we've got assert isinstance(dom, TextFileDOM) if not dom.data_is_consistent: message = "Inconsistency error. " + dom.inconsistent_reason log.warning(message) raise ValueError(message=message) if len(dom.comment_lines): self.comment += "\n".join(dom.comment_lines) # >oO debug record log.debug(" |- adding constants") log.debug( LogFmt( " |- columns: '{0}' rows: '{1}' comment lines: '{2}' metas: '{3}'", len(dom.rows[0]), len(dom.rows), len(dom.comment_lines), len(dom.metas))) try: table = provider.get_type_table(self.table_path) except Exception as ex: if 'No table found by exact path' in ex.message: # TODO replace with good exception type # it is safe to use len(dom.rows[0]) because dom.data_is_consistant checked that print( self._get_notable_instruction(self.table_path, len(dom.rows[0]), len(dom.rows))) # try to create assignment = provider.create_assignment(dom, self.table_path, self.run_min, self.run_max, self.variation, self.comment) log.info(assignment.request) return 0