Skip to content

Commit

Permalink
move "Using PyGlossary as a library" README section to doc/lib-usage.md
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 13, 2024
1 parent 4be9225 commit 2104851
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 86 deletions.
88 changes: 2 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,93 +272,9 @@ you can place it in this directory:

[![Packaging status](https://repology.org/badge/vertical-allrepos/pyglossary.svg?columns=3&header=PyGlossary)](https://repology.org/project/pyglossary/versions)

## Using PyGlossary as a Python library
## Using PyGlossary as a library

There are a few examples in [doc/lib-examples](./doc/lib-examples) directory.

Here is a basic script that converts any supported glossary format to [Tabfile](./doc/p/tabfile.md):

```python
import sys
from pyglossary import Glossary

# Glossary.init() should be called only once, so make sure you put it
# in the right place
Glossary.init()

glos = Glossary()
glos.convert(
inputFilename=sys.argv[1],
outputFilename=f"{sys.argv[1]}.txt",
# although it can detect format for *.txt, you can still pass outputFormat
outputFormat="Tabfile",
# you can pass readOptions or writeOptions as a dict
# writeOptions={"encoding": "utf-8"},
)
```

And if you choose to use `glossary_v2`:

```python
import sys
from pyglossary.glossary_v2 import ConvertArgs, Glossary

# Glossary.init() should be called only once, so make sure you put it
# in the right place
Glossary.init()

glos = Glossary()
glos.convert(ConvertArgs(
inputFilename=sys.argv[1],
outputFilename=f"{sys.argv[1]}.txt",
# although it can detect format for *.txt, you can still pass outputFormat
outputFormat="Tabfile",
# you can pass readOptions or writeOptions as a dict
# writeOptions={"encoding": "utf-8"},
))
```

You may look at docstring of `Glossary.convert` for full list of keyword arguments.

If you need to add entries inside your Python program (rather than converting one glossary into another), then you use `write` instead of `convert`, here is an example:

```python
from pyglossary import Glossary

Glossary.init()

glos = Glossary()
mydict = {
"a": "test1",
"b": "test2",
"c": "test3",
}
for word, defi in mydict.items():
glos.addEntryObj(glos.newEntry(
word,
defi,
defiFormat="m", # "m" for plain text, "h" for HTML
))

glos.setInfo("title", "My Test StarDict")
glos.setInfo("author", "John Doe")
glos.write("test.ifo", format="Stardict")
```

**Note:** `addEntryObj` is renamed to `addEntry` in `pyglossary.glossary_v2`.

**Note:** Switching to `glossary_v2` is optional and recommended.

And if you need to read a glossary from file into a `Glossary` object in RAM (without immediately converting it), you can use `glos.read(filename, format=inputFormat)`. Be wary of RAM usage in this case.

If you want to include images, css, js or other files in a glossary that you are creating, you need to add them as **Data Entries**, for example:

```python
with open(os.path.join(imageDir, "a.jpeg")) as fp:
glos.addEntry(glos.newDataEntry("img/a.jpeg", fp.read()))
```

The first argument to `newDataEntry` must be the relative path (that generally html codes of your definitions points to).
See [doc/lib-usage.md](./doc/lib-usage.md) for how to use PyGlossary as a Python library.

## Internal glossary structure

Expand Down
87 changes: 87 additions & 0 deletions doc/lib-usage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Using PyGlossary as a Python library

There are a few examples in [doc/lib-examples](./doc/lib-examples) directory.

Here is a basic script that converts any supported glossary format to [Tabfile](./doc/p/tabfile.md):

```python
import sys
from pyglossary import Glossary

# Glossary.init() should be called only once, so make sure you put it
# in the right place
Glossary.init()

glos = Glossary()
glos.convert(
inputFilename=sys.argv[1],
outputFilename=f"{sys.argv[1]}.txt",
# although it can detect format for *.txt, you can still pass outputFormat
outputFormat="Tabfile",
# you can pass readOptions or writeOptions as a dict
# writeOptions={"encoding": "utf-8"},
)
```

And if you choose to use `glossary_v2`:

```python
import sys
from pyglossary.glossary_v2 import ConvertArgs, Glossary

# Glossary.init() should be called only once, so make sure you put it
# in the right place
Glossary.init()

glos = Glossary()
glos.convert(ConvertArgs(
inputFilename=sys.argv[1],
outputFilename=f"{sys.argv[1]}.txt",
# although it can detect format for *.txt, you can still pass outputFormat
outputFormat="Tabfile",
# you can pass readOptions or writeOptions as a dict
# writeOptions={"encoding": "utf-8"},
))
```

You may look at docstring of `Glossary.convert` for full list of keyword arguments.

If you need to add entries inside your Python program (rather than converting one glossary into another), then you use `write` instead of `convert`, here is an example:

```python
from pyglossary import Glossary

Glossary.init()

glos = Glossary()
mydict = {
"a": "test1",
"b": "test2",
"c": "test3",
}
for word, defi in mydict.items():
glos.addEntryObj(glos.newEntry(
word,
defi,
defiFormat="m", # "m" for plain text, "h" for HTML
))

glos.setInfo("title", "My Test StarDict")
glos.setInfo("author", "John Doe")
glos.write("test.ifo", format="Stardict")
```

**Note:** `addEntryObj` is renamed to `addEntry` in `pyglossary.glossary_v2`.

**Note:** Switching to `glossary_v2` is optional and recommended.

And if you need to read a glossary from file into a `Glossary` object in RAM (without immediately converting it), you can use `glos.read(filename, format=inputFormat)`. Be wary of RAM usage in this case.

If you want to include images, css, js or other files in a glossary that you are creating, you need to add them as **Data Entries**, for example:

```python
with open(os.path.join(imageDir, "a.jpeg")) as fp:
glos.addEntry(glos.newDataEntry("img/a.jpeg", fp.read()))
```

The first argument to `newDataEntry` must be the relative path (that generally html codes of your definitions points to).

0 comments on commit 2104851

Please sign in to comment.