Skip to content

Commit

Permalink
Django contrib compatibility improved
Browse files Browse the repository at this point in the history
  • Loading branch information
idlesign committed Jul 14, 2019
1 parent 475407a commit 94b074b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ uwsgiconf changelog
===================


Unreleased
----------
+ Django contrib compatibility improved.


v0.15.2
-------
! runtime.async module is deprecated if favor of runtime.asynced.
Expand Down
9 changes: 9 additions & 0 deletions tests/contrib/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ class Error(Exception):
lambda project_name: '%s' % fifofile)


@pytest.mark.skipif(PY2, reason='Not tested on PY2')
def test_mutate_existing_section(patch_base_command):
from uwsgiconf.contrib.django.uwsgify.toolbox import SectionMutator

mutator = SectionMutator.spawn(dir_base=os.path.dirname(__file__))
assert mutator.section.name == 'testdummy'


@pytest.mark.skipif(PY2, reason='Not tested on PY2')
def test_uwsgi_run(monkeypatch, patch_project_dir, stub):

Expand All @@ -62,6 +70,7 @@ class Settings(object):
from uwsgiconf.contrib.django.uwsgify.management.commands.uwsgi_run import Command

Command().handle(compile=False, use_static_handler=True)
Command().handle(compile=True)


@pytest.mark.skipif(PY2, reason='Not tested on PY2')
Expand Down
10 changes: 10 additions & 0 deletions tests/contrib/uwsgicfg.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from uwsgiconf.config import configure_uwsgi


def get_configurations():
from uwsgiconf.presets.nice import PythonSection
section = PythonSection(name='testdummy')
return section


configure_uwsgi(get_configurations)
43 changes: 34 additions & 9 deletions uwsgiconf/contrib/django/uwsgify/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def spawn(cls, options=None, dir_base=None):

if os.path.exists(path_conf):
# Read an existing config for further modification of first section.
section = cls._get_section_existing(name_module, name_project)
section = cls._get_section_existing(path_conf, name_module, name_project)

else:
# Create section on-fly.
Expand All @@ -114,21 +114,34 @@ def spawn(cls, options=None, dir_base=None):
return mutator

@classmethod
def _get_section_existing(self, name_module, name_project):
def _get_section_existing(self, path_conf, name_module, name_project):
"""Loads config section from existing configuration file (aka uwsgicfg.py)
:param str|unicode path_conf:
:param str|unicode name_module:
:param str|unicode name_project:
:rtype: Section
"""
from importlib import import_module

from uwsgiconf.utils import PY3
from uwsgiconf.settings import CONFIGS_MODULE_ATTR

config = getattr(
import_module(os.path.splitext(name_module)[0], package=name_project),
CONFIGS_MODULE_ATTR)[0]
def load():
module_fake_name = '%s.%s' % (name_project, os.path.splitext(name_module)[0])

if not PY3: # pragma: nocover
import imp
return imp.load_source(module_fake_name, path_conf)

import importlib.util

spec = importlib.util.spec_from_file_location(module_fake_name, path_conf)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

return module

config = getattr(load(), CONFIGS_MODULE_ATTR)[0]

return config.sections[0]

Expand All @@ -145,8 +158,16 @@ def _get_section_new(cls, dir_base):

wsgi_app = settings.WSGI_APPLICATION

path_wsgi, filename, _, = wsgi_app.split('.')
path_wsgi = os.path.join(dir_base, path_wsgi, '%s.py' %filename)
app_package, filename, _, = wsgi_app.split('.')

def get_wsgi_path(base):
return os.path.join(base, app_package, '%s.py' %filename)

path_wsgi = get_wsgi_path(dir_base)

if not os.path.exists(path_wsgi):
# Maybe dir_base already contains app_package.
path_wsgi = get_wsgi_path(os.path.dirname(dir_base))

section = PythonSection(
wsgi_module=path_wsgi,
Expand Down Expand Up @@ -244,5 +265,9 @@ def run_uwsgi(config_section, compile_only=False):
config.print_ini()
return

from uwsgiconf.utils import UwsgiRunner

UwsgiRunner.prepare_env() # Use uwsgi command from venv if any.

config_path = config.tofile()
os.execvp('uwsgi', ['uwsgi', '--ini=%s' % config_path])

0 comments on commit 94b074b

Please sign in to comment.