piwigo start at pwg.images.addChunk.
[mediagoblin.git] / mediagoblin / plugins / piwigo / tools.py
CommitLineData
4234fffa
E
1# GNU MediaGoblin -- federated, autonomous media hosting
2# Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17import logging
18
19import lxml.etree as ET
20from werkzeug.exceptions import MethodNotAllowed
21
22from mediagoblin.tools.response import Response
23
24
25_log = logging.getLogger(__name__)
26
27
28class PwgNamedArray(list):
29 def __init__(self, l, item_name, as_attrib=()):
30 self.item_name = item_name
31 self.as_attrib = as_attrib
32 list.__init__(self, l)
33
34 def fill_element_xml(self, el):
35 for it in self:
36 n = ET.SubElement(el, self.item_name)
37 if isinstance(it, dict):
38 _fill_element_dict(n, it, self.as_attrib)
39 else:
40 _fill_element(n, it)
41
42
43def _fill_element_dict(el, data, as_attr=()):
1330abf7 44 for k, v in data.iteritems():
4234fffa
E
45 if k in as_attr:
46 if not isinstance(v, basestring):
47 v = str(v)
48 el.set(k, v)
49 else:
50 n = ET.SubElement(el, k)
51 _fill_element(n, v)
52
53
54def _fill_element(el, data):
55 if isinstance(data, bool):
56 if data:
57 el.text = "1"
58 else:
59 el.text = "0"
60 elif isinstance(data, basestring):
61 el.text = data
62 elif isinstance(data, int):
63 el.text = str(data)
64 elif isinstance(data, dict):
65 _fill_element_dict(el, data)
66 elif isinstance(data, PwgNamedArray):
67 data.fill_element_xml(el)
68 else:
69 _log.warn("Can't convert to xml: %r", data)
70
71
72def response_xml(result):
73 r = ET.Element("rsp")
74 r.set("stat", "ok")
75 _fill_element(r, result)
76 return Response(ET.tostring(r, encoding="utf-8", xml_declaration=True),
77 mimetype='text/xml')
78
79
80class CmdTable(object):
81 _cmd_table = {}
82
83 def __init__(self, cmd_name, only_post=False):
84 assert not cmd_name in self._cmd_table
85 self.cmd_name = cmd_name
86 self.only_post = only_post
87
88 def __call__(self, to_be_wrapped):
89 assert not self.cmd_name in self._cmd_table
90 self._cmd_table[self.cmd_name] = (to_be_wrapped, self.only_post)
91 return to_be_wrapped
92
93 @classmethod
94 def find_func(cls, request):
95 if request.method == "GET":
96 cmd_name = request.args.get("method")
97 else:
98 cmd_name = request.form.get("method")
99 entry = cls._cmd_table.get(cmd_name)
100 if not entry:
101 return entry
cf0816c1 102 _log.debug("Found method %s", cmd_name)
4234fffa
E
103 func, only_post = entry
104 if only_post and request.method != "POST":
105 _log.warn("Method %s only allowed for POST", cmd_name)
106 raise MethodNotAllowed()
107 return func