def Load(self, filename, params={}, output_file_name=None, extra_languages=None, load_translations=True): self.filename_base = os.path.splitext(output_file_name or os.path.basename(filename))[0] self.orig_filename_base = os.path.splitext( os.path.basename(filename))[0] self.filedir = os.path.dirname(filename) self.resources = self.__load_resource( filename, params, extra_languages=extra_languages, load_translations=load_translations) #self.all_source_files.add(filename) file_list = ScanDir(self.filedir) for x in file_list: #print >>sys.stderr, "Adding", x, ":", x[0], x.startswith(".") if x.startswith("."): raise Exception(x) self.all_source_files.update(file_list) self.active_source_files.update(file_list) self["grit"] = dict(self.resources["resources"].attrs) if "outputs" in self.resources["nodes"]["internals"]: new_top = empty.OutputsNode() new_top.name = "outputs" _seq, old_top = self.resources["nodes"]["internals"]["outputs"][0] new_top.attrs = dict(old_top.attrs) for old_node in iter_gritnode(old_top, node_io.OutputNode): #if (self.orig_filename_base != self.filename_base and # old_node.attrs.get("type", None) == "data_package"): # old_node.attrs["filename"] = old_node.attrs["filename"].replace( # self.orig_filename_base+"_", self.filename_base+"_") #if self.orig_filename_base == "components_google_chrome_strings": # print >>sys.stderr, old_node.attrs["filename"] new_top.AddChild(old_node) old_node.parent = new_top self["outputs"] = new_top if "translations" in self.resources["nodes"]["internals"]: new_top = empty.TranslationsNode() new_top.name = "translations" _seq, old_top = self.resources["nodes"]["internals"][ "translations"][0] new_top.attrs = dict(old_top.attrs) for old_node in recursive_iter_gritnode( self.resources["resources"], node_io.FileNode): new_node = node_io.FileNode() new_node.name = "file" new_node.attrs = dict(old_node.attrs) if "path" in new_node.attrs: self.all_source_files.add(new_node.attrs["path"].replace( "\\", "/")) new_node.attrs["path"] = os.path.join( os.getcwd(), old_node.ToRealPath(old_node.GetInputPath())) new_top.AddChild(new_node) new_node.parent = new_top self["translations"] = new_top if "release" in self.resources["nodes"]["internals"]: new_release = misc.ReleaseNode() new_release.name = "release" _seq, old_top = self.resources["nodes"]["internals"]["release"][0] new_release.attrs = dict(old_top.attrs) self["release"] = new_release if "includes" in self.resources["nodes"]["internals"]: _seq, old_top = self.resources["nodes"]["internals"]["includes"][0] if self.keep_sequence: new_top = old_top self.__remove_part_nodes(new_top) else: new_top = empty.IncludesNode() new_top.name = "includes" new_top.attrs = dict(old_top.attrs) self["includes"] = new_top self["include-files"] = {} for _seq, old_node in sorted([ x for x in self.resources["nodes"].values() if isinstance(x, tuple) and isinstance(x[1], include.IncludeNode) ]): new_node = include.IncludeNode() new_node.name = "include" new_node.attrs = dict(old_node.attrs) if "file" in new_node.attrs: old_file = new_node.attrs["file"].replace("\\", "/") new_node.attrs["file"] = old_file self._AddFile(old_file) self["include-files"][new_node.attrs["name"]] = (_seq, new_node) if "messages" in self.resources["nodes"]["internals"]: _seq, old_top = self.resources["nodes"]["internals"]["messages"][0] if self.keep_sequence: new_top = old_top self.__remove_part_nodes(new_top) else: new_top = empty.MessagesNode() new_top.name = "messages" new_top.attrs = dict(old_top.attrs) self["messages"] = new_top self["message-entries"] = {} for _seq, old_node in sorted([ x for x in self.resources["nodes"].values() if isinstance(x, tuple) and isinstance(x[1], message.MessageNode) ]): #if old_node.GetCliques(): # print old_node.GetCliques()[0].MessageForLanguage("no").GetPresentableContent() self["message-entries"][old_node.attrs["name"]] = (_seq, old_node) if "structures" in self.resources["nodes"]["internals"]: _seq, old_top = self.resources["nodes"]["internals"]["structures"][ 0] if self.keep_sequence: new_top = old_top self.__remove_part_nodes(new_top) else: new_top = empty.StructuresNode() new_top.name = "structures" new_top.attrs = dict(old_top.attrs) self["structures"] = new_top self["structure-entries"] = {} for _seq, old_node in sorted([ x for x in self.resources["nodes"].values() if isinstance(x, tuple) and isinstance(x[1], structure.StructureNode) ]): if self.keep_sequence: new_node = old_node else: new_node = structure.StructureNode() new_node.name = "structure" new_node.attrs = dict(old_node.attrs) if "file" in new_node.attrs: old_file = new_node.attrs["file"].replace("\\", "/") new_node.attrs["file"] = old_file self._AddFile(old_file) self["structure-entries"][new_node.attrs["name"]] = (_seq, new_node)
def Update(self, other, load_translations=True): self.active_other_files.update(other.active_source_files) if "outputs" in other and "outputs" not in self: self["outputs"] = other["outputs"] elif "outputs" in other: top_node = self["outputs"] for node in other["outputs"].ActiveDescendants(): if not isinstance(node, node_io.OutputNode): continue top_node.AddChild(node) node.parent = top_node if "releases" in other and "releases" not in self: self["releases"] = other["releases"] if "includes" in other and "includes" not in self: self["includes"] = other["includes"] self.setdefault("includes", {}) for name, seq_node in other.get("include-files", {}).items(): #seq_node[1].attrs["vivaldi-added"]="1" if self.keep_sequence: self.__replace_node(seq_node[1], self["includes"]) if name in self.get("include-files", {}): self["include-files"][name] = (self["include-files"][name][0], seq_node[1]) else: self.setdefault("include-files", {})[name] = seq_node if "messages" in other and "messages" not in self: self["messages"] = other["messages"] self.setdefault("message-entries", {}) for name, seq_node in other.get("message-entries", {}).items(): #seq_node[1].attrs["vivaldi-added"]="1" if self.keep_sequence: self.__replace_node(seq_node[1], self["messages"]) if name in self.get("message-entries", {}): if load_translations: new_clique = seq_node[1].GetCliques()[0].clique old_clique = self["message-entries"][name][1].GetCliques( )[0].clique for lang in old_clique: if lang not in new_clique: new_clique[lang] = old_clique[lang] self["message-entries"][name] = ( self["message-entries"][name][0], seq_node[1]) else: self.setdefault("message-entries", {})[name] = seq_node if "structures" in other and "structures" not in self: self["structures"] = other["structures"] self.setdefault("structure-entries", {}) for name, seq_node in other.get("structure-entries", {}).items(): #seq_node[1].attrs["vivaldi-added"]="1" if self.keep_sequence: self.__replace_node(seq_node[1], self["structures"]) if name in self["structure-entries"]: self["structure-entries"][name] = ( self["structure-entries"][name][0], seq_node[1]) else: self["structure-entries"][name] = seq_node translations_node = None for lnode in sorted(list( recursive_iter_gritnode(self.resources["resources"], node_io.FileNode)), key=lambda x: x.GetLang()): lang = lnode.GetLang() if lang == "en" or lang == "x-P-pseudo": continue if lang in self.translation_names: continue if "generated_translations" not in self: translations_node = empty.TranslationsNode() translations_node.name = "translations" old_top = self.get("translations", other.get("translations")) translations_node.attrs = dict(old_top.attrs) self["generated_translations"] = translations_node new_node = node_io.FileNode() new_node.name = "file" #print self.translation_dir filename = os.path.join(self.translation_dir, self.filename_base + "_" + lang + ".xtb").replace("\\", "/") self.translation_names[lang] = filename new_node.attrs["path"] = filename new_node.attrs["lang"] = lang translations_node.AddChild(new_node) new_node.parent = translations_node
def merge_resource(origin_file, overlay_file, target_location, params={}, action=PRINT_NONE, json_file=None, root_dir=None, translation_dir="resources", output_file_name=None, translation_stamp=None): util.PathSearcher.Configure([ os.path.dirname(target_location), os.path.dirname(overlay_file), os.path.dirname(origin_file), ]) mergeresource = GritFile(translation_dir=translation_dir) mergeresource.Load(overlay_file, params, output_file_name=output_file_name, load_translations=True) extra_translations = mergeresource.GetTranslationsCopy() mainresource = GritFile(translation_dir=translation_dir, keep_sequence=True) mainresource.Load(origin_file, params, output_file_name=output_file_name, extra_languages=extra_translations, load_translations=True) if action in [WRITE_GRD_FILES, PRINT_ALL]: mainresource.ReplaceGoogle(exceptions=REPLACE_GOOGLE_EXCEPTIONS) mainresource.Update(mergeresource, load_translations=True) resource_target_file = mainresource.filename_base + ".grd" resource_target_dir = os.path.join(target_location, translation_dir) try: os.makedirs(resource_target_dir) except: pass # Just ignore the errors here. Most likely the dir exists, # otherwise next step will fail instead if action in [WRITE_GRD_FILES, PRINT_ALL]: for lang, extra_translation_items in mergeresource.resources[ "resources"].UberClique().additional_translations_.items(): xtb_callback = mainresource.resources["resources"].UberClique( ).GenerateXtbParserCallback(lang, override_exisiting=True) for tid, string in extra_translation_items.items(): #print "Item", tid, type(string), string xtb_callback(tid, string) #translations.setdefault(lang, {})[tid] = util.EscapeHtml(string) translations = dict(mainresource.Translations()) for lnode in iter_gritnode( mainresource.get("generated_translations", empty.TranslationsNode()), node_io.FileNode): translations.setdefault(lnode.GetLang(), {}) for lang, translation in translations.items(): if lang == "en" or lang == "x-P-pseudo": continue # found in the grd file if action == PRINT_ALL: print() print("Writing generated language file",\ mainresource.GetLanguageFileName(lang)) print("*" * 40) xtb_filename = os.path.join(target_location, mainresource.GetLanguageFileName(lang)) xtbfile = io.StringIO() xtbfile.write(u"""<?xml version="1.0" encoding="utf-8" ?>\n""" """<!DOCTYPE translationbundle>\n""" """<translationbundle lang="%s">""" % (lang if lang != "nb" else "no")) for msgid, text in sorted(translation.items()): try: xtbfile.write( u"""<translation id="%s">%s</translation>\n""" % (msgid, text)) except: print(repr(msgid), "|", repr(text), "|",\ repr(text.encode("utf8")), file=sys.stderr) raise xtbfile.write(u"</translationbundle>\n") refresh_file.conditional_refresh_file( xtb_filename, xtbfile.getvalue().encode("utf8"), translation_stamp) xtbfile.close() if action == PRINT_ALL: print() print("Writing generated file", resource_target_file) print("*" * 40) new_resource = mainresource.GritNode() content = new_resource.FormatXml(" ", False) grd_filename = os.path.join(target_location, resource_target_file) grdfile = io.StringIO() grdfile.write(unicode("""<?xml version="1.0" encoding="utf-8"?>\n""")) grdfile.write(content) refresh_file.conditional_refresh_file( grd_filename, grdfile.getvalue().encode("utf-8")) grdfile.close() special_update_sources = [] special_update_targets = [] special_copy_sources = [] special_copy_targets = [] if json_file and root_dir: import update_file target_name = os.path.basename(target_location) update_config = update_file.UpdateConfig(json_file, target_name, target_location, root_dir) special_update_sources = update_config.get_update_sources() special_update_targets = update_config.get_update_targets() special_copy_sources = update_config.get_copy_sources() special_copy_targets = update_config.get_copy_targets() update_config.update_files(force=True) update_config.copy_files(force=True) mainresource.RemoveSpecialUpdateSources(special_update_sources + special_copy_sources) sources = { PRINT_MAIN_SOURCES: mainresource.GetAllMainSources(), PRINT_SECONDARY_SOURCES: mergeresource.GetAllMainSources(), PRINT_MAIN_RESOURCES: mainresource.GetActiveSources(), PRINT_SECONDARY_RESOURCES: mainresource.GetActiveOtherSources(), PRINT_TARGET_SOURCES: [ os.path.normpath(os.path.join(target_location, x)).replace("\\", "/") for x in mainresource.GetTargetFiles() ], PRINT_TARGET_RESOURCES: [ os.path.normpath(os.path.join(target_location, x)).replace("\\", "/") for x in mainresource.GetMigrateTargetFiles() ], PRINT_SPECIAL_UPDATE_SOURCES: special_update_sources + special_copy_sources, PRINT_SPECIAL_UPDATE_TARGETS: special_update_targets + special_copy_targets, } if False and action != SETUP_RESOURCES: source_dir = os.path.dirname(origin_file) # initial copy, to please HTML flattening by grit for x in sources[PRINT_MAIN_RESOURCES]: try: try: os.makedirs( os.path.join(target_location, os.path.dirname(x))) except: pass refresh_file.conditional_copy(os.path.join(source_dir, x), os.path.join(target_location, x)) #print >>sys.stderr, "Copied", x, "to", target_location except shutil.Error: continue # ignore, same file except: print("Copying", x, "failed", file=sys.stderr) raise pass source_dir = os.path.dirname(overlay_file) # initial copy, to please HTML flattening by grit for x in sources[PRINT_SECONDARY_RESOURCES]: try: try: os.makedirs( os.path.join(target_location, os.path.dirname(x))) except: pass refresh_file.conditional_copy(os.path.join(source_dir, x), os.path.join(target_location, x)) #print >>sys.stderr, "Copied", x, "to", target_location except shutil.Error: continue # ignore, same file except: print("Copying", x, "failed", file=sys.stderr) raise pass json.dump(sources, open(os.path.join(target_location, "target_info.json"), "w")) return write_targets(action, sources, os.path.dirname(origin_file), os.path.dirname(overlay_file))