30a64c67c1
- Add PEP8 section to tox.ini - Add hacking to requirements to enforce OpenStack style requirements - Fix formatting issues flagged by flake8 check - Add copyright notices to all remaining files - Update .gitignore file Change-Id: I6f6f97ca1291000acee7e925826bf853b2cc8744
51 lines
1.7 KiB
Python
51 lines
1.7 KiB
Python
# Copyright (c) 2014 Dark Secret Software Inc.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
# implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
import datetime
|
|
import json
|
|
import timex
|
|
|
|
|
|
class ObjectEncoder(json.JSONEncoder):
|
|
ENCODE_MAP = {datetime.datetime: "datetime",
|
|
timex.TimeRange: "timex.TimeRange",
|
|
timex.Timestamp: "timex.Timestamp"}
|
|
|
|
def default(self, obj):
|
|
if type(obj) in self.ENCODE_MAP:
|
|
typename = self.ENCODE_MAP[type(obj)]
|
|
encoder = getattr(self,
|
|
'_encode_' + typename.replace('.', '_').lower())
|
|
return encoder(obj, typename)
|
|
return super(ObjectEncoder, self).default(obj)
|
|
|
|
def _encode_datetime(self, obj, name):
|
|
return {'__type__': name,
|
|
'datetime': obj.isoformat()}
|
|
|
|
def _encode_timex_timestamp(self, obj, name):
|
|
return {'__type__': name,
|
|
'timestamp': obj.timestamp.isoformat()}
|
|
|
|
def _encode_timex_timerange(self, obj, name):
|
|
return {'__type__': name,
|
|
'begin': obj.begin.isoformat(),
|
|
'end': obj.end.isoformat()}
|
|
|
|
|
|
def dumps(obj, **kw):
|
|
kw['cls'] = ObjectEncoder
|
|
return json.dumps(obj, **kw)
|