PK!uhdump_env/__init__.py# -*- coding: utf-8 -*- PK!9//dump_env/cli.py#!/usr/bin/env python # -*- coding: utf-8 -*- import argparse import sys from dump_env.dumper import dump def _create_parser() -> argparse.ArgumentParser: parser = argparse.ArgumentParser() parser.add_argument( '-t', '--template', default='', type=str, help='Adds template path', ) parser.add_argument('-p', '--prefix', action='append', help='Adds prefix') return parser def main() -> None: """ Runs dump-env script. Examples: This example will dump all environ variables:: $ dump-env This example will dump all environ variables starting with `PIP_`:: $ dump-env -p 'PIP_' This example will dump all environ variables starting with `PIP_` and update them with variables starting with `SECRET_`:: $ dump-env -p 'PIP_' -p 'SECRET_' This example will dump everything from `.env.template` file and all env variables with `SECRET_` prefix into a `.env` file:: $ dump-env -p 'SECRET_' -t .env.template > .env """ parser = _create_parser() args = parser.parse_args() variables = dump(args.template, args.prefix) for env_name, env_value in variables.items(): sys.stdout.write('{0}={1}'.format(env_name, env_value) + '\n') PK!re e dump_env/dumper.py# -*- coding: utf-8 -*- from collections import OrderedDict from os import environ from typing import Dict, List, Mapping, Optional Store = Mapping[str, str] def parse(source: str) -> Store: """ Reads the source `.env` file and load key-values. Args: source (str): `.env` template filepath Returns: """ parsed_data = {} with open(source) as env_file: for line in env_file: line = line.strip() if not line or line.startswith('#') or '=' not in line: # Ignore comments and lines without assignment. continue # Remove whitespaces and quotes: env_name, env_value = line.split('=', 1) env_name = env_name.strip() env_value = env_value.strip().strip('\'"') parsed_data[env_name] = env_value return parsed_data def _preload_existing_vars(prefix: str) -> Store: if not prefix: # If prefix is empty just return all the env variables. return environ prefixed = {} # Prefix is not empty, do the search and replacement: for env_name, env_value in environ.items(): if not env_name.startswith(prefix): # Skip vars with no prefix. continue prefixed[env_name.replace(prefix, '', 1)] = env_value return prefixed def dump( template: str = '', prefixes: Optional[List[str]] = None, ) -> Dict[str, str]: """ This function is used to dump .env files. As a source you can use both: 1. env.template file (`''` by default) 2. env vars prefixed with some prefix (`''` by default) Args: template (str): The path of the `.env` template file, use an empty string when there is no template file. prefixes (List[str]): List of string prefixes to use only certain env variables, could be an empty string to use all available variables. Returns: OrderedDict: ordered key-value pairs. """ if prefixes is None: prefixes = [''] store: Dict[str, str] = {} if template: # Loading env values from template file: store.update(parse(template)) # Loading env variables from `os.environ`: for prefix in prefixes: store.update(_preload_existing_vars(prefix)) # Sort keys and keep them ordered: return OrderedDict(sorted(store.items())) PK!Hr4,.)dump_env-1.0.0.dist-info/entry_points.txtN+I/N.,()J)-M+1 L<..PK!SE// dump_env-1.0.0.dist-info/LICENSEMIT License Copyright (c) 2017 Nikita Sobolev 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!HڽTUdump_env-1.0.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!Hu6!dump_env-1.0.0.dist-info/METADATAWms8_\6U&$Y9NB&Bva P-$2eM( oziq4 N*Ωե10S% kwꔝ5ux$Py2%r]&@34Q& *9>oŧ;o֨Q|P̙IJq6}f:T(\~tLhԆ4CNN5[izfebY9hTtoPs(&\_6xgQ.1 =T28j11 HLE;1`tTLYsL8?S_?^M`8SxjO᷸}Waq{} ̭f\:-cwݛx识}=ŪNxy.~oUݛ9voys6/?͟[Mqջ_=[kek\.\&MXf:l>%'xԒ.9tLnƒ&SQyӺk 8@BwZY"SK(o-} mhb|tuc\@1BY[{C?zBXQ/~(6"? w``ľ%%n.~06d۫vjm+4!Mk{Smm0"d 1 Ÿs(DaTrlj(I27WXH@ Ɠ^ 0I!]ADp G2):;i`Y ,؇`'?O,J8ҷ&3,"KV7؍8 F9#D@Pc}lE[a{;ؽ5:._$|_E|Tqji=51,4%Q oׁ0qR=A`fHlEy@uj=4d۟fЩf!<9["D$Kt@8b m! :050# 84N..3poW%H6*iAZ9|׆Q7A QVH8cm"-?F.~lo6r=yCq\>>HVqhYmVncѐWn|/\$_ϸGbrmFDY;4Zf{lWhE