hired.persistence#
dol-based persistence foundation for hired (Storage v2).
All hired persistence lives outside the repository under a single canonical
data root (~/.local/share/hired/ by default, overridable via HIRED_DATA_DIR).
Stores are MutableMapping views over the filesystem (via dol) with
filesystem codecs that keep keys extension-less while writing properly-extensioned
files. Per-candidate data is split two levels deep: a cross-JD user/ subtree
(UserStore) and per-engagement jds/<jd_id>/ subtrees (JDStore).
Multi-tenant: every candidate lives under users/<user>/. Entry points take an
optional user; resolve_user() falls back to $HIRED_USER, then "me",
and list_users() enumerates the candidates present.
See misc/docs/DESIGN.md §4 for the full layout and rationale.
Functions
|
Return the canonical hired data directory, joined with |
|
Filesystem base for a candidate: |
|
The candidate an operation targets: |
|
Return |
Candidates with data under the data root's |
|
|
Engagement ids under |
|
A |
|
A |
|
A |
|
One-time copy of a legacy store into the canonical root. |
|
Migrate the user to v2 if a legacy layout is detected. |
|
True iff any legacy v1 flat directory remains for this user. |
|
Migrate one user's flat v1 data to the v2 layout. |
Classes
|
Per-candidate cross-JD storage: raw sources + agent-maintained |
|
Per-engagement storage under |
|
Validated CRUD over a |
- class hired.persistence.JDStore(user, jd_id, *, root=None)[source]#
Bases:
objectPer-engagement storage under
users/<user>/jds/<jd_id>/.An engagement is one or a group of related JDs of the same company; it holds the parsed jobs, alignment reports (+ archived history), company research, and interview-prep briefings for that company’s role(s).
>>> import tempfile, os >>> os.environ['HIRED_DATA_DIR'] = tempfile.mkdtemp() >>> jd = JDStore('me', 'acme') >>> jd.reports['role-1'] = {'verdict': {'recommendation': 'apply'}} >>> jd.reports['role-1']['verdict']['recommendation'] 'apply' >>> jd.write_meta({'company': 'Acme'}) >>> jd.read_meta()['company'] 'Acme'
- class hired.persistence.Repository[M: BaseModel](store, *, model=None)[source]#
Bases:
Generic[M]Validated CRUD over a
MutableMappingfor a single pydantic model type.Subclasses set
modelandkey_of(). The store holds plain JSON-able dicts; models are validated on the way in and out.
- class hired.persistence.UserStore(user=None, *, root=None)[source]#
Bases:
objectPer-candidate cross-JD storage: raw sources + agent-maintained
info.rawis a bytes store;factsandqaare json stores;synopsisis a single markdown file accessed viaread_synopsis()/write_synopsis().>>> import tempfile, os >>> os.environ['HIRED_DATA_DIR'] = tempfile.mkdtemp() >>> us = UserStore() # default user: $HIRED_USER, else "me" >>> us.facts['f1'] = {'statement': 'knows Python'} >>> us.facts['f1'] {'statement': 'knows Python'} >>> us.raw['cv.pdf'] = b'%PDF-1.4' >>> us.raw['cv.pdf'] b'%PDF-1.4' >>> us.write_synopsis('# me') >>> us.read_synopsis() '# me'
- hired.persistence.app_data_dir(*subpaths, make=True)[source]#
Return the canonical hired data directory, joined with
subpaths.The directory is created when
makeis true (the default).>>> import os, tempfile >>> os.environ['HIRED_DATA_DIR'] = tempfile.mkdtemp() >>> root = app_data_dir() >>> os.path.isdir(root) True >>> app_data_dir('users', 'me').endswith(os.path.join('users', 'me')) True
- Return type:
- hired.persistence.bytes_store(rootdir)[source]#
A
MutableMappingof rawbytes; keys are real filenames (extension kept).- Return type:
- hired.persistence.ensure_v2(user=None, *, root=None)[source]#
Migrate the user to v2 if a legacy layout is detected. Returns True if migrated.
- Return type:
- hired.persistence.is_legacy_layout(user=None, *, root=None)[source]#
True iff any legacy v1 flat directory remains for this user.
Gated on the presence of legacy dirs, not on the existence of
user/— a fully-migrated user has had its legacy dirs removed, so this returns False; a partially-migrated (interrupted) user still has some, so a re-run resumes.- Return type:
- hired.persistence.json_store(rootdir)[source]#
A
MutableMappingof JSON-able values: bare-id keys,<id>.jsonfiles.Built on
JsonFiles(dict↔JSON value codec) + a.jsonkey codec. Missing intermediate directories are created on write, so nested keys (e.g."job/stamp") work transparently.- Return type:
- hired.persistence.list_jds(user=None, *, root=None)[source]#
Engagement ids under
users/<user>/jds/(sorted; empty if none).
- hired.persistence.list_users()[source]#
Candidates with data under the data root’s
users/(sorted; empty if none).>>> import os, tempfile >>> os.environ['HIRED_DATA_DIR'] = tempfile.mkdtemp() >>> list_users() [] >>> _ = UserStore('alice').facts >>> list_users() ['alice']
- hired.persistence.markdown_store(rootdir)[source]#
A
MutableMappingofstrvalues: bare keys,<key>.mdfiles.- Return type:
- hired.persistence.migrate_legacy(legacy_path, target_path)[source]#
One-time copy of a legacy store into the canonical root.
If
target_pathdoes not yet exist butlegacy_pathdoes, copy it (file or directory) so historical data is preserved when storage roots are unified. No-op otherwise. Returns True iff a copy happened.- Return type:
- hired.persistence.migrate_user_to_v2(user=None, *, root=None, company_of=None, dry_run=False)[source]#
Migrate one user’s flat v1 data to the v2 layout. Returns the move plan.
Idempotent: a no-op (empty plan) if the user is already on v2 or has no legacy data. With
dry_run=Truenothing is moved — the returned[(src, dst), ...]plan can be inspected first.
- hired.persistence.resolve_user(user=None)[source]#
The candidate an operation targets:
user, else$HIRED_USER, else"me".This is the only place the single-tenant default lives; every user-scoped entry point passes its
userargument through here. An explicitly given name (even"") is validated, never silently replaced by the default.>>> import os >>> _ = os.environ.pop('HIRED_USER', None) >>> resolve_user() 'me' >>> resolve_user('alice') 'alice' >>> os.environ['HIRED_USER'] = 'bob' >>> resolve_user() 'bob' >>> del os.environ['HIRED_USER']
- Return type:
- hired.persistence.user_base(user=None, *, root=None)[source]#
Filesystem base for a candidate:
<root>or<data>/users/<user>.- Return type:
- hired.persistence.validate_name(name, *, kind='name')[source]#
Return
nameif it is a safe, portable single path segment, else raise.User, engagement and job ids become directory or file names, so they are restricted to ASCII letters, digits and
._-(starting with a letter or digit, not ending in.), and may not be a Windows device name. That rules out separators,.., drive prefixes (C:x), hidden names and NUL bytes — so one id can never address another candidate’s (or an arbitrary) directory.kind="user"also requires lower case (see_USER_PATTERN).>>> validate_name('acme-2024') 'acme-2024' >>> validate_name('../bob', kind='engagement id') Traceback (most recent call last): ... ValueError: Invalid engagement id '../bob': use ASCII letters, digits and '._-', starting with a letter or digit and not ending in '.' >>> validate_name('Alice', kind='user') Traceback (most recent call last): ... ValueError: Invalid user 'Alice': use lower-case ASCII letters, digits and '._-', starting with a letter or digit and not ending in '.'
- Return type:
Modules
Canonical data root, filesystem codecs, and the two-level candidate layout. |
|
One-time migration of legacy flat per-user data to the Storage v2 layout. |
|
A small repository base that maps validated domain models to a key-value store. |