def fetch_as_json(url): res = open_request(url) if res.status == 200: return json.loads(res.read()) else: console.error("Error fetching URL {}".format(url)) raise StatusError(res.status)
def main(): (options, _) = get_args() file = options.config objname = options.name if file.endswith(".py"): config_base = try_file(file) if config_base == False: # I don't think it works... but let's put it here config_base = try_file(file + ".py") else: config_base = try_file(file + ".py") if config_base == False: console.error("Config file {} not found or not readable".format( "{}, {}.py".format(file, file) if file.endswith("py") else file)) sys.exit(1) console.debug("Parsing config file: {}".format(file)) if objname not in dir(config_base): console.error( "Exports not found. Please make sure your config in in a object called '{}'." .format(objname)) sys.exit(1) config_object = getattr(config_base, objname) console.debug("Config load:") displayable_config_object = sanitize(config_object) console.debug( json.dumps(displayable_config_object, indent=2, sort_keys=True)) build(config_object)
def write_titles_to_file(self, filename): try: with open(filename, "w") as file: file.write("\n".join(self.titles)) except Exception as e: console.error("File {} is not writable: {}".format( filename, str(e))) sys.exit(1)
def export_words(self, converter="opencc", **kwargs): if converter == "opencc": console.debug("Exporting {} words with OpenCC".format( len(self.words))) from mw2fcitx.exporters.opencc import export self.exports = export(self.words, **kwargs) elif type(converter) == type(self.export_words): console.debug("Exporting {} words with custom converter".format( len(self.words))) self.exports = converter(self.words, **kwargs) else: console.error("No such exporter: {}".format(converter))
def generate_dict(self, generator="pinyin", **kwargs): if generator == "pinyin": from mw2fcitx.dictgen import pinyin dest = kwargs.get("output") if not dest: console.error( "Dictgen 'pinyin' can only output to files. Please give the file path in the 'output' argument." ) return pinyin(self.exports, **kwargs) elif generator == "rime": from mw2fcitx.dictgen import rime self.dict = rime(self.exports, **kwargs) else: console.error("No such dictgen: {}".format(generator))
def load_titles_from_file(self, filename, **kwargs): if not os.access(filename, os.R_OK): console.error("File {} is not readable".format(filename)) sys.exit(1) self.load_titles(open(filename, "r").read())