@@ -1,102 +1 @@
|
||||
try:
|
||||
from collections.abc import MutableMapping
|
||||
except ImportError: # pragma: no cover
|
||||
from collections import MutableMapping
|
||||
import json
|
||||
import os
|
||||
|
||||
from django.conf.urls import url, include
|
||||
|
||||
DJANGO_SETTINGS_DIR = 'main'
|
||||
|
||||
|
||||
class _BaseDict(MutableMapping):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(_BaseDict, self).__init__(*args, **kwargs)
|
||||
self._store = dict()
|
||||
self.update(dict(*args, **kwargs))
|
||||
|
||||
def __getitem__(self, key):
|
||||
return self._store[self.__keytransform__(key)]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
self._store[self.__keytransform__(key)] = value
|
||||
|
||||
def __delitem__(self, key):
|
||||
del self._store[self.__keytransform__(key)]
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._store)
|
||||
|
||||
def __len__(self):
|
||||
return len(self._store)
|
||||
|
||||
@staticmethod
|
||||
def __keytransform__(key):
|
||||
return key
|
||||
|
||||
|
||||
class DeployedAppsConfig(_BaseDict): # pylint: disable=too-many-ancestors
|
||||
def load(self, path=None):
|
||||
if path is None:
|
||||
path = self._json_file
|
||||
if os.path.exists(path):
|
||||
with open(path, 'r') as f:
|
||||
self._store = json.load(f)
|
||||
else:
|
||||
self._store = dict()
|
||||
|
||||
def write(self, path=None):
|
||||
if path is None:
|
||||
path = self._json_file
|
||||
with open(path, 'w') as f:
|
||||
json.dump(self._store, f, indent=4)
|
||||
|
||||
def __init__(self, project_dir=None, settings_dir=None):
|
||||
assert (project_dir or settings_dir), 'DeployedAppsConfig(): ' \
|
||||
'Either keyword argument project_dir or settings_dir' \
|
||||
' must be set.'
|
||||
assert (not (project_dir and settings_dir)), 'DeployedAppsConfig(): ' \
|
||||
'Keyword arguments project_dir and settings_dir' \
|
||||
' are mutually exclusive.'
|
||||
super(DeployedAppsConfig, self).__init__()
|
||||
if project_dir is not None:
|
||||
settings_dir = os.path.join(project_dir, DJANGO_SETTINGS_DIR)
|
||||
self._json_file = os.path.join(settings_dir, 'django_deploy.json')
|
||||
self.load()
|
||||
|
||||
|
||||
def get_installed_apps(file_path, installed_apps):
|
||||
settings_dir = os.path.dirname(os.path.abspath(file_path))
|
||||
config = DeployedAppsConfig(settings_dir=settings_dir)
|
||||
app_list = installed_apps[:]
|
||||
for wanting_app in config:
|
||||
wanted_apps = config[wanting_app].get('INSTALLED_APPS', [])
|
||||
for wanted_app in wanted_apps:
|
||||
if wanted_app not in app_list:
|
||||
app_list.append(wanted_app)
|
||||
return app_list
|
||||
|
||||
|
||||
def get_urlpatterns(file_path, urlpatterns):
|
||||
settings_dir = os.path.dirname(os.path.abspath(file_path))
|
||||
config = DeployedAppsConfig(settings_dir=settings_dir)
|
||||
urls_list = urlpatterns[:]
|
||||
patterns = []
|
||||
for url_obj in urls_list:
|
||||
# Django 1 vs Django 2
|
||||
if url_obj.__class__.__name__.startswith('Regex'): # pragma: no cover
|
||||
patterns.append(url_obj.regex.pattern)
|
||||
else: # pragma: no cover
|
||||
patterns.append(str(url_obj.pattern))
|
||||
for wanting_app in config:
|
||||
wanted_urls = config[wanting_app].get('urlpatterns', [])
|
||||
for wanted_url in wanted_urls:
|
||||
pattern = wanted_url['pattern']
|
||||
if pattern in patterns:
|
||||
continue
|
||||
if wanted_url['type'] == 'include':
|
||||
url_obj = url(pattern, include(wanted_url['module']))
|
||||
urls_list.append(url_obj)
|
||||
patterns.append(pattern)
|
||||
return urls_list
|
||||
|
||||
Reference in New Issue
Block a user