/usr/lib/python3/dist-packages/piston_mini_client/serializers.py is in python3-piston-mini-client 0.7.5-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | # -*- coding: utf-8 -*-
# Copyright 2010-2012 Canonical Ltd. This software is licensed under the
# GNU Lesser General Public License version 3 (see the file LICENSE).
"""Classes that define ways for your API methods to serialize arguments
into a request."""
__all__ = [
'JSONSerializer',
'FormSerializer',
]
import json
try:
from urllib import urlencode
except ImportError:
# Python 3
from urllib.parse import urlencode
from piston_mini_client import PistonSerializable
class JSONSerializer(object):
"""A serializer that renders JSON.
This is the default serializer for content type *application/json*.
"""
class PistonSerializableEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, PistonSerializable):
return o.as_serializable()
return json.JSONEncoder.default(self, o)
def serialize(self, obj):
"""Serialize ``obj`` into JSON.
As well as the usual basic JSON-encodable types, this serializer knows
how to serialize ``PistonSerializable`` objects.
"""
return json.dumps(obj, cls=self.PistonSerializableEncoder)
class FormSerializer(object):
"""A serializer that renders form-urlencoded content.
This is the default serializer for content type
*application/x-www-form-urlencoded*.
.. note:: this serializer doesn't support nested structures.
It should be initialized with a dictionary, sequence of pairs, or
``PistonSerializable``.
"""
def serialize(self, obj):
if isinstance(obj, PistonSerializable):
obj = obj.as_serializable()
try:
return urlencode(obj)
except TypeError:
raise TypeError("Attempted to serialize invalid object %s" % obj)
serializers = {
'application/json': JSONSerializer(),
'application/x-www-form-urlencoded': FormSerializer(),
}
def get_serializer(content_type):
return serializers.get(content_type)
|