def __write_kv_and_features_to_file(file_path, key_values=None, features=None, format_=None, separator=None, skip_features=False, naming_convention='pascal'): try: exported_keyvalues = __export_keyvalues(key_values, format_, separator, None) if features and not skip_features: exported_features = __export_features(features, naming_convention) exported_keyvalues.update(exported_features) with open(file_path, 'w', encoding='utf-8') as fp: if format_ == 'json': json.dump(exported_keyvalues, fp, indent=2, ensure_ascii=False) elif format_ == 'yaml': yaml.safe_dump(exported_keyvalues, fp, sort_keys=False, width=float('inf')) elif format_ == 'properties': javaproperties.dump(exported_keyvalues, fp) except Exception as exception: raise CLIError("Failed to export key-values to file. " + str(exception))
def modify_conf_properties(filename, modified_properties): with open(filename, 'r') as fp: properties = javaproperties.load(fp) with open(filename, 'w') as fp: properties.update(modified_properties) javaproperties.dump(properties, fp) return properties
def set(self, key, value): if os.path.exists(self.__filename): with open(self.__filename, 'r') as fread: result = javaproperties.load(fread) result[key] = value os.remove(self.__filename) with open(self.__filename, 'w') as fwrite: javaproperties.dump(result, fwrite, timestamp=None) return False
def _write(self, folder): name = "{}.job".format(self.name) path = os.path.join(folder, name) with open(path, "w") as f: javaproperties.dump(self.properties, f, comments=name, timestamp=False, sort_keys=True)
def _write(self, folder): name = "{}.properties".format("_".join( [param_class.name for param_class in self.params_class])) path = os.path.join(folder, name) with open(path, "w") as f: javaproperties.dump(self.properties, f, comments=name, timestamp=False, sort_keys=True)
def change_properties_value(self, prop_file_path, prop_key, value): """ Changes the value of the parameter ``prop_key`` inside the properties file located at ``prop_file_path`` to the given ``value`` """ prop_file = io.open(prop_file_path, "r", encoding="utf-8") properties = javaproperties.load(prop_file) properties[prop_key] = str(value) with io.open(prop_file_path, "w", encoding="utf-8") as fp: javaproperties.dump(properties, fp, sort_keys=True)
def __write_kv_to_file(file_path, key_values=None, format_=None, separator=None): try: exported_keyvalues = __export_keyvalues( key_values, format_, separator, None) with open(file_path, 'w') as fp: if format_ == 'json': json.dump(exported_keyvalues, fp, indent=2) elif format_ == 'yaml': yaml.dump(exported_keyvalues, fp) elif format_ == 'properties': with open(file_path, 'w') as fp: javaproperties.dump(exported_keyvalues, fp) except Exception as exception: raise CLIError("Fail to export key-values to file." + str(exception))
def format(outfile, separator, file, encoding, ensure_ascii): """Format/"canonicalize" a Java .properties file""" with click.open_file(file, encoding=encoding) as fpin: with click.open_file( outfile, "w", encoding=encoding, errors="javapropertiesreplace", ) as fpout: dump( load(fpin), fpout, sort_keys=True, separator=separator, ensure_ascii=ensure_ascii, )
def updatePropertyFile(path, stcm, mavenPath=''): jp = Properties() jp.load(open(osp.join(stcm, 'paths.properties'))) newProperties = {} for k, v in jp.items(): if k == 'mavenHome': if not mavenPath == '': newProperties[k] = mavenPath elif k == 'PathToSetup': newProperties[k] = path else: newProperties[k] = v with open(osp.join(stcm, 'paths.properties'), 'w') as f: javaproperties.dump(newProperties, f)
def _dump(self, obj: Report, file: IO[str]): # Create a Java properties dictionary properties = {constants.PROPERTY_PARENTID: str(obj.database_id)} # Add the fields and their types as properties for field in obj: name = str(field) properties[name] = str(obj.get_value(field)) properties[ name + constants.DATATYPE_SUFFIX] = field.datatype.to_display() print(javaproperties.to_comment(javaproperties.java_timestamp()), file=file) print(javaproperties.to_comment( "Simple report format (= Java properties file format)"), file=file) javaproperties.dump(properties, file, timestamp=False, sort_keys=True)
def json2properties(ctx, infile, outfile, separator, encoding, comment): """Convert a JSON object to a Java .properties file""" with infile: props = json.load(infile, parse_float=Decimal) if not isinstance(props, dict): ctx.fail('Only dicts can be converted to .properties') strprops = [] for k, v in iteritems(props): assert isinstance(k, string_types) if isinstance(v, (list, dict)): ctx.fail('Dictionary values must be scalars, not lists or dicts') elif isinstance(v, string_types): strprops.append((k, v)) elif isinstance(v, Decimal): strprops.append((k, str(v))) else: strprops.append((k, json.dumps(v))) strprops.sort() with click.open_file(outfile, 'w', encoding=encoding) as fp: dump(strprops, fp, separator=separator, comments=comment)
def json2properties(ctx, infile, outfile, separator, encoding, comment, ensure_ascii, sort_keys): """Convert a JSON object to a Java .properties file""" with infile: props = json.load( infile, parse_float=Decimal, object_pairs_hook=OrderedDict, ) if not isinstance(props, dict): ctx.fail("Only dicts can be converted to .properties") strprops = [] for k, v in props.items(): assert isinstance(k, str) if isinstance(v, (list, dict)): ctx.fail("Dictionary values must be scalars, not lists or dicts") elif isinstance(v, str): strprops.append((k, v)) elif isinstance(v, Decimal): strprops.append((k, str(v))) else: strprops.append((k, json.dumps(v))) with click.open_file( outfile, "w", encoding=encoding, errors="javapropertiesreplace", ) as fp: dump( strprops, fp, separator=separator, comments=comment, ensure_ascii=ensure_ascii, ensure_ascii_comments=ensure_ascii, sort_keys=sort_keys, )
def format(outfile, separator, file, encoding): """ Format/"canonicalize" a Java .properties file """ with click.open_file(file, encoding=encoding) as fpin: with click.open_file(outfile, 'w', encoding=encoding) as fpout: dump(load(fpin), fpout, sort_keys=True, separator=separator)