def save_model(self, model, lmid): type_, class_, name = lmid.split(":") filename = self.get_filename(lmid) backup_filename = self.get_backup_filename(filename) if filename and \ model.modified: if model.load_error: _logger.warning( "Not saving modified language model '{}' " "due to previous error on load.".format(filename)) else: _logger.info("Saving language model '{}'".format(filename)) try: # create the path path = os.path.dirname(filename) XDGDirs.assure_user_dir_exists(path) if 1: # save to temp file basename, ext = os.path.splitext(filename) tempfile = basename + ".tmp" model.save(tempfile) # rename to final file if os.path.exists(filename): os.rename(filename, backup_filename) os.rename(tempfile, filename) model.modified = False except (IOError, OSError) as e: _logger.warning( "Failed to save language model '{}': {} ({})".format( filename, os.strerror(e.errno), e.errno))
def save(self): """ Save this theme. """ domdoc = minidom.Document() try: theme_element = domdoc.createElement("theme") theme_element.setAttribute("name", self._name) theme_element.setAttribute("format", str(self.THEME_FORMAT)) domdoc.appendChild(theme_element) for name, _type, _default in self.attributes: if name == "color_scheme_basename": element = domdoc.createElement("color_scheme") text = domdoc.createTextNode(self.color_scheme_basename) element.appendChild(text) theme_element.appendChild(element) elif name == "key_label_overrides": overrides_element = \ domdoc.createElement("key_label_overrides") theme_element.appendChild(overrides_element) tuples = self.key_label_overrides for key_id, values in list(tuples.items()): element = domdoc.createElement("key") element.setAttribute("id", key_id) element.setAttribute("label", values[0]) element.setAttribute("group", values[1]) overrides_element.appendChild(element) else: value = getattr(self, name) if _type == "s": pass elif _type == "i": value = str(value) elif _type == "d": value = str(round(float(value), 2)) elif _type == "ad": value = ", ".join(str(d) for d in value) else: assert (False) # attribute of unknown type element = domdoc.createElement(name) text = domdoc.createTextNode(value) element.appendChild(text) theme_element.appendChild(element) pretty_xml = toprettyxml(domdoc) XDGDirs.assure_user_dir_exists(self.user_path()) with open_utf8(self._filename, "w") as _file: if sys.version_info.major >= 3: _file.write(pretty_xml) else: _file.write(pretty_xml.encode("UTF-8")) except Exception as xxx_todo_changeme2: (ex) = xxx_todo_changeme2 raise Exceptions.ThemeFileError(_("Error saving ") + self._filename, chained_exception=ex) finally: domdoc.unlink()
def save(self): """ Save this theme. """ domdoc = minidom.Document() try: theme_element = domdoc.createElement("theme") theme_element.setAttribute("name", self._name) theme_element.setAttribute("format", str(self.THEME_FORMAT)) domdoc.appendChild(theme_element) for name, _type, _default in self.attributes: if name == "color_scheme_basename": element = domdoc.createElement("color_scheme") text = domdoc.createTextNode(self.color_scheme_basename) element.appendChild(text) theme_element.appendChild(element) elif name == "key_label_overrides": overrides_element = \ domdoc.createElement("key_label_overrides") theme_element.appendChild(overrides_element) tuples = self.key_label_overrides for key_id, values in list(tuples.items()): element = domdoc.createElement("key") element.setAttribute("id", key_id) element.setAttribute("label", values[0]) element.setAttribute("group", values[1]) overrides_element.appendChild(element) else: value = getattr(self, name) if _type == "s": pass elif _type == "i": value = str(value) elif _type == "d": value = str(round(float(value), 2)) elif _type == "ad": value = ", ".join(str(d) for d in value) else: assert(False) # attribute of unknown type element = domdoc.createElement(name) text = domdoc.createTextNode(value) element.appendChild(text) theme_element.appendChild(element) pretty_xml = toprettyxml(domdoc) XDGDirs.assure_user_dir_exists(self.user_path()) with open_utf8(self._filename, "w") as _file: if sys.version_info.major >= 3: _file.write(pretty_xml) else: _file.write(pretty_xml.encode("UTF-8")) except Exception as xxx_todo_changeme2: (ex) = xxx_todo_changeme2 raise Exceptions.ThemeFileError(_("Error saving ") + self._filename, chained_exception = ex) finally: domdoc.unlink()
def copy_layout(src_filename, dst_filename): src_dir = os.path.dirname(src_filename) dst_dir, name_ext = os.path.split(dst_filename) dst_basename, ext = os.path.splitext(name_ext) _logger.info( _format("copying layout '{}' to '{}'", src_filename, dst_filename)) domdoc = None svg_filenames = {} fallback_layers = {} try: with open_utf8(src_filename) as f: domdoc = minidom.parse(f) keyboard_node = domdoc.documentElement # check layout format format = LayoutLoaderSVG.LAYOUT_FORMAT_LEGACY if keyboard_node.hasAttribute("format"): format = Version.from_string( keyboard_node.attributes["format"].value) keyboard_node.attributes["id"] = dst_basename if format < LayoutLoaderSVG.LAYOUT_FORMAT_LAYOUT_TREE: raise Exceptions.LayoutFileError( \ _format("copy_layouts failed, unsupported layout format '{}'.", format)) else: # replace the basename of all svg filenames for node in LayoutLoaderSVG._iter_dom_nodes(keyboard_node): if LayoutLoaderSVG.is_layout_node(node): if node.hasAttribute("filename"): filename = node.attributes["filename"].value # Create a replacement layer name for the unlikely # case that the svg-filename doesn't contain a # layer section (as in path/basename-layer.ext). fallback_layer_name = fallback_layers.get( filename, "Layer" + str(len(fallback_layers))) fallback_layers[filename] = fallback_layer_name # replace the basename of this filename new_filename = LayoutLoaderSVG._replace_basename( \ filename, dst_basename, fallback_layer_name) node.attributes[ "filename"].value = new_filename svg_filenames[filename] = new_filename if domdoc: XDGDirs.assure_user_dir_exists(config.get_user_layout_dir()) # write the new layout file with open_utf8(dst_filename, "w") as f: xml = toprettyxml(domdoc) if sys.version_info.major == 2: # python 2? xml = xml.encode("UTF-8") f.write(xml) # copy the svg files for src, dst in list(svg_filenames.items()): dir, name = os.path.split(src) if not dir: src = os.path.join(src_dir, name) dir, name = os.path.split(dst) if not dir: dst = os.path.join(dst_dir, name) _logger.info(_format("copying svg file '{}' to '{}'", \ src, dst)) shutil.copyfile(src, dst) except OSError as ex: _logger.error("copy_layout failed: " + \ unicode_str(ex)) except Exceptions.LayoutFileError as ex: _logger.error(unicode_str(ex))
def copy_layout(src_filename, dst_filename): src_dir = os.path.dirname(src_filename) dst_dir, name_ext = os.path.split(dst_filename) dst_basename, ext = os.path.splitext(name_ext) _logger.info(_format("copying layout '{}' to '{}'", src_filename, dst_filename)) domdoc = None svg_filenames = {} fallback_layers = {} try: with open_utf8(src_filename) as f: domdoc = minidom.parse(f) keyboard_node = domdoc.documentElement # check layout format format = LayoutLoaderSVG.LAYOUT_FORMAT_LEGACY if keyboard_node.hasAttribute("format"): format = Version.from_string(keyboard_node.attributes["format"].value) keyboard_node.attributes["id"] = dst_basename if format < LayoutLoaderSVG.LAYOUT_FORMAT_LAYOUT_TREE: raise Exceptions.LayoutFileError( \ _format("copy_layouts failed, unsupported layout format '{}'.", format)) else: # replace the basename of all svg filenames for node in LayoutLoaderSVG._iter_dom_nodes(keyboard_node): if LayoutLoaderSVG.is_layout_node(node): if node.hasAttribute("filename"): filename = node.attributes["filename"].value # Create a replacement layer name for the unlikely # case that the svg-filename doesn't contain a # layer section (as in path/basename-layer.ext). fallback_layer_name = fallback_layers.get(filename, "Layer" + str(len(fallback_layers))) fallback_layers[filename] = fallback_layer_name # replace the basename of this filename new_filename = LayoutLoaderSVG._replace_basename( \ filename, dst_basename, fallback_layer_name) node.attributes["filename"].value = new_filename svg_filenames[filename] = new_filename if domdoc: XDGDirs.assure_user_dir_exists(config.get_user_layout_dir()) # write the new layout file with open_utf8(dst_filename, "w") as f: xml = toprettyxml(domdoc) if sys.version_info.major == 2: # python 2? xml = xml.encode("UTF-8") f.write(xml) # copy the svg files for src, dst in list(svg_filenames.items()): dir, name = os.path.split(src) if not dir: src = os.path.join(src_dir, name) dir, name = os.path.split(dst) if not dir: dst = os.path.join(dst_dir, name) _logger.info(_format("copying svg file '{}' to '{}'", \ src, dst)) shutil.copyfile(src, dst) except OSError as ex: _logger.error("copy_layout failed: " + \ unicode_str(ex)) except Exceptions.LayoutFileError as ex: _logger.error(unicode_str(ex))