Skip to content

Commit eedff21

Browse files
committed
chore: Update dependencies and add coding conventions document
1 parent 8e6b107 commit eedff21

File tree

4 files changed

+632
-584
lines changed

4 files changed

+632
-584
lines changed

CONVENTIONS.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Coding Convetions
2+
3+
- Write pragmatic, easily testable, and performant code!
4+
- Prefer short and pure functions where possible!
5+
- Keep the number of function arguments as low as possible!
6+
- Don´t use nested functions!
7+
- Write concise and to-the-point docstrings for all functions!
8+
- Use type comments style (PEP 484) instead of function annotations!
9+
- Always add a correct PEP 484 style type comment as the first line after the function definition!
10+
- Use built-in collection types as generic types for annotations (PEP 585)!
11+
- Use the | (pipe) operator for writing union types (PEP 604)!
12+
13+
Example function with type annotations and docstring:
14+
15+
```python
16+
def tokenize_chunks(chunks, max_len=None):
17+
# type: (list[str], int|None) -> dict
18+
"""
19+
Tokenize text chunks into model-compatible formats.
20+
21+
:param chunks: Text chunks to tokenize.
22+
:param max_len: Truncates chunks above max_len characters
23+
:return: Dictionary of tokenized data including input IDs, attention masks, and type IDs.
24+
"""
25+
```

accfix/ace_fix.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,10 @@ def add_acc_meta_fxl(opf_tree):
6262
# type: (ElementTree) -> Generator[str, None, None]
6363
"""Update or add default Fixed Layout metadata required by ACE"""
6464
root = opf_tree.getroot()
65-
namespaces = {"opf": "http://www.idpf.org/2007/opf"}
65+
namespaces = {
66+
"opf": "http://www.idpf.org/2007/opf",
67+
"tdm": "http://www.w3.org/ns/tdmrep#",
68+
}
6669
metadata_element = root.find(".//opf:metadata", namespaces=namespaces)
6770

6871
meta_elements = [
@@ -76,6 +79,7 @@ def add_acc_meta_fxl(opf_tree):
7679
"schema:accessibilitySummary",
7780
"Fixed Layout with html text placed over background images.",
7881
),
82+
("tdm:reservation", "1"),
7983
]
8084

8185
for property_value, text_value in meta_elements:
@@ -146,6 +150,7 @@ def fix_hotspot_links_kf8(html_tree):
146150

147151
if __name__ == "__main__":
148152
fp = r"../scratch/test1.epub"
149-
epb = Epub(fp, clone=True)
150-
for message in add_acc_meta_fxl(epb.opf_tree()):
153+
cp = "../scratch/test1_clone.epub"
154+
epb = Epub(fp, clone=True, clone_path="../scratch/test1_clone.epub")
155+
for message in ace_fix_mec(epb):
151156
print(message)

accfix/epub.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,26 @@
1212

1313

1414
class Epub:
15-
def __init__(self, path, clone=True):
16-
# type: (str|Path, bool) -> None
15+
def __init__(self, path, clone=True, clone_path=None):
16+
# type: (str|Path, bool, str|Path|None) -> None
1717
"""EPUB file object for reading and writing members.
1818
1919
:param path: Path to the EPUB file.
20-
:param clone: Create a temporary copy of the EPUB file and open that one.
20+
:param clone: Create a (temporary) copy of the EPUB file and open that one.
21+
:param clone_path: Custom file path for the cloned EPUB file.
2122
"""
2223
self._path = Path(path)
2324
self.name = self._path.name
2425
log.debug("Opening EPUB file: {name}".format(name=self._path.name))
2526
self._clone = None
2627
if clone:
27-
temp_dir = tempfile.mkdtemp()
28-
self._clone = Path(temp_dir) / self._path.name
28+
if clone_path:
29+
self._clone = Path(clone_path)
30+
else:
31+
temp_dir = tempfile.mkdtemp()
32+
self._clone = Path(temp_dir) / self._path.name
2933
shutil.copy2(self._path, self._clone)
30-
log.debug(f"Cloning EPUB file to: {self._clone.parent}")
34+
log.debug(f"Cloning EPUB file to: {self._clone}")
3135
self._zf = ZipFileR(self.path, mode="a")
3236

3337
def __del__(self):

0 commit comments

Comments
 (0)