PKMN*p``django_environs/__init__.py"""A cli tool for bootstraping django projects with environs settings""" __version__ = "0.1.1" PKhN ;django_environs/__main__.py""" Invokes django-environs when the django_environs module is run as a script. Example: python -m django_environs startproject """ from django_environs import management if __name__ == "__main__": management.execute_from_command_line() PKN@n n django_environs/template.pyimport re from typing import Iterable, NamedTuple, Optional from django.template import Engine from redbaron import RedBaron DJANGO_SETTINGS_FRAGMENT = "Django settings" class Replacement(NamedTuple): name: str env_type: str default: Optional[str] = None value_template: str = "{value}" env_variable: Optional[str] = None REPLACEMENTS = tuple( Replacement(*rep) for rep in ( ("BASE_DIR", "path", "str(environs.Path(__file__).parents[1])"), ("SECRET_KEY", "str"), ("DEBUG", "bool"), ("ALLOWED_HOSTS", "list"), ( "DATABASES", "dj_db_url", "'sqlite:///' + str(BASE_DIR / 'db.sqlite3')", "{{'default': {value}}}", "DATABASE_URL", ), ("LANGUAGE_CODE", "str"), ("TIME_ZONE", "str"), ("USE_I18N", "bool"), ("USE_L10N", "bool"), ("USE_TZ", "bool"), ("STATIC_URL", "str"), ) ) class RedBaronSettings(RedBaron): def fix(self, replacements: Iterable[Replacement]): self.add_env() self.replace_settings(replacements) self.replace_comment() def add_env(self): # replace os import with environs import_node = self.find("ImportNode") import_node.value = "environs" # initialize env start = import_node.index_on_parent + 1 lines = ( "\n", "# Read env variables", "# https://github.com/sloria/environs", "env = environs.Env()", "\n", ) for idx, line in zip(range(start, start + len(lines)), lines): self.insert(idx, line) def replace_settings(self, replacements: Iterable[Replacement]): for rep in replacements: node = self.find("name", rep.name) if rep.default is None: # get default value from template default = node.parent.value.dumps() else: default = rep.default value = "env.{env_type}('{env_variable}', default={default})".format( env_type=rep.env_type, env_variable=rep.env_variable or rep.name, default=default, ) node.parent.value = rep.value_template.format(value=value) def replace_comment(self): to_replace = "os.path.join(BASE_DIR, ...)" compiled = re.compile(".*{to_replace}".format(to_replace=re.escape(to_replace))) comment = self.find("CommentNode", value=compiled) comment.value = comment.value.replace(to_replace, "BASE_DIR / '...'") class EnvironsEngine(Engine): def from_string(self, template_code): if DJANGO_SETTINGS_FRAGMENT in template_code: red = RedBaronSettings(template_code) red.fix(REPLACEMENTS) template_code = red.dumps() return super().from_string(template_code) PKֺN}L&django_environs/management/__init__.pyimport functools from unittest.mock import patch from django.core.management import ( execute_from_command_line as django_execute_from_command_line, ) from django.core.management import find_commands from django_environs.template import EnvironsEngine @functools.lru_cache(maxsize=None) def get_commands(): commands = {name: "django_environs" for name in find_commands(__path__[0])} return commands @patch("django.core.management.get_commands", new=get_commands) @patch("django.template.Engine", new=EnvironsEngine) def execute_from_command_line(**kwargs): """Run a ManagementUtility.""" django_execute_from_command_line(**kwargs) PKgN:{{3django_environs/management/commands/startproject.pyfrom django.core.management.commands.startproject import Command as DjangoCommand class Command(DjangoCommand): pass PK!HއKX0django_environs-0.1.1.dist-info/entry_points.txt5Q R:I"Kw7Ԣ'DllڬtFw{>B}:&&pPKgN;;'django_environs-0.1.1.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2019 Jarosław Wygoda 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. PK!HPO%django_environs-0.1.1.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,szd&Y)r$[)T&UrPK!H)(django_environs-0.1.1.dist-info/METADATASn@}߯/4m $mZ>DΦw'+1nOg̜sf|$I)]?3cZ( ':sA2ϥ݅08S@Zgp-DZ#+*Ҧ!xsLyȸ0RF.ogb\J>Hݷr ?*TY*ۺ_JeyW;F7+1A[ee{g ,ȻIHHgtN+ +COalXbzݦ):2+YcNTLVߊӟ&Q~sQyIĆx5Vh7>H9mo ҏO!nT˜wUf/V!8ڻT1B!SKThKidb{L G2(S] ыߵ9.ydW*];8^X+yF)s!#¢fv LG|M籨 uAdJb‹"ᕁ-=w dH}PK!Ho&django_environs-0.1.1.dist-info/RECORDKw@F-\dSAP y 4,LNjsoU}8jkܽ1ˆk3Rr.K=9