def test_safe_loads(): d: Dict[str, str] = {} rval = safe_loads(d) assert rval == d assert rval is not d rval['foo'] = 'bar' assert 'foo' not in d s = '{"foo": "bar"}' assert safe_loads(s) == {"foo": "bar"}
def params_from_strings(params, param_values, app, ignore_errors=False): """ Convert a dictionary of strings as produced by `params_to_strings` back into parameter values (decode the json representation and then allow each parameter to convert the basic types into the parameters preferred form). """ rval = dict() param_values = param_values or {} for key, value in param_values.items(): param = params.get(key) if not param or not (param.type == 'text' and value == 'null'): # safe_loads attempts to handle some, but not all divergent handling # between JSON types and python types. TODO: We should let the # parameters handle all conversion, since they know what is an # appropriate coercion between types. e.g 'false' should be a string # in a text parameter, while it should be a boolean in a boolean parameter. # This would resolve a lot of back and forth in the various to/from methods. value = safe_loads(value) if param: try: value = param.value_from_basic(value, app, ignore_errors) except ParameterValueError: continue rval[key] = value return rval
def __main__(): if len(sys.argv) < 4: print >> sys.stderr, 'usage: upload.py <root> <datatypes_conf> <json paramfile> <output spec> ...' sys.exit(1) output_paths = parse_outputs(sys.argv[4:]) json_file = open('galaxy.json', 'w') registry = Registry() registry.load_datatypes(root_dir=sys.argv[1], config=sys.argv[2]) for line in open(sys.argv[3], 'r'): dataset = safe_loads(line) dataset = util.bunch.Bunch(**safe_dict(dataset)) try: output_path = output_paths[int(dataset.dataset_id)][0] except: print >> sys.stderr, 'Output path for dataset %s not found on command line' % dataset.dataset_id sys.exit(1) if dataset.type == 'composite': files_path = output_paths[int(dataset.dataset_id)][1] add_composite_file(dataset, registry, json_file, output_path, files_path) else: add_file(dataset, registry, json_file, output_path) # clean up paramfile # TODO: this will not work when running as the actual user unless the # parent directory is writable by the user. try: os.remove(sys.argv[3]) except: pass
def __set_default_label(self, step, module, state): """ Previously data input modules had a `name` attribute to rename individual steps. Here, this value is transferred to the actual `label` attribute which is available for all module types, unique, and mapped to its own database column. """ if not module.label and module.type in ['data_input', 'data_collection_input']: new_state = safe_loads(state) default_label = new_state.get('name') if str(default_label).lower() not in ['input dataset', 'input dataset collection']: step.label = module.label = default_label
def recover_state(self, state, **kwds): """ Recover state `dict` from simple dictionary describing configuration state (potentially from persisted step state). Sub-classes should supply a `default_state` method which contains the initial state `dict` with key, value pairs for all available attributes. """ self.state = DefaultToolState() inputs = self.get_inputs() if inputs: self.state.decode(state, Bunch(inputs=inputs), self.trans.app) else: self.state.inputs = safe_loads(state) or {}
def recover_state( self, state, **kwds ): """ Recover state `dict` from simple dictionary describing configuration state (potentially from persisted step state). Sub-classes should supply a `default_state` method which contains the initial state `dict` with key, value pairs for all available attributes. """ self.state = DefaultToolState() inputs = self.get_inputs() if inputs: self.state.decode( state, Bunch( inputs=inputs ), self.trans.app ) else: self.state.inputs = safe_loads( state ) or {}
def params_from_strings(params, param_values, app, ignore_errors=False): """ Convert a dictionary of strings as produced by `params_to_strings` back into parameter values (decode the json representation and then allow each parameter to convert the basic types into the parameters preferred form). """ rval = dict() param_values = param_values or {} for key, value in param_values.items(): value = json_fix(safe_loads(value)) if key in params: value = params[key].value_from_basic(value, app, ignore_errors) rval[key] = value return rval