DB backends (db)
The file-DB provider registry and its built-in backends. A third-party
backend subclasses DbProvider with its own NAME and advertises it in the
pkgforge.dbs entry-point group; see Adding a
backend. The checks every backend must
pass are in Contract suite.
pkgforge.exceptions.DbError
Bases: PkgForgeError, ValueError
A file DB's on-disk content could not be used as one.
Raised for a parse error, non-UTF-8 bytes, content that is not in the
chosen format, a JSON Lines line or YAML top-level document that is not a
mapping, or a record that breaks the schema (see :func:_check_record).
Caught by :func:pkgforge.cli.main's error boundary like any
:class:~pkgforge.exceptions.PkgForgeError (one stderr line, exit 1). A
:class:ValueError because it reports malformed input.
pkgforge.db.Db = typing.Dict[str, 'typing.Optional[FileEntry]']
module-attribute
pkgforge.db.DbProvider(path)
Bases: Registered, ABC
Storage backend for a file DB, bound to a filesystem path.
A subclass registers by declaring its own NAME (plus, optionally,
ALIASES); see :class:pkgforge._registry.Registered. SUFFIXES
(file suffixes that infer this format from a --db path -- case-
insensitive; a missing leading dot is added, since :attr:Path.suffix
always includes one and a dotless entry could otherwise never match; a
suffix with more than one dot, e.g. ".tar.gz", raises ValueError
at class-creation time, since :attr:Path.suffix only ever returns the
last dot-segment and such an entry could never match either) are
normalized -- and validated -- from the class's own body before
registration, so a bad suffix leaves the class entirely unregistered
rather than half-registered.
Source code in src/pkgforge/db/__init__.py
75 76 | |
ALIASES = ()
class-attribute
NAME = ''
class-attribute
SUFFIXES = ()
class-attribute
path = path
instance-attribute
__init_subclass__(**kwargs)
Source code in src/pkgforge/db/__init__.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | |
add(path, entry)
abstractmethod
Record entry for path.
Source code in src/pkgforge/db/__init__.py
107 108 109 | |
batch()
Optionally batch a run of add/remove calls for efficiency.
A context manager yielding self. The default implementation
(this one) does nothing extra -- every add/remove inside it
still writes (and, for the append-log backends, still locks)
exactly as it would outside one -- so an existing or third-party
provider that doesn't override this keeps working unchanged.
:class:~pkgforge.db.SQLiteDb overrides it to hold one
connection open across the whole batch and commit periodically
instead of connecting, creating the schema and committing once per
call.
Source code in src/pkgforge/db/__init__.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
compact()
abstractmethod
Collapse redundant history: keep every live entry and drop the
tombstones of removed paths, so load() holds no None after it.
Source code in src/pkgforge/db/__init__.py
115 116 117 118 | |
init()
abstractmethod
Create or reset an empty DB.
Source code in src/pkgforge/db/__init__.py
120 121 122 | |
load()
abstractmethod
Return the full DB as {path: entry-or-None}.
Source code in src/pkgforge/db/__init__.py
103 104 105 | |
remove(path)
abstractmethod
Mark path removed.
Source code in src/pkgforge/db/__init__.py
111 112 113 | |
pkgforge.db.open_db(path, fmt=None)
Resolve and construct the :class:DbProvider for path.
An explicit fmt wins; otherwise the suffix decides, defaulting to JSON
Lines. The file's content is never inspected: a file that is not in the
resolved format fails when it is loaded.
Source code in src/pkgforge/db/__init__.py
161 162 163 164 165 166 167 168 | |
pkgforge.db.format_for_suffix(path)
Infer a format from path's suffix, else :data:DEFAULT_FORMAT.
A suffix no registered class holds loads the advertised pkgforge.dbs
entry points (a plugin that fails to load is logged and skipped) and looks
again.
Source code in src/pkgforge/db/__init__.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
pkgforge.db.jsonl_line(path, entry)
One JSON Lines record for path/entry (None writes a removal
marker), terminated with a single newline: the exact text
:class:JSONLinesDb appends.
Source code in src/pkgforge/db/_jsonl.py
20 21 22 23 24 | |
pkgforge.db.parse_jsonl(text, source='text')
Parse JSON Lines text (what :func:jsonl_line writes) into a
:data:~pkgforge.db.Db; the last record for a path wins.
source names the text in errors, which read {source}:{line}: ....
Raises :class:~pkgforge.exceptions.DbError for a malformed record.
Source code in src/pkgforge/db/_jsonl.py
96 97 98 99 100 101 102 103 | |
Built-in backends
pkgforge.db.JSONLinesDb(path)
Bases: AppendLogDb
Append-only JSON Lines: one JSON object per line, last per path wins.
Source code in src/pkgforge/db/__init__.py
75 76 | |
NAME = 'jsonl'
class-attribute
instance-attribute
SUFFIXES = ('.jsonl', '.ndjson')
class-attribute
instance-attribute
load()
Source code in src/pkgforge/db/_jsonl.py
112 113 114 115 116 | |
pkgforge.db.YAMLDb(path)
Bases: AppendLogDb
Append-only YAML: a single mapping, appended key by key -- the last duplicate key wins.
Reading relies on the YAML loader letting a later duplicate mapping key
win -- a property of this backend's format, not a general guarantee. A
null value is a removal.
Source code in src/pkgforge/db/__init__.py
75 76 | |
NAME = 'yaml'
class-attribute
instance-attribute
SUFFIXES = ('.yaml', '.yml')
class-attribute
instance-attribute
init()
Source code in src/pkgforge/db/_yaml.py
132 133 134 135 136 | |
load()
Source code in src/pkgforge/db/_yaml.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
pkgforge.db.SQLiteDb(path)
Bases: DbProvider
SQLite store: one row per path, upserted in place (no append log).
Source code in src/pkgforge/db/__init__.py
75 76 | |
NAME = 'sqlite'
class-attribute
instance-attribute
SUFFIXES = ('.db', '.sqlite', '.sqlite3')
class-attribute
instance-attribute
add(path, entry)
Source code in src/pkgforge/db/_sqlite.py
276 277 278 279 280 281 282 | |
batch()
Hold one connection open across many add/remove calls,
committing every :data:_BATCH_ROWS rows and once more on exit
(exception included, so a killed batch keeps whatever it already
committed, like the append-log backends) instead of connecting,
creating the schema and committing once per call.
Not reentrant (a nested with provider.batch(): reuses the same
connection and row counter, which is harmless but pointless);
callers only ever open one at a time (see Project._batch).
Source code in src/pkgforge/db/_sqlite.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
compact()
Source code in src/pkgforge/db/_sqlite.py
292 293 294 295 296 297 298 299 300 301 | |
init()
Source code in src/pkgforge/db/_sqlite.py
303 304 305 306 | |
load()
Source code in src/pkgforge/db/_sqlite.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
remove(path)
Source code in src/pkgforge/db/_sqlite.py
284 285 286 287 288 289 290 | |