-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Discussion: extending base HPI/overlays/overrides #102
Comments
@seanbreckenridge would be interested to hear your thoughts! |
Agree that emacs is a good model to use here.
Definitely think adding
Yeah, I agree this is a somewhat meh solution, though probably the easiest for the user and has the lower barrier to entry. It also doesn't require a lot of custom python code to implement. Personally never been a huge fan of configuration based on symlinks (though I do know some people live by it, so technically its a fine solution)
Feel like this causes similar issues to what the pre-pip DAL was, and now it'd be up to the user to debug this if they're trying to add code on top of HPI
Dont think you'd want to include an entire plugin manager, but I think this pattern is at least better than using importlib -- its more visible to a new user and 'overriding' a dynamic function which just has Despite my disdain for symlinks, I do think they're the easiest solution here. I'm trying to balance:
I'm leaning towards the former, as it gives the user more flexibility. At some point, even an overlay may not give you enough customizability over core components/functionality you may want to change. If that cant be fixed by a monkey patch, thats when you fork, I suppose. |
Yeah, at least certainly not reinvent from scratch! I guess another important principle I keep in mind is that there shouldn't be any 'hard' requirements for base modules like special base classes/interfaces/etc -- should be as code agnostic and rely on common Python mechanisms as long as possible. That way it would be easy to plug in custom stuff with no hassle. So I'd avoid plugin systems that try to impose such structure, but would be cool to try something that's can simplify monkey patching (e.g. one example I like is patchy). In that sense all of the approaches above: symlinks/overrides/dynamic imports work, so it's up to the 'downstream' user what to choose. But still would be nice to have some 'natural' way of doing this. Oh, just recalled, another issue with symlinks is that it's not very |
Related: I've figured out how to use default config instapaper:
export_path = '...'
config reddit:
export_path = '...' in runtime corresponds to the module |
Just as an update on my current state with HPI/extending this. Is to be expected with a fork that diverges, but its become more difficult to merge changes back in from here back into my fork. At some point in the future, I will probably experiment with symlinks/overrides to see what issues I run into there. i.e. going to try and patch/merge/combine my changes in some way onto a fresh fork of this repo I will probably try to do symlinks first, as it seems to be the easiest to implement (let me know if you think otherwise), but still not sure on the long-term usability of that As a tangential aside: The more I develop my own modules, the more I feel it would be nicer if it could be completely decoupled. Taking my discord module as a basic example: I pretty much always write a package, that provides the functionality irrespective of HPI: https://github.com/seanbreckenridge/discord_data And then the corresponding 'HPI connection file' file, which handles grabbing the path to the data from the config, any possible caching, and helper/utility functions based on the domain, and calls the underlying function from the library. https://github.com/seanbreckenridge/HPI/blob/master/my/discord.py If how things are cached/discovered is all limited to For the sake of it, to further explore the possibility; one could have a block in the config where you can point to (a path on your system/it's done in python (my.config) anyways so you can choose to resolve however you want) additional 'HPI connection files', which get mangled like |
Yeah, sorry about it, I moved some things recently (hopefully in the direction of simplifying). But yeah, symlinks are probably the easiest way.
Yes, I totally agree! (this is kind of what I mean about data access layer).
Yep, good point, in fact -- it would probably be better to start gradually using
Hmm. Maybe not a bad idea? Could be files or anything else that needs to be added to PYTHONPATH, basically. Yeah, mypy won't work with these, but I guess many people don't care about it anyway, so it could be nice to have this way of setting things up. Definitely easier than messing with editable installs for some people. |
After trying and disliking how much had to be changed with a symlink based approach, have migrated towards a namespace-based approach: https://github.com/seanbreckenridge/HPI It does require me to manually edit the |
remove overlapping tests that I dont maintain code for anymore (test against the my HPI-to-master repo) do mypy -p my instead of mypy ./my so changes are picked up, remove MYPY_PATH so it checks this repo instead of my configuration fix lots of mypy errors, including pushing tons of new versions for all my packages, because I didn't include py.typed in the package data for files I don't maintain, dont import items from core using relative imports (see karlicoss/HPI#102) for discussion
Created a naive implementation to manage my easy-install.pth file: https://github.com/seanbreckenridge/reorder_editable Isnt a perfect solution, but added a check in |
…itching to namespace packages for now just a manual ad-hoc test, will try to set it up on CI later relevant to the discussion here: https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/270465792 also potentially relevant to - #89 (will try to apply to this to reddit/__init__.py later) - #102
…itching to namespace packages for now just a manual ad-hoc test, will try to set it up on CI later relevant to the discussion here: https://memex.zulipchat.com/#narrow/stream/279601-hpi/topic/extending.20HPI/near/270465792 also potentially relevant to - #89 (will try to apply to this to reddit/__init__.py later) - #102
Now we have a generic helper that is relatively easy to use when we switch more modules to |
Related issues: #12, #46; but I think worth a separate discussion.
From my experience, it's pretty hard to predict how other people want to use their data:
The list is endless! So it would be nice if it was possible to easily override small bits of HPI modules/mix in other data, etc.
The main goals are:
Once again, I see Emacs as a good role model. Everything is really decentralized, you have some core library, you have certain patterns that everyone follows... but apart from that the modules are mostly independent.
Many people still use 'monolith' base configurations (e.g. Doom/Spacemacs), because it's kinda convenient, as long as you have a maintainer. Arguably this is what this repository is at the moment, although it's obviously not as popular as Emacs distributions.
Emacs fits these goals well:
You can even literally override whole functions as a means of quickly getting the behaviour you want.
How to achieve this within HPI:
For combining independent modules together (say, something like
my/youtube.py
andmy/vimeo.py
coming from different repositories), the easiest is to use:Now, the tricky case is when you want to partially override something.
The first option is: fork & apply your modifications on top. For example: https://github.com/seanbreckenridge/HPI
Not sure if there is much to discuss here, so straight to the second and a more flexible option.
Once again, we rely on namespace packages! I'll just explain on a couple of examples, since it's easier.
example: mixing in a data source
The main idea is that you can override
all.py
(also some discussion here), and remove/add extra data sources.Since
all.py
is tiny, it's not a big problem to just copy/paste it and apply your changes.Some existing modules implemented with this approach in mind:
(I still haven't settled on the naming.
all
andmain
as the entry point kind of both make sense)example:
my.calendar.holidays
As you can guess, this module is responsible for flagging days as holidays, by exposing
is_holiday
function.As a reasonable default, it's just using the user's country of residence and flags national holidays.
However, you might also want to mix in your work vacation, and this is harder to make uniform for everyone, and it's a good candidate for a custom user override:
Thanks to namespace packages, when I import
my.calendar.holidays
it will hit my override first, monkey patch theis_holiday
function, and expose the rest intact due toimport *
.For example,
hpi doctor my.calendar.holiday
will run against the override, reusing thestats
function or any other original functions.My personal HPI override has more example code, and I'll gradually move some stuff from this repository there as well
(for example most things in my.body don't make much sense for other people).
Things I'm not sure about with this approach:
for now, I'm using a symlink (
/code/hpi-overlay/src/my/orig -> /code/hpi/src/my
)This is simple enough, but maintaining the symlink manually, referencing the 'original' package through
my.orig
.. meh.Also not sure what to do if there are multiple overrides, e.g. 'chain' (although this is probably a bit extreme).
it's probably possible to do something hacky and dynamic. E.g. take
__path__
, remove the first entry (which would be the 'override'), and then useimportlib
to import the 'original' module.The downside is that it's gonna be unfriendly to mypy (and generally a bit too magical?).
another option is to have some sort of dynamic 'hook', which is imported before anything else.
In the hook code, you import the original module and monkey patch. Same downsides, a bit too dynamic and not mypy friendly, but possible.
Caveats I know of:
packages can't contain
__init__
, otherwise the whole namespace package thing doesn't workyou need to be careful about the namespace package resolution order. It seems that the last installed package will be the last in the import order.
so you'd need to run
pip install -e /path/to/override
and thenpip install -e /path/to/original
(even if it's already installed).another option is to reorder stuff in
~/.local/lib/python3.x/site-packages/easy-install.pth
manually, but it's not very robust either (although at least it clearly shows the order)hpi doctor my.module
displays some helpful info, but it's still easy to forget/mess it up by accident.import *
doesn't import functions that start from the underscore ('private').Possible to get around this dynamically, but would be nice to cooperate with mypy somehow..
Happy to hear suggestions and thoughts on that. Once there's been some discussion, I'll move this to
doc/
, perhaps.TODOS:
~/.config/my
as the 'default' overlay. In fact, treating it like a proper namespace package (at the moment it's a bit of dynamic hackery) might make everything even cleaner and simpler.The text was updated successfully, but these errors were encountered: