Esempio n. 1
0
def write_ext(glos, filename, sort=True, dictZip=True):
    if sort:
        g = glos.copy()
        g.data.sort()
    else:
        g = glos
    try:
        import _stardictbuilder
    except ImportError:
        log.error('Binary module "_stardictbuilder" can not be imported! '+\
            'Using internal StarDict builder')
        return g.writeStardict(filename, sort=False)
    db = _stardictbuilder.new_StarDictBuilder(filename)
    _stardictbuilder.StarDictBuilder_swigregister(db)
    for item in g.data:
        _stardictbuilder.StarDictBuilder_addHeadword(db, item[0], item[1], '')
    _stardictbuilder.StarDictBuilder_setTitle(db, g.getInfo('name'))
    _stardictbuilder.StarDictBuilder_setAuthor(db, g.getInfo('author'))
    _stardictbuilder.StarDictBuilder_setLicense(db, g.getInfo('license'))
    _stardictbuilder.StarDictBuilder_setOrigLang(db, g.getInfo('origLang'))
    _stardictbuilder.StarDictBuilder_setDestLang(db, g.getInfo('destLang'))
    _stardictbuilder.StarDictBuilder_setDescription(db, g.getInfo('description'))
    _stardictbuilder.StarDictBuilder_setComments(db, g.getInfo('comments'))
    _stardictbuilder.StarDictBuilder_setEmail(db, g.getInfo('email'))
    _stardictbuilder.StarDictBuilder_setWebsite(db, g.getInfo('website'))
    _stardictbuilder.StarDictBuilder_setVersion(db, g.getInfo('version'))
    _stardictbuilder.StarDictBuilder_setcreationTime(db, '')
    _stardictbuilder.StarDictBuilder_setLastUpdate(db, '')
    _stardictbuilder.StarDictBuilder_finish(db)
    if dictZip:
        if filename[-4:]=='.ifo':
            filename = filename[:-4]
            runDictzip(filename)
Esempio n. 2
0
def write_ext(glos, filename, sort=True, dictZip=True):
    if sort:
        g = glos.copy()
        g.data.sort()
    else:
        g = glos
    try:
        import _stardictbuilder
    except ImportError:
        log.error('Binary module "_stardictbuilder" can not be imported! '+\
            'Using internal StarDict builder')
        return g.writeStardict(filename, sort=False)
    db = _stardictbuilder.new_StarDictBuilder(filename)
    _stardictbuilder.StarDictBuilder_swigregister(db)
    for item in g.data:
        _stardictbuilder.StarDictBuilder_addHeadword(db, item[0], item[1], '')
    _stardictbuilder.StarDictBuilder_setTitle(db, g.getInfo('name'))
    _stardictbuilder.StarDictBuilder_setAuthor(db, g.getInfo('author'))
    _stardictbuilder.StarDictBuilder_setLicense(db, g.getInfo('license'))
    _stardictbuilder.StarDictBuilder_setOrigLang(db, g.getInfo('origLang'))
    _stardictbuilder.StarDictBuilder_setDestLang(db, g.getInfo('destLang'))
    _stardictbuilder.StarDictBuilder_setDescription(db,
                                                    g.getInfo('description'))
    _stardictbuilder.StarDictBuilder_setComments(db, g.getInfo('comments'))
    _stardictbuilder.StarDictBuilder_setEmail(db, g.getInfo('email'))
    _stardictbuilder.StarDictBuilder_setWebsite(db, g.getInfo('website'))
    _stardictbuilder.StarDictBuilder_setVersion(db, g.getInfo('version'))
    _stardictbuilder.StarDictBuilder_setcreationTime(db, '')
    _stardictbuilder.StarDictBuilder_setLastUpdate(db, '')
    _stardictbuilder.StarDictBuilder_finish(db)
    if dictZip:
        if filename[-4:] == '.ifo':
            filename = filename[:-4]
            runDictzip(filename)
Esempio n. 3
0
def write(glos, filename, dictzip=True, install=True):  ## FIXME
    from pyglossary.text_utils import runDictzip
    (filename_nox, ext) = splitext(filename)
    if ext.lower() == '.index':
        filename = filename_nox
    indexFd = open(filename + '.index', 'wb')
    dictFd = open(filename + '.dict', 'wb')
    dictMark = 0
    for entry in glos:
        word = toBytes(entry.getWord())
        defi = toBytes(entry.getDefi())
        lm = len(defi)
        indexFd.write(word + b'\t' + intToIndexStr(dictMark) + b'\t' +
                      intToIndexStr(lm) + b'\n')  ## FIXME
        dictFd.write(toBytes(defi))
        dictMark += lm
    indexFd.close()
    dictFd.close()
    #for key, value in glos.iterInfo():
    #    if not value:
    #        continue
    #    pass ## FIXME
    if dictzip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Esempio n. 4
0
def write(glos, filename, sort=True, dictZip=True, install=True):  ## FIXME
    if sort:
        glos = glos.copy()
        glos.sortWords()
    (filename_nox, ext) = splitext(filename)
    if ext.lower() == '.index':
        filename = filename_nox
    indexFd = open(filename + '.index', 'wb')
    dictFd = open(filename + '.dict', 'wb')
    dictMark = 0
    for entry in glos:
        word = entry.getWord()
        defi = entry.getDefi()
        lm = len(defi)
        indexFd.write(word + '\t' + intToIndexStr(dictMark) + '\t' +
                      intToIndexStr(lm) + '\n')  ## FIXME
        dictFd.write(defi)
        dictMark += lm
    indexFd.close()
    dictFd.close()
    #for key in glos.infoKeys():
    #    value = glos.getInfo(key)
    #    if value!='':
    #        pass ## FIXME
    if dictZip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Esempio n. 5
0
 def write(
     self,
     filename: str,
     dictzip: bool = False,
     install: bool = True,
 ) -> Generator[None, "BaseEntry", None]:
     from pyglossary.text_utils import runDictzip
     glos = self._glos
     filename_nox, ext = splitext(filename)
     if ext.lower() == ".index":
         filename = filename_nox
     dictdb = DictDB(filename, "write", 1)
     while True:
         entry = yield
         if entry is None:
             break
         if entry.isData():
             # does dictd support resources? and how? FIXME
             continue
         dictdb.addentry(entry.b_defi, entry.l_word)
     dictdb.finish(dosort=1)
     if dictzip:
         runDictzip(filename)
     if install:
         installToDictd(filename, dictzip,
                        glos.getInfo("name").replace(" ", "_"))
Esempio n. 6
0
def write(glos, filename, dictzip=True, install=True):  # FIXME
	from pyglossary.text_utils import runDictzip
	(filename_nox, ext) = splitext(filename)
	if ext.lower() == ".index":
		filename = filename_nox
	indexFd = open(filename+".index", "wb")
	dictFd = open(filename+".dict", "wb")
	dictMark = 0
	for entry in glos:
		if entry.isData():
			# does dictd support resources? and how? FIXME
			continue
		word = toBytes(entry.getWord())
		defi = toBytes(entry.getDefi())
		lm = len(defi)
		indexFd.write(
			word + b"\t" +
			intToIndexStr(dictMark) + b"\t" +
			intToIndexStr(lm) + b"\n"
		)  # FIXME
		dictFd.write(toBytes(defi))
		dictMark += lm
	indexFd.close()
	dictFd.close()
	# for key, value in glos.iterInfo():
	#	if not value:
	#		continue
	#	pass  # FIXME
	if dictzip:
		runDictzip(filename)
	if install:
		installToDictd(filename, glos.getInfo("name").replace(" ", "_"))
Esempio n. 7
0
	def write(
		self,
		filename,
		dictzip=True,
	):
		fileBasePath = ""
		##
		if splitext(filename)[1].lower() == ".ifo":
			fileBasePath = splitext(filename)[0]
		elif filename.endswith(os.sep):
			if not isdir(filename):
				os.makedirs(filename)
			fileBasePath = join(filename, split(filename[:-1])[-1])
		elif isdir(filename):
			fileBasePath = join(filename, split(filename)[-1])
		##
		if fileBasePath:
			fileBasePath = realpath(fileBasePath)
		self._filename = fileBasePath
		self._resDir = join(dirname(self._filename), "res")

		self.writeGeneral()
#		if self.glossaryHasAdditionalDefinitions():
#			self.writeGeneral()
#		else:
#			defiFormat = self.detectMainDefinitionFormat()
#			if defiFormat == None:
#				self.writeGeneral()
#			else:
#				self.writeCompact(defiFormat)

		if dictzip:
			runDictzip(self._filename)
Esempio n. 8
0
    def run(self, dictZip, resOverwrite):
        ## no more direct access to glos.data, must use glos.sortWords for sorting
        ## no support for cmp argument because it's not supported in Python 3
        ## using my key function `stardictStrKey` might not be safe
        #self.glos.sortWords(key=stardictStrKey)
        ## the safest way in Python 3 is using functools.cmp_to_key
        self.glos.sortWords(key=cmp_to_key(stardict_strcmp))

        self.writeGeneral()
        #if self.glossaryHasAdditionalDefinitions():
        #    self.writeGeneral()
        #else:
        #    defiFormat = self.detectMainDefinitionFormat()
        #    if defiFormat == None:
        #        self.writeGeneral()
        #    else:
        #        self.writeCompact(defiFormat)

        if dictZip:
            runDictzip(self.fileBasePath)
        self.copyResources(
            self.glos.resPath,
            join(os.path.dirname(self.fileBasePath), 'res'),
            resOverwrite
        )
Esempio n. 9
0
def write(glos, filename, dictzip=True, install=True):  # FIXME
    from pyglossary.text_utils import runDictzip
    (filename_nox, ext) = splitext(filename)
    if ext.lower() == ".index":
        filename = filename_nox
    indexFd = open(filename + ".index", "wb")
    dictFd = open(filename + ".dict", "wb")
    dictMark = 0
    for entry in glos:
        if entry.isData():
            # does dictd support resources? and how? FIXME
            continue
        word = toBytes(entry.getWord())
        defi = toBytes(entry.getDefi())
        lm = len(defi)
        indexFd.write(word + b"\t" + intToIndexStr(dictMark) + b"\t" +
                      intToIndexStr(lm) + b"\n")  # FIXME
        dictFd.write(toBytes(defi))
        dictMark += lm
    indexFd.close()
    dictFd.close()
    # for key, value in glos.iterInfo():
    #	if not value:
    #		continue
    #	pass  # FIXME
    if dictzip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo("name").replace(" ", "_"))
Esempio n. 10
0
    def write(
        self,
        filename,
        dictzip=True,
    ):
        fileBasePath = ""
        ##
        if splitext(filename)[1].lower() == ".ifo":
            fileBasePath = splitext(filename)[0]
        elif filename.endswith(os.sep):
            if not isdir(filename):
                os.makedirs(filename)
            fileBasePath = join(filename, split(filename[:-1])[-1])
        elif isdir(filename):
            fileBasePath = join(filename, split(filename)[-1])
        ##
        if fileBasePath:
            fileBasePath = realpath(fileBasePath)
        self._filename = fileBasePath
        self._resDir = join(dirname(self._filename), "res")

        self.writeGeneral()
        #		if self.glossaryHasAdditionalDefinitions():
        #			self.writeGeneral()
        #		else:
        #			defiFormat = self.detectMainDefinitionFormat()
        #			if defiFormat == None:
        #				self.writeGeneral()
        #			else:
        #				self.writeCompact(defiFormat)

        if dictzip:
            runDictzip(self._filename)
Esempio n. 11
0
    def run(self, dictzip, resOverwrite):
        ## no more direct access to glos.data, must use glos.sortWords for sorting
        ## no support for cmp argument because it's not supported in Python 3
        ## key function's argument is a str (word)
        self.glos.sortWords(
            key = sortKey,
        )

        self.writeGeneral()
        #if self.glossaryHasAdditionalDefinitions():
        #    self.writeGeneral()
        #else:
        #    defiFormat = self.detectMainDefinitionFormat()
        #    if defiFormat == None:
        #        self.writeGeneral()
        #    else:
        #        self.writeCompact(defiFormat)

        if dictzip:
            runDictzip(self.fileBasePath)
        self.copyResources(
            self.glos.resPath,
            join(os.path.dirname(self.fileBasePath), 'res'),
            resOverwrite
        )
Esempio n. 12
0
    def write(
            self,
            filename: str,
            dictzip: bool = True,
            sametypesequence: str = "",  # type: Literal["", "h", "m"]
    ) -> None:
        fileBasePath = filename
        ##
        if splitext(filename)[1].lower() == ".ifo":
            fileBasePath = splitext(filename)[0]
        elif filename.endswith(os.sep):
            if not isdir(filename):
                os.makedirs(filename)
            fileBasePath = join(filename, split(filename[:-1])[-1])
        elif isdir(filename):
            fileBasePath = join(filename, split(filename)[-1])
        ##
        if fileBasePath:
            fileBasePath = realpath(fileBasePath)
        self._filename = fileBasePath
        self._resDir = join(dirname(self._filename), "res")

        if sametypesequence:
            log.debug(
                f"Using write option sametypesequence={sametypesequence}")
            self.writeCompact(sametypesequence)
        else:
            self.writeGeneral()

        if dictzip:
            runDictzip(self._filename)
Esempio n. 13
0
def write(glos, filename, dictzip=True, install=True):## FIXME
    from pyglossary.text_utils import runDictzip
    (filename_nox, ext) = splitext(filename)
    if ext.lower()=='.index':
        filename = filename_nox
    indexFd = open(filename+'.index', 'wb')
    dictFd = open(filename+'.dict', 'wb')
    dictMark = 0
    for entry in glos:
        word = toBytes(entry.getWord())
        defi = toBytes(entry.getDefi())
        lm = len(defi)
        indexFd.write(word + b'\t' + intToIndexStr(dictMark) + b'\t' + intToIndexStr(lm) + b'\n')## FIXME
        dictFd.write(toBytes(defi))
        dictMark += lm
    indexFd.close()
    dictFd.close()
    #for key, value in glos.iterInfo():
    #    if not value:
    #        continue
    #    pass ## FIXME
    if dictzip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Esempio n. 14
0
 def write(self) -> Generator[None, "BaseEntry", None]:
     if self._sametypesequence:
         yield from self.writeCompact(self._sametypesequence)
     else:
         yield from self.writeGeneral()
     if self._dictzip:
         runDictzip(self._filename)
Esempio n. 15
0
def write(glos, filename, sort=True, dictZip=True, install=True):## FIXME
    if sort:
        glos = glos.copy()
        glos.sortWords()
    (filename_nox, ext) = splitext(filename)
    if ext.lower()=='.index':
        filename = filename_nox
    indexFd = open(filename+'.index', 'wb')
    dictFd = open(filename+'.dict', 'wb')
    dictMark = 0
    for entry in glos:
        word = entry.getWord()
        defi = entry.getDefi()
        lm = len(defi)
        indexFd.write(word + '\t' + intToIndexStr(dictMark) + '\t' + intToIndexStr(lm) + '\n')## FIXME
        dictFd.write(defi)
        dictMark += lm
    indexFd.close()
    dictFd.close()
    #for key in glos.infoKeys():
    #    value = glos.getInfo(key)
    #    if value!='':
    #        pass ## FIXME
    if dictZip:
        runDictzip(filename)
    if install:
        installToDictd(filename, glos.getInfo('name').replace(' ', '_'))
Esempio n. 16
0
 def finish(self):
     from pyglossary.text_utils import runDictzip
     self._dictdb.finish(dosort=1)
     if self._dictzip:
         runDictzip(self._filename)
     if self._install:
         installToDictd(
             self._filename,
             self._dictzip,
             self._glos.getInfo("name").replace(" ", "_"),
         )
     self._filename = None
Esempio n. 17
0
	def write(self) -> "Generator[None, BaseEntry, None]":
		if self._sametypesequence:
			if self._merge_syns:
				yield from self.writeCompactMergeSyns(self._sametypesequence)
			else:
				yield from self.writeCompact(self._sametypesequence)
		else:
			if self._merge_syns:
				yield from self.writeGeneralMergeSyns()
			else:
				yield from self.writeGeneral()
		if self._dictzip:
			runDictzip(self._filename)
Esempio n. 18
0
	def write(
		self,
		filename: str,
		dictzip: bool = True,
		sametypesequence: str = "", # type: Literal["", "h", "m"]
		stardict_client: bool = False,
	) -> None:
		self._stardict_client = stardict_client
		fileBasePath = filename
		##
		if splitext(filename)[1].lower() == ".ifo":
			fileBasePath = splitext(filename)[0]
		elif filename.endswith(os.sep):
			if not isdir(filename):
				os.makedirs(filename)
			fileBasePath = join(filename, split(filename[:-1])[-1])
		elif isdir(filename):
			fileBasePath = join(filename, split(filename)[-1])

		parentDir = split(fileBasePath)[0]
		if not isdir(parentDir):
			log.info(f"Creating directory {parentDir}")
			os.mkdir(parentDir)
		##
		if fileBasePath:
			fileBasePath = realpath(fileBasePath)
		self._filename = fileBasePath
		self._resDir = join(dirname(self._filename), "res")

		self._sourceLang = self._glos.sourceLang
		self._targetLang = self._glos.targetLang

		if sametypesequence:
			log.debug(f"Using write option sametypesequence={sametypesequence}")
		else:
			stat = self._glos.collectDefiFormat(100)
			log.info(f"defiFormat stat: {stat}")
			if stat["m"] > 0.97:
				log.info(f"Auto-selecting sametypesequence=m")
				sametypesequence = "m"
			elif stat["h"] > 0.5:
				log.info(f"Auto-selecting sametypesequence=h")
				sametypesequence = "h"

		if sametypesequence:
			yield from self.writeCompact(sametypesequence)
		else:
			yield from self.writeGeneral()

		if dictzip:
			runDictzip(self._filename)
Esempio n. 19
0
    def run(self, dictZip, resOverwrite):
        self.glos.data.sort(stardict_strcmp, lambda x: x[0])

        if self.GlossaryHasAdditionalDefinitions():
            self.writeGeneral()
        else:
            articleFormat = self.DetectMainDefinitionFormat()
            if articleFormat == None:
                self.writeGeneral()
            else:
                self.writeCompact(articleFormat)

        if dictZip:
            runDictzip(self.fileBasePath)
        self.copyResources(self.glos.resPath, join(os.path.dirname(self.fileBasePath), "res"), resOverwrite)
Esempio n. 20
0
    def run(self, dictzip, resOverwrite):
        self.writeGeneral()
        #if self.glossaryHasAdditionalDefinitions():
        #    self.writeGeneral()
        #else:
        #    defiFormat = self.detectMainDefinitionFormat()
        #    if defiFormat == None:
        #        self.writeGeneral()
        #    else:
        #        self.writeCompact(defiFormat)

        if dictzip:
            runDictzip(self.fileBasePath)
        self.copyResources(self.glos.resPath,
                           join(os.path.dirname(self.fileBasePath), 'res'),
                           resOverwrite)
Esempio n. 21
0
    def run(self, dictZip, resOverwrite):
        self.glos.data.sort(stardict_strcmp, lambda x: x[0])

        if self.GlossaryHasAdditionalDefinitions():
            self.writeGeneral()
        else:
            articleFormat = self.DetectMainDefinitionFormat()
            if articleFormat == None:
                self.writeGeneral()
            else:
                self.writeCompact(articleFormat)

        if dictZip:
            runDictzip(self.fileBasePath)
        self.copyResources(self.glos.resPath,
                           join(os.path.dirname(self.fileBasePath), 'res'),
                           resOverwrite)
Esempio n. 22
0
    def run(self, dictzip, resOverwrite):
        self.writeGeneral()
        #if self.glossaryHasAdditionalDefinitions():
        #    self.writeGeneral()
        #else:
        #    defiFormat = self.detectMainDefinitionFormat()
        #    if defiFormat == None:
        #        self.writeGeneral()
        #    else:
        #        self.writeCompact(defiFormat)

        if dictzip:
            runDictzip(self.fileBasePath)
        self.copyResources(
            self.glos.resPath,
            join(os.path.dirname(self.fileBasePath), 'res'),
            resOverwrite
        )
Esempio n. 23
0
    def run(self, dictZip, resOverwrite):
        ## no more direct access to glos.data, must use glos.sortWords for sorting
        ## no support for cmp argument because it's not supported in Python 3
        ## key function's argument is a str (word)
        self.glos.sortWords(key=stardictStrKey, )

        self.writeGeneral()
        #if self.glossaryHasAdditionalDefinitions():
        #    self.writeGeneral()
        #else:
        #    defiFormat = self.detectMainDefinitionFormat()
        #    if defiFormat == None:
        #        self.writeGeneral()
        #    else:
        #        self.writeCompact(defiFormat)

        if dictZip:
            runDictzip(self.fileBasePath)
        self.copyResources(self.glos.resPath,
                           join(os.path.dirname(self.fileBasePath), 'res'),
                           resOverwrite)
Esempio n. 24
0
def write(
    glos: GlossaryType,
    filename: str,
    dictzip: bool = False,
    install: bool = True,
) -> None:
    from pyglossary.text_utils import runDictzip
    (filename_nox, ext) = splitext(filename)
    if ext.lower() == ".index":
        filename = filename_nox
    dictdb = DictDB(filename, "write", 1)
    for entry in glos:
        if entry.isData():
            # does dictd support resources? and how? FIXME
            continue
        dictdb.addentry(entry.b_defi, entry.l_word)
    dictdb.finish(dosort=1)
    if dictzip:
        runDictzip(filename)
    if install:
        installToDictd(filename, dictzip,
                       glos.getInfo("name").replace(" ", "_"))
Esempio n. 25
0
    def write(
            self,
            filename: str,
            dictzip: bool = True,
            sametypesequence: str = "",  # type: Literal["", "h", "m"]
    ) -> None:
        fileBasePath = filename
        ##
        if splitext(filename)[1].lower() == ".ifo":
            fileBasePath = splitext(filename)[0]
        elif filename.endswith(os.sep):
            if not isdir(filename):
                os.makedirs(filename)
            fileBasePath = join(filename, split(filename[:-1])[-1])
        elif isdir(filename):
            fileBasePath = join(filename, split(filename)[-1])
        ##
        if fileBasePath:
            fileBasePath = realpath(fileBasePath)
        self._filename = fileBasePath
        self._resDir = join(dirname(self._filename), "res")

        if sametypesequence:
            log.debug("Using write option sametypesequence=%s" %
                      sametypesequence)
            self.writeCompact(sametypesequence)
#		elif self.glossaryHasAdditionalDefinitions():
#			self.writeGeneral()
        else:
            #			defiFormat = self.detectMainDefinitionFormat()
            #			if defiFormat == None:
            self.writeGeneral()
#			else:
#				self.writeCompact(defiFormat)

        if dictzip:
            runDictzip(self._filename)