def _create_sessionid(self): rand = os.urandom(16) now = time.time() secret_key = self.config.secret_key session_id = sha1("%s%s%s%s" % (rand, now, safestr(self.remote_ip), secret_key)) session_id = session_id.hexdigest() return str(session_id).upper() + '|' + self.config.session_version
def _key_to_file(self, key): """ Convert the filename into an md5 string. We'll turn the first couple bits of the path into directory prefixes to be nice to filesystems that have problems with large numbers of files in a directory. Thus, a cache key of "foo" gets turnned into a file named ``{cache-dir}ac/bd/18db4cc2f85cedef654fccc4a4d8``. """ path = hashlib.md5(safestr(key)).hexdigest() path = os.path.join(path[:2], path[2:4], path[4:]) return os.path.join(self._dir, path)
def query(self, paramstyle=None): """ Returns the query part of the sql query. >>> q = SQLQuery(["SELECT * FROM test WHERE name=", SQLParam('joe')]) >>> q.query() 'SELECT * FROM test WHERE name=%s' >>> q.query(paramstyle='qmark') 'SELECT * FROM test WHERE name=?' """ s = [] for x in self.items: if isinstance(x, SQLParam): x = x.get_marker(paramstyle) s.append(safestr(x)) else: x = safestr(x) # automatically escape % characters in the query # For backward compatability, ignore escaping when the query looks already escaped if paramstyle in ['format', 'pyformat']: if '%' in x and '%%' not in x: x = x.replace('%', '%%') s.append(x) return "".join(s)
def make_key(self, key, version=None): # Python 2 memcache requires the key to be a byte string. return safestr(super(BaseMemcachedCache, self).make_key(key, version))
def unpickle(self, value): """ Unpickles the given value. """ value = safestr(value) return pickle.loads(value)
def __str__(self): return safestr(self._str())