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
def process_file(home_dir, rule_file_name, ccdb_parent_path): #parse xml file rule_file_path = os.path.join(home_dir, rule_file_name); xmldoc = minidom.parse(rule_file_path) #>oO print " Processing file " + rule_file_path print " ***********************************************" #get type tables xml_tables = xmldoc.getElementsByTagName('type') #iterate type tables for xml_table in xml_tables: #parameters table_name = xml_table.attributes['name'].value nrows = int(xml_table.attributes['nrow'].value) is_name_value_format = bool(int(xml_table.attributes['namevalue'].value)) #comments comments = '' xml_comments = xml_table.getElementsByTagName('comment') if len(xml_comments): comments = " ".join(t.nodeValue for t in xml_comments[0].childNodes if t.nodeType == t.TEXT_NODE) comments.replace("\r\n", "\\n") comments.replace("\n","\\n") comments.replace('"',"'") #comments = comments.replace("\\n",os.linesep) #print out what we've got print " Process table: " + table_name print " Comments: " print " " + comments[0:50] print " Rows Number: " + str(nrows) print " Is namevalue: " + repr(is_name_value_format) #iterate columns, create columns command columns_create_command = '' xml_columns = xml_table.getElementsByTagName('column') if not is_verbose: print " Columns : " + repr(len(xml_columns)) else: print " Columns: " for xml_column in xml_columns: column_name = xml_column.attributes['name'].value column_type = xml_column.attributes['type'].value if(is_verbose): print " {:<35} = {}".format(column_name, column_type) columns_create_command+=' "{}={}"'.format(column_name, column_type) #create table command table_path = (ccdb_parent_path + "/" + table_name).replace("//","/") create_table_command = 'ccdb ' + ccdbcmd_opts +' mktbl --no-quantity {0} -r {1} {2} "#{3}"' create_table_command = create_table_command.format(table_path, nrows, columns_create_command, "") print " Create command" print " " + create_table_command # print " " + create_table_command[0:50]+" ... " if(execute_ccdb_commands): (code, response) = get_status_output(create_table_command) print code, response if code!=0 or "error" in response or "failed" in response: exit("Conversion aborted") print print " Filling data " print " =============================================" #now fill it with data data_file_path = os.path.join(ccdb_parent_path, table_name) data_file_path = (calib_dir + "/" + data_file_path).replace("//","/") print " Data file is: " + data_file_path #read dom dom = ccdb.TextFileDOM() if(is_name_value_format): dom = ccdb.read_namevalue_text_file(data_file_path) else: dom = ccdb.read_ccdb_text_file(data_file_path) #print verbose info if is_verbose: print dom.column_names print dom.rows for i in range(min(len(dom.column_names),50)): print "{:>35} {}".format(dom.column_names[i], dom.rows[0][i]) add_command = "ccdb " + ccdbcmd_opts + " add --c-comments " if(is_name_value_format) : add_command += "--name-value " add_command += table_path +" -v default -r 0- " + data_file_path print add_command if(execute_ccdb_commands): (code, response) = get_status_output(add_command) print code, response if code!=0 or "error" in response or "failed" in response: exit("Conversion aborted") print " =============================================" print " Finished with file "
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" + "\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
def process_file(home_dir, rule_file_name, ccdb_parent_path): #parse xml file rule_file_path = os.path.join(home_dir, rule_file_name) xmldoc = minidom.parse(rule_file_path) #>oO print " Processing file " + rule_file_path print " ***********************************************" #get type tables xml_tables = xmldoc.getElementsByTagName('type') #iterate type tables for xml_table in xml_tables: #parameters table_name = xml_table.attributes['name'].value nrows = int(xml_table.attributes['nrow'].value) is_name_value_format = bool( int(xml_table.attributes['namevalue'].value)) #comments comments = '' xml_comments = xml_table.getElementsByTagName('comment') if len(xml_comments): comments = " ".join(t.nodeValue for t in xml_comments[0].childNodes if t.nodeType == t.TEXT_NODE) comments.replace("\r\n", "\\n") comments.replace("\n", "\\n") comments.replace('"', "'") #comments = comments.replace("\\n",os.linesep) #print out what we've got print " Process table: " + table_name print " Comments: " print " " + comments[0:50] print " Rows Number: " + str(nrows) print " Is namevalue: " + repr(is_name_value_format) #iterate columns, create columns command columns_create_command = '' xml_columns = xml_table.getElementsByTagName('column') if not is_verbose: print " Columns : " + repr(len(xml_columns)) else: print " Columns: " for xml_column in xml_columns: column_name = xml_column.attributes['name'].value column_type = xml_column.attributes['type'].value if (is_verbose): print " {:<35} = {}".format(column_name, column_type) columns_create_command += ' "{}={}"'.format( column_name, column_type) #create table command table_path = (ccdb_parent_path + "/" + table_name).replace("//", "/") create_table_command = 'ccdb ' + ccdbcmd_opts + ' mktbl --no-quantity {0} -r {1} {2} "#{3}"' create_table_command = create_table_command.format( table_path, nrows, columns_create_command, "") print " Create command" print " " + create_table_command # print " " + create_table_command[0:50]+" ... " if (execute_ccdb_commands): (code, response) = get_status_output(create_table_command) print code, response if code != 0 or "error" in response or "failed" in response: exit("Conversion aborted") print print " Filling data " print " =============================================" #now fill it with data data_file_path = os.path.join(ccdb_parent_path, table_name) data_file_path = (calib_dir + "/" + data_file_path).replace("//", "/") print " Data file is: " + data_file_path #read dom dom = ccdb.TextFileDOM() if (is_name_value_format): dom = ccdb.read_namevalue_text_file(data_file_path) else: dom = ccdb.read_ccdb_text_file(data_file_path) #print verbose info if is_verbose: print dom.column_names print dom.rows for i in range(min(len(dom.column_names), 50)): print "{:>35} {}".format(dom.column_names[i], dom.rows[0][i]) add_command = "ccdb " + ccdbcmd_opts + " add --c-comments " if (is_name_value_format): add_command += "--name-value " add_command += table_path + " -v default -r 0- " + data_file_path print add_command if (execute_ccdb_commands): (code, response) = get_status_output(add_command) print code, response if code != 0 or "error" in response or "failed" in response: exit("Conversion aborted") print " =============================================" print " Finished with file "