|
Package Dropbox ::
Package web2py ::
Package gluon ::
Module serializers
|
|
1 """
2 This file is part of the web2py Web Framework
3 Copyrighted by Massimo Di Pierro <mdipierro@cs.depaul.edu>
4 License: LGPLv3 (http://www.gnu.org/licenses/lgpl.html)
5 """
6 import datetime
7 import decimal
8 from storage import Storage
9 from html import TAG, XmlComponent
10 from html import xmlescape
11 from languages import lazyT
12 import contrib.rss2 as rss2
13
14 try:
15 import json as json_parser
16 except ImportError:
17 try:
18 import simplejson as json_parser
19 except:
20 import contrib.simplejson as json_parser
21
22
24 if hasattr(o, 'custom_json') and callable(o.custom_json):
25 return o.custom_json()
26 if isinstance(o, (datetime.date,
27 datetime.datetime,
28 datetime.time)):
29 return o.isoformat()[:19].replace('T', ' ')
30 elif isinstance(o, (int, long)):
31 return int(o)
32 elif isinstance(o, decimal.Decimal):
33 return str(o)
34 elif isinstance(o, lazyT):
35 return str(o)
36 elif isinstance(o, XmlComponent):
37 return str(o)
38 elif hasattr(o, 'as_list') and callable(o.as_list):
39 return o.as_list()
40 elif hasattr(o, 'as_dict') and callable(o.as_dict):
41 return o.as_dict()
42 else:
43 raise TypeError(repr(o) + " is not JSON serializable")
44
45
46 -def xml_rec(value, key, quote=True):
47 if hasattr(value, 'custom_xml') and callable(value.custom_xml):
48 return value.custom_xml()
49 elif isinstance(value, (dict, Storage)):
50 return TAG[key](*[TAG[k](xml_rec(v, '', quote))
51 for k, v in value.items()])
52 elif isinstance(value, list):
53 return TAG[key](*[TAG.item(xml_rec(item, '', quote)) for item in value])
54 elif hasattr(value, 'as_list') and callable(value.as_list):
55 return str(xml_rec(value.as_list(), '', quote))
56 elif hasattr(value, 'as_dict') and callable(value.as_dict):
57 return str(xml_rec(value.as_dict(), '', quote))
58 else:
59 return xmlescape(value, quote)
60
61
62 -def xml(value, encoding='UTF-8', key='document', quote=True):
63 return ('<?xml version="1.0" encoding="%s"?>' % encoding) + str(xml_rec(value, key, quote))
64
65
67 return json_parser.dumps(value, default=default)
68
69
72
73
74 -def ics(events, title=None, link=None, timeshift=0, **ignored):
75 import datetime
76 title = title or '(unkown)'
77 if link and not callable(link):
78 link = lambda item, prefix=link: prefix.replace(
79 '[id]', str(item['id']))
80 s = 'BEGIN:VCALENDAR'
81 s += '\nVERSION:2.0'
82 s += '\nX-WR-CALNAME:%s' % title
83 s += '\nSUMMARY:%s' % title
84 s += '\nPRODID:Generated by web2py'
85 s += '\nCALSCALE:GREGORIAN'
86 s += '\nMETHOD:PUBLISH'
87 for item in events:
88 s += '\nBEGIN:VEVENT'
89 s += '\nUID:%s' % item['id']
90 if link:
91 s += '\nURL:%s' % link(item)
92 shift = datetime.timedelta(seconds=3600 * timeshift)
93 start = item['start_datetime'] + shift
94 stop = item['stop_datetime'] + shift
95 s += '\nDTSTART:%s' % start.strftime('%Y%m%dT%H%M%S')
96 s += '\nDTEND:%s' % stop.strftime('%Y%m%dT%H%M%S')
97 s += '\nSUMMARY:%s' % item['title']
98 s += '\nEND:VEVENT'
99 s += '\nEND:VCALENDAR'
100 return s
101
102
104 if not 'entries' in feed and 'items' in feed:
105 feed['entries'] = feed['items']
106 now = datetime.datetime.now()
107 rss = rss2.RSS2(title=str(feed.get('title', '(notitle)')),
108 link=str(feed.get('link', None)),
109 description=str(feed.get('description', '')),
110 lastBuildDate=feed.get('created_on', now),
111 items=[rss2.RSSItem(
112 title=str(entry.get('title', '(notitle)')),
113 link=str(entry.get('link', None)),
114 description=str(entry.get('description', '')),
115 pubDate=entry.get('created_on', now)
116 ) for entry in feed.get('entries', [])])
117 return rss.to_xml(encoding='utf-8')
118