Created
July 12, 2012 10:05
-
-
Save yongsun/3097155 to your computer and use it in GitHub Desktop.
A simple python utility for updating and merging Java properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys, os | |
import re | |
class Properties(object): | |
def __init__(self, prop_name): | |
self._keys = [] # the keys in parsing order | |
self._props = {} # the property dict | |
self._rawprops = {} # the raw property dict | |
self._comments = {} # the comment dict | |
self.__load(prop_name) | |
def __load(self, prop_name): | |
try: | |
stream = open(prop_name) | |
lines = stream.readlines() | |
self.__parse(lines) | |
except IOError, e: | |
print >> sys.stderr, "\nERROR: Unable to open/read the properties file: ", prop_name, "!!!" | |
usage() | |
sys.exit(3) | |
def __parse(self, lines): | |
comment = '' | |
i = iter(lines) | |
for line in i: | |
line = line.strip () | |
if not line or line[0] == '#': | |
comment += line + '\n' | |
continue | |
m = re.search (r'\s*(.*?)\s*=\s*(.*)$', line) | |
if m: | |
key, value = m.group(1), m.group(2) | |
rawvalue = value | |
else: | |
continue | |
while (line[-1] == '\\'): | |
line = i.next().strip() | |
value = value[:-1] + line | |
rawvalue += '\n' + line | |
self._keys.append (key) | |
self._props[key] = value | |
self._rawprops[key] = rawvalue | |
self._comments[key] = comment | |
comment = '' | |
def getKeys (self): | |
return self._keys | |
def getPropertyDict (self): | |
return self._props | |
def getProperty (self, key): | |
return self._props.get(key,'') | |
def getRawProperty (self, key): | |
return self._rawprops.get(key,'') | |
def getComment (self, key): | |
return self._comments.get(key,'') | |
def __getitem__ (self, name): | |
""" To support direct dictionary like access """ | |
return self.getProperty(name) | |
def dump (self, out=sys.stdout): | |
for key in self._keys: | |
out.write (self.getComment(key)) | |
out.write (''.join((key,'=', self.getRawProperty(key), '\n'))) | |
def merge (eng_fname, loc1_fname, loc2_fname): | |
eng = Properties (eng_fname) | |
loc1 = Properties (loc1_fname) | |
loc2 = Properties (loc2_fname) | |
for key in eng.getKeys(): | |
sys.stdout.write(eng.getComment(key)) | |
if key in loc1.getPropertyDict(): | |
sys.stdout.write(''.join((key,'=',loc1[key],'\n'))) | |
elif key in loc2.getPropertyDict(): | |
sys.stdout.write(''.join((key,'=',loc2[key],'\n'))) | |
else: | |
sys.stderr.write(''.join(("Key \'",key,'\' is messing !!!\n'))) | |
def update (eng_orig_fname, eng_new_fname): | |
eng_new = Properties (eng_new_fname) | |
eng_orig = Properties (eng_orig_fname) | |
updated = [] | |
added = [] | |
for key in eng_new.getKeys(): | |
if key in eng_orig.getPropertyDict(): | |
if eng_new[key] != eng_orig[key]: | |
updated.append (key) | |
else: | |
added.append (key) | |
wc = 0 | |
print >> sys.stderr, "==========", len(added), "udded properties: ==========\n" | |
for key in added: | |
sys.stdout.write (eng_new.getComment(key)) | |
sys.stdout.write (''.join((key,'=', eng_new.getRawProperty(key), '\n'))) | |
wc += len(eng_new[key].split()) | |
print >> sys.stderr, "\n========== wordcount:", wc, "==========" | |
wc = 0 | |
print >> sys.stderr, "\n==========", len(updated), "updated properties:==========\n" | |
for key in updated: | |
sys.stdout.write (eng_new.getComment(key)) | |
sys.stdout.write (''.join((key,'=', eng_new.getRawProperty(key), '\n'))) | |
wc += len(eng_new[key].split()) | |
print >> sys.stderr, "\n========== wordcount:", wc, "==========" | |
def status (eng_fname, loc_fname): | |
eng = Properties (eng_fname) | |
loc = Properties (loc_fname) | |
wc = 0 | |
missed = [] | |
for key in eng.getKeys(): | |
if key not in loc.getPropertyDict(): | |
missed.append (key) | |
wc = 0 | |
print >> sys.stderr, "==========", len(missed), "untranslated properties: ==========\n" | |
for key in missed: | |
sys.stdout.write (eng.getComment(key)) | |
sys.stdout.write (''.join((key,'=', eng.getRawProperty(key), '\n'))) | |
wc += len(eng[key].split()) | |
print >> sys.stderr, "\n========== wordcount:", wc, "==========" | |
def wordcount (eng_fname): | |
eng = Properties (eng_fname) | |
wc = 0 | |
for key in eng.getKeys(): | |
wc += len(eng[key].split()) | |
print eng_fname, ':', wc, "\n" | |
def usage (): | |
print >> sys.stderr, '''\nUsage: | |
proputil merge eng.properties loc1.properties loc2.properties | |
merge loc1.propeties and loc2.properties, output to standard output, | |
NOTE: loc1.properties has higher piority! | |
proputil update eng_orig.properties eng_new.properties | |
report the updated and newly added properties from eng_orig.properties | |
to eng_new.properties | |
proputil status eng.properties loc.properties | |
report the localization status of loc.properties | |
proputil wordcount test.properties | |
report the wordcount of test.properties | |
''' | |
def main (): | |
if len(sys.argv) < 2: | |
usage () | |
sys.exit(1) | |
op = sys.argv[1] | |
try: | |
if op in ("merge", "update", "status", "wordcount"): | |
eval(op) (*sys.argv[2:]) | |
elif op in ("-h", "-help", "--help"): | |
usage () | |
sys.exit (0) | |
else: | |
print >> sys.stderr, "\nERROR: Not supported operations!!!" | |
usage () | |
sys.exit (1) | |
except TypeError: | |
print >> sys.stderr, "\nERROR: Invalid arguments!!!" | |
usage () | |
sys.exit (2) | |
if __name__ == "__main__": | |
main () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment