def setc(self, name, value, default=None): """ Utility method that updates internal configuration dictionary Logs cases when values from the original config file are overridden by command line arguments or defaults (in the case of missing optional parameters. """ assert (hasattr(self, 'c')) assert (type(self.c) == dict) assert (hasattr(self, '_c')) assert (type(self._c) == dict) try: if (value is not None and json.dumps(self._c[name]) != json.dumps(value)): msg = cat('config value [ %s ] for [ %s ] overwritten with ', '[ %s ]') % (self._c[name], name, json.dumps(value)) self.c[name] = value log.warn(msg) elif name in self._c: self.c[name] = self._c[name] except KeyError as e: assert (e.args[0] == name) log.debug('[ %s ] not in config, setting [ %s = %s ]' % (e.args[0], name, json.dumps(value))) self.c[name] = value if name not in self.c: if default is not None: self.c[name] = default log.warn( cat('no value available for [ %s ], using default: ', '[ %s ]') % (name, json.dumps(default))) else: log.warn( cat('no value available for [ %s ] and no default ', 'supplied; setting to [ None ]') % name) self.c[name] = None
def root_definition(self): if self.root_set: return self.tao.getRootDefinition() else: msg = cat('Root definition does not exist! Check the property ', 'TaoiVariable.root_set first!') log.error(msg) raise TaoiVariableError(msg)
def root_definition(self, v): if isinstance(v, TA.VariableDefinition): self.tao.setRootDefinition(v) else: msg = cat('root_definition must be set to an instance of %s, ', 'but an instance of %s was supplied') % ( type(TA.VariableDefinition), type(v)) log.error(msg) raise TaoiVariableError(msg)
def __init__(self, variable=None, **kwargs): try: if variable is None: log.debug(cat('No instance variable to copy - instantiating ', 'a new one')) self.tao = TA.Variable() elif type(variable) == TA.Variable: log.debug('Copying from supplied variable') self.tao = variable elif type(variable) == TaoiVariable: msg = cat('Copy-from-instance logic has not been implemented', ' yet. Please use an instance of TA.Variable') log.error(msg) raise Exception(msg) else: msg = '"variable" argument of type %s not allowed!' % ( type(variable),) log.error(msg) raise Exception(msg) self.update_from_dict(kwargs) except Exception as e: if not isinstance(e, TaoiVariableError): raise
def config_workspace(self, config, workspace): """ Reads the config and workspace options and sets class members Somewhat convoluted logic searches for config file as a path relative to the workspace (if supplied), otherwise ???????????????????????? """ # TODO this doesn't support absolute paths !!!!!!!!!!!!!!!!!!!!!!! _workspace = os.getcwd() if workspace is None else workspace self.c = dict() self._c = dict() try: if config is None: raise Exception('no config file supplied') with open(os.path.join(_workspace, config)) as f: self._c.update(yaml.safe_load(f)) log.debug('successfully parsed yaml config file') except Exception as e: if workspace is None: log.debug( cat('No workspace provided, attempt to use current ', 'directory')) else: log.debug( cat('attempting to use supplied workspace without ', 'yaml config')) finally: if not os.access(_workspace, os.R_OK): msg = 'No read access to workspace path: %s' % _workspace log.error(msg) raise Exception(msg) if workspace is not None and workspace in self.c: log.warn( cat('workspace specified in config and as argument; ', 'using supplied argument: %s') % workspace) # when debugging log the original config dicts log.debug('supplied config = %s' % json.dumps(self._c)) # update the workspace param in the master config self.setc('workspace', _workspace)
def run_cost_effectiveness(self): """ Runs the cost-effectiveness anaysis in the tree and returns a dict of the columns """ if self.tree.getCalculationMethod() != 'ct_costEff': msg = cat('Attempted to run cost-effectiveness analysis on a ', 'tree that does not support it!') log.error(msg) raise TaoiError(msg) report = self.tree.runAnalysis(TA.AnalysisType.costEffectivenes, None, self.tree.getRoot()).getTextReport() headers = report.getHeaders() rows = report.getRows() d = defaultdict(list) for r in rows: i = 0 for h in headers: d[h].append(r[i]) i += 1 return d
def main(): parser = argparse.ArgumentParser(description='TreeAge Object Interface') parser.add_argument('-d', '--debug', action='store_true', default=False, help='Enables debugging logic and logging') parser.add_argument('-t', '--treefile', default=None, help='Tree file (xml)') parser.add_argument('-H', '--host', default='localhost', help='Host running TreeAgePro') parser.add_argument( '-o', '--outdir', default=None, help=cat('Output directory (defaults to workspace if it is ', 'supplied, current working directory otherwise)')) parser.add_argument( '-w', '--workspace', default=None, help=cat('Path prefix for workspace (defaults to current working ', 'directory)')) parser.add_argument( '-p', '--prefix', default=None, help='Prefix used for all output files (defaults to a timestamp)') parser.add_argument( '-c', '--config', default=None, help='YAML-format config file (command line args override config)') parser.add_argument('-s', '--summary', action='store_true', default=False, help='prints a summary of tree attributes') args = parser.parse_args() if args.debug: logging.basicConfig(level=logging.DEBUG) else: logging.basicConfig(level=logging.INFO) global log log = logging.getLogger('trolley') log.info(args) ts = TrolleySession(host=args.host, treefile=args.treefile, workspace=args.workspace, debug=args.debug, auto=True, config=args.config) if args.summary: ts.print_summary()