Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

marrow.templating common templating/serialization API addition. #1

Closed
wants to merge 3 commits into from
Closed
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
63 changes: 55 additions & 8 deletions itsdangerous.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,19 +202,20 @@ def load_payload(self, payload):
"""Loads the encoded object. This implementation uses simplejson."""
return simplejson.loads(payload)

def dump_payload(self, obj):
def dump_payload(self, obj, **kw):
"""Dumps the encoded object into a bytestring. This implementation
uses simplejson.
uses simplejson. Additional keyword arguments are passed to
simplejson.
"""
return simplejson.dumps(obj)
return simplejson.dumps(obj, **kw)

def make_signer(self):
"""A method that creates a new instance of the signer to be used.
The default implementation uses the :class:`Signer` baseclass.
"""
return Signer(self.secret_key, self.salt)

def dumps(self, obj):
def dumps(self, obj, **kw):
"""Returns URL-safe, sha1 signed base64 compressed JSON string.

If compress is True (the default) checks if compressing using zlib can
Expand All @@ -223,12 +224,17 @@ def dumps(self, obj):

Salt can be used to further salt the hash, in case you're worried
that the NSA might try to brute-force your SHA-1 protected secret.

Additional keyword arguments are passed to dump_payload, then on to
simplejson.
"""
return self.make_signer().sign(self.dump_payload(obj))
return self.make_signer().sign(self.dump_payload(obj, **kw))

def dump(self, obj, f):
"""Like :meth:`dumps` but dumps into a file."""
f.write(self.dumps(obj))
def dump(self, obj, f, **kw):
"""Like :meth:`dumps` but dumps into a file.

Additional keyword arguments are passed along to simplejson."""
f.write(self.dumps(obj, **kw))

def loads(self, s):
"""Reverse of :meth:`dumps`, raises :exc:`BadSignature` if the
Expand Down Expand Up @@ -298,3 +304,44 @@ class URLSafeTimedSerializer(URLSafeSerializerMixin, TimedSerializer):
safe string consisting of the upper and lowercase character of the
alphabet as well as ``'_'``, ``'-'`` and ``'.'``.
"""


def marrow_render(data, template=None, content_type='text/plain', i18n=None, key=None, salt='itsdangerous', timed=False, safe=False, **kw):
"""Serialization API adapter for ``marrow.templating``.

Expected arguments:

key: the secret key to use

Optional arguments:

salt: hash salting value
timed: utilize the TimedSerializer if ``True``
safe: utilize the URLSafeSerializerMixin if ``True``

Additional keyword arguments are passed to the underlying serializer.

Sample usage:

>>> from marrow.templating.core import Engines
>>> render = Engines()
>>> render.dangerous(dict(hello="world"), key="test")
('text/plain', '{"hello": "world"}.i7oUbyl26mYKMMfaU-yNLWGSInc')

With additional options:

>>> from marrow.templating.core import Engines
>>> Engines().dangerous(dict(hello="world"), key="test", salt="foo", timed=True, safe=True)
('text/plain', 'eyJoZWxsbyI6IndvcmxkIn0.5hp-.AGgqxapfM5TXZ4bPYkD75usBakE')

"""

if key is None:
raise ValueError('You must specify a private key')

serializers = [[Serializer, URLSafeSerializer],
[TimedSerializer, URLSafeTimedSerializer]]

serializer = serializers[int(timed)][int(safe)](key, salt)

return content_type, serializer.dumps(data, **kw)
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
classifiers=[
'License :: OSI Approved :: BSD License',
'Programming Language :: Python'
]
],
entry_points={
'marrow.templating': [
'dangerous = itsdangerous:marrow_render'
]
}
)