def load_contacts_and_settings(self): self.contacts = [] self.call_protocol = self.settings.get_stripped( "call_protocol", "main", self.CALLING_PROTOCOL) self.cell_protocol = self.settings.get_stripped( "cell_protocol", "main", self.CALLING_PROTOCOL) self.home_protocol = self.settings.get_stripped( "home_protocol", "main", self.CALLING_PROTOCOL) self.mail_protocol = self.settings.get_stripped( "mail_protocol", "main", self.MAILING_PROTOCOL) self.vcard_files = self.get_vcf_files() # Install a demo vCard file if non is configured (will be ignored once configured) if len(self.vcard_files) == 0: sample_vcf_path = os.path.join(kp.user_config_dir(), self.SAMPLE_VCF) if not os.path.exists(sample_vcf_path): sample_vcf_text = self.load_text_resource( self.PACKAGED_SAMPLE_VCF).replace("\r\n", "\n") with open(sample_vcf_path, "w") as f: f.write(sample_vcf_text) f.close() self.load_vcard_file(sample_vcf_path) return for vcard_file in self.vcard_files: vcard_file_path = os.path.join(kp.user_config_dir(), vcard_file.filename) if vcard_file.custom_tag: self.info( f"Loading {vcard_file.filename} with custom VCARD tags") try: if vcard_file.source and os.path.exists(vcard_file.source): copyfile(vcard_file.source, vcard_file_path) if not os.path.exists(vcard_file_path): if vcard_file.source != None: self.err( f"Failed to load vCard file '{vcard_file_path}'. File does not exist and cannot be copied from {vcard_file.source}" ) else: self.err( f"Failed to load vCard file '{vcard_file_path}'. File does not exist" ) continue self.load_vcard_file(vcard_file_path, vcard_file) except LookupError as exc: self.err( f"Failed to load vCard (.vcf) file {vcard_file_path}, {exc}" ) self.err(f"Available encodings are: \n{set(aliases.keys())}") except Exception as exc: self.err( f"Failed to load vCard (.vcf) file {vcard_file_path}, {exc}" )
def read_defs(self, defs_file): defs = None try: # Either file exist in the user profile dir cvtdefs = os.path.join(kp.user_config_dir(), defs_file) if os.path.exists(cvtdefs): self.info( f"Loading custom conversion definition file '{cvtdefs}'") self.customized_config = True with open(cvtdefs, "r", encoding="utf-8") as f: defs = json.load(f) else: # ... or it may be in the plugin try: cvtdefs = os.path.join("data/", defs_file) defs_text = self.load_text_resource(cvtdefs) defs = json.loads(defs_text) self.dbg( f"Loaded internal conversion definitions '{cvtdefs}'") except Exception as exc: defs = {"measures": {}} self.dbg( f"Did not load internal definitions file '{cvtdefs}', {exc}" ) pass except Exception as exc: self.warn(f"Failed to load definitions file '{cvtdefs}', {exc}") return defs
def on_execute(self, item, action): if item and item.category() == self.ITEMCAT_RESULT: kpu.set_clipboard(item.data_bag()) elif item and item.category() == self.ITEMCAT_RELOAD_DEFS: self.reconfigure() self.on_catalog() elif item and item.category() == self.ITEMCAT_CREATE_CUSTOM_DEFS: try: builtin_cvtdefs = os.path.join("data/", self.CVTDEF_FILE) builtin_cvtdefs_text = self.load_text_resource( builtin_cvtdefs).replace("\r\n", "\n") custom_cvtdefs = os.path.join(kp.user_config_dir(), self.CVTDEF_FILE) if os.path.exists(custom_cvtdefs): self.warn( f"Customized conversion file '{custom_cvtdefs}' already exists. It hasn't been overwritten" ) else: with open(custom_cvtdefs, "w", encoding="utf-8") as f: f.write(builtin_cvtdefs_text) f.close() kpu.explore_file(custom_cvtdefs) self.reconfigure() self.on_catalog() except Exception as exc: self.warn( f"Failed to create custom conversion definition file '{custom_cvtdefs}', {exc}" )
def _save_settings(self): """Save the user config file with all installed packages """ self.dbg("Saving settings") save_path = os.path.join(kp.user_config_dir(), "{}.ini".format(self.package_full_name())) config = configparser.ConfigParser() config.read(save_path) if "main" not in config: config.add_section("main") if "repository" in config[ "main"] and config["main"]["repository"] != self._repo_url: config["main"]["repository"] = self._repo_url if "alternative_repository" in config["main"] and config["main"][ "alternative_repository"] != self._alt_repo_url: config["main"]["alternative_repository"] = self._alt_repo_url config["main"]["installed_packages"] = "\n{}".format("\n".join( self._installed_packages)) with open(save_path, "w") as ini_file: config.write(ini_file)
def load_conversions(self): try: cvtdefs = os.path.join(kp.user_config_dir(), self.CVTDEF_FILE) if os.path.exists(cvtdefs): self.info( f"Loading custom conversion definition file '{cvtdefs}'") self.customized_config = True with open(cvtdefs, "r") as f: defs = json.load(f) else: self.customized_config = False cvtdefs = os.path.join("data/", self.CVTDEF_FILE) defs_text = self.load_text_resource(cvtdefs) defs = json.loads(defs_text) except Exception as exc: self.warn(f"Failed to load definitions file '{cvtdefs}', {exc}") return self.measures = { measure["name"]: measure for measure in defs["measures"] } self.all_units = {} for measure in defs["measures"]: for unit in measure["units"]: for alias in unit["aliases"]: alias = alias.lower() if alias in self.all_units: self.warn(f"Alias {alias} is defined multiple times") self.all_units[alias] = measure
def _load_resource_icon(self, name): full_name = self.package_full_name() package_path = self.RES_ICON_PATH.format(package=full_name, name=name) config_icon_path = os.path.join( keypirinha.user_config_dir(), self.RES_ICON_CONFIG_PATH.format(name=name)) cache_dir = keypirinha.package_cache_dir(full_name) # create package dir in cache dir try: os.makedirs(cache_dir) except: pass # copy to cache so that cache:// works try: shutil.copy(config_icon_path, cache_dir) package_path = self.CACHE_ICON_CONFIG_PATH.format( package=full_name, name=name) except Exception as e: self.dbg("Could not copy {file} to cache {cache} ".format( file=config_icon_path, cache=cache_dir)) return self.load_icon([config_icon_path, package_path])