Skip to content
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

Refactor to be more easily importable #50

Closed
albertyw opened this issue Sep 11, 2016 · 7 comments
Closed

Refactor to be more easily importable #50

albertyw opened this issue Sep 11, 2016 · 7 comments

Comments

@albertyw
Copy link
Contributor

The github-backup package cannot be importable because of how its named. I think most of the code should be moved to the github_backup source code and the executable script file should be minimal. This will help with #49 and for writing code that calls this as a library.

@albertyw albertyw mentioned this issue Sep 11, 2016
@josegonzalez
Copy link
Owner

Go for it. Honestly, I more or less merge anything that doesn't break functionality I use, so if you think this is a good move, thats fine by me.

@josegonzalez
Copy link
Owner

Doesn't seem like this is going to happen, but PRs welcome :)

@timm3
Copy link
Contributor

timm3 commented Jun 19, 2019

I apologize for bumping a dead issue, but thought I could mention a workaround I put together until a PR gets made addressing this.

I used some importlib magic in my code to expose the github_backup library functions.
This has only been used in an Ubuntu 18.04 & lambda environment, and has not been tested with other operating systems or flavors.
This exact code may not work, as I had to exclude most of the content, and only copied what I recall as being relevant.
You should not use this in production. Just sayin'.

import os
import sys

from importlib.util import spec_from_loader, module_from_spec
from importlib.machinery import SourceFileLoader

try:
    # "normal" pycharm development
    import github_backup
except ModuleNotFoundError:
    # docker/lambda development
    # `package` is in the path because that's what I named the directory for my lambda package...
    import package.github_backup as github_backup
init_path = github_backup.__file__
if "/lib" in init_path:
    init_ancestor = init_path.split('/lib', maxsplit=1)[0]
else:
    init_ancestor = init_path.split('/github_backup', maxsplit=1)[0]
desired_path = os.sep.join([init_ancestor, 'bin', 'github-backup'])
zspec = spec_from_loader("github_backup", SourceFileLoader("github_backup", desired_path))
zmodule = module_from_spec(zspec)
zspec.loader.exec_module(zmodule)
sys.modules["github_backup"] = zmodule
github_backup = zmodule

You can then pump arguments into sys.argv before calling github_backup.main(), though I did it before this oddball loading business.

EDIT:
I should add that this isn't everything required to make it work in lambda. I also had to change the way github_backup imports __version__ from "itself" to used the import path package.github_backup.

@timm3
Copy link
Contributor

timm3 commented Jul 8, 2019

@josegonzalez Is there a particular reason for structuring the module such that the actual code resides in bin/github-backup while the module's init file resides in github_backup/__init__.py?

What was the motivation for putting the code in the bin directory?

@josegonzalez
Copy link
Owner

None, other than it was a very small binary before and I didn't see a need to put it into the namespace.

timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 9, 2019
timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 9, 2019
@timm3
Copy link
Contributor

timm3 commented Jul 9, 2019

@josegonzalez are there any functions you wouldn't want exposed when the library gets imported?

@josegonzalez
Copy link
Owner

I don't really care to be honest. I've been merging almost every pull request, so go nuts.

timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 9, 2019
timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 9, 2019
josegonzalez#50: refactor for friendlier import
timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 10, 2019
timm3 added a commit to timm3/python-github-backup that referenced this issue Jul 10, 2019
josegonzalez#50 update: undid imports in __init__
timm3 added a commit to timm3/python-github-backup that referenced this issue Feb 10, 2020
timm3 added a commit to timm3/python-github-backup that referenced this issue Feb 13, 2020
josegonzalez added a commit that referenced this issue Feb 14, 2020
#50 - refactor for friendlier import
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants