-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathenviron.py
1165 lines (970 loc) · 37.6 KB
/
environ.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# This file is part of the django-environ.
#
# Copyright (c) 2021-2024, Serghei Iakovlev <[email protected]>
# Copyright (c) 2013-2021, Daniele Faraglia <[email protected]>
#
# For the full copyright and license information, please view
# the LICENSE.txt file that was distributed with this source code.
"""
Django-environ allows you to utilize 12factor inspired environment
variables to configure your Django application.
"""
import ast
import itertools
import logging
import os
import re
import sys
import warnings
from urllib.parse import (
parse_qs,
ParseResult,
quote,
unquote,
unquote_plus,
urlparse,
urlunparse,
)
from .compat import (
DJANGO_POSTGRES,
ImproperlyConfigured,
json,
PYMEMCACHE_DRIVER,
REDIS_DRIVER,
)
from .fileaware_mapping import FileAwareMapping
Openable = (str, os.PathLike)
logger = logging.getLogger(__name__)
def _cast(value):
# Safely evaluate an expression node or a string containing a Python
# literal or container display.
# https://docs.python.org/3/library/ast.html#ast.literal_eval
try:
return ast.literal_eval(value)
except (ValueError, SyntaxError):
return value
def _cast_int(v):
"""Return int if possible."""
return int(v) if hasattr(v, 'isdigit') and v.isdigit() else v
def _cast_urlstr(v):
return unquote(v) if isinstance(v, str) else v
def _urlparse_quote(url):
return urlparse(quote(url, safe=':/?&=@'))
class NoValue:
"""Represent of no value object."""
def __repr__(self):
return f'<{self.__class__.__name__}>'
class Env:
"""Provide scheme-based lookups of environment variables so that each
caller doesn't have to pass in ``cast`` and ``default`` parameters.
Usage:::
import environ
import os
env = environ.Env(
# set casting, default value
MAIL_ENABLED=(bool, False),
SMTP_LOGIN=(str, 'DEFAULT')
)
# Set the project base directory
BASE_DIR = os.path.dirname(
os.path.dirname(os.path.abspath(__file__))
)
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# False if not in os.environ due to casting above
MAIL_ENABLED = env('MAIL_ENABLED')
# 'DEFAULT' if not in os.environ due to casting above
SMTP_LOGIN = env('SMTP_LOGIN')
"""
ENVIRON = os.environ
NOTSET = NoValue()
BOOLEAN_TRUE_STRINGS = ('true', 'on', 'ok', 'y', 'yes', '1')
URL_CLASS = ParseResult
POSTGRES_FAMILY = ['postgres', 'postgresql', 'psql', 'pgsql', 'postgis']
DEFAULT_DATABASE_ENV = 'DATABASE_URL'
DB_SCHEMES = {
'postgres': DJANGO_POSTGRES,
'postgresql': DJANGO_POSTGRES,
'psql': DJANGO_POSTGRES,
'pgsql': DJANGO_POSTGRES,
'postgis': 'django.contrib.gis.db.backends.postgis',
'cockroachdb': 'django_cockroachdb',
'mysql': 'django.db.backends.mysql',
'mysql2': 'django.db.backends.mysql',
'mysql-connector': 'mysql.connector.django',
'mysqlgis': 'django.contrib.gis.db.backends.mysql',
'mssql': 'mssql',
'oracle': 'django.db.backends.oracle',
'pyodbc': 'sql_server.pyodbc',
'redshift': 'django_redshift_backend',
'spatialite': 'django.contrib.gis.db.backends.spatialite',
'sqlite': 'django.db.backends.sqlite3',
'ldap': 'ldapdb.backends.ldap',
}
_DB_BASE_OPTIONS = [
'CONN_MAX_AGE',
'ATOMIC_REQUESTS',
'AUTOCOMMIT',
'DISABLE_SERVER_SIDE_CURSORS',
'CONN_HEALTH_CHECKS',
]
DEFAULT_CACHE_ENV = 'CACHE_URL'
CACHE_SCHEMES = {
'dbcache': 'django.core.cache.backends.db.DatabaseCache',
'dummycache': 'django.core.cache.backends.dummy.DummyCache',
'filecache': 'django.core.cache.backends.filebased.FileBasedCache',
'locmemcache': 'django.core.cache.backends.locmem.LocMemCache',
'memcache': 'django.core.cache.backends.memcached.MemcachedCache',
'pymemcache': PYMEMCACHE_DRIVER,
'pylibmc': 'django.core.cache.backends.memcached.PyLibMCCache',
'rediscache': REDIS_DRIVER,
'redis': REDIS_DRIVER,
'rediss': REDIS_DRIVER,
}
_CACHE_BASE_OPTIONS = [
'TIMEOUT',
'KEY_PREFIX',
'VERSION',
'KEY_FUNCTION',
'BINARY',
]
DEFAULT_EMAIL_ENV = 'EMAIL_URL'
EMAIL_SCHEMES = {
'smtp': 'django.core.mail.backends.smtp.EmailBackend',
'smtps': 'django.core.mail.backends.smtp.EmailBackend',
'smtp+tls': 'django.core.mail.backends.smtp.EmailBackend',
'smtp+ssl': 'django.core.mail.backends.smtp.EmailBackend',
'consolemail': 'django.core.mail.backends.console.EmailBackend',
'filemail': 'django.core.mail.backends.filebased.EmailBackend',
'memorymail': 'django.core.mail.backends.locmem.EmailBackend',
'dummymail': 'django.core.mail.backends.dummy.EmailBackend'
}
_EMAIL_BASE_OPTIONS = ['EMAIL_USE_TLS', 'EMAIL_USE_SSL']
DEFAULT_SEARCH_ENV = 'SEARCH_URL'
SEARCH_SCHEMES = {
"elasticsearch": "haystack.backends.elasticsearch_backend."
"ElasticsearchSearchEngine",
"elasticsearch2": "haystack.backends.elasticsearch2_backend."
"Elasticsearch2SearchEngine",
"elasticsearch5": "haystack.backends.elasticsearch5_backend."
"Elasticsearch5SearchEngine",
"elasticsearch7": "haystack.backends.elasticsearch7_backend."
"Elasticsearch7SearchEngine",
"solr": "haystack.backends.solr_backend.SolrEngine",
"whoosh": "haystack.backends.whoosh_backend.WhooshEngine",
"xapian": "haystack.backends.xapian_backend.XapianEngine",
"simple": "haystack.backends.simple_backend.SimpleEngine",
}
ELASTICSEARCH_FAMILY = [scheme + s for scheme in SEARCH_SCHEMES
if scheme.startswith("elasticsearch")
for s in ('', 's')]
CLOUDSQL = 'cloudsql'
DEFAULT_CHANNELS_ENV = "CHANNELS_URL"
CHANNELS_SCHEMES = {
"inmemory": "channels.layers.InMemoryChannelLayer",
"redis": "channels_redis.core.RedisChannelLayer",
"redis+pubsub": "channels_redis.pubsub.RedisPubSubChannelLayer"
}
def __init__(self, **scheme):
self.smart_cast = True
self.escape_proxy = False
self.prefix = ""
self.scheme = scheme
def __call__(self, var, cast=None, default=NOTSET, parse_default=False):
return self.get_value(
var,
cast=cast,
default=default,
parse_default=parse_default
)
def __contains__(self, var):
return var in self.ENVIRON
def str(self, var, default=NOTSET, multiline=False):
"""
:rtype: str
"""
value = self.get_value(var, cast=str, default=default)
if multiline:
return re.sub(r'(\\r)?\\n', r'\n', value)
return value
def bytes(self, var, default=NOTSET, encoding='utf8'):
"""
:rtype: bytes
"""
value = self.get_value(var, cast=str, default=default)
if hasattr(value, 'encode'):
return value.encode(encoding)
return value
def bool(self, var, default=NOTSET):
"""
:rtype: bool
"""
return self.get_value(var, cast=bool, default=default)
def int(self, var, default=NOTSET):
"""
:rtype: int
"""
return self.get_value(var, cast=int, default=default)
def float(self, var, default=NOTSET):
"""
:rtype: float
"""
return self.get_value(var, cast=float, default=default)
def json(self, var, default=NOTSET):
"""
:returns: Json parsed
"""
return self.get_value(var, cast=json.loads, default=default)
def list(self, var, cast=None, default=NOTSET):
"""
:rtype: list
"""
return self.get_value(
var,
cast=list if not cast else [cast],
default=default
)
def tuple(self, var, cast=None, default=NOTSET):
"""
:rtype: tuple
"""
return self.get_value(
var,
cast=tuple if not cast else (cast,),
default=default
)
def dict(self, var, cast=dict, default=NOTSET):
"""
:rtype: dict
"""
return self.get_value(var, cast=cast, default=default)
def url(self, var, default=NOTSET):
"""
:rtype: urllib.parse.ParseResult
"""
return self.get_value(
var,
cast=urlparse,
default=default,
parse_default=True
)
def db_url(self, var=DEFAULT_DATABASE_ENV, default=NOTSET, engine=None):
"""Returns a config dictionary, defaulting to DATABASE_URL.
The db method is an alias for db_url.
:rtype: dict
"""
return self.db_url_config(
self.get_value(var, default=default),
engine=engine
)
db = db_url
def cache_url(self, var=DEFAULT_CACHE_ENV, default=NOTSET, backend=None):
"""Returns a config dictionary, defaulting to CACHE_URL.
The cache method is an alias for cache_url.
:rtype: dict
"""
return self.cache_url_config(
self.url(var, default=default),
backend=backend
)
cache = cache_url
def email_url(self, var=DEFAULT_EMAIL_ENV, default=NOTSET, backend=None):
"""Returns a config dictionary, defaulting to EMAIL_URL.
The email method is an alias for email_url.
:rtype: dict
"""
return self.email_url_config(
self.url(var, default=default),
backend=backend
)
email = email_url
def search_url(self, var=DEFAULT_SEARCH_ENV, default=NOTSET, engine=None):
"""Returns a config dictionary, defaulting to SEARCH_URL.
:rtype: dict
"""
return self.search_url_config(
self.url(var, default=default),
engine=engine
)
def channels_url(self, var=DEFAULT_CHANNELS_ENV, default=NOTSET,
backend=None):
"""Returns a config dictionary, defaulting to CHANNELS_URL.
:rtype: dict
"""
return self.channels_url_config(
self.url(var, default=default),
backend=backend
)
channels = channels_url
def path(self, var, default=NOTSET, **kwargs):
"""
:rtype: Path
"""
return Path(self.get_value(var, default=default), **kwargs)
def get_value(self, var, cast=None, default=NOTSET, parse_default=False):
"""Return value for given environment variable.
:param str var:
Name of variable.
:param collections.abc.Callable or None cast:
Type to cast return value as.
:param default:
If var not present in environ, return this instead.
:param bool parse_default:
Force to parse default.
:returns: Value from environment or default (if set).
:rtype: typing.IO[typing.Any]
"""
logger.debug(
"get '%s' casted as '%s' with default '%s'",
var, cast, default)
var_name = f'{self.prefix}{var}'
if var_name in self.scheme:
var_info = self.scheme[var_name]
try:
has_default = len(var_info) == 2
except TypeError:
has_default = False
if has_default:
if not cast:
cast = var_info[0]
if default is self.NOTSET:
try:
default = var_info[1]
except IndexError:
pass
else:
if not cast:
cast = var_info
try:
value = self.ENVIRON[var_name]
except KeyError as exc:
if default is self.NOTSET:
error_msg = f'Set the {var_name} environment variable'
raise ImproperlyConfigured(error_msg) from exc
value = default
# Resolve any proxied values
prefix = b'$' if isinstance(value, bytes) else '$'
escape = rb'\$' if isinstance(value, bytes) else r'\$'
if hasattr(value, 'startswith') and value.startswith(prefix):
value = value.lstrip(prefix)
value = self.get_value(value, cast=cast, default=default)
if self.escape_proxy and hasattr(value, 'replace'):
value = value.replace(escape, prefix)
# Smart casting
if self.smart_cast:
if cast is None and default is not None and \
not isinstance(default, NoValue):
cast = type(default)
value = None if default is None and value == '' else value
if value != default or (parse_default and value is not None):
value = self.parse_value(value, cast)
return value
@classmethod
def parse_value(cls, value, cast):
"""Parse and cast provided value
:param value: Stringed value.
:param cast: Type to cast return value as.
:returns: Casted value
"""
if cast is None:
return value
if cast is bool:
try:
value = int(value) != 0
except ValueError:
value = value.lower().strip() in cls.BOOLEAN_TRUE_STRINGS
elif isinstance(cast, list):
value = list(map(cast[0], [x for x in value.split(',') if x]))
elif isinstance(cast, tuple):
val = value.strip('(').strip(')').split(',')
value = tuple(map(cast[0], [x for x in val if x]))
elif isinstance(cast, dict):
key_cast = cast.get('key', str)
value_cast = cast.get('value', str)
value_cast_by_key = cast.get('cast', {})
value = dict(map(
lambda kv: (
key_cast(kv[0]),
cls.parse_value(
kv[1],
value_cast_by_key.get(kv[0], value_cast)
)
),
[val.split('=') for val in value.split(';') if val]
))
elif cast is dict:
value = dict([v.split('=', 1) for v in value.split(',') if v])
elif cast is list:
value = [x for x in value.split(',') if x]
elif cast is tuple:
val = value.strip('(').strip(')').split(',')
# pylint: disable=consider-using-generator
value = tuple([x for x in val if x])
elif cast is float:
# clean string
float_str = re.sub(r'[^\d,.-]', '', value)
# split for avoid thousand separator and different
# locale comma/dot symbol
parts = re.split(r'[,.]', float_str)
if len(parts) == 1:
float_str = parts[0]
else:
float_str = f"{''.join(parts[0:-1])}.{parts[-1]}"
value = float(float_str)
else:
value = cast(value)
return value
@classmethod
# pylint: disable=too-many-statements
def db_url_config(cls, url, engine=None):
# pylint: enable-msg=too-many-statements
"""Parse an arbitrary database URL.
Supports the following URL schemas:
* PostgreSQL: ``postgres[ql]?://`` or ``p[g]?sql://``
* PostGIS: ``postgis://``
* MySQL: ``mysql://`` or ``mysql2://``
* MySQL (GIS): ``mysqlgis://``
* MySQL Connector Python from Oracle: ``mysql-connector://``
* SQLite: ``sqlite://``
* SQLite with SpatiaLite for GeoDjango: ``spatialite://``
* Oracle: ``oracle://``
* Microsoft SQL Server: ``mssql://``
* PyODBC: ``pyodbc://``
* Amazon Redshift: ``redshift://``
* LDAP: ``ldap://``
:param urllib.parse.ParseResult or str url:
Database URL to parse.
:param str or None engine:
If None, the database engine is evaluates from the ``url``.
:return: Parsed database URL.
:rtype: dict
"""
if not isinstance(url, cls.URL_CLASS):
if url == 'sqlite://:memory:':
# this is a special case, because if we pass this URL into
# urlparse, urlparse will choke trying to interpret "memory"
# as a port number
return {
'ENGINE': cls.DB_SCHEMES['sqlite'],
'NAME': ':memory:'
}
# note: no other settings are required for sqlite
try:
url = urlparse(url)
# handle Invalid IPv6 URL
except ValueError:
url = _urlparse_quote(url)
config = {}
# handle unexpected URL schemes with special characters
if not url.path:
url = _urlparse_quote(urlunparse(url))
# Remove query strings.
path = url.path[1:]
path = unquote_plus(path.split('?', 2)[0])
if url.scheme == 'sqlite':
if path == '':
# if we are using sqlite and we have no path, then assume we
# want an in-memory database (this is the behaviour of
# sqlalchemy)
path = ':memory:'
if url.netloc:
warnings.warn(
f'SQLite URL contains host component {url.netloc!r}, '
'it will be ignored',
stacklevel=3
)
if url.scheme == 'ldap':
path = f'{url.scheme}://{url.hostname}'
if url.port:
path += f':{url.port}'
user_host = url.netloc.rsplit('@', 1)
if url.scheme in cls.POSTGRES_FAMILY and ',' in user_host[-1]:
# Parsing postgres cluster dsn
hinfo = list(
itertools.zip_longest(
*(
host.rsplit(':', 1)
for host in user_host[-1].split(',')
)
)
)
hostname = ','.join(hinfo[0])
port = ','.join(filter(None, hinfo[1])) if len(hinfo) == 2 else ''
else:
hostname = url.hostname
port = url.port
# Update with environment configuration.
config.update({
'NAME': path or '',
'USER': _cast_urlstr(url.username) or '',
'PASSWORD': _cast_urlstr(url.password) or '',
'HOST': hostname or '',
'PORT': _cast_int(port) or '',
})
if (
url.scheme in cls.POSTGRES_FAMILY and path.startswith('/')
or cls.CLOUDSQL in path and path.startswith('/')
):
config['HOST'], config['NAME'] = path.rsplit('/', 1)
if url.scheme == 'oracle' and path == '':
config['NAME'] = config['HOST']
config['HOST'] = ''
if url.scheme == 'oracle':
# Django oracle/base.py strips port and fails on non-string value
if not config['PORT']:
del config['PORT']
else:
config['PORT'] = str(config['PORT'])
if url.query:
config_options = {}
for k, v in parse_qs(url.query).items():
if k.upper() in cls._DB_BASE_OPTIONS:
config.update({k.upper(): _cast(v[0])})
else:
config_options.update({k: _cast_int(v[0])})
config['OPTIONS'] = config_options
if engine:
config['ENGINE'] = engine
else:
config['ENGINE'] = url.scheme
if config['ENGINE'] in cls.DB_SCHEMES:
config['ENGINE'] = cls.DB_SCHEMES[config['ENGINE']]
if not config.get('ENGINE', False):
warnings.warn(f'Engine not recognized from url: {config}')
return {}
return config
@classmethod
def cache_url_config(cls, url, backend=None):
"""Parse an arbitrary cache URL.
:param urllib.parse.ParseResult or str url:
Cache URL to parse.
:param str or None backend:
If None, the backend is evaluates from the ``url``.
:return: Parsed cache URL.
:rtype: dict
"""
if not isinstance(url, cls.URL_CLASS):
if not url:
return {}
url = urlparse(url)
if url.scheme not in cls.CACHE_SCHEMES:
raise ImproperlyConfigured(f'Invalid cache schema {url.scheme}')
location = url.netloc.split(',')
if len(location) == 1:
location = location[0]
config = {
'BACKEND': cls.CACHE_SCHEMES[url.scheme],
'LOCATION': location,
}
# Add the drive to LOCATION
if url.scheme == 'filecache':
config.update({
'LOCATION': url.netloc + url.path,
})
# urlparse('pymemcache://127.0.0.1:11211')
# => netloc='127.0.0.1:11211', path=''
#
# urlparse('pymemcache://memcached:11211/?key_prefix=ci')
# => netloc='memcached:11211', path='/'
#
# urlparse('memcache:///tmp/memcached.sock')
# => netloc='', path='/tmp/memcached.sock'
if not url.netloc and url.scheme in ['memcache', 'pymemcache']:
config.update({
'LOCATION': 'unix:' + url.path,
})
elif url.scheme.startswith('redis'):
if url.hostname:
scheme = url.scheme.replace('cache', '')
else:
scheme = 'unix'
locations = [scheme + '://' + loc + url.path
for loc in url.netloc.split(',')]
if len(locations) == 1:
config['LOCATION'] = locations[0]
else:
config['LOCATION'] = locations
if url.query:
config_options = {}
for k, v in parse_qs(url.query).items():
opt = {k.upper(): _cast(v[0])}
if k.upper() in cls._CACHE_BASE_OPTIONS:
config.update(opt)
else:
config_options.update(opt)
config['OPTIONS'] = config_options
if backend:
config['BACKEND'] = backend
return config
@classmethod
def email_url_config(cls, url, backend=None):
"""Parse an arbitrary email URL.
:param urllib.parse.ParseResult or str url:
Email URL to parse.
:param str or None backend:
If None, the backend is evaluates from the ``url``.
:return: Parsed email URL.
:rtype: dict
"""
config = {}
url = urlparse(url) if not isinstance(url, cls.URL_CLASS) else url
# Remove query strings
path = url.path[1:]
path = unquote_plus(path.split('?', 2)[0])
# Update with environment configuration
config.update({
'EMAIL_FILE_PATH': path,
'EMAIL_HOST_USER': _cast_urlstr(url.username),
'EMAIL_HOST_PASSWORD': _cast_urlstr(url.password),
'EMAIL_HOST': url.hostname,
'EMAIL_PORT': _cast_int(url.port),
})
if backend:
config['EMAIL_BACKEND'] = backend
elif url.scheme not in cls.EMAIL_SCHEMES:
raise ImproperlyConfigured(f'Invalid email schema {url.scheme}')
elif url.scheme in cls.EMAIL_SCHEMES:
config['EMAIL_BACKEND'] = cls.EMAIL_SCHEMES[url.scheme]
if url.scheme in ('smtps', 'smtp+tls'):
config['EMAIL_USE_TLS'] = True
elif url.scheme == 'smtp+ssl':
config['EMAIL_USE_SSL'] = True
if url.query:
config_options = {}
for k, v in parse_qs(url.query).items():
opt = {k.upper(): _cast_int(v[0])}
if k.upper() in cls._EMAIL_BASE_OPTIONS:
config.update(opt)
else:
config_options.update(opt)
config['OPTIONS'] = config_options
return config
@classmethod
def channels_url_config(cls, url, backend=None):
"""Parse an arbitrary channels URL.
:param urllib.parse.ParseResult or str url:
Email URL to parse.
:param str or None backend:
If None, the backend is evaluates from the ``url``.
:return: Parsed channels URL.
:rtype: dict
"""
config = {}
url = urlparse(url) if not isinstance(url, cls.URL_CLASS) else url
if backend:
config["BACKEND"] = backend
elif url.scheme not in cls.CHANNELS_SCHEMES:
raise ImproperlyConfigured(f"Invalid channels schema {url.scheme}")
else:
config["BACKEND"] = cls.CHANNELS_SCHEMES[url.scheme]
if url.scheme in ("redis", "redis+pubsub"):
config["CONFIG"] = {
"hosts": [url._replace(scheme="redis").geturl()]
}
return config
@classmethod
def _parse_common_search_params(cls, url):
cfg = {}
prs = {}
if not url.query or str(url.query) == '':
return cfg, prs
prs = parse_qs(url.query)
if 'EXCLUDED_INDEXES' in prs:
cfg['EXCLUDED_INDEXES'] = prs['EXCLUDED_INDEXES'][0].split(',')
if 'INCLUDE_SPELLING' in prs:
val = prs['INCLUDE_SPELLING'][0]
cfg['INCLUDE_SPELLING'] = cls.parse_value(val, bool)
if 'BATCH_SIZE' in prs:
cfg['BATCH_SIZE'] = cls.parse_value(prs['BATCH_SIZE'][0], int)
return cfg, prs
@classmethod
def _parse_elasticsearch_search_params(cls, url, path, secure, params):
cfg = {}
split = path.rsplit('/', 1)
if len(split) > 1:
path = '/'.join(split[:-1])
index = split[-1]
else:
path = ""
index = split[0]
cfg['URL'] = urlunparse(
('https' if secure else 'http', url[1], path, '', '', '')
)
if 'TIMEOUT' in params:
cfg['TIMEOUT'] = cls.parse_value(params['TIMEOUT'][0], int)
if 'KWARGS' in params:
cfg['KWARGS'] = params['KWARGS'][0]
cfg['INDEX_NAME'] = index
return cfg
@classmethod
def _parse_solr_search_params(cls, url, path, params):
cfg = {}
cfg['URL'] = urlunparse(('http',) + url[1:2] + (path,) + ('', '', ''))
if 'TIMEOUT' in params:
cfg['TIMEOUT'] = cls.parse_value(params['TIMEOUT'][0], int)
if 'KWARGS' in params:
cfg['KWARGS'] = params['KWARGS'][0]
return cfg
@classmethod
def _parse_whoosh_search_params(cls, params):
cfg = {}
if 'STORAGE' in params:
cfg['STORAGE'] = params['STORAGE'][0]
if 'POST_LIMIT' in params:
cfg['POST_LIMIT'] = cls.parse_value(params['POST_LIMIT'][0], int)
return cfg
@classmethod
def _parse_xapian_search_params(cls, params):
cfg = {}
if 'FLAGS' in params:
cfg['FLAGS'] = params['FLAGS'][0]
return cfg
@classmethod
def search_url_config(cls, url, engine=None):
"""Parse an arbitrary search URL.
:param urllib.parse.ParseResult or str url:
Search URL to parse.
:param str or None engine:
If None, the engine is evaluating from the ``url``.
:return: Parsed search URL.
:rtype: dict
"""
config = {}
url = urlparse(url) if not isinstance(url, cls.URL_CLASS) else url
# Remove query strings.
path = unquote_plus(url.path[1:].split('?', 2)[0])
scheme = url.scheme
secure = False
# elasticsearch supports secure schemes, similar to http -> https
if scheme in cls.ELASTICSEARCH_FAMILY and scheme.endswith('s'):
scheme = scheme[:-1]
secure = True
if scheme not in cls.SEARCH_SCHEMES:
raise ImproperlyConfigured(f'Invalid search schema {url.scheme}')
config['ENGINE'] = cls.SEARCH_SCHEMES[scheme]
# check commons params
cfg, params = cls._parse_common_search_params(url)
config.update(cfg)
if url.scheme == 'simple':
return config
# remove trailing slash
if path.endswith('/'):
path = path[:-1]
if url.scheme == 'solr':
config.update(cls._parse_solr_search_params(url, path, params))
return config
if url.scheme in cls.ELASTICSEARCH_FAMILY:
config.update(cls._parse_elasticsearch_search_params(
url, path, secure, params))
return config
config['PATH'] = '/' + path
if url.scheme == 'whoosh':
config.update(cls._parse_whoosh_search_params(params))
elif url.scheme == 'xapian':
config.update(cls._parse_xapian_search_params(params))
if engine:
config['ENGINE'] = engine
return config
@classmethod
def read_env(cls, env_file=None, overwrite=False, parse_comments=False,
encoding='utf8', **overrides):
r"""Read a .env file into os.environ.
If not given a path to a dotenv path, does filthy magic stack
backtracking to find the dotenv in the same directory as the file that
called ``read_env``.
Existing environment variables take precedent and are NOT overwritten
by the file content. ``overwrite=True`` will force an overwrite of
existing environment variables.
Refs:
* https://wellfire.co/learn/easier-12-factor-django
:param env_file: The path to the ``.env`` file your application should
use. If a path is not provided, `read_env` will attempt to import
the Django settings module from the Django project root.
:param overwrite: ``overwrite=True`` will force an overwrite of
existing environment variables.
:param parse_comments: Determines whether to recognize and ignore
inline comments in the .env file. Default is False.
:param encoding: The encoding to use when reading the environment file.
:param \**overrides: Any additional keyword arguments provided directly
to read_env will be added to the environment. If the key matches an
existing environment variable, the value will be overridden.
"""
if env_file is None:
# pylint: disable=protected-access
frame = sys._getframe()
env_file = os.path.join(
os.path.dirname(frame.f_back.f_code.co_filename),
'.env'
)
if not os.path.exists(env_file):
logger.info(
"%s doesn't exist - if you're not configuring your "
"environment separately, create one.", env_file)
return
try:
if isinstance(env_file, Openable):
# Python 3.5 support (wrap path with str).
with open(str(env_file), encoding=encoding) as f:
content = f.read()
else:
with env_file as f:
content = f.read()
except OSError:
logger.info(
"%s not found - if you're not configuring your "
"environment separately, check this.", env_file)
return
logger.debug('Read environment variables from: %s', env_file)
def _keep_escaped_format_characters(match):
"""Keep escaped newline/tabs in quoted strings"""
escaped_char = match.group(1)
if escaped_char in 'rnt':
return '\\' + escaped_char
return escaped_char
for line in content.splitlines():
m1 = re.match(r'\A(?:export )?([A-Za-z_0-9]+)=(.*)\Z', line)
if m1:
# Example:
#
# line: KEY_499=abc#def
# key: KEY_499
# val: abc#def
key, val = m1.group(1), m1.group(2)
if not parse_comments:
# Default behavior
#
# Look for value in single quotes
m2 = re.match(r"\A'(.*)'\Z", val)
if m2:
val = m2.group(1)
else:
# Ignore post-# comments (outside quotes).
# Something like ['val' # comment] becomes ['val'].
m2 = re.match(r"\A\s*'(?<!\\)(.*)'\s*(#.*\s*)?\Z", val)
if m2:
val = m2.group(1)