Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if args.inputFilename:
if args.outputFilename and ui_type != "none":
ui_type = "cmd" # silently? FIXME
else:
if ui_type == "cmd":
log.error("no input file given, try --help")
exit(1)
if ui_type == "none":
if args.reverse:
log.error("--reverse does not work with --ui=none")
sys.exit(1)
glos = Glossary()
glos.convert(
args.inputFilename,
inputFormat=args.inputFormat,
outputFilename=args.outputFilename,
outputFormat=args.outputFormat,
readOptions=readOptions,
writeOptions=writeOptions,
**convertOptions
)
sys.exit(0)
elif ui_type == "cmd":
from ui import ui_cmd
sys.exit(0 if ui_cmd.UI().run(
args.inputFilename,
outputFilename=args.outputFilename,
inputFormat=args.inputFormat,
core.StdLogHandler(noColor=args.noColor),
)
# with the logger setted up, we can import other pyglossary modules, so they
# can do some loggging in right way.
core.checkCreateConfDir()
##############################
from pyglossary.glossary import Glossary
from ui.ui_cmd import COMMAND, help, parseFormatOptionsStr
if args.verbosity != defaultVerbosity:
Glossary.init()
##############################
def dashToCamelCase(text): # converts "hello-PYTHON-user" to "helloPythonUser"
parts = text.split("-")
parts[0] = parts[0].lower()
for i in range(1, len(parts)):
parts[i] = parts[i].capitalize()
return "".join(parts)
ui_list = (
"gtk",
"tk",
"qt",
)
self.progressEnd()
yield wordCount
@classmethod
def init(cls):
cls.readFormats = []
cls.writeFormats = []
cls.readExt = []
cls.writeExt = []
cls.readDesc = []
cls.writeDesc = []
cls.loadPlugins(join(dirname(__file__), "plugins"))
cls.loadPlugins(userPluginsDir)
Glossary.init()
prefOptions = {}
for param in prefOptionsKeys:
value = getattr(args, param, None)
if value is not None:
prefOptions[param] = value
convertOptions = {}
for param in convertOptionsKeys:
value = getattr(args, param, None)
if value is not None:
convertOptions[param] = value
if args.inputFilename and readOptions:
inputFormat = Glossary.detectInputFormat(args.inputFilename, format=args.inputFormat)
if not inputFormat:
log.error("Could not detect format for input file %s" % args.inputFilename)
sys.exit(1)
readOptionsProp = Glossary.formatsOptionsProp[inputFormat]
for optName, optValue in readOptions.items():
if optName not in Glossary.formatsReadOptions[inputFormat]:
log.error("Invalid option name %s for format %s" % (optName, inputFormat))
sys.exit(1)
prop = readOptionsProp[optName]
optValueNew, ok = prop.evaluate(optValue)
if not ok or not prop.validate(optValueNew):
log.error("Invalid option value %s=%r for format %s" % (optName, optValue, inputFormat))
sys.exit(1)
readOptions[optName] = optValueNew
if args.outputFilename and writeOptions:
def write(self,fname):
info, self.data = self.dictFromSqlite()
self._status = "Writing to output file..."
g = Glossary()
g.data = self.data
g.setInfo("bookname", info["bookname"].encode("utf-8"))
g.write(fname)
def editor(self):
g = Glossary()
g.data = self.data
if "dictname" in self.plugin:
g.setInfo("bookname", self.plugin["dictname"])
ed = glossEditor(g)
ed.write("data/%s/db.sqlite" % self.plugin["name"])
return ed
def export(self):
fname = os.path.join(self.output_directory, "stardict.ifo")
with sqlite3.connect(self.output_db) as conn:
c = conn.cursor()
info = {}; data = []
for row in c.execute('''SELECT key,value FROM info''').fetchall():
info[row[0]]=row[1]
c.execute('''SELECT word,def,id FROM dict''')
rows = c.fetchall(); no = len(rows)
syns = c.execute('''SELECT syn,wid FROM synonyms''').fetchall()
syn_list = {x[2]:[] for x in rows}
[syn_list[syn[1]].append(syn[0]) for syn in syns]
if "sametypesequence" in info: defiFormat = info["sametypesequence"]
else: defiFormat = "h"
g = Glossary()
for i,row in enumerate(rows):
self._status = "Reading from db entry %d of %d..." % (i,no)
g.addEntry(
[row[0]] + syn_list[row[2]],
row[1],
defiFormat=defiFormat)
self._status = "Writing to output file..."
g.setInfo("bookname", info["bookname"])
g.write(fname, "Stardict")
def _do_bgl(self, filename):
data = []
g = Glossary()
g.read(filename)
if "dictname" not in self.plugin:
self.plugin["dictname"] = g.getInfo("bookname")
shutil.move("%s_files" % filename, "data/%s/res" % self.plugin["name"])
for d in g.data:
term, definition, alts = d
definition = definition.decode("utf-8")
term = term.decode("utf-8")
if "alts" in alts:
for i in range(len(alts["alts"])):
alts["alts"][i] = alts["alts"][i].decode("utf-8")
definition = re.sub(r"(?i)^[^<]+<br>\n]+>", "", definition)
encoded_str = definition.encode("utf-8")
parser = etree.HTMLParser(encoding="utf-8")
doc = pq(etree.fromstring(encoded_str, parser=parser))
def post_setup(self, cursor):
g = Glossary()
res_dirname = os.path.join(self.output_directory, "res")
g.read(self.bgl_file, verbose=0, resPath=res_dirname)
self.g_data = g._data
self.dictname = g.getInfo("bookname")
cursor.executemany('''
INSERT INTO raw (uri, flag)
VALUES (?,?)
''', [(i, FLAGS["MEMORY"]) for i in range(len(self.g_data))])
self.stages['Processor'] = BglProcessor(self)