Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
dvjn committed Jun 6, 2020
0 parents commit 7424bc2
Show file tree
Hide file tree
Showing 16 changed files with 598 additions and 0 deletions.
69 changes: 69 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Build and Publish

on:
release:
types:
- created

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: |
pip install -U wheel pip setuptools
pip install -e '.[test]'
- name: Lint and Test
run: |
python setup.py test
deploy-sdist:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: 3.8
- name: Install dependencies
run: |
pip install -U wheel pip setuptools
pip install -e '.[publish]'
- name: Build and publish sdist
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py sdist
twine check dist/*
twine upload --non-interactive --skip-existing dist/*
deploy-wheels:
needs: test
strategy:
matrix:
platform: [macos-latest, windows-latest]
pyversion: [3.6, 3.7, 3.8]
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.pyversion }}
- name: Install dependencies
run: |
pip install -U wheel pip setuptools
pip install -e '.[publish]'
- name: Build and publish wheel
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_TOKEN }}
run: |
python setup.py bdist_wheel
twine check dist/*.whl
twine upload --non-interactive --skip-existing dist/*.whl
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Integration Tests

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:
strategy:
matrix:
platform: [ubuntu-latest, macos-latest, windows-latest]
pyversion: [3.6, 3.7, 3.8]
fail-fast: false
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.pyversion }}
- name: Install dependencies
run: |
pip install -U wheel pip setuptools
pip install -e '.[test]'
- name: Test
run: |
python setup.py test
44 changes: 44 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
.pytest_cache/
.coverage

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# misc
.python-version
__pypackages__/
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 Divya Jain

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.
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
========
importit
========

Import python code from anywhere.


Installation
------------

.. code-block:: shell
pip install importit
3 changes: 3 additions & 0 deletions importit/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .importer import import_code, import_local_file, import_remote_file, import_gist

__all__ = ["import_code", "import_local_file", "import_remote_file", "import_gist"]
101 changes: 101 additions & 0 deletions importit/importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
"""Defines various importer functions."""

import sys
from json import loads
from .util.file import get_local_file_content, get_remote_file_content
from .util.module import create_empty_module, create_module_from_code

from types import ModuleType


def import_code(module_name: str, source_code: str, origin: str = None) -> ModuleType:
"""Imports python code as a module.
Args:
module_name: The name to be given to imported module.
source_code: The code to be imported.
origin: The origin of the code. Defaults to None.
Returns:
Python code imported as a module.
Raises:
ImportError: Raised when the code can't be imported.
"""
module = create_module_from_code(module_name, source_code, origin)
sys.modules[module_name] = module

return module


def import_local_file(module_name: str, file_path: str) -> ModuleType:
"""Imports a local file as a module.
Args:
module_name: The name to be given to the module.
file_path: The path of the file to be imported.
Returns:
File from the path imported as a module.
Raises:
ImportError: Raised when the file can't be imported.
"""
source_code = get_local_file_content(file_path)

module = create_module_from_code(module_name, source_code, origin=file_path)
sys.modules[module_name] = module

return module


def import_remote_file(module_name: str, url: str) -> ModuleType:
"""Imports a remote file as a module.
Args:
module_name: The name to be given to imported module.
url: The url of file to be imported.
Returns:
File from the URL imported as a module.
Raises:
ImportError: Raised when the file can't be imported.
"""
source_code = get_remote_file_content(url)

module = create_module_from_code(module_name, source_code, origin=url)
sys.modules[module_name] = module

return module


def import_gist(module_name: str, gist_id: str) -> ModuleType:
"""Imports a gist as a module.
Args:
module_name: The name to be given to imported module.
gist_id: The id of the gist to be imported.
Returns:
Gist imported as a module.
Raises:
ImportError: Raised when the gist can't be imported.
"""
gist = loads(get_remote_file_content("https://api.github.com/gists/" + gist_id))

module = create_empty_module(module_name)

submodules = {
filename[:-3]: create_module_from_code(
filename[:-3], gist_file["content"], gist_file["raw_url"]
)
for filename, gist_file in gist["files"].items()
if filename.endswith(".py")
}

module.__dict__.update(submodules)
sys.modules[module_name] = module

return module
Empty file added importit/util/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions importit/util/file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from urllib.request import urlopen


def get_local_file_content(file_path: str) -> str:
"""Gets the contents of a local file.
Args:
file_path: The path of the file.
Returns:
The content fetched from the local file.
"""
with open(file_path, "r") as opened_file:
content: str = opened_file.read()
return content


def get_remote_file_content(url: str) -> str:
"""Gets the contents of a remote file.
Args:
url: The url of the file.
Returns:
The content fetched from remote file.
"""
with urlopen(url) as loaded_file:
content: str = loaded_file.read().decode("utf-8")
return content
36 changes: 36 additions & 0 deletions importit/util/module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from importlib.util import spec_from_loader, module_from_spec

from types import ModuleType


def create_empty_module(module_name: str, origin: str = None) -> ModuleType:
"""Creates a blank module.
Args:
module_name: The name to be given to the module.
origin: The origin of the module. Defaults to None.
Returns:
A blank module.
"""
spec = spec_from_loader(module_name, loader=None, origin=origin)
module = module_from_spec(spec)
return module


def create_module_from_code(
module_name: str, source_code: str, origin: str = None
) -> ModuleType:
"""Creates a module from python code.
Args:
module_name: The name to be given to the module.
source_code: The source code for the module.
origin: The origin of the module. Defaults to None.
Returns:
A new module from the source code.
"""
module = create_empty_module(module_name, origin)
exec(source_code, module.__dict__)
return module
Loading

0 comments on commit 7424bc2

Please sign in to comment.