Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ Each record is a `GedcomStructure` with a `tag`, an optional `xref` and `pointer

`loads` and `dumps` are the string equivalents. Non-conforming input raises `GedcomParseError`, a `ValueError` carrying `line_number`. A pointer to a record that does not exist is kept rather than rejected; `gedcom7.validate(records)` lists each one as a `dangling-pointer` error.

### GEDZIP

```python
with gedcom7.GedzipReader("family.gdz") as archive:
records = archive.records
with archive.open("media/photo.jpg") as f: # a FILE payload
data = f.read()

gedcom7.dump_gedzip(records, "out.gdz", {"media/photo.jpg": data})
```

File content can be given as bytes, a path, or a function returning an open binary file. Where the files are stored is left to the caller.

## Development

```
Expand Down
3 changes: 3 additions & 0 deletions gedcom7/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
GedcomValidationError,
)
from .formatter import format_value, set_value
from .gedzip import GedzipReader, dump_gedzip
from .parser import load, loads
from .serializer import dump, dumps, generate_schema
from .validator import Error, validate
Expand All @@ -19,7 +20,9 @@
"GedcomSerializeError",
"GedcomValidationError",
"Error",
"GedzipReader",
"dump",
"dump_gedzip",
"dumps",
"format_value",
"generate_schema",
Expand Down
228 changes: 228 additions & 0 deletions gedcom7/gedzip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
"""Read and write GEDZIP archives: a dataset and the local files it names."""

from __future__ import annotations

import os
import posixpath
import re
import shutil
import zipfile
from typing import IO, TYPE_CHECKING, BinaryIO, Union, cast
from urllib.parse import quote, unquote

from . import const, grammar
from .exceptions import GedcomParseError, GedcomSerializeError
from .parser import load
from .serializer import dumps

if TYPE_CHECKING:
from collections.abc import Callable, Iterable, Iterator, Mapping
from types import TracebackType

from .types import GedcomStructure

DATASET = "gedcom.ged"

FILE_PATH = "https://gedcom.io/terms/v7/type-FilePath"

# What a local file's content can be given as: its bytes, a path to read it
# from, or a function opening it.
FileSource = Union[bytes, str, "os.PathLike[str]", "Callable[[], IO[bytes]]"]

_SCHEME = re.compile(r"[A-Za-z][A-Za-z0-9+.-]*:")
_BANNED = re.compile(grammar.banned)
# Characters a file path payload carries literally; anything else is escaped.
_SAFE = "/!$&'()*+,;=@-._~"
# A valid URL string: URL code points and percent escapes, no query or fragment.
_URL_PATH = re.compile(
r"(?:[A-Za-z0-9!$&'()*+,\-./:;=@_~]|%[0-9A-Fa-f]{2}|[^\x00-\x7f])*"
)


def is_local_file(path: str) -> bool:
"""Say whether a file path payload names a local file, not a URL."""
if not path or _SCHEME.match(path) or "?" in path or "#" in path:
return False
try:
name = unquote(path, errors="strict")
Comment thread
Copilot marked this conversation as resolved.
Resolved
except UnicodeDecodeError:
return False
if name.startswith("/") or "\\" in name or _BANNED.search(name):
return False
return ".." not in name.split("/")


def entry_name(path: str) -> str:
"""Get the archive entry a local file path payload names."""
if not is_local_file(path):
raise ValueError(f"{path!r} is not a local file path")
return unquote(path, errors="strict")


def file_path(name: str) -> str:
"""Get the file path payload naming an archive entry."""
path = quote(name, safe=_SAFE)
if not is_local_file(path):
raise ValueError(f"{name!r} cannot be a local file in a GEDZIP")
return path


def file_structures(
records: Iterable[GedcomStructure],
) -> Iterator[GedcomStructure]:
"""Yield every structure whose payload is a file path."""
stack = list(reversed(list(records)))
while stack:
structure = stack.pop()
type_id = structure.type_id
if type_id is not None and const.payloads.get(type_id) == FILE_PATH:
yield structure
stack.extend(reversed(structure.children))


def local_files(records: Iterable[GedcomStructure]) -> list[str]:
"""List the local file paths a dataset names, each once, in order."""
paths = (s.text for s in file_structures(records))
return list(dict.fromkeys(p for p in paths if is_local_file(p)))


class GedzipReader:
"""A GEDZIP archive opened for reading.

::

with gedcom7.GedzipReader("family.gdz") as archive:
records = archive.records
with archive.open("media/photo.jpg") as f:
data = f.read()
"""

def __init__(self, file: str | os.PathLike[str] | IO[bytes]) -> None:
"""Open the archive and load its dataset."""
try:
self._zip = zipfile.ZipFile(file)
except zipfile.BadZipFile as exc:
raise GedcomParseError(f"not a GEDZIP archive: {exc}") from exc
try:
with self._zip.open(DATASET) as fp:
self.records: list[GedcomStructure] = load(cast(BinaryIO, fp))
except KeyError:
self._zip.close()
raise GedcomParseError(f"GEDZIP archive has no {DATASET}") from None
except BaseException:
self._zip.close()
raise

def has(self, path: str) -> bool:
"""Say whether the archive holds the local file a payload names."""
try:
self._zip.getinfo(entry_name(path))
except (KeyError, ValueError):
return False
return True

def open(self, path: str) -> IO[bytes]:
"""Open the local file a payload names.

Raises :class:`KeyError` if the archive does not hold it.
"""
return self._zip.open(entry_name(path))

def missing(self) -> list[str]:
"""List the local file paths the dataset names but the archive lacks."""
return [p for p in local_files(self.records) if not self.has(p)]

def close(self) -> None:
"""Close the archive."""
self._zip.close()

def __enter__(self) -> GedzipReader:
"""Enter a ``with`` block."""
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc: BaseException | None,
tb: TracebackType | None,
) -> None:
"""Close the archive on leaving a ``with`` block."""
self.close()


def _check_paths(
records: list[GedcomStructure], files: Mapping[str, FileSource | None]
) -> None:
for structure in file_structures(records):
path = structure.text
if is_local_file(path):
if path not in files:
raise GedcomSerializeError(f"no content given for local file {path}")
elif path.lower().startswith("file:"):
raise GedcomSerializeError(f"a GEDZIP cannot point at file URL {path}")
names: dict[str, str] = {}
for path, source in files.items():
if not is_local_file(path) or not _URL_PATH.fullmatch(path):
raise GedcomSerializeError(f"{path!r} is not a valid local file path")
# Unzipping drops "." and empty segments, so names differing only in
# those land on the same file.
name = posixpath.normpath(entry_name(path))
if name == DATASET:
raise GedcomSerializeError(f"a local file cannot be named {DATASET}")
if source is not None:
if name in names:
raise GedcomSerializeError(
f"{names[name]} and {path} name the same file {name}"
)
names[name] = path


def _write_file(archive: zipfile.ZipFile, name: str, source: FileSource) -> None:
if isinstance(source, bytes):
archive.writestr(name, source)
return
# Not ZipFile.write, which would normalize the name.
with (
(
open(source, "rb") if isinstance(source, (str, os.PathLike)) else source()
) as src,
archive.open(name, "w", force_zip64=True) as dst,
):
shutil.copyfileobj(src, dst)


def dump_gedzip(
records: Iterable[GedcomStructure],
file: str | os.PathLike[str] | BinaryIO,
files: Mapping[str, FileSource | None],
*,
compress_files: bool = False,
line_terminator: str = "\n",
byte_order_mark: bool = True,
validate: bool = False,
) -> None:
"""Write a dataset and its local files as a GEDZIP archive.

``files`` maps each local file path payload in the dataset to its content,
or to None to leave a file that is unavailable out.
The dataset is compressed; the files only with ``compress_files=True``, as
most media is compressed already.

Raises :class:`~gedcom7.exceptions.GedcomSerializeError` if a local file has
no content, is named ``gedcom.ged``, is not a valid URL string, or shares its
name with another, or if a payload is a ``file:`` URL.
"""
records = list(records)
_check_paths(records, files)
Comment thread
Copilot marked this conversation as resolved.
Resolved
data = dumps(
records,
line_terminator=line_terminator,
byte_order_mark=byte_order_mark,
validate=validate,
).encode("utf-8")
method = zipfile.ZIP_DEFLATED if compress_files else zipfile.ZIP_STORED
with zipfile.ZipFile(file, "w", compression=method) as archive:
archive.writestr(DATASET, data, compress_type=zipfile.ZIP_DEFLATED)
for path, source in files.items():
if source is not None:
_write_file(archive, entry_name(path), source)
Loading
Loading