Repository: nvbn/import_from_github_com Branch: master Commit: 7668a0954ed4 Files: 8 Total size: 5.0 KB Directory structure: gitextract_d64dhyhs/ ├── .gitignore ├── LICENSE ├── README.md ├── github_com/ │ └── __init__.py ├── requirements.txt ├── setup.py └── tests/ ├── __init__.py └── test_github_com.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache nosetests.xml coverage.xml *,cover # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ .idea ================================================ FILE: LICENSE ================================================ The MIT License (MIT) Copyright (c) 2016 Vladimir Iakovlev Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================ # Import from `github_com` (Please don't use it) Experimental Python module finder/loader from github, like in golang. So, in golang we can import like: ```go import "github.com/parnurzeal/gorequest" ``` But in python we should install package by our hands: ```bash pip install requests ``` And import it like: ```python import requests ``` But with this magic package and power of [PEP-0302](https://www.python.org/dev/peps/pep-0302/) we can do it automatically: ```python from github_com.kennethreitz import requests assert requests.get('https://github.com/nvbn/import_from_github_com').status_code == 200 ``` ## Installation You should have git, Python 3.2+ and pip: ```bash pip install import_from_github_com ``` ## License MIT Project License can be found [here](LICENSE.md). ================================================ FILE: github_com/__init__.py ================================================ import sys import pip class IntermediateModule: """Module for paths like `github_com.nvbn`.""" def __init__(self, fullname): self.__package__ = fullname self.__path__ = fullname.split('.') self.__name__ = fullname class GithubComFinder: """Handles `github_com....` modules.""" def find_module(self, module_name, package_path): if module_name.startswith('github_com'): return GithubComLoader() class GithubComLoader: """Installs and imports modules from github.""" def _is_installed(self, fullname): try: self._import_module(fullname) return True except ImportError: return False def _import_module(self, fullname): actual_name = '.'.join(fullname.split('.')[2:]) return __import__(actual_name) def _install_module(self, fullname): if not self._is_installed(fullname): url = fullname.replace('.', '/') \ .replace('github_com', 'git+https://github.com', 1) pip.main(['install', url]) def _is_repository_path(self, fullname): return fullname.count('.') == 2 def _is_intermediate_path(self, fullname): return fullname.count('.') < 2 def load_module(self, fullname): if self._is_repository_path(fullname): self._install_module(fullname) if self._is_intermediate_path(fullname): module = IntermediateModule(fullname) else: module = self._import_module(fullname) sys.modules[fullname] = module sys.meta_path.append(GithubComFinder()) ================================================ FILE: requirements.txt ================================================ pytest ================================================ FILE: setup.py ================================================ #!/usr/bin/env python import sys from setuptools import setup, find_packages if sys.version_info < (3, 2): raise Exception('Only Python 3.2+ is supported') setup(name='import_from_github_com', version='0.1', description="Python module finder/loader from github, like in golang", author='Vladimir Iakovlev', author_email='nvbn.rm@gmail.com', url='https://github.com/nvbn/import_from_github_com', license='MIT', packages=find_packages(exclude=['ez_setup', 'examples', 'tests', 'release']), include_package_data=True, zip_safe=False) ================================================ FILE: tests/__init__.py ================================================ ================================================ FILE: tests/test_github_com.py ================================================ def test_import_module(): from github_com.kennethreitz import requests assert requests.get('https://github.com').status_code == 200 def test_import_from_module(): from github_com.kennethreitz.requests import get assert get('https://github.com').status_code == 200