Created
June 13, 2017 14:54
-
-
Save hanya/d9f831614d4454282634d672f3542fd6 to your computer and use it in GitHub Desktop.
Copies translated entries and merge with new en.catkeys for Haiku catkeys
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
import argparse | |
class Catkeys: | |
def __init__(self, path): | |
self.path = path | |
with open(path, "r") as f: | |
lines = f.readlines() | |
if not lines: | |
raise Exception('no data: ' + path) | |
data = {} | |
for line in lines[1:]: # ignore first line | |
parts = line.split("\t") | |
if len(parts) == 4: | |
# original \t context \t comment \t translated | |
data[(parts[0], parts[1])] = parts[3].rstrip() | |
self.data = data | |
self.header = lines[0].rstrip() | |
def get(self, key): | |
return self.data.get(key, None) | |
def set(self, key, value): | |
self.data[key] = value | |
def write(self, path): | |
with open(path, "w") as f: | |
f.write(self.header) | |
f.write("\n") | |
for key, value in self.data.items(): | |
f.write("{}\t{}\t\t{}\n".format(key[0], key[1], value)) | |
def copy(en, src, dest): | |
csrc = Catkeys(src) | |
cdest = Catkeys(en) | |
for key, value in cdest.data.items(): | |
v = csrc.get(key) | |
if v: | |
cdest.set(key, v) | |
cdest.write(dest) | |
return 0 | |
def main(): | |
parser = argparse.ArgumentParser( | |
description='Copy translated data into new file') | |
parser.add_argument('--cat', | |
help='en.catkeys') | |
parser.add_argument('--src', | |
help='File to read') | |
parser.add_argument('--dest', | |
help='File to be written') | |
args = parser.parse_args() | |
return copy(args.cat, args.src, args.dest) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment