diff pinlib/pinlib.py @ 82:d7d67887102f

move pinlib to a shared location and have pics use it
author paulo
date Wed, 29 Jun 2016 00:11:55 -0700
parents laterlinks2/pinlib.py@f833a888c548
children d521df55f56c
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/pinlib/pinlib.py	Wed Jun 29 00:11:55 2016 -0700
     1.3 @@ -0,0 +1,37 @@
     1.4 +import urlparse
     1.5 +
     1.6 +
     1.7 +def parse_cookies(environ):
     1.8 +	return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
     1.9 +
    1.10 +
    1.11 +class PinFailError(Exception):
    1.12 +	pass
    1.13 +
    1.14 +
    1.15 +class PinMan(object):
    1.16 +	def __init__(self, key):
    1.17 +		assert key
    1.18 +		self._key = key
    1.19 +		self._fn = "_%s" % key
    1.20 +
    1.21 +	def load(self):
    1.22 +		ret = None
    1.23 +	
    1.24 +		try:
    1.25 +			with open(self._fn) as pin_f:
    1.26 +				ret = pin_f.read().strip()
    1.27 +		except IOError:
    1.28 +			pass
    1.29 +	
    1.30 +		return ret
    1.31 +	
    1.32 +	def check(self, cookies):
    1.33 +		if self._key not in cookies:
    1.34 +			raise PinFailError()
    1.35 +	
    1.36 +		pin = cookies[self._key][0]
    1.37 +		if pin != self.load():
    1.38 +			raise PinFailError()
    1.39 +	
    1.40 +		return pin