1. Janto Dreijer
  2. scikits_index

Commits

Janto Dreijer  committed 1919c0b

memchache variation

  • Participants
  • Parent commits 6869a68
  • Branches default

Comments (0)

Files changed (4)

File code/scikits.py

View file
 			n = memcache.get(key)
 			if n is None:
 				n = 0
-			memcache.set(key, (n+1) % len(packages))
-			n %= len(packages) # in case a kit is removed from repo
+			memcache.set(key, (n+1) % len(packages)) # set the next package to be fetched
+			n %= len(packages) # in case a kit is removed
 			package = packages[n]
 			self.logger.info("forcing fetch of package: %s" % package.name)
 			package.info(force_fetch=True)
 					if package_name in packages:
 						continue
 
-					repo_url = ""
+					repo_url = "" #XXX where can we get this?
 					package = Package(name=package_name, repo_url=repo_url)
 					packages[package.name] = package
 
 	template = PageTemplate.all().filter("name =", name).get()
 	if template is not None:
 		return template.text
-	return getattr(templates, name+"_template")
+	return getattr(templates, name+"_template").strip()
 
 class PageTemplate(db.Model):
 	name = db.StringProperty(required=True)
 		user = users.get_current_user()
 		self.write("<p>")
 		if not user:
-			self.write('only site admins allowed here.')
+			self.write('only site admins allowed here.\n')
 			self.write('<a href="%s">sign in</a>' % users.create_login_url("/admin"))
 			self.print_footer()
 			return
 		if not users.is_current_user_admin():
-			self.write('only site admin allowed here.')
+			self.write('only site admin allowed here.\n')
 			self.write('<a href="%s">sign out</a>.' % users.create_logout_url("/admin"))
 			self.print_footer()
 			return
-		self.write("welcome %s. " % user.nickname())
+		self.write("welcome %s.\n" % user.nickname())
 		self.write('<a href="%s">sign out</a>.' % users.create_logout_url("/admin"))
 		self.write("</p>")
 
 			if address:
 				mail.send_mail(sender="jantod@gmail.com",
 					to=address,
-					subject="backup %s" % t,
-					body="""Here's the backup""",
-					attachments=[("templates.py.rss", collect_templates())] #XXX google keeps escaping the <> chars! arg!
+					subject="scikits index backup %s" % t,
+					body="""Here's the backup. GAE escaped the html tag chars. Sorry.""",
+					attachments=[("templates.py", collect_templates())] #XXX GAE keeps escaping the <> chars! arg!
 				)
 				self.write("sent backup to %s at %s" % (address, t))
 		self.write("""
 			template = PageTemplate.all().filter("name =", template_name).get()
 			if template:
 				template_text = htmlquote(template.text)
-				modified_time = template.modified
-				modified_username = template.username
+				modified_time = "modified %s," % template.modified
+				modified_username = "by %s" % template.username
 			else:
 				template_text = htmlquote(get_template(template_name))
-				modified_time = None
-				modified_username = None
+				modified_time = "never modified,"
+				modified_username = "loading from <i>template.py</i>"
 
 			self.write("""
 <h2>%(template_name)s</h2>
 <input type="hidden" name="template_name" value="%(template_name)s">
 <br />
 <input type="submit" value="Save" />
-modified %(modified_time)s by %(modified_username)s
+%(modified_time)s %(modified_username)s
 </form>
 </p>
 			""" % locals())

File code/templates.py

View file
 
 footer_template = """
 
-</script>
-
 </div> <!-- section -->
 </div> <!-- body -->
 </div> <!-- bodywrapper -->

File code/tools.py

View file
 	sink = Sink()
 	return rdfxml.parseRDF(s, base=None, sink=sink).result
 
+class Cache(object):
+
+	"""
+	memcache that only notifies when object expires.
+	"""
+
+	@classmethod
+	def get(self, key):
+		result = memcache.get(key)
+		if result is None:
+			return None, True
+		value, timeout = result
+		expired = False
+		if timeout is not None and timeout < time.time():
+			expired = True
+		return value, expired
+
+	@classmethod
+	def set(self, key, value, duration=None):
+		timeout = (time.time()+duration) if duration is not None else None
+		return memcache.set(key=key, value=(value, timeout))
+
+#~ def get_url(url, force_fetch=False):
+	#~ result = memcache.get(url)
+	#~ if result is None or force_fetch:
+		#~ logger.debug("fetching %s" % url)
+		#~ result = urlfetch.fetch(url)
+		#~ assert memcache.set(key=url, value=result, time=FETCH_CACHE_AGE), url
+	#~ else:
+		#~ logger.debug("cache hit for %s" % url)
+	#~ return result
+
 def get_url(url, force_fetch=False):
-	result = memcache.get(url)
-	if result is None or force_fetch:
+	response, expired = Cache.get(url)
+	if expired or force_fetch:
 		logger.debug("fetching %s" % url)
-		result = urlfetch.fetch(url)
-		assert memcache.set(key=url, value=result, time=FETCH_CACHE_AGE), url
+		try:
+			response = urlfetch.fetch(url)
+		except: # failed
+			if response is not None: # got a value in the past
+				logger.warn("returning old value for %s" % url)
+			else:
+				raise
+		else:
+			assert Cache.set(key=url, value=response, duration=FETCH_CACHE_AGE), url
 	else:
 		logger.debug("cache hit for %s" % url)
-	return result
+
+	return response
 
 class GoogleXMLRPCTransport(object):
 	"""Handles an HTTP transaction to an XML-RPC server."""

File misc/urlfetch_stub.py

View file
         try:
           socket.setdefaulttimeout(_API_CALL_DEADLINE)
           full_path = protocol + "://" + host + "/" + full_path
-          print full_path
           connection.request(method, full_path, payload, adjusted_headers)
           http_response = connection.getresponse()
           http_response_data = http_response.read()