home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / modules / cookie.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  1.7 KB  |  51 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: cookie.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. import Cookie, time, calendar
  11.  
  12. __doc__ = """Cookie module gives users full control over browser cookie
  13. functionality. """
  14.  
  15. class cookie(spyceModule):
  16.   def start(self):
  17.     self._cookie = None
  18.   def get(self, key=None):
  19.     "Get brower cookie(s)"
  20.     if self._cookie == None:
  21.       self._cookie = {}
  22.       cookie = Cookie.SimpleCookie(self._api.getModule('request').env('HTTP_COOKIE'))
  23.       for c in cookie.keys():
  24.         self._cookie[c] = cookie[c].value
  25.     if key:
  26.       if self._cookie.has_key(key):
  27.         return self._cookie[key]
  28.     else: return self._cookie
  29.   def __getitem__(self, key):
  30.     "Get browser cookie(s)"
  31.     return self.get(key)
  32.   def set(self, key, value, expire=None, domain=None, path=None, secure=0):
  33.     "Set browser cookie"
  34.     if value==None:  # delete (set to expire one week ago)
  35.       return self.set(key, '', -60*60*24*7, domain, path, secure)
  36.     text = '%s=%s' % (key, value)
  37.     if expire != None: text = text + ';EXPIRES=%s' % time.strftime(
  38.       '%a, %d-%b-%y %H:%M:%S GMT',
  39.       time.gmtime(time.time()+expire))
  40.     if domain: text = text + ';DOMAIN=%s' % domain
  41.     if path: text = text + ';PATH=%s' % path
  42.     if secure: text = text + ';SECURE'
  43.     self._api.getModule('response').addHeader('Set-Cookie', text)
  44.   def delete(self, key):
  45.     "Delete browser cookie"
  46.     self.set(key, None)
  47.   def __delitem__(self, key):
  48.     "Delete browser cookie"
  49.     return self.delete(self, key)
  50.  
  51.