diff options
| author | Ben Finney <bignose@debian.org> | 2017-08-26 21:11:04 +1000 |
|---|---|---|
| committer | git-ubuntu importer <ubuntu-devel-discuss@lists.ubuntu.com> | 2017-11-09 22:40:30 +0000 |
| commit | a345d811962806ac04caeacb15d3ab6136b4820c (patch) | |
| tree | a1e69da5635e28fcd3db4ece238c65d4aabba581 | |
0.4.1-1 (patches unapplied)import/0.4.1-1ubuntu/eoan-develubuntu/eoanubuntu/disco-develubuntu/discoubuntu/cosmic-develubuntu/cosmicubuntu/bionic-proposedubuntu/bionic-develubuntu/bionicdebian/buster
Imported using git-ubuntu import.
Notes
Notes:
| -rw-r--r-- | PKG-INFO | 30 | ||||
| -rw-r--r-- | README.rst | 20 | ||||
| -rw-r--r-- | debian/README.source | 45 | ||||
| -rw-r--r-- | debian/changelog | 7 | ||||
| -rw-r--r-- | debian/compat | 1 | ||||
| -rw-r--r-- | debian/control | 45 | ||||
| -rw-r--r-- | debian/copyright | 50 | ||||
| -rw-r--r-- | debian/gbp.conf | 21 | ||||
| -rw-r--r-- | debian/patches/migrate-from-times-to-arrow.patch | 52 | ||||
| -rw-r--r-- | debian/patches/series | 1 | ||||
| -rwxr-xr-x | debian/rules | 59 | ||||
| -rw-r--r-- | debian/source/format | 1 | ||||
| -rw-r--r-- | debian/tests/control | 20 | ||||
| -rwxr-xr-x | debian/tests/smoke-python2 | 39 | ||||
| -rwxr-xr-x | debian/tests/smoke-python3 | 39 | ||||
| -rw-r--r-- | debian/tests/smoke_test.py | 129 | ||||
| -rw-r--r-- | debian/watch | 19 | ||||
| -rw-r--r-- | jsonext.egg-info/PKG-INFO | 30 | ||||
| -rw-r--r-- | jsonext.egg-info/SOURCES.txt | 9 | ||||
| -rw-r--r-- | jsonext.egg-info/dependency_links.txt | 1 | ||||
| -rw-r--r-- | jsonext.egg-info/requires.txt | 1 | ||||
| -rw-r--r-- | jsonext.egg-info/top_level.txt | 1 | ||||
| -rw-r--r-- | jsonext/__init__.py | 22 | ||||
| -rw-r--r-- | jsonext/mixins.py | 95 | ||||
| -rw-r--r-- | setup.cfg | 5 | ||||
| -rw-r--r-- | setup.py | 26 |
26 files changed, 768 insertions, 0 deletions
diff --git a/PKG-INFO b/PKG-INFO new file mode 100644 index 0000000..9fc7f68 --- /dev/null +++ b/PKG-INFO @@ -0,0 +1,30 @@ +Metadata-Version: 1.0 +Name: jsonext +Version: 0.4.1 +Summary: Well-structured helpers to help serializing commonly encountered structures to JSON (like datetime, to_dict(), etc. +Home-page: http://github.com/mbr/jsonext +Author: Marc Brinkmann +Author-email: git@marcbrinkmann.de +License: MIT +Description: jsonext + ======= + + .. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master + :target: https://travis-ci.org/mbr/jsonext + + + jsonext makes it easy to serialize objects outside of the standard Python + primitives to JSON:: + + >>> import jsonext + >>> from datetime import datetime + >>> jsonext.dumps(datetime.now()) + '"2014-03-22T22:17:18.304528+00:00"' + >>> jsonext.dumps(i**2 for i in range(10)) + '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' + + It uses mixins to the standard encoder to achieve this and is easily reuse- + and extensible. Check out the `documentation <http://pythonhosted + .org/jsonext>`_ for details. + +Platform: UNKNOWN diff --git a/README.rst b/README.rst new file mode 100644 index 0000000..4b099e8 --- /dev/null +++ b/README.rst @@ -0,0 +1,20 @@ +jsonext +======= + +.. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master + :target: https://travis-ci.org/mbr/jsonext + + +jsonext makes it easy to serialize objects outside of the standard Python +primitives to JSON:: + + >>> import jsonext + >>> from datetime import datetime + >>> jsonext.dumps(datetime.now()) + '"2014-03-22T22:17:18.304528+00:00"' + >>> jsonext.dumps(i**2 for i in range(10)) + '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' + +It uses mixins to the standard encoder to achieve this and is easily reuse- +and extensible. Check out the `documentation <http://pythonhosted +.org/jsonext>`_ for details. diff --git a/debian/README.source b/debian/README.source new file mode 100644 index 0000000..7bf3e5e --- /dev/null +++ b/debian/README.source @@ -0,0 +1,45 @@ +========================================== +Debian source package for ‘python-jsonext’ +========================================== + +Build source package from VCS +============================= + +The source for the Debian packaging is managed in the ‘master’ branch +of the Git repository. + +To build the source package from the Git VCS: + +* Get the upstream source:: + + $ debian/rules get-packaged-orig-source + + Ensure the upstream source is placed where Git-BuildPackage will + find it for building the Debian source package. + +* Finalise the latest release in the Debian changelog:: + + $ debchange --release "" + + This is needed temporarily to build the source package with Debian + packaging tools. + + While developing the package, do not commit to the VCS any Debian + changelog that has any content in the release trailer for the latest + entry. + + Until that release is ready to finalise, it has not been uploaded to + Debian and so does not have a correct distribution, uploader, or + release timestamp. The header and trailer for the entry should + remain open during development to reflect this. + +* Build the source package with Git-BuildPackage:: + + $ gbp buildpackage + + Git-BuildPackage is configured (via the ‘debian/gbp.conf’ file) to + build the source package by overlaying the Debian packaging files + onto the upstream source. + + + -- Ben Finney <bignose@debian.org>, Sat, 26 Aug 2017 13:45:03 +1000 diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..b4f4507 --- /dev/null +++ b/debian/changelog @@ -0,0 +1,7 @@ +python-jsonext (0.4.1-1) unstable; urgency=medium + + * The “علي محمد باقر النمر (Ali Mohammed Baqir al-Nimr)” release. + * Initial release. + Closes: bug#873285. + + -- Ben Finney <bignose@debian.org> Sat, 26 Aug 2017 21:11:04 +1000 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..f599e28 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +10 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..5fec2c8 --- /dev/null +++ b/debian/control @@ -0,0 +1,45 @@ +Source: python-jsonext +Maintainer: Ben Finney <bignose@debian.org> +Section: python +Priority: optional +Standards-Version: 4.0.1 +Build-Depends: + python3-pytest-runner, + python3-arrow, + python3-six, + python3-setuptools, + python3-all, + python-pytest-runner, + python-arrow, + python-six, + python-setuptools, + python-all (>= 2.7~), + dh-python, + debhelper (>= 10~) +Homepage: https://pypi.org/project/jsonext/ +VCS-Git: https://anonscm.debian.org/git/users/bignose/debian/pkg-python-jsonext.git +VCS-Browser: https://anonscm.debian.org/git/users/bignose/debian/pkg-python-jsonext.git +X-Python-Version: >= 2.7 +X-Python3-Version: >= 3.3 + +Package: python3-jsonext +Architecture: all +Depends: + ${python3:Depends}, + ${misc:Depends} +Description: serialisation to/from JSON for extra types — Python 3 + ‘jsonext’ makes easy JSON serialisation of Python objects outside of + the standard Python built-in-types. + . + This package installs the library for Python 3. + +Package: python-jsonext +Architecture: all +Depends: + ${python:Depends}, + ${misc:Depends} +Description: serialisation to/from JSON for extra types — Python 2 + ‘jsonext’ makes easy JSON serialisation of Python objects outside of + the standard Python built-in-types. + . + This package installs the library for Python 2. diff --git a/debian/copyright b/debian/copyright new file mode 100644 index 0000000..d40b7eb --- /dev/null +++ b/debian/copyright @@ -0,0 +1,50 @@ +Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/ +Upstream-Name: jsonext +Upstream-Contact: Marc Brinkmann <git@marcbrinkmann.de> +Source: https://pypi.org/project/jsonext/ + +Files: * +Copyright: + © 2014–2015 Marc Brinkmann <git@marcbrinkmann.de> +License: Expat + +Files: debian/* +Copyright: + © 2009–2017 Ben Finney <bignose@debian.org> +License: GPL-3+ +License-Grant: + This is free software; you may copy, modify and/or distribute this work + under the terms of the GNU General Public License, version 3 or later. + No warranty expressed or implied. + + +License: Expat + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + . + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + . + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + +License: GPL-3+ + On Debian systems, the complete text of the GNU General Public License + version 3 can be found in ‘/usr/share/common-licenses/GPL-3’. + + +# Local variables: +# coding: utf-8 +# mode: debian-copyright +# fill-column: 78 +# End: +# vim: fileencoding=utf-8 textwidth=78 : diff --git a/debian/gbp.conf b/debian/gbp.conf new file mode 100644 index 0000000..c96c7ac --- /dev/null +++ b/debian/gbp.conf @@ -0,0 +1,21 @@ +# debian/gbp.conf +# Git-buildpackage configuration for this Debian source package. +# Manpage: ‘gbp.conf(5)’. + +[DEFAULT] + +# Overlay the exported source onto original upstream source? +overlay = true + +# Store the pristine upstream source in this VCS repository? +pristine-tar = false + +# Create the upstream source from this VCS repository? +create-orig = false + + +# Local variables: +# coding: utf-8 +# mode: conf +# End: +# vim: fileencoding=utf-8 filetype=conf noexpandtabs : diff --git a/debian/patches/migrate-from-times-to-arrow.patch b/debian/patches/migrate-from-times-to-arrow.patch new file mode 100644 index 0000000..39641f7 --- /dev/null +++ b/debian/patches/migrate-from-times-to-arrow.patch @@ -0,0 +1,52 @@ +Description: Use arrow instead of times module. +Author: Marc Brinkmann <git@marcbrinkmann.de> +Last-Update: 2015-10-26 22:16:53 +1100 +Comment: + Git commit hash 043a9004902479729220e164c75e0512e371f992. + . + The ‘times’ library is not packaged for Debian; the ‘arrow’ + library is. This change is already incorporated upstream. + +--- old/jsonext/mixins.py ++++ new/jsonext/mixins.py +@@ -1,5 +1,5 @@ + import datetime +-import times ++import arrow + + + class JSONDateTimeMixin(object): +@@ -22,9 +22,10 @@ class JSONDateTimeMixin(object): + >>> jsonext.dumps(d) + '"2013-11-17"' + """ ++ + def default(self, o): + if isinstance(o, datetime.datetime): +- return times.format(o, 'Zulu') ++ return arrow.get(o).to('utc').isoformat() + if isinstance(o, datetime.date): + return o.isoformat() + return super(JSONDateTimeMixin, self).default(o) +--- old/setup.py ++++ new/setup.py +@@ -22,5 +22,4 @@ setup( + url='http://github.com/mbr/jsonext', + license='MIT', + packages=find_packages(exclude=['tests']), +- install_requires=['times'], +-) ++ install_requires=['arrow'], ) + + + + +Local variables: +coding: utf-8 +mode: diff +time-stamp-format: "%:y-%02m-%02d" +time-stamp-start: "^Last-Update:[ ]+" +time-stamp-end: "$" +time-stamp-line-limit: 20 +End: +vim: fileencoding=utf-8 filetype=diff : diff --git a/debian/patches/series b/debian/patches/series new file mode 100644 index 0000000..55e4ec5 --- /dev/null +++ b/debian/patches/series @@ -0,0 +1 @@ +migrate-from-times-to-arrow.patch diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..a3378ae --- /dev/null +++ b/debian/rules @@ -0,0 +1,59 @@ +#! /usr/bin/make -f + +# debian/rules +# Part of Debian ‘python-jsonext’ package. +# +# Copyright © 2009–2017 Ben Finney <bignose@debian.org> +# This is free software; you may copy, modify and/or distribute this work +# under the terms of the GNU General Public License, version 3 or later. +# No warranty expressed or implied. + +# Send HTTP traffic to the “discard” service during packaging actions. +export http_proxy = http://127.0.1.1:9/ +export https_proxy = ${http_proxy} + +export PYBUILD_NAME = jsonext + + +%: + dh $@ --with python3,python2 --buildsystem=pybuild + + +.PHONY: get-packaged-orig-source +get-packaged-orig-source: + $(uscan-download-source) --download-current-version + +# Policy §4.9 strongly recommends the ‘get-orig-source’ target: +# “This target is optional, but providing it if possible is a good idea.” +# +# This target is an anomaly: per Policy §4.9, it fetches the *latest* +# upstream source, regardless of this package's version. To fetch the +# upstream source corresponding to this package's *declared* version +# in ‘debian/changelog’, use ‘get-packaged-orig-source’ instead. +.PHONY: get-orig-source +get-orig-source: + $(uscan-download-source) + +get-orig-source get-packaged-orig-source: http_proxy = +get-orig-source get-packaged-orig-source: makefile_dir = $(abspath $(dir $(firstword ${MAKEFILE_LIST}))) +get-orig-source get-packaged-orig-source: package_dir = $(abspath $(dir ${makefile_dir})) + +define uscan-download-source + uscan --noconf --verbose \ + --force-download \ + --rename \ + --destdir=$(CURDIR) \ + --check-dirname-level=0 ${package_dir} +endef # uscan-download-source + + +override_dh_auto_clean: + dh_auto_clean + $(RM) -r ${GENERATED_FILES} + + +# Local variables: +# mode: makefile +# coding: utf-8 +# End: +# vim: filetype=make fileencoding=utf-8 : diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..163aaf8 --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (quilt) diff --git a/debian/tests/control b/debian/tests/control new file mode 100644 index 0000000..7cdae03 --- /dev/null +++ b/debian/tests/control @@ -0,0 +1,20 @@ +# debian/tests/control +# Control file for Debian ‘autopkgtests’. +# Documentation: ‘/usr/share/doc/autopkgtest/README.package-tests.rst.gz’. + +Tests: smoke-python2 +Depends: + python-pkg-resources, + python-jsonext + +Tests: smoke-python3 +Depends: + python3-pkg-resources, + python3-jsonext + + +# Local variables: +# coding: utf-8 +# mode: conf +# End: +# vim: fileencoding=utf-8 filetype=conf : diff --git a/debian/tests/smoke-python2 b/debian/tests/smoke-python2 new file mode 100755 index 0000000..72493bc --- /dev/null +++ b/debian/tests/smoke-python2 @@ -0,0 +1,39 @@ +#! /bin/bash +# +# debian/tests/smoke-python2 +# Part of Debian ‘python-jsonext’ package. +# +# Copyright © 2016–2017 Ben Finney <bignose@debian.org> +# This is free software; you may copy, modify and/or distribute this work +# under the terms of the GNU General Public License, version 3 or later. +# No warranty expressed or implied. +# +# Smoke test for package in Python 2 environments. + +set -o errexit +set -o errtrace +set -o nounset + +DISTRIBUTION_NAME=jsonext +MODULE_NAMES=( + jsonext + ) + +test_opts="--distribution=$DISTRIBUTION_NAME" +for mod in ${MODULE_NAMES[@]} ; do + # Accumulate the module names. + test_opts="$test_opts --module=$mod" +done + +for py in $(pyversions -i) ; do + printf "Python command: %s\n" $py + $py debian/tests/smoke_test.py $test_opts + printf "\n" +done + + +# Local variables: +# coding: utf-8 +# mode: shell-script +# End: +# vim: fileencoding=utf-8 filetype=sh : diff --git a/debian/tests/smoke-python3 b/debian/tests/smoke-python3 new file mode 100755 index 0000000..28929cc --- /dev/null +++ b/debian/tests/smoke-python3 @@ -0,0 +1,39 @@ +#! /bin/bash +# +# debian/tests/smoke-python3 +# Part of Debian ‘python-jsonext’ package. +# +# Copyright © 2016–2017 Ben Finney <bignose@debian.org> +# This is free software; you may copy, modify and/or distribute this work +# under the terms of the GNU General Public License, version 3 or later. +# No warranty expressed or implied. +# +# Smoke test for package in Python 3 environments. + +set -o errexit +set -o errtrace +set -o nounset + +DISTRIBUTION_NAME=jsonext +MODULE_NAMES=( + jsonext + ) + +test_opts="--distribution=$DISTRIBUTION_NAME" +for mod in ${MODULE_NAMES[@]} ; do + # Accumulate the module names. + test_opts="$test_opts --module=$mod" +done + +for py in $(py3versions -i) ; do + printf "Python command: %s\n" $py + $py debian/tests/smoke_test.py $test_opts + printf "\n" +done + + +# Local variables: +# coding: utf-8 +# mode: shell-script +# End: +# vim: fileencoding=utf-8 filetype=sh : diff --git a/debian/tests/smoke_test.py b/debian/tests/smoke_test.py new file mode 100644 index 0000000..a3e1e82 --- /dev/null +++ b/debian/tests/smoke_test.py @@ -0,0 +1,129 @@ +# -*- coding: utf-8 -*- +# +# debian/tests/smoke_test.py +# +# Copyright © 2016–2017 Ben Finney <bignose@debian.org> +# This is free software; you may copy, modify and/or distribute this work +# under the terms of the GNU General Public License, version 3 or later. +# No warranty expressed or implied. + +""" Post-install Python smoke test for use in Debian autopkgtest. + + Written for both Python 2 and Python 3, to test all installed + versions of a package. + + Smoke test the distribution:: + --distribution=DISTRIBUTION + + Smoke test one or more modules:: + --module=MODULE_FOO --module=MODULE_BAR --module=MODULE_BAZ + """ + +import sys +import argparse +import importlib +import pkg_resources + + +def emit_implementation(): + """ Emit the details of the current Python implementation. + + :return: ``None``. + """ + sys.stdout.write( + "Interpreter: {command}\n{version}\n".format( + command=sys.executable, version=sys.version)) + + +def emit_distribution(name): + """ Get the distribution `name` and emit its representation. + + :param name: Name of the distribution to retrieve. + :return: ``None``. + """ + distribution = pkg_resources.get_distribution(name) + sys.stdout.write( + "Distribution ‘{name}’:\n\t{distribution!r}\n".format( + name=name, distribution=distribution)) + + +def emit_module(name): + """ Import the module `name` and emit the module representation. + + :param name: Full name of the module to import. + :return: ``None``. + """ + module = importlib.import_module(name) + sys.stdout.write( + "Package ‘{name}’:\n\t{module!r}\n".format( + name=name, module=module)) + + +def suite(args): + """ Run the full suite of tests. + + :param args: Namespace of arguments parsed from `ArgumentParser`. + :return: ``None``. + """ + emit_implementation() + + if args.distribution_name: + emit_distribution(args.distribution_name) + + for module_name in args.module_names: + emit_module(module_name) + + +class SmokeTestArgumentParser(argparse.ArgumentParser): + """ Command-line argument parser for this program. """ + + def __init__(self, *args, **kwargs): + super(SmokeTestArgumentParser, self).__init__(*args, **kwargs) + + self.add_argument( + '--distribution', + dest='distribution_name', type=str, + metavar="DISTRIBUTION", help=( + "Test the Python distribution named DISTRIBUTION.")) + self.add_argument( + '--module', + dest='module_names', type=str, nargs='+', + metavar="MODULE", help=( + "Test the Python module named MODULE.")) + + +def main(argv=None): + """ Mainline code for this module. + + :param argv: Sequence of all command line arguments. + (Default: `sys.argv`) + :return: The exit status (integer) for exit from the process. + """ + exit_status = 0 + + if argv is None: + argv = sys.argv + + try: + program_name = argv[0] + parser = SmokeTestArgumentParser(prog=program_name) + args = parser.parse_args(argv[1:]) + + suite(args) + + except SystemExit as exc: + exit_status = exc.code + + return exit_status + + +if __name__ == "__main__": + exit_status = main(sys.argv) + sys.exit(exit_status) + + +# Local variables: +# coding: utf-8 +# mode: python +# End: +# vim: fileencoding=utf-8 filetype=python : diff --git a/debian/watch b/debian/watch new file mode 100644 index 0000000..7ee3ac8 --- /dev/null +++ b/debian/watch @@ -0,0 +1,19 @@ +# debian/watch +# Debian UScan configuration for ‘python-jsonext’ package. +# Manual page: ‘uscan(1)’. + +# Version of UScan configuration format. +version = 4 + +# Source tarball from Python Package Index. +opts = "uversionmangle=s/(\d)((?:a|b|c|rc)\d*)$/$1~$2/" \ + http://pypi.debian.net/jsonext/ \ + /jsonext/jsonext-@ANY_VERSION@@ARCHIVE_EXT@ \ + debian uupdate + + +# Local variables: +# coding: utf-8 +# mode: conf +# End: +# vim: fileencoding=utf-8 filetype=conf : diff --git a/jsonext.egg-info/PKG-INFO b/jsonext.egg-info/PKG-INFO new file mode 100644 index 0000000..9fc7f68 --- /dev/null +++ b/jsonext.egg-info/PKG-INFO @@ -0,0 +1,30 @@ +Metadata-Version: 1.0 +Name: jsonext +Version: 0.4.1 +Summary: Well-structured helpers to help serializing commonly encountered structures to JSON (like datetime, to_dict(), etc. +Home-page: http://github.com/mbr/jsonext +Author: Marc Brinkmann +Author-email: git@marcbrinkmann.de +License: MIT +Description: jsonext + ======= + + .. image:: https://travis-ci.org/mbr/jsonext.svg?branch=master + :target: https://travis-ci.org/mbr/jsonext + + + jsonext makes it easy to serialize objects outside of the standard Python + primitives to JSON:: + + >>> import jsonext + >>> from datetime import datetime + >>> jsonext.dumps(datetime.now()) + '"2014-03-22T22:17:18.304528+00:00"' + >>> jsonext.dumps(i**2 for i in range(10)) + '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' + + It uses mixins to the standard encoder to achieve this and is easily reuse- + and extensible. Check out the `documentation <http://pythonhosted + .org/jsonext>`_ for details. + +Platform: UNKNOWN diff --git a/jsonext.egg-info/SOURCES.txt b/jsonext.egg-info/SOURCES.txt new file mode 100644 index 0000000..1476aa2 --- /dev/null +++ b/jsonext.egg-info/SOURCES.txt @@ -0,0 +1,9 @@ +README.rst +setup.py +jsonext/__init__.py +jsonext/mixins.py +jsonext.egg-info/PKG-INFO +jsonext.egg-info/SOURCES.txt +jsonext.egg-info/dependency_links.txt +jsonext.egg-info/requires.txt +jsonext.egg-info/top_level.txt
\ No newline at end of file diff --git a/jsonext.egg-info/dependency_links.txt b/jsonext.egg-info/dependency_links.txt new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/jsonext.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/jsonext.egg-info/requires.txt b/jsonext.egg-info/requires.txt new file mode 100644 index 0000000..f89da44 --- /dev/null +++ b/jsonext.egg-info/requires.txt @@ -0,0 +1 @@ +times
\ No newline at end of file diff --git a/jsonext.egg-info/top_level.txt b/jsonext.egg-info/top_level.txt new file mode 100644 index 0000000..8ca97ef --- /dev/null +++ b/jsonext.egg-info/top_level.txt @@ -0,0 +1 @@ +jsonext diff --git a/jsonext/__init__.py b/jsonext/__init__.py new file mode 100644 index 0000000..ac23cd3 --- /dev/null +++ b/jsonext/__init__.py @@ -0,0 +1,22 @@ +"""JSON-rendering helpers. + +This module provides mixins for the stdlib :class:`json.JSONEncoder` class, +adding serialization methods for other object types, such as +:class:`~datetime.datetime` objects or iterables. + +All these are ready to use by using :data:`~jsonext.dumps`. +""" + +import functools +import json + +from .mixins import JSONDateTimeMixin, JSONIterableMixin, JSONToDictMixin, \ + JSONStringifyMixin + + +class JSONEncoder(JSONDateTimeMixin, JSONIterableMixin, JSONToDictMixin, + JSONStringifyMixin, json.JSONEncoder): + pass + + +dumps = functools.partial(json.dumps, cls=JSONEncoder) diff --git a/jsonext/mixins.py b/jsonext/mixins.py new file mode 100644 index 0000000..987c4e2 --- /dev/null +++ b/jsonext/mixins.py @@ -0,0 +1,95 @@ +import datetime +import times + + +class JSONDateTimeMixin(object): + """A mixin for JSONEncoders, encoding :class:`datetime.datetime` and + :class:`datetime.date` objects by converting them to strings that can be + parsed by all modern browsers JS Date() object. + + All timestamps are converted to UTC before being serialized. + + Date objects simply use :meth:`~datetime.date.isoformat`. + + >>> import jsonext + >>> from datetime import datetime + >>> dt = datetime(2013, 11, 17, 12, 00, 00) # Python 3.3.3 release! + >>> jsonext.dumps(dt) + '"2013-11-17T12:00:00+00:00"' + >>> d = dt.date() + >>> d + datetime.date(2013, 11, 17) + >>> jsonext.dumps(d) + '"2013-11-17"' + """ + def default(self, o): + if isinstance(o, datetime.datetime): + return times.format(o, 'Zulu') + if isinstance(o, datetime.date): + return o.isoformat() + return super(JSONDateTimeMixin, self).default(o) + + +class JSONIterableMixin(object): + """A mixin for JSONEncoders, encoding any iterable type by converting it to + a list. + + Especially useful for SQLAlchemy results that look a lot like regular lists + or iterators, but will trip up the encoder. Beware of infinite + generators. + + >>> import jsonext + >>> gen = (i**2 for i in range(10)) + >>> jsonext.dumps(gen) + '[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]' + """ + def default(self, o): + try: + iterable = iter(o) + except TypeError: + pass + else: + return list(iterable) + return super(JSONIterableMixin, self).default(o) + + +class JSONToDictMixin(object): + """A mixin for JSONEncoders, encoding any object with a to_dict() method + by calling that method and encoding the return value. + + >>> import jsonext + >>> class Foo(object): + ... def __init__(self, a, b): + ... self.a = a + ... self.b = b + ... def to_dict(self): + ... return {'A': self.a, 'B': self.b} + ... + >>> items = [Foo(1,2), Foo(3,4)] + >>> jsonext.dumps(items) + '[{"A": 1, "B": 2}, {"A": 3, "B": 4}]' + """ + def default(self, o): + if hasattr(o, 'to_dict'): + return o.to_dict() + return super(JSONToDictMixin, self).default(o) + + +class JSONStringifyMixin(object): + """A mixing for JSONEncoders, encoding any object that has a ``__str__`` + method with the return value of said function. + + >>> import jsonext + >>> from decimal import Decimal as D + >>> x = D('123.456') + >>> jsonext.dumps(x) + '"123.456"' + >>> from datetime import timedelta + >>> t = timedelta(days=5, seconds=12345) + >>> jsonext.dumps(t) + '"5 days, 3:25:45"' + """ + def default(self, o): + if hasattr(o, '__str__'): + return str(o) + return super(JSONStringifyMixin, self).default(o) diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..861a9f5 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,5 @@ +[egg_info] +tag_build = +tag_date = 0 +tag_svn_revision = 0 + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..4d8c5c7 --- /dev/null +++ b/setup.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import os + +from setuptools import setup, find_packages + + +def read(fname): + return open(os.path.join(os.path.dirname(__file__), fname)).read() + + +setup( + name='jsonext', + version='0.4.1', + description='Well-structured helpers to help serializing commonly ' + 'encountered structures to JSON (like datetime, to_dict(), ' + ' etc.', + long_description=read('README.rst'), + author='Marc Brinkmann', + author_email='git@marcbrinkmann.de', + url='http://github.com/mbr/jsonext', + license='MIT', + packages=find_packages(exclude=['tests']), + install_requires=['times'], +) |
