annotate 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
rev   line source
paulo@78 1 import urlparse
paulo@78 2
paulo@78 3
paulo@82 4 def parse_cookies(environ):
paulo@82 5 return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
paulo@78 6
paulo@78 7
paulo@78 8 class PinFailError(Exception):
paulo@78 9 pass
paulo@78 10
paulo@78 11
paulo@82 12 class PinMan(object):
paulo@82 13 def __init__(self, key):
paulo@82 14 assert key
paulo@82 15 self._key = key
paulo@82 16 self._fn = "_%s" % key
paulo@78 17
paulo@82 18 def load(self):
paulo@82 19 ret = None
paulo@82 20
paulo@82 21 try:
paulo@82 22 with open(self._fn) as pin_f:
paulo@82 23 ret = pin_f.read().strip()
paulo@82 24 except IOError:
paulo@82 25 pass
paulo@82 26
paulo@82 27 return ret
paulo@82 28
paulo@82 29 def check(self, cookies):
paulo@82 30 if self._key not in cookies:
paulo@82 31 raise PinFailError()
paulo@82 32
paulo@82 33 pin = cookies[self._key][0]
paulo@82 34 if pin != self.load():
paulo@82 35 raise PinFailError()
paulo@82 36
paulo@82 37 return pin