Skip to content

Commit

Permalink
Merge pull request #13 from karlicoss/my.config
Browse files Browse the repository at this point in the history
initial steps for my.config
  • Loading branch information
karlicoss authored Apr 11, 2020
2 parents 97896fc + 4641e5d commit f31ff71
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 38 deletions.
26 changes: 15 additions & 11 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -54,7 +53,12 @@ def run():
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:

Expand Down
8 changes: 8 additions & 0 deletions doc/DEVELOPMENT.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
* IDE setup: make sure my.config is in your package search path
In runtime, ~my.config~ is imported from the user config directory dynamically.

However, Pycharm/Emacs/whatever you use won't be able to figure that out, so you'd need to adjust your IDE configuration.

- Pycharm: basically, follow the instruction [[https://stackoverflow.com/a/55278260/706389][here]]

i.e. create a new interpreter configuration (e.g. name it "Python 3.7 (for HPI)"), and add =~/.config/my=.
12 changes: 0 additions & 12 deletions my/__init__.py

This file was deleted.

9 changes: 5 additions & 4 deletions my/hypothesis.py
Original file line number Diff line number Diff line change
@@ -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

###

Expand All @@ -17,7 +19,6 @@
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..
Expand Down
47 changes: 47 additions & 0 deletions my/init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'''
A hook to insert user's config directory into Python's search path.
- Ideally that would be in __init__.py (so it's executed without having to import explicityly)
But, with namespace packages, we can't have __init__.py in the parent subpackage
(see http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html#the-init-py-trap)
Please let me know if you are aware of a better way of dealing with this!
'''


# separate function to present namespace pollution
def setup_config():
from pathlib import Path
import sys
import os
import warnings

# 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?

if not mycfg_dir.exists():
warnings.warn(f"my.config package isn't found! (expected at {mycfg_dir}). This might result in issues.")
from . import mycfg_stub as mycfg
sys.modules['my.config'] = mycfg
else:
mp = str(mycfg_dir)
if mp not in sys.path:
sys.path.insert(0, mp)

try:
import my.config
except ImportError as ex:
warnings.warn(f"Importing my.config failed! (error: {ex}). This might result in issues.")


setup_config()
del setup_config
8 changes: 8 additions & 0 deletions mycfg_template/my/config/__init__.py
Original file line number Diff line number Diff line change
@@ -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'
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions mycfg_template/mycfg/__init__.py

This file was deleted.

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def main():
'testing': [
'pytest',
'pytz',
'pylint',
],
},
)
Expand Down
11 changes: 6 additions & 5 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ skip_missing_interpreters = True
# TODO ugh. unclear how to reuse setup.cfg deps in tox
[testenv]
passenv = CI CI_* CIRCLE*
changedir = {toxworkdir}/{envname}
setenv = MY_CONFIG = mycfg_template
commands =
# pip install -e .[testing]
pip install -e .
# TODO ??
# python -m pytest {posargs}
python3 -c 'import my; print(my.__path__)'
python3 -c 'import my; import mycfg.repos' # shouldn't fail at least
python3 -c 'import my.init; import my.config; print(my.config.__path__)'
python3 -c 'import my.init; import my.config; import my.config.repos' # shouldn't fail at least
# TODO run demo.py? just make sure with_my is a bit cleverer?
# TODO e.g. under CI, rely on installing

Expand Down Expand Up @@ -40,4 +40,5 @@ skip_install = true
commands =
pip install -e .[testing]
# for now ignore import errors until I figure out how to import everything for CI checking..
python -m pylint -E -d import-error my
# TODO FIXME ugh. fix later, after properly switched to my.config
# python -m pylint -E -d import-error my

0 comments on commit f31ff71

Please # to comment.