def setproperties(fpin, fpout, newprops, preserve_timestamp=False, separator='='): in_header = True prevsrc = None for k, _, src in parse(fpin): if in_header: if k is None: if prevsrc is not None: print(prevsrc, end='', file=fpout) prevsrc = src continue else: if prevsrc is not None: if preserve_timestamp: print(prevsrc, end='', file=fpout) else: if not re.match(TIMESTAMP_RGX, prevsrc, flags=re.U): print(prevsrc, end='', file=fpout) print(to_comment(java_timestamp()), file=fpout) elif not preserve_timestamp: print(to_comment(java_timestamp()), file=fpout) in_header = False if k in newprops: if newprops[k] is not None: print(join_key_value(k, newprops[k], separator=separator), file=fpout) newprops[k] = None else: # In case the last line of the file ends with a trailing line # continuation: src = re.sub(r'(?<!\\)((?:\\\\)*)\\$', r'\1', src) print(src.rstrip('\r\n'), file=fpout) for key, value in iteritems(newprops): if value is not None: print(join_key_value(key, value, separator=separator), file=fpout)
def setproperties(fpin, fpout, newprops, preserve_timestamp=False, separator="=", ensure_ascii=True): in_header = True prev = None for kv in parse(fpin): if in_header: if not isinstance(kv, KeyValue): if prev is not None: print(prev.source, end="", file=fpout) prev = kv continue else: if prev is not None: if preserve_timestamp: print(prev.source, end="", file=fpout) else: if not prev.is_timestamp(): print(prev.source, end="", file=fpout) print(to_comment(java_timestamp()), file=fpout) elif not preserve_timestamp: print(to_comment(java_timestamp()), file=fpout) in_header = False if kv.key in newprops: if newprops[kv.key] is not None: print( join_key_value( kv.key, newprops[kv.key], separator=separator, ensure_ascii=ensure_ascii, ), file=fpout, ) newprops[kv.key] = None else: # Use `source_stripped` in case the last line of the file ends with # a trailing line continuation: print(kv.source_stripped, file=fpout) for key, value in newprops.items(): if value is not None: print( join_key_value(key, value, separator=separator, ensure_ascii=ensure_ascii), file=fpout, )
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 test_dumps_comments(c, ensure_ascii_comments): s = dumps( {"key": "value"}, timestamp=False, comments=c, ensure_ascii_comments=ensure_ascii_comments, ) assert s == to_comment(c, ensure_ascii=ensure_ascii_comments) \ + '\nkey=value\n' if ensure_ascii_comments is None: assert s == dumps({"key": "value"}, timestamp=False, comments=c)
def select(ctx, default_value, defaults, escaped, separator, file, key, encoding, outfile): """ Extract key-value pairs from a Java .properties file """ ok = True with click.open_file(outfile, 'w', encoding=encoding) as fpout: print(to_comment(java_timestamp()), file=fpout) for k,v in getselect(file, key, defaults, default_value, encoding, escaped): if v is None: click.echo(u'{0}: {1}: key not found' .format(ctx.command_path, k), err=True) ok = False else: print(join_key_value(k, v, separator=separator), file=fpout) ctx.exit(0 if ok else 1)
def select( ctx, default_value, defaults, escaped, separator, file, key, encoding, outfile, ensure_ascii, quiet, ): """Extract key-value pairs from a Java .properties file""" ok = True with click.open_file( outfile, "w", encoding=encoding, errors="javapropertiesreplace", ) as fpout: print(to_comment(java_timestamp()), file=fpout) for k, v in getselect(file, key, defaults, default_value, encoding, escaped): if v is not None: print( join_key_value(k, v, separator=separator, ensure_ascii=ensure_ascii), file=fpout, ) elif not quiet: click.echo(f"{ctx.command_path}: {k}: key not found", err=True) ok = False ctx.exit(0 if ok else 1)
def test_to_comment_commented(): assert to_comment('#This is a double comment.') == \ '##This is a double comment.'
def test_to_comment_controls(): # All C0 and C1 control characters other than \n and \r s = ''.join( unichr(i) for i in list(range(0x20)) + list(range(0x7F, 0xA0)) if i not in (10, 13)) assert to_comment(s) == '#' + s
def test_to_comment_astral_plane(): assert to_comment('goat=\U0001F410') == '#goat=\\ud83d\\udc10'
def test_to_comment_non_latin_1(): assert to_comment('snowman=\u2603') == '#snowman=\\u2603'
def test_to_comment_trailing_linefeed(): assert to_comment('This comment has a trailing newline.\n') == \ '#This comment has a trailing newline.\n#'
def test_to_comment_ensure_ascii(cin, cout): assert to_comment(cin, ensure_ascii=True) == cout
def test_to_comment_latin_1(): assert to_comment('edh=\xF0') == '#edh=\xF0'
def test_to_comment_inner_bang(): assert to_comment('This is a comment.\n!This is also a comment.') == \ '#This is a comment.\n!This is also a comment.'
def test_to_comment_inner_hash(): assert to_comment('This is a comment.\n#This is also a comment.') == \ '#This is a comment.\n#This is also a comment.'
def test_to_comment_multiline(): assert to_comment('This is a comment.\nThis is also a comment.') == \ '#This is a comment.\n#This is also a comment.'
def test_to_comment_trailing_cr(): assert to_comment('This comment has a trailing carriage return.\r') == \ '#This comment has a trailing carriage return.\n#'
def test_to_comment_trailing_crlf(): assert to_comment('This comment has a trailing CRLF.\r\n') == \ '#This comment has a trailing CRLF.\n#'
def test_to_comment(cin, cout): assert to_comment(cin) == cout assert to_comment(cin, ensure_ascii=None) == cout
def test_to_comment_single_line(): assert to_comment('This is a comment.') == '#This is a comment.'
def test_to_comment_x100(): assert to_comment('\u0100 is the lowest non-Latin-1 character.') == \ '#\\u0100 is the lowest non-Latin-1 character.'
def test_to_comment_xFF(): assert to_comment('\xFF is the highest Latin-1 character.') == \ '#\xFF is the highest Latin-1 character.'
def test_to_comment_no_ensure_ascii(cin, cout): assert to_comment(cin, ensure_ascii=False) == cout