Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,138 changes: 541 additions & 597 deletions BeautifulSoup.py

Large diffs are not rendered by default.

47 changes: 19 additions & 28 deletions CustomCookie.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ def _quote(str, LegalChars=_LegalChars,
# the string in doublequotes and precede quote (with a \)
# special characters.
#
if "" == translate(str, idmap, LegalChars):
if translate(str, idmap, LegalChars) == "":
return str
else:
return '"' + _nulljoin( map(_Translator.get, str, str) ) + '"'
return f'"{_nulljoin(map(_Translator.get, str, str))}"'
Comment on lines -320 to +323
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _quote refactored with the following changes:

# end _quote


Expand Down Expand Up @@ -359,12 +359,10 @@ def _unquote(str):
if Omatch: j = Omatch.start(0)
if Qmatch: k = Qmatch.start(0)
if Qmatch and ( not Omatch or k < j ): # QuotePatt matched
res.append(str[i:k])
res.append(str[k+1])
res.extend((str[i:k], str[k+1]))
i = k+2
else: # OctalPatt matched
res.append(str[i:j])
res.append( chr( int(str[j+1:j+4], 8) ) )
else: # OctalPatt matched
res.extend((str[i:j], chr( int(str[j+1:j+4], 8) )))
Comment on lines -362 to +365
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function _unquote refactored with the following changes:

i = j+4
return _nulljoin(res)
# end _unquote
Expand Down Expand Up @@ -437,8 +435,8 @@ def __init__(self):

def __setitem__(self, K, V):
K = K.lower()
if not K in self._reserved:
raise CookieError("Invalid Attribute %s" % K)
if K not in self._reserved:
raise CookieError(f"Invalid Attribute {K}")
Comment on lines -440 to +439
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Morsel.__setitem__ refactored with the following changes:

dict.__setitem__(self, K, V)
# end __setitem__

Expand All @@ -452,9 +450,9 @@ def set(self, key, val, coded_val,
# First we verify that the key isn't a reserved word
# Second we make sure it only contains legal characters
if key.lower() in self._reserved:
raise CookieError("Attempt to set a reserved key: %s" % key)
if "" != translate(key, idmap, LegalChars):
raise CookieError("Illegal key value: %s" % key)
raise CookieError(f"Attempt to set a reserved key: {key}")
if translate(key, idmap, LegalChars) != "":
raise CookieError(f"Illegal key value: {key}")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Morsel.set refactored with the following changes:


# It's a good key, so save it.
self.key = key
Expand All @@ -463,13 +461,12 @@ def set(self, key, val, coded_val,
# end set

def output(self, attrs=None, header = "Set-Cookie:"):
return "%s %s" % ( header, self.OutputString(attrs) )
return f"{header} {self.OutputString(attrs)}"
Comment on lines -466 to +464
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Morsel.output refactored with the following changes:


__str__ = output

def __repr__(self):
return '<%s: %s=%s>' % (self.__class__.__name__,
self.key, repr(self.value) )
return f'<{self.__class__.__name__}: {self.key}={repr(self.value)}>'
Comment on lines -471 to +469
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Morsel.__repr__ refactored with the following changes:


def js_output(self, attrs=None):
# Print javascript
Expand All @@ -489,7 +486,7 @@ def OutputString(self, attrs=None):
RA = result.append

# First, the key=value pair
RA("%s=%s" % (self.key, self.coded_value))
RA(f"{self.key}={self.coded_value}")
Comment on lines -492 to +489
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Morsel.OutputString refactored with the following changes:


# Now add any defined attributes
if attrs is None:
Expand All @@ -500,15 +497,15 @@ def OutputString(self, attrs=None):
if V == "": continue
if K not in attrs: continue
if K == "expires" and type(V) == type(1):
RA("%s=%s" % (self._reserved[K], _getdate(V)))
RA(f"{self._reserved[K]}={_getdate(V)}")
elif K == "max-age" and type(V) == type(1):
RA("%s=%d" % (self._reserved[K], V))
elif K == "secure":
RA(str(self._reserved[K]))
elif K == "httponly":
RA(str(self._reserved[K]))
else:
RA("%s=%s" % (self._reserved[K], V))
RA(f"{self._reserved[K]}={V}")

# Return the result
return _semispacejoin(result)
Expand Down Expand Up @@ -589,31 +586,25 @@ def __setitem__(self, key, value):

def output(self, attrs=None, header="Set-Cookie:", sep="\015\012"):
"""Return a string suitable for HTTP."""
result = []
items = self.items()
items.sort()
for K,V in items:
result.append( V.output(attrs, header) )
result = [V.output(attrs, header) for K, V in items]
Comment on lines -592 to +591
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseCookie.output refactored with the following changes:

return sep.join(result)
# end output

__str__ = output

def __repr__(self):
L = []
items = self.items()
items.sort()
for K,V in items:
L.append( '%s=%s' % (K,repr(V.value) ) )
return '<%s: %s>' % (self.__class__.__name__, _spacejoin(L))
L = [f'{K}={repr(V.value)}' for K, V in items]
return f'<{self.__class__.__name__}: {_spacejoin(L)}>'
Comment on lines -603 to +601
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseCookie.__repr__ refactored with the following changes:


def js_output(self, attrs=None):
"""Return a string suitable for JavaScript."""
result = []
items = self.items()
items.sort()
for K,V in items:
result.append( V.js_output(attrs) )
result = [V.js_output(attrs) for K, V in items]
Comment on lines -612 to +607
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function BaseCookie.js_output refactored with the following changes:

return _nulljoin(result)
# end js_output

Expand Down
37 changes: 13 additions & 24 deletions appengine_utilities/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,20 @@ def _clean_cache(self):
# result.delete()

def _validate_key(self, key):
if key == None:
if key is None:
raise KeyError

def _validate_value(self, value):
if value == None:
if value is None:
raise ValueError

def _validate_timeout(self, timeout):
if timeout == None:
if timeout is None:
timeout = datetime.datetime.now() +\
datetime.timedelta(seconds=DEFAULT_TIMEOUT)
datetime.timedelta(seconds=DEFAULT_TIMEOUT)
if type(timeout) == type(1):
timeout = datetime.datetime.now() + \
datetime.timedelta(seconds = timeout)
datetime.timedelta(seconds = timeout)
if type(timeout) != datetime.datetime:
raise TypeError
if timeout < datetime.datetime.now():
Expand Down Expand Up @@ -145,7 +145,7 @@ def add(self, key = None, value = None, timeout = None):
pass

memcache_timeout = timeout - datetime.datetime.now()
memcache.set('cache-'+key, value, int(memcache_timeout.seconds))
memcache.set(f'cache-{key}', value, int(memcache_timeout.seconds))

if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheAdded')
Expand All @@ -172,7 +172,7 @@ def set(self, key = None, value = None, timeout = None):
pass

memcache_timeout = timeout - datetime.datetime.now()
memcache.set('cache-'+key, value, int(memcache_timeout.seconds))
memcache.set(f'cache-{key}', value, int(memcache_timeout.seconds))

if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheSet')
Expand All @@ -189,22 +189,14 @@ def _read(self, key = None):
query.filter('cachekey', key)
query.filter('timeout > ', datetime.datetime.now())
results = query.fetch(1)
if len(results) is 0:
return None
return results[0]

if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheReadFromDatastore')
if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheRead')
return None if len(results) is 0 else results[0]

def delete(self, key = None):
"""
Deletes a cache object determined by the key.
"""
memcache.delete('cache-'+key)
result = self._read(key)
if result:
memcache.delete(f'cache-{key}')
if result := self._read(key):
if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheDeleted')
result.delete()
Expand All @@ -213,19 +205,16 @@ def get(self, key):
"""
get is used to return the cache value associated with the key passed.
"""
mc = memcache.get('cache-'+key)
if mc:
if mc := memcache.get(f'cache-{key}'):
if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheReadFromMemcache')
if 'AEU_Events' in __main__.__dict__:
__main__.AEU_Events.fire_event('cacheRead')
return mc
result = self._read(key)
if result:
if result := self._read(key):
timeout = result.timeout - datetime.datetime.now()
# print timeout.seconds
memcache.set('cache-'+key, pickle.loads(result.value),
int(timeout.seconds))
memcache.set(f'cache-{key}', pickle.loads(result.value), int(timeout.seconds))
return pickle.loads(result.value)
else:
raise KeyError
Expand Down
Loading