Skip to content

Commit

Permalink
Fix issues in api
Browse files Browse the repository at this point in the history
1. Add AssimilateError to symbols that are available for import
2. Assure settings files are read from API and can contain settings for overdue and hooks
  • Loading branch information
Ken Kundert authored and Ken Kundert committed Jan 10, 2025
1 parent 3785e14 commit 94cef1c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion assimilate/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "0.0a0"
__released__ = "2024-12-08"

from .assimilate import Assimilate
from .assimilate import Assimilate, Error as AssimilateError
9 changes: 6 additions & 3 deletions assimilate/assimilate.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@
PROGRAM_NAME,
SHARED_SETTINGS_FILE,
)
from . import overdue
# unused here, but needed so that overdue extensions to settings file are
# added before the files are read
from .shlib import (
Run, cd, cwd, render_command, to_path,
set_prefs as set_shlib_prefs
Expand Down Expand Up @@ -234,14 +237,15 @@ def __init__(self, config=None, assimilate_opts=None, shared_settings=None, **kw
self.assimilate_opts = {"no-log": True}
# no-log as True is suitable for API default
if config:
if assimilate_opts.get("config"):
if assimilate_opts and assimilate_opts.get("config"):
assert config == assimilate_opts.get("config")
else:
if assimilate_opts.get("config"):
config = assimilate_opts.get("config")

# read shared setting if not already available
if shared_settings is None:
Hooks.provision_hooks()
shared_settings = read_settings('shared')
self.run_name = kwargs.pop('run_name', None)

Expand Down Expand Up @@ -331,8 +335,7 @@ def read_config(self, name, shared_settings, **kwargs):
self.settings["config_name"] = config

# add command name to settings so it can be used in expansions
if 'cmd_name' in kwargs:
self.settings['cmd_name'] = kwargs['cmd_name']
self.settings['cmd_name'] = kwargs.get('cmd_name', '')


# check() {{{2
Expand Down
29 changes: 12 additions & 17 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ separately:

.. code-block:: python
from assimilate import Assimilate
from assimilate import Assimilate, AssimilateError
from pathlib import Path
destination = Path('keys')
destination = Path('key')
with Assimilate('home') as assimilate:
borg = assimilate.run_borg(
cmd = 'key export',
args = [assimilate.destination(), destination / '.config/borg.repokey']
)
if borg.stdout:
print(borg.stdout.rstrip())
try:
with Assimilate('home') as assimilate:
borg = assimilate.run_borg(
cmd = 'key export',
args = [destination]
)
if borg.stdout:
print(borg.stdout.strip())
except AssimilateError as e:
e.report()
*Assimilate* takes the config name as an argument, if not given the default
config is used. You can also pass list of *Assimilate* options and the path to
Expand All @@ -50,14 +53,6 @@ The path to the repository.
The *Assimilate* version number as a 3-tuple (major, minor, patch).


**destination(archive)**

Returns the full path to the archive. If Archive is False or None, then the path
to the repository it returned. If Archive is True, then the default archive name
as taken from settings file is used. This is only appropriate when creating new
repositories.


**run_borg(cmd, args, borg_opts, assimilate_opts)**

Runs a *Borg* command.
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ ignore = []

[tool.ruff.lint.per-file-ignores]
"assimilate/__init__.py" = ["F401"]
"assimilate/assimilate.py" = ["F401"]
"assimilate/utilities.py" = ["F401"]
28 changes: 28 additions & 0 deletions tests/assimilate.nt
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,27 @@ assimilate:
contents: bbb
mtime: 2024-11-26

extract-key:
contents:
> #!/usr/bin/env python3
>
> from assimilate import Assimilate, AssimilateError
> from pathlib import Path
>
> destination = Path('repokey')
>
> try:
> with Assimilate('test') as assimilate:
> borg = assimilate.run_borg(
> cmd = 'key export',
> args = (destination,),
> )
> if borg.stdout:
> print(borg.stdout.strip())
> except AssimilateError as e:
> e.report()
mode: 0o777

tests:
compel — version {{{2:
run: assimilate version
Expand Down Expand Up @@ -2278,6 +2299,13 @@ assimilate:
matches regex:
> {aid0} .* {ar0} .*

dusty — api {{{2:
run: ./extract-key
checks:
repokey:
matches regex:
> BORG_KEY .*

daemon — composite config that uses patterns and implicit archive {{{1:
initialization:
create:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_assimilate.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def file_ops(operations):

mode = attributes.get("mode", None)
if mode:
path.chmod(mode)
path.chmod(int(mode, base=0))

mtime = attributes.get("mtime", None)
if mtime:
Expand Down

0 comments on commit 94cef1c

Please sign in to comment.