Skip to content

Instantly share code, notes, and snippets.

@maxanier
Created April 16, 2017 12:26
Show Gist options
  • Save maxanier/3a31b5549644afe213c6a3fe8d497fe4 to your computer and use it in GitHub Desktop.
Save maxanier/3a31b5549644afe213c6a3fe8d497fe4 to your computer and use it in GitHub Desktop.
Convert all localisation keys from camecase to snakecase
import re
import sys, getopt
def convert(name):
s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
def main(argv):
inputfile = ''
outputfile = ''
try:
argv[0]
opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])
except:
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('test.py -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
print('Input file is "{0}"'.format(inputfile))
print('Output file is "{0}"'.format(outputfile))
with open(inputfile) as f:
content = f.readlines()
for i,line in enumerate(content):
if((line[0] is not '#') and (line[0] is not '\n')):
parts = line.split('=')
print(parts)
content[i]=convert(parts[0])+'='+parts[1]
with open(outputfile,'w') as f:
f.writelines(content)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment