summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2011-08-12 17:54:49 -0400
committerEli Collins <elic@assurancetechnologies.com>2011-08-12 17:54:49 -0400
commit7d7155d8d604e4dd931e8ea9772be1d6eed53fa8 (patch)
treedcc8d630eba44f4854a2518c97560a3fcf2e3287
parenta78aa856f274d1dcf0fd0d1216edde0ea1ff8197 (diff)
downloadpasslib-7d7155d8d604e4dd931e8ea9772be1d6eed53fa8.tar.gz
provide fallback
-rw-r--r--CHANGES11
-rw-r--r--passlib/context.py33
2 files changed, 38 insertions, 6 deletions
diff --git a/CHANGES b/CHANGES
index 4677cbf..db5fb8b 100644
--- a/CHANGES
+++ b/CHANGES
@@ -6,15 +6,20 @@ Release History
**1.5.1** (NOT YET RELEASED)
+ * Google App Engine compatibility:
+
+ - bugfix: make ``passlib.hash.__loader__`` attribute writable -
+ needed by Google App Engine [issue 19]
+
+ - provide fallback for loading passlib/default.cfg
+ if pkg_resources is not present.
+
* bugfix: fixed error thrown by CryptContext.verify
when issuing min_verify_time warning [issue 17]
* removed min_verify_time setting from custom_app_context,
min_verify_time is too host & load dependant to be hardcoded [issue 17].
- * bugfix: make ``passlib.hash.__loader__`` attribute writable -
- needed by Google App Engine [issue 19]
-
**1.5** (2011-07-11)
*"20% more unicode than the leading breakfast cereal"*
diff --git a/passlib/context.py b/passlib/context.py
index e3da021..4cdb07c 100644
--- a/passlib/context.py
+++ b/passlib/context.py
@@ -25,7 +25,11 @@ import time
import os
from warnings import warn
#site
-from pkg_resources import resource_string
+try:
+ from pkg_resources import resource_string
+except ImportError:
+ #not available eg: under GAE
+ resource_string = None
#libs
from passlib.registry import get_crypt_handler, _unload_handler_name
from passlib.utils import to_bytes, to_unicode, bytes, Undef, \
@@ -700,6 +704,7 @@ class CryptPolicy(object):
k,v = self._escape_ini_pair(k,v)
parser.set(section, k,v)
+ #XXX: rename as "to_stream" or "write_to_stream" ?
def to_file(self, stream, section="passlib"):
"serialize to INI format and write to specified stream"
p = SafeConfigParser()
@@ -734,8 +739,30 @@ class CryptPolicy(object):
#eoc
#=========================================================
-#load the default policy instance setup by passlib, which all CryptContexts inherit by default
-default_policy = CryptPolicy.from_string(resource_string("passlib", "default.cfg"))
+#=========================================================
+#load default policy from default.cfg
+#=========================================================
+def _load_default_policy():
+ "helper to try to load default policy from file"
+ #if pkg_resources available, try to read out of egg (common case)
+ if resource_string:
+ try:
+ return CryptPolicy.from_string(resource_string("passlib", "default.cfg"))
+ except IOError:
+ log.warn("error reading passlib/default.cfg, is passlib installed correctly?")
+ pass
+
+ #failing that, see if we can read it from package dir
+ path = os.path.abspath(os.path.join(os.path.dirname(__file__), "default.cfg"))
+ if os.path.exists(path):
+ with open(path, "rb") as fh:
+ return CryptPolicy.from_string(fh.read())
+
+ #give up - this is not desirable at all, could use another fallback.
+ log.error("can't find passlib/default.cfg, is passlib installed correctly?")
+ return CryptPolicy()
+
+default_policy = _load_default_policy()
#=========================================================
#