def dupe_key(fname, old, new): if not os.path.exists(fname): return orig = open(fname).read() obj = parse(orig) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {fname} {ent}") # locate the existing key for item in obj.body: if isinstance(item, Message): if item.id.name == old: item2 = copy.deepcopy(item) item2.id.name = new obj.body.append(item2) break # print(item.id.name) # print(item.value.elements) # for e in item.value.elements: # print(e.value) modified = serialize(obj, with_junk=True) # escape leading dots modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) # ensure the resulting serialized file is valid by parsing again obj = parse(modified) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"introduced junk! {fname} {ent}") # it's ok, write it out open(fname, "w").write(modified)
def write_entry(fname, key, entry): assert entry entry.id.name = key if not os.path.exists(fname): orig = "" else: with open(fname) as file: orig = file.read() obj = parse(orig) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {fname} {ent}") obj.body.append(entry) modified = serialize(obj, with_junk=True) # escape leading dots modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) # ensure the resulting serialized file is valid by parsing again obj = parse(modified) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"introduced junk! {fname} {ent}") # it's ok, write it out with open(fname, "w") as file: file.write(modified)
def check_file(path: str, fix: bool) -> bool: "True if file is ok." orig_text = open(path).read() obj = parse(orig_text, with_spans=False) # make sure there's no junk for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {path} {ent}") # serialize new_text = serialize(obj) # make sure serializing did not introduce new junk obj = parse(new_text, with_spans=False) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file introduced junk! {path} {ent}") if new_text == orig_text: return True if fix: print(f"Fixing {path}") open(path, "w", newline="\n", encoding="utf8").write(new_text) return True else: print(f"Bad formatting in {path}") return False
def add_simple_message(fname, key, message): orig = "" if os.path.exists(fname): with open(fname) as file: orig = file.read() obj = parse(orig) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {fname} {ent}") obj.body.append(Message(Identifier(key), Pattern([TextElement(message)]))) modified = serialize(obj, with_junk=True) # escape leading dots modified = re.sub(r"(?ms)^( +)\.", '\\1{"."}', modified) # ensure the resulting serialized file is valid by parsing again obj = parse(modified) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"introduced junk! {fname} {ent}") # it's ok, write it out with open(fname, "w") as file: file.write(modified)
def check_file(path: str, fix: bool) -> bool: "True if file is ok." orig_text = open(path).read() obj = parse(orig_text, with_spans=False) # make sure there's no junk for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {path} {ent}") # serialize new_text = serialize(obj) # make sure serializing did not introduce new junk obj = parse(new_text, with_spans=False) for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file introduced junk! {path} {ent}") if new_text == orig_text: return check_missing_terms(path) if fix: print(f"Fixing {path}") open(path, "w", newline="\n", encoding="utf8").write(new_text) return True else: print(f"Bad formatting in {path}") print("\n".join( difflib.unified_diff( orig_text.splitlines(), new_text.splitlines(), fromfile="bad", tofile="good", lineterm="", ))) return False
def cldr_to_ftl(langs, config): page_url = "" # stem of address for individual Region or Language pages find_str = "" # searched tag to extract data name_str = "" # stem of name of ftl variables if config == 0: page_url = "http://icu-project.org/trac/browser/trunk/icu4c/source/data/lang/" find_str = "Languages{" name_str = "language-name-{}" elif config == 1: page_url = "http://icu-project.org/trac/browser/trunk/icu4c/source/data/region/" find_str = "Countries{" name_str = "region-name-{}" directory = "ftl_files/" for lang in langs: new_directory = re.sub(".txt", "", lang) if not os.path.exists(directory + new_directory): os.makedirs(directory + new_directory) new_file = new_directory + "/resources.ftl" wout = open(directory + new_file, "a") if lang[-4:] == ".txt": source = urllib.request.urlopen(page_url + lang + "?format=txt") text = source.read().decode('utf-8', "strict") text_lines = text.split("\n") interior = 0 catch = False for line in text_lines: line = re.sub("\ufeff", "", line) if bool(re.search("^//", line)) == False and bool( re.search("[(/**)(**/)]", line)) == False and line != '': line = re.sub("\s+", " ", line) line = re.sub("^\s", "", line) if "{" in line and "}" not in line: interior += 1 if find_str in line: catch = True elif "}" in line and "{" not in line: interior -= 1 catch = False elif len(re.findall("{", line)) < 2 and len( re.findall("}", line)) < 2: if catch == True: parts = re.split("[\{\}\"]", line) res = ast.Resource() l10n_id = ast.Identifier(name_str.format(parts[0])) value = ast.Pattern([ast.TextElement(parts[2])]) msg = ast.Message(l10n_id, value) res.body.append(msg) s = serialize(res) wout.write(s) wout.close()
def add_simple_message(fname, key, message): orig = "" if os.path.exists(fname): orig = open(fname).read() obj = parse(orig) obj.body.append(Message(Identifier(key), Pattern([TextElement(message)]))) modified = serialize(obj) open(fname, "w").write(modified)
def transform_string_in_file(path): obj = parse(open(path).read(), with_spans=False) changed = False for ent in obj.body: if isinstance(ent, Junk): raise Exception(f"file had junk! {path} {ent}") if getattr(ent, "id", None): key = ent.id.name for (target_key, src, dst) in target_repls: if key == target_key: for elem in ent.value.elements: newval = elem.value.replace(src, dst) if newval != elem.value: elem.value = newval changed = True if changed: open(path, "w", encoding="utf8").write(serialize(obj)) print("updated", path)
def pretty_print(fileType, data): ast = parse(data) print(serialize(ast))
def pretty_print(fileType, data): resource = ast.from_json(data) print(serialize(resource))
def serialize(self, out): body = [unit.to_entry() for unit in self.units] out.write(serialize(ast.Resource(body)).encode(self.encoding))