85d77310ac93835a9487aa0645922727d5dc5903
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2013 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
19 import lxml
.etree
as ET
20 from werkzeug
.exceptions
import MethodNotAllowed
22 from mediagoblin
.tools
.response
import Response
25 _log
= logging
.getLogger(__name__
)
28 class 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
)
34 def fill_element_xml(self
, el
):
36 n
= ET
.SubElement(el
, self
.item_name
)
37 if isinstance(it
, dict):
38 _fill_element_dict(n
, it
, self
.as_attrib
)
43 def _fill_element_dict(el
, data
, as_attr
=()):
44 for k
, v
in data
.iteritems():
46 if not isinstance(v
, basestring
):
50 n
= ET
.SubElement(el
, k
)
54 def _fill_element(el
, data
):
55 if isinstance(data
, bool):
60 elif isinstance(data
, basestring
):
62 elif isinstance(data
, int):
64 elif isinstance(data
, dict):
65 _fill_element_dict(el
, data
)
66 elif isinstance(data
, PwgNamedArray
):
67 data
.fill_element_xml(el
)
69 _log
.warn("Can't convert to xml: %r", data
)
72 def response_xml(result
):
75 _fill_element(r
, result
)
76 return Response(ET
.tostring(r
, encoding
="utf-8", xml_declaration
=True),
80 class CmdTable(object):
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
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
)
94 def find_func(cls
, request
):
95 if request
.method
== "GET":
96 cmd_name
= request
.args
.get("method")
98 cmd_name
= request
.form
.get("method")
99 entry
= cls
._cmd
_table
.get(cmd_name
)
102 _log
.debug("Found method %s", cmd_name
)
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()