From: Eevee Date: Thu, 13 May 2010 06:23:05 +0000 (-0700) Subject: Make plumbing respect the same env vars as the CLI. #180 X-Git-Tag: veekun-promotions/2010051501~2 X-Git-Url: http://git.veekun.com/zzz-pokedex.git/commitdiff_plain/47218a7fb65bb2ad2e1c0808d4a2c56046f3740b Make plumbing respect the same env vars as the CLI. #180 --- diff --git a/pokedex/__init__.py b/pokedex/__init__.py index 810783f..ad11043 100644 --- a/pokedex/__init__.py +++ b/pokedex/__init__.py @@ -46,6 +46,9 @@ def get_session(options): session. """ + # WARNING: This logic duplicates that in db.connect(), because there's no + # other reliable way to tell where the engine actually came from. Keep it + # up to date! engine_uri = options.engine_uri got_from = None if engine_uri: @@ -70,6 +73,9 @@ def get_lookup(options, session=None, recreate=False): PokedexLookup object. """ + # WARNING: This logic duplicates that in PokedexLookup, because there's no + # other reliable way to tell where the engine actually came from. Keep it + # up to date! if recreate and not session: raise ValueError("get_lookup() needs an explicit session to regen the index") diff --git a/pokedex/db/__init__.py b/pokedex/db/__init__.py index 9e1b38c..e8976c4 100644 --- a/pokedex/db/__init__.py +++ b/pokedex/db/__init__.py @@ -1,3 +1,4 @@ +import os import pkg_resources from sqlalchemy import MetaData, Table, create_engine, orm @@ -13,7 +14,10 @@ def connect(uri=None, session_args={}, engine_args={}): Calling this function also binds the metadata object to the created engine. """ - # Default to a URI within the package, which was hopefully created at some point + # Fall back to the environment, then a URI within the package + if not uri: + uri = os.environ.get('POKEDEX_DB_ENGINE', None) + if not uri: sqlite_path = pkg_resources.resource_filename('pokedex', 'data/pokedex.sqlite') diff --git a/pokedex/lookup.py b/pokedex/lookup.py index cf6a15f..a75136b 100644 --- a/pokedex/lookup.py +++ b/pokedex/lookup.py @@ -104,6 +104,9 @@ class PokedexLookup(object): # Defaults if not directory: + directory = os.environ.get('POKEDEX_INDEX_DIR', None) + + if not directory: directory = pkg_resources.resource_filename('pokedex', 'data/whoosh-index') self.directory = directory