Exemple #1
0
 def open(self, filename: str):
     try:
         import icu
     except ModuleNotFoundError as e:
         e.msg += f", run `{pip} install PyICU` to install"
         raise e
     from pyglossary.plugin_lib import slob
     if isfile(filename):
         raise IOError(f"File '{filename}' already exists")
     self._filename = filename
     kwargs = {}
     kwargs["compression"] = self._compression
     self._slobWriter = slobWriter = slob.create(
         filename, observer=self._slobObserver, **kwargs)
     slobWriter.tag("label", self._glos.getInfo("name"))
Exemple #2
0
 def write(
     self,
     filename: str,
     compression: str = "",
     content_type: str = "",
 ) -> Generator[None, "BaseEntry", None]:
     try:
         import icu
     except ModuleNotFoundError as e:
         e.msg += f", run `{pip} install PyICU` to install"
         raise e
     from pyglossary.plugin_lib import slob
     glos = self._glos
     kwargs = {}
     if compression:
         kwargs["compression"] = compression
     # must not pass compression=None to slob.create()
     with slob.create(filename, **kwargs) as slobWriter:
         name = glos.getInfo("name")
         slobWriter.tag("label", toStr(name))
         while True:
             entry = yield
             if entry is None:
                 break
             words = entry.l_word
             b_defi = entry.defi.encode("utf-8")
             _ctype = content_type
             if not _ctype:
                 entry.detectDefiFormat()
                 defiFormat = entry.defiFormat
                 if defiFormat == "h":
                     _ctype = "text/html; charset=utf-8"
                     b_defi = b_defi.replace(b'"bword://', b'"')
                     b_defi = b_defi.replace(b"'bword://", b"'")
                 elif defiFormat == "m":
                     _ctype = "text/plain; charset=utf-8"
                 else:
                     _ctype = "text/plain; charset=utf-8"
             slobWriter.add(
                 b_defi,
                 *tuple(words),
                 content_type=_ctype,
             )
Exemple #3
0
def write(
    glos: GlossaryType,
    filename: str,
    compression: str = "",
    content_type: str = "",
):
    from pyglossary.plugin_lib import slob
    kwargs = {}
    if compression:
        kwargs["compression"] = compression
    # must not pass compression=None to slob.create()
    with slob.create(filename, **kwargs) as slobWriter:
        for entry in glos:
            words = entry.getWords()
            b_defi = entry.getDefi().encode("utf-8")
            slobWriter.add(
                b_defi,
                *tuple(words),
                content_type=content_type,
            )
Exemple #4
0
def write(
    glos: GlossaryType,
    filename: str,
    compression: str = "",
    content_type: str = "",
):
    from pyglossary.plugin_lib import slob
    kwargs = {}
    if compression:
        kwargs["compression"] = compression
    # must not pass compression=None to slob.create()
    with slob.create(filename, **kwargs) as slobWriter:
        name = glos.getInfo("name")
        slobWriter.tag("label", toStr(name))
        for entry in glos:
            words = entry.l_word
            b_defi = entry.defi.encode("utf-8")
            slobWriter.add(
                b_defi,
                *tuple(words),
                content_type=content_type,
            )