Skip to content

Commit

Permalink
Merge branch 'release/0.1.9' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
fasih committed Oct 20, 2020
2 parents 432c445 + 4c8a8af commit 11d47c6
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 44 deletions.
42 changes: 42 additions & 0 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions

name: Test Python Package

on:
push:
branches: [ develop ]
pull_request:
branches: [ develop ]

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.6, 3.7, 3.8]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8 pytest pytest-cov codecov
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
pytest
- name: Coverage by codecov
run: |
codecov
31 changes: 31 additions & 0 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This workflows will upload a Python Package using Twine when a release is created
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries

name: Upload Python Package

on:
release:
types: [created]

jobs:
deploy:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ coverage.xml
.hypothesis/
.pytest_cache/


# Translations
*.mo
*.pot
Expand Down Expand Up @@ -127,3 +128,6 @@ dmypy.json

# Pyre type checker
.pyre/

# dsl
tests/.gitignore
6 changes: 5 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ django-settings-local - Manage Django Local Settings

.. image:: https://badge.fury.io/py/django-settings-local.svg
:target: http://badge.fury.io/py/django-settings-local

.. image:: https://github.com/fasih/django-settings-local/workflows/Test%20Python%20Package/badge.svg
:target: https://github.com/fasih/django-settings-local/actions?query=workflow%3A%22Test+Python+Package%22
.. image:: https://codecov.io/gh/fasih/django-settings-local/branch/develop/graph/badge.svg?token=YDO44RQBSS
:target: https://codecov.io/gh/fasih/django-settings-local

*****
Usage
*****
Expand Down
11 changes: 11 additions & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
codecov:
archive:
uploads: no

coverage:
precision: 0
range: 80...100

comment:
layout: "header, diff, changes"

4 changes: 4 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ where=src
[options.entry_points]
console_scripts =
dsl = django_settings_local.__main__:main

[tool:pytest]
addopts = --tb=short --strict -ra --cov-report=xml --cov=src/
testpaths = tests
8 changes: 5 additions & 3 deletions src/django_settings_local/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
from pathlib import Path
from .cute_python import snek

__version__ = '0.1.8'
__version__ = '0.1.9'

DSL_CACHE = os.environ.get("DSL_CACHE")

def pritty_print(msg):
print('\n','#'*len(msg),'\n',msg,'\n','#'*len(msg))

def import_local(g):
PROJECT_APP = Path(g['__file__']).resolve(strict=True).parent.name
CWD = Path(os.getcwd())
PWD = Path(g['__file__']).resolve(strict=True).parent
PROJECT_APP = ".".join(PWD.relative_to(CWD).parts)
try:
msg = "Django local settings applied"
settings_local = importlib.import_module(".settings_local", PROJECT_APP)
Expand All @@ -23,7 +25,7 @@ def import_local(g):
settings_local.patch_globals(g)
except ImportError:
msg = "Use `settings_local.py` in the project app to override global settings"
except AttributeError:
except (AttributeError, TypeError):
msg = "For global settings mutation, see format of settings_local.py `dsl -f`"
finally:
if not DSL_CACHE:
Expand Down
44 changes: 4 additions & 40 deletions src/django_settings_local/__main__.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
import argparse, os, shutil

from pathlib import Path
from .cute_python import snek

FILE_NAME = 'settings_local.py'

"""Entry-point for the :program:`dsl` command."""
import sys

def main():
print(snek)

parser = argparse.ArgumentParser(description='Create Django Local Settings')
parser.add_argument('-p', metavar='PATH', help='Django Project Application Path')
parser.add_argument('-f', action='store_true', help='Show Django Local Settings Format')
args = parser.parse_args()

WORKING_DIR = Path(args.p or os.getcwd())
PACKAGE_DIR = Path(__file__).resolve(strict=True).parent

GIT_IGNORE = WORKING_DIR.parent/'.gitignore'
DJANGO_SETTINGS = WORKING_DIR/'settings.py'

if args.f:
with open(PACKAGE_DIR/FILE_NAME, 'r') as fb:
print(fb.read())
elif os.path.isfile(WORKING_DIR/FILE_NAME):
print(f"{FILE_NAME} is already there at {WORKING_DIR}")
else:
shutil.copyfile(PACKAGE_DIR/FILE_NAME, WORKING_DIR/FILE_NAME)
if os.path.isfile(GIT_IGNORE):
fp = open(GIT_IGNORE, 'a')
else:
fp = open(GIT_IGNORE, 'w')
fp.write('\n# Added by `dsl` (Django Setting Local) \nsettings_local*\n')
fp.close()

fp = open(DJANGO_SETTINGS, 'a')
fp.write("\n\n\n\n")
fp.write("\n##################### END OF SETTINGS ################################")
fp.write("\nfrom django_settings_local import import_local;import_local(globals())")
fp.write("\n##################### END OF SETTINGS ################################")
fp.close()
from .dsl import main as _main
sys.exit(_main())

if __name__ == '__main__':
main()
44 changes: 44 additions & 0 deletions src/django_settings_local/dsl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import argparse, os, shutil

from pathlib import Path
from .cute_python import snek


FILE_NAME = 'settings_local.py'


def main():
print(snek)

parser = argparse.ArgumentParser(description='Create Django Local Settings')
parser.add_argument('-p', metavar='PATH', help='Django Project Application Path')
parser.add_argument('-f', action='store_true', help='Show Django Local Settings Format')
args = parser.parse_args()

WORKING_DIR = Path(args.p or os.getcwd())
PACKAGE_DIR = Path(__file__).resolve(strict=True).parent

GIT_IGNORE = WORKING_DIR.parent/'.gitignore'
DJANGO_SETTINGS = WORKING_DIR/'settings.py'

if args.f:
with open(PACKAGE_DIR/FILE_NAME, 'r') as fb:
print(fb.read())
elif os.path.isfile(WORKING_DIR/FILE_NAME):
print(f"{FILE_NAME} is already there at {WORKING_DIR}")
else:
shutil.copyfile(PACKAGE_DIR/FILE_NAME, WORKING_DIR/FILE_NAME)
if os.path.isfile(GIT_IGNORE):
fp = open(GIT_IGNORE, 'a')
else:
fp = open(GIT_IGNORE, 'w')
fp.write('\n# Added by `dsl` (Django Setting Local) \nsettings_local*\n')
fp.close()

fp = open(DJANGO_SETTINGS, 'a')
fp.write("\n\n\n\n")
fp.write("\n##################### END OF SETTINGS ################################")
fp.write("\nfrom django_settings_local import import_local;import_local(globals())")
fp.write("\n##################### END OF SETTINGS ################################")
fp.close()

Empty file added tests/__init__.py
Empty file.
1 change: 1 addition & 0 deletions tests/django_project_app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEBUG = False
4 changes: 4 additions & 0 deletions tests/invalid_patch_globals/settings_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DEBUG = True
ALLOWED_HOSTS = ['*']

patch_globals = "Not a function"
4 changes: 4 additions & 0 deletions tests/invalid_patch_globals/test_invalid_patch_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src.django_settings_local import import_local

def test_import_local():
import_local(globals())
1 change: 1 addition & 0 deletions tests/no_patch_globals/settings_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DEBUG = True
4 changes: 4 additions & 0 deletions tests/no_patch_globals/test_no_patch_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src.django_settings_local import import_local

def test_import_local():
import_local(globals())
27 changes: 27 additions & 0 deletions tests/test_settings_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import importlib, os
from src.django_settings_local import dsl, import_local, settings_local

def test_import_local():
import_local(globals())

def test_valid_patch_globals():
settings_local.patch_globals(globals())

def test_invalid_patch_globals():
try:
settings_local.patch_globals(1,2)
except TypeError:
pass

def test_main_module():
main_module = importlib.import_module("src.django_settings_local.__main__")
try:
os.chdir("tests/django_project_app")
main_module.main()
except SystemExit:
os.chdir("../../")

def test_main_script():
os.chdir("tests/django_project_app")
dsl.main(); dsl.main()
os.chdir("../../")
5 changes: 5 additions & 0 deletions tests/valid_patch_globals/settings_local.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DEBUG = True
ALLOWED_HOSTS = ['*']

def patch_globals(g):
pass
4 changes: 4 additions & 0 deletions tests/valid_patch_globals/test_valid_patch_globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from src.django_settings_local import import_local

def test_import_local():
import_local(globals())

0 comments on commit 11d47c6

Please sign in to comment.