diff --git a/demo.py b/demo.py index 3373e35..88e7554 100755 --- a/demo.py +++ b/demo.py @@ -30,20 +30,19 @@ def run(): ], stderr=DEVNULL) # - - # 4. create private with_my file and set path to private configuration - with_my = 'my_repo/with_my' - copy('my_repo/with_my.example', with_my) - + # 4. point my.config to the Hypothesis data mycfg_root = abspath('my_repo/mycfg_template') - # edit the config and set path to private configuration - my = Path(with_my).read_text().replace("MYCFG_DIR = ''", "MYCFG_DIR = '{}'".format(mycfg_root)) - Path(with_my).write_text(my) + init_file = Path(mycfg_root) / 'my/config/__init__.py' + init_file.write_text(init_file.read_text().replace( + '/path/to/hypothesis/data', + hypothesis_backups, + )) # - # 5. now we can use it! + # 4. now we can use it! + os.chdir(my_repo) - check_call(['my_repo/with_my', 'python3', '-c', ''' + check_call(['python3', '-c', ''' import my.hypothesis pages = my.hypothesis.get_pages() @@ -54,7 +53,12 @@ for page in islice(pages, 0, 8): print('Title: ' + page.title) print('{} annotations'.format(len(page.highlights))) print() -''']) +'''], env={ + # this is just to prevent demo.py from using real data + # normally, it will rely on having my.config in ~/.config/my + 'MY_CONFIG': mycfg_root, + **os.environ, +}) # that should result in something like this: diff --git a/my/hypothesis.py b/my/hypothesis.py index 821975b..30229eb 100644 --- a/my/hypothesis.py +++ b/my/hypothesis.py @@ -1,13 +1,15 @@ """ Hypothes.is highlights and annotations """ +from . import init from .common import PathIsh -import mycfg.repos.hypexport as hypexport +import my.config.repos.hypexport as hypexport +from my.config.repos.hypexport import dal -from mycfg import paths -export_path: PathIsh = paths.hypothesis.export_path +from my.config import hypothesis as config +export_path: PathIsh = config.export_path ### @@ -17,7 +19,6 @@ from .common import get_files, cproperty, group_by_key from .error import Res, sort_res_by -from mycfg.repos.hypexport import dal # TODO weird. not sure why e.g. from dal import Highlight doesn't work.. diff --git a/my/init.py b/my/init.py index 99ad837..e923d42 100644 --- a/my/init.py +++ b/my/init.py @@ -13,11 +13,18 @@ A hook to insert user's config directory into Python's search path. def setup_config(): from pathlib import Path import sys + import os import warnings - # TODO use appdir?? - cfg_dir = Path('~/.config').expanduser() - mycfg_dir = cfg_dir / 'my' + # not sure if that's necessary, i.e. could rely on PYTHONPATH instead + # on the other hand, by using MY_CONFIG we are guaranteed to load it from the desired path? + mvar = os.environ.get('MY_CONFIG') + if mvar is not None: + mycfg_dir = Path(mvar) + else: + # TODO use appdir?? + cfg_dir = Path('~/.config').expanduser() + mycfg_dir = cfg_dir / 'my' # TODO maybe try importing first and if it's present, don't do anything? diff --git a/mycfg_template/my/config/__init__.py b/mycfg_template/my/config/__init__.py new file mode 100644 index 0000000..dd427b6 --- /dev/null +++ b/mycfg_template/my/config/__init__.py @@ -0,0 +1,8 @@ +""" +Feel free to remove this if you don't need it/add your own custom settings and use them +""" + +class hypothesis: + # expects outputs from https://github.com/karlicoss/hypexport + # (it's just the standard Hypothes.is export format) + export_path = '/path/to/hypothesis/data' diff --git a/mycfg_template/mycfg/repos/.gitkeep b/mycfg_template/my/config/repos/.gitkeep similarity index 100% rename from mycfg_template/mycfg/repos/.gitkeep rename to mycfg_template/my/config/repos/.gitkeep diff --git a/mycfg_template/mycfg/repos/hypexport b/mycfg_template/my/config/repos/hypexport similarity index 100% rename from mycfg_template/mycfg/repos/hypexport rename to mycfg_template/my/config/repos/hypexport diff --git a/mycfg_template/mycfg/__init__.py b/mycfg_template/mycfg/__init__.py deleted file mode 100644 index d2d6356..0000000 --- a/mycfg_template/mycfg/__init__.py +++ /dev/null @@ -1,6 +0,0 @@ -class paths: - """ - Feel free to remove this if you don't need it/add your own custom settings and use them - """ - class hypothesis: - export_path = '/tmp/my_demo/backups/hypothesis'