changeset 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 256b8df1c686
children d521df55f56c
files laterlinks2/laterlinks_app.py laterlinks2/pinlib.py pics2/pics_app.py pinlib/pinlib.py
diffstat 4 files changed, 41 insertions(+), 50 deletions(-) [+]
line diff
     1.1 --- a/laterlinks2/laterlinks_app.py	Fri Jun 17 22:24:17 2016 -0700
     1.2 +++ b/laterlinks2/laterlinks_app.py	Wed Jun 29 00:11:55 2016 -0700
     1.3 @@ -88,7 +88,7 @@
     1.4  
     1.5  	if is_post:
     1.6  		try:
     1.7 -			pinlib.check(cookies)
     1.8 +			pinlib.PinMan("llpin").check(cookies)
     1.9  		except pinlib.PinFailError:
    1.10  			raise PinFailError
    1.11  		if inp["submit"][0] == "Add":
     2.1 --- a/laterlinks2/pinlib.py	Fri Jun 17 22:24:17 2016 -0700
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,36 +0,0 @@
     2.4 -import urlparse
     2.5 -
     2.6 -
     2.7 -PIN_KEY = "llpin"
     2.8 -PIN_FN = "_%s" % PIN_KEY
     2.9 -
    2.10 -
    2.11 -class PinFailError(Exception):
    2.12 -	pass
    2.13 -
    2.14 -
    2.15 -def load():
    2.16 -	ret = None
    2.17 -
    2.18 -	try:
    2.19 -		with open(PIN_FN) as pin_f:
    2.20 -			ret = pin_f.read().strip()
    2.21 -	except IOError:
    2.22 -		pass
    2.23 -
    2.24 -	return ret
    2.25 -
    2.26 -
    2.27 -def parse_cookies(environ):
    2.28 -	return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
    2.29 -
    2.30 -
    2.31 -def check(cookies):
    2.32 -	if PIN_KEY not in cookies:
    2.33 -		raise PinFailError()
    2.34 -
    2.35 -	pin = cookies[PIN_KEY][0]
    2.36 -	if pin != load():
    2.37 -		raise PinFailError()
    2.38 -
    2.39 -	return pin
     3.1 --- a/pics2/pics_app.py	Fri Jun 17 22:24:17 2016 -0700
     3.2 +++ b/pics2/pics_app.py	Wed Jun 29 00:11:55 2016 -0700
     3.3 @@ -8,6 +8,8 @@
     3.4  
     3.5  import html
     3.6  
     3.7 +import pinlib
     3.8 +
     3.9  import logging
    3.10  logging.basicConfig(
    3.11  	level=logging.INFO,
    3.12 @@ -16,18 +18,6 @@
    3.13  )
    3.14  
    3.15  
    3.16 -def _lahat():
    3.17 -	ret = None
    3.18 -
    3.19 -	try:
    3.20 -		with open("_lahat") as lahat_f:
    3.21 -			ret = lahat_f.read().strip()
    3.22 -	except IOError:
    3.23 -		pass
    3.24 -
    3.25 -	return ret
    3.26 -
    3.27 -
    3.28  def _is_pics_dir(dirpath):
    3.29  	return os.path.exists(os.path.join(dirpath, "_picsroot"))
    3.30  
    3.31 @@ -152,7 +142,7 @@
    3.32  	
    3.33  		self._qs = urlparse.parse_qs(self._environ["QUERY_STRING"])
    3.34  		logging.debug("self._qs = %s" % self._qs)
    3.35 -		if self._qs.get("lahat", [''])[0] == _lahat():
    3.36 +		if pinlib.PinMan("lahat").check(pinlib.parse_cookies(self._environ)):
    3.37  			self._show_index = True
    3.38  
    3.39  		if len(ppi) >= 2 and _is_pics_dir(ppi[1]):
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/pinlib/pinlib.py	Wed Jun 29 00:11:55 2016 -0700
     4.3 @@ -0,0 +1,37 @@
     4.4 +import urlparse
     4.5 +
     4.6 +
     4.7 +def parse_cookies(environ):
     4.8 +	return urlparse.parse_qs(environ.get("HTTP_COOKIE", ""))
     4.9 +
    4.10 +
    4.11 +class PinFailError(Exception):
    4.12 +	pass
    4.13 +
    4.14 +
    4.15 +class PinMan(object):
    4.16 +	def __init__(self, key):
    4.17 +		assert key
    4.18 +		self._key = key
    4.19 +		self._fn = "_%s" % key
    4.20 +
    4.21 +	def load(self):
    4.22 +		ret = None
    4.23 +	
    4.24 +		try:
    4.25 +			with open(self._fn) as pin_f:
    4.26 +				ret = pin_f.read().strip()
    4.27 +		except IOError:
    4.28 +			pass
    4.29 +	
    4.30 +		return ret
    4.31 +	
    4.32 +	def check(self, cookies):
    4.33 +		if self._key not in cookies:
    4.34 +			raise PinFailError()
    4.35 +	
    4.36 +		pin = cookies[self._key][0]
    4.37 +		if pin != self.load():
    4.38 +			raise PinFailError()
    4.39 +	
    4.40 +		return pin