def writeXmlMenu(self, filename, json_dir, dist=1): """Updates a XML menu file based on inforamtion from a JSON file (used to apply a previously calculated algorithm distribution over multiple modules). Returns path and filename of created XML menu. """ # TODO # Load mapping from JSON with open(os.path.join(json_dir, 'menu.json')) as fp: json_data = json.load(fp) menu = tmTable.Menu() scale = tmTable.Scale() ext_signal = tmTable.ExtSignal() logging.info("reading source XML menu file %s", filename) message = tmTable.xml2menu(filename, menu, scale, ext_signal, False) if message: logging.error(f"{filename}: {message}") raise RuntimeError(message) menu_name = menu.menu["name"] logging.info("processing menu \"%s\" ... ", menu_name) # Update menu information logging.info("updating menu information...") logging.info("uuid_menu : %s", json_data["menu_uuid"]) logging.info("uuid_firmware : %s", json_data["firmware_uuid"]) logging.info("n_modules : %s", json_data["n_modules"]) # Update menu information menu.menu["uuid_menu"] = str(json_data["menu_uuid"]) menu.menu["uuid_firmware"] = str(json_data["firmware_uuid"]) menu.menu["n_modules"] = str(json_data["n_modules"]) menu.menu["is_valid"] = "1" # Collect algorithm names names = [algorithm["name"] for algorithm in menu.algorithms] # Update algorithm for name, index, module_id, module_index in json_data["algorithms"]: algorithm = tmTable.Row() id_ = names.index(name) # Copy attributes for k, v in menu.algorithms[id_].items(): algorithm[k] = v # Update attributes algorithm["index"] = str(index) algorithm["module_id"] = str(module_id) algorithm["module_index"] = str(module_index) menu.algorithms[id_] = algorithm target = os.path.join(json_dir, f'{menu_name}-d{dist}.xml') logging.info("writing target XML menu file %s", target) tmTable.menu2xml(menu, scale, ext_signal, target) return target
def run_process_cuts(self): for algorithm in self.menu.algorithms: # Cuts if algorithm.name not in self.tables.menu.cuts.keys(): self.tables.menu.cuts[algorithm.name] = [] for name in algorithm.cuts(): logging.debug("processing cut: %s", name) cut = self.menu.cutByName(name) if not cut: message = "missing cut: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Create cut row row = tmTable.Row() row[kName] = safe_str(cut.name, "cut name") row[kObject] = cut.object row[kType] = cut.type if cut.data: row[kMinimum] = format(0., FORMAT_FLOAT) row[kMaximum] = format(0., FORMAT_FLOAT) row[kData] = cut.data else: row[kMinimum] = format(float(cut.minimum), FORMAT_FLOAT) row[kMaximum] = format(float(cut.maximum), FORMAT_FLOAT) row[kData] = "" row[kComment] = cut.comment #Validate cut row if not tmTable.isCut(row): message = "invalid cut: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Append cut row logging.debug("appending cut: %s", dict(row)) self.tables.menu.cuts[algorithm.name] = self.tables.menu.cuts[ algorithm.name] + (row, )
def run_process_externals(self): for algorithm in self.menu.algorithms: # Externals if algorithm.name not in self.tables.menu.externals.keys(): self.tables.menu.externals[algorithm.name] = [] for name in algorithm.externals(): external = self.menu.externalByName(name) if not external: message = "missing external signal: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Create external row row = tmTable.Row() row[kName] = safe_str(external.name, "external_name") row[kBxOffset] = format(external.bx_offset, FORMAT_BX_OFFSET) # Validate external row if not tmTable.isExternalRequirement(row): message = "invalid external signal: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Append external row logging.debug("appending external signal: %s", dict(row)) self.tables.menu.externals[ algorithm.name] = self.tables.menu.externals[ algorithm.name] + (row, )
def run_process_objects(self): for algorithm in self.menu.algorithms: # Objects if algorithm.name not in self.tables.menu.objects.keys(): self.tables.menu.objects[algorithm.name] = [] for name in algorithm.objects(): object_ = self.menu.objectByName(name) if not object_: message = "missing object requirement: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Create object row row = tmTable.Row() row[kName] = safe_str(object_.name, "object name") row[kType] = object_.type row[kThreshold] = format(object_.decodeThreshold(), FORMAT_FLOAT) row[kComparisonOperator] = object_.comparison_operator row[kBxOffset] = format(object_.bx_offset, FORMAT_BX_OFFSET) # Validate object row if not tmTable.isObjectRequirement(row): message = "invalid object requirement: {0}".format(name) logging.error(message) raise XmlEncoderError(message) # Append object trow logging.debug("appending object requirement: %s", dict(row)) self.tables.menu.objects[ algorithm. name] = self.tables.menu.objects[algorithm.name] + (row, )
def main(): parser = argparse.ArgumentParser() parser.add_argument('-m', action='store', dest='menu_path', default=menu_path, help='path to a menu xml file') parser.add_argument('-j', action='store', dest='json_path', default=json_path, help='path to a json file') results = parser.parse_args() json_data = None with open(results.json_path) as fp: json_data = json.load(fp) menu = tmTable.Menu() scale = tmTable.Scale() ext_signal = tmTable.ExtSignal() msg = tmTable.xml2menu(results.menu_path, menu, scale, ext_signal, False) if msg: print('err> %s: %s' % (results.menu_path, msg)) sys.exit(1) print("inf> processing %s ... " % menu.menu["name"]) menu.menu["uuid_menu"] = str(json_data["menu_uuid"]) menu.menu["uuid_firmware"] = str(json_data["firmware_uuid"]) menu.menu["n_modules"] = str(json_data["n_modules"]) menu.menu["is_valid"] = "1" names = [] for algorithm in menu.algorithms: names.append(algorithm["name"]) for name, index, module_id, module_index in json_data["algorithms"]: algorithm = tmTable.Row() id = names.index(name) for k, v in menu.algorithms[id].items(): algorithm[k] = v algorithm["index"] = str(index) algorithm["module_id"] = str(module_id) algorithm["module_index"] = str(module_index) menu.algorithms[id] = algorithm tmTable.menu2xml(menu, scale, ext_signal, 'menu.xml')
def run_process_algorithms(self): for algorithm in self.menu.algorithms: # Create algorithm row row = tmTable.Row() row[kIndex] = format(algorithm.index, FORMAT_INDEX) row[kModuleId] = "0" row[kModuleIndex] = format(algorithm.index, FORMAT_INDEX) row[kName] = safe_str(algorithm.name, "algorithm name") row[kExpression] = AlgorithmFormatter.compress( algorithm.expression) row[kComment] = algorithm.comment row[kLabels] = encode_labels(algorithm.labels) # Validate algorithm row if not tmTable.isAlgorithm(row): message = "invalid algorithm ({algorithm.index}): {algorithm.name}".format( algorithm=algorithm) logging.error(message) raise XmlEncoderError(message) # Append algorithm row logging.debug("appending algorithm: %s", dict(row)) self.tables.menu.algorithms.append(row)
import tmTable bin = tmTable.Row() bin["minimum"] = "+2.5000000000000000f+02" bin["maximum"] = "+2.5500000000000000E+02" bin["number"] = "1" print "** bin ", print tmTable.isBin(bin) scale = tmTable.Row() scale["object"] = "TAU" scale["type"] = "ET" scale["minimum"] = "+2.5000000000000000E+02" scale["maximum"] = "+2.5500000000000000E+02" scale["step"] = "+3.5500000000000000E+02" scale["n_bits"] = "+3.5500000000000000E+02" print "** scale ", print tmTable.isScale(scale) scaleSet = tmTable.Row() scaleSet["name"] = "hoge" print "** scaleSet ", print tmTable.isScaleSet(scaleSet) extSignal = tmTable.Row() extSignal["name"] = "name" extSignal["system"] = "system" extSignal["cable"] = "0" extSignal["channel"] = "1" print "** extSignal ", print tmTable.isExtSignal(extSignal)