Package: django-service-urls
django-service-urlsdjango-service-urlsis a setting helper for django to represent databases, caches and email settings via a single string.This work is based ondj-database-urlandhttps://github.com/django/django/pull/8562.ExampleOriginal config:DATABASES={'default':{'ENGINE':'django.db.backends.postgresql','NAME':'mydb','HOST':'localhost','PORT':5432,'USER':'myuser','PASSWORD':'mypasswd',},}CACHES={'default':{'BACKEND':'django.core.cache.backends.memcached.MemcachedCache','LOCATION':'127.0.0.1:11211',},}EMAIL_BACKEND='django.core.mail.backends.smtp.EmailBackend'HOST='localhost'PORT=2525HOST_USER=''HOST_PASSWORD=''USE_TLS=TrueUSE_SSL=FalseSSL_CERTFILE='/etc/ssl/cert'SSL_KEYFILE='/etc/ssl/key'TIMEOUT=600USE_LOCALTIME=FalseReplace with:DATABASES={'default':os.environ.get('DATABASE_DEFAULT','postgres://myuser:mypasswd@localhost:5432/mydb'),}CACHES={'default':os.environ.get('CACHE_DEFAULT','memcached://127.0.0.1:11211'),}EMAIL_BACKEND=os.environ.get('EMAIL_BACKEND','smtps://localhost:2525?ssl_certfile=/etc/ssl/cert&ssl_keyfile=/etc/ssl/key&timeout=600')BackendsCurrentlydjango-service-urlssupports three different services:DATABASES (django_service_urls.db)ServiceBackendURLStringPostgresqldjango.db.backends.postgresqlpostgres://user:passws@host:port/dbPostgresql Socketdjango.db.backends.postgresqlpostgres://%2Fvar%2Frun%2Fpostgresql/dbPostgresql (dj-database-url compat alias)django.db.backends.postgresqlpostgresql://user:passwd@host:port/dbPostgresql (dj-database-url compat alias)django.db.backends.postgresqlpgsql://user:passwd@host:port/dbPostgisdjango.contrib.gis.db.backends.postgispostgis://user:passwd@host:port/dbSqlite (memory)django.db.backends.sqlite3sqlite://:memory: or sqlite://Sqlite (file)django.db.backends.sqlite3sqlite:///var/db/database.dbSpatialite (memory)django.contrib.gis.db.backends.spatialitespatialite://:memory: or spatialite://Spatialite (file)django.contrib.gis.db.backends.spatialitespatialite:///var/db/database.dbMysqldjango.db.backends.mysqlmysql://user:passwd@host:port/dbMysql + GISdjango.contrib.gis.db.backends.mysqlmysql+gis://user:passwd@host:port/dbOracledjango.db.backends.oracleoracle://user:passwd@host:port/dbOracle + GISdjango.contrib.gis.db.backends.oracleoracle+gis://user:passwd@host:port/dbCACHES (django_service_urls.cache)ServiceBackendURLStringMemorydjango.core.cache.backends.locmem.LocMemCachememory://Memorydjango.core.cache.backends.locmem.LocMemCachememory://abcDatabasedjango.core.cache.backends.db.DatabaseCachedb://table-nameDummydjango.core.cache.backends.dummy.DummyCachedummy://Dummydjango.core.cache.backends.dummy.DummyCachedummy://abcPyMemcached: single ipdjango.core.cache.backends.memcached.PyMemcachedCachepymemcached://1.2.3.4:1567PyLibMCCache: single ipdjango.core.cache.backends.memcached.PyLibMCCachepylibmccache://1.2.3.4:1567Memcached: single ipdjango.core.cache.backends.memcached.MemcachedCachememcached://1.2.3.4:1567PyMemcached multiple ipsdjango.core.cache.backends.memcached.PyMemcachedCachepymemcached://1.2.3.4:1567,1.2.3.5:1568PyLibMCCache multiple ipsdjango.core.cache.backends.memcached.PyLibMCCachepylibmccache://1.2.3.4:1567,1.2.3.5:1568Memcached multiple ipsdjango.core.cache.backends.memcached.MemcachedCachememcached://1.2.3.4:1567,1.2.3.5:1568PyMemcached no portdjango.core.cache.backends.memcached.PyMemcachedCachepymemcached://1.2.3.4PyLibMCCache no portdjango.core.cache.backends.memcached.PyLibMCCachepylibmccache://1.2.3.4Memcached no portdjango.core.cache.backends.memcached.MemcachedCachememcached://1.2.3.4PyMemcached unix socketdjango.core.cache.backends.memcached.PyMemcachedCachepymemcached:///tmp/memcached.sockPyLibMCCache unix socketdjango.core.cache.backends.memcached.PyLibMCCachepylibmccache:///tmp/memcached.sockMemcached unix socketdjango.core.cache.backends.memcached.MemcachedCachememcached:///tmp/memcached.sockFiledjango.core.cache.backends.filebased.FileBasedCachefile://C:/abc/def/xyzFiledjango.core.cache.backends.filebased.FileBasedCachefile:///abc/def/xyzEMAIL (django_service_urls.email)ServiceBackendURLStringConsoledjango.core.mail.backends.console.EmailBackendconsole://SMTPdjango.core.mail.backends.smtp.EmailBackendsmtp://localhost:25SMTPS (smtp+tls alias)django.core.mail.backends.smtp.EmailBackendsmtps://localhost:465SMTP+TLSdjango.core.mail.backends.smtp.EmailBackendsmtp+tls://localhost:465SMTP+SSLdjango.core.mail.backends.smtp.EmailBackendsmtp+ssl://localhost:587Filedjango.core.mail.backends.filebased.EmailBackendfile:///var/log/emailsMemorydjango.core.mail.backends.locmem.EmailBackendmemory://Dummydjango.core.mail.backends.dummy.EmailBackenddummy://InstallationInstall package$python3-mpipinstalldjango-service-urlsaddimport django_service_urls.loadsin yourmanage.py#!/usr/bin/env pythonimportosimportsysimportdjango_service_urls.loadsdefmain():os.environ.setdefault('DJANGO_SETTINGS_MODULE','project_name.settings')try:fromdjango.core.managementimportexecute_from_command_lineexceptImportErrorasexc:raiseImportError("Couldn't import Django. Are you sure it's installed and ""available on your PYTHONPATH environment variable? Did you ""forget to activate a virtual environment?")fromexcexecute_from_command_line(sys.argv)if__name__=='__main__':main()and inwsgi.pyimportosimportdjango_service_urls.loadsfromdjango.core.wsgiimportget_wsgi_applicationos.environ.setdefault('DJANGO_SETTINGS_MODULE','project_name.settings')application=get_wsgi_application()Extenddjango-service-urlsAdd another handlerYou can add another handler to an already existing handler:my_postgres_backend/service_url.pyfromdjango_service_urls.servicesimportdb,postgresql_config_from_url# postgresql forkpostgresql_config_from_url=db.register(('mypgbackend','my_postgres_backend'))(postgresql_config_from_url)yourapp/settings.pyimportmy_postgres_backend.service_urlDATABASES={'default':'mypgbackend://user:pwd@:/mydb'}Add another servicefromdjango_service_urlsimportServiceclassSearchService(Service):defconfig_from_url(self,engine,scheme,url):parsed=self.parse_url(url)return{'ENGINE':engine,# here all options from parsed}search=SearchService()@search.register(('myengine','my_search_engine'))defsearch_config_from_url(backend,engine,scheme,url):returnbackend.config_from_url(engine,scheme,url)mypy integrationIf you need to load the initializer from mypy you could add[mypy]plugins=django_service_urls.mypyin yourmypy.iniorsetup.cfgfile.pyproject.tomlconfiguration is also supported:[tool.mypy]plugins=["django_service_urls.mypy"]