def execute(self, chain, request, env={}): if "payload" in env: marshaller = Converters.marshaller_for(request.headers.get("Content-type")) env["body"] = marshaller.marshal(env["payload"]) del (env["payload"]) return chain.follow(request, env)
def execute(self, chain, request, env={}): if "payload" in env: content_type = request.headers.get("Content-Type") marshaller = Converters.marshaller_for(content_type) env["body"] = marshaller.marshal(env["payload"]) del (env["payload"]) return chain.follow(request, env)
def resource(self): if 'content-type' in self.response[0]: contenttype = self.response[0]['content-type'].split(';')[0] else: contenttype = None converter = Converters.marshaller_for(contenttype) return converter.unmarshal(self.body)
def test_single_style(self): html = "<div style='background-color: blue;'></div>" expected_html = '<div class="newClass1">\n</div>' expected_css = '.newClass1 {\n\tbackground-color:blue,\n}\n\n' cssClasses, newHtml = Converters().convertStyleToClass(html) self.assertEqual(newHtml, expected_html) self.assertEqual(cssClasses, expected_css)
def resource(self): if "content-type" in self.response[0]: contenttype = self.response[0]["content-type"].split(";")[0] else: contenttype = None converter = Converters.marshaller_for(contenttype) return converter.unmarshal(self.body)
def test_duplicate_unordered_classes(self): html = "<div style='text-align: center; background-color: blue;'><div style='background-color: blue; text-align: center;'></div></div>" expected_html = '<div class="newClass1">\n <div class="newClass1">\n </div>\n</div>' expected_css = '.newClass1 {\n\tbackground-color:blue,\n\ttext-align:center,\n}\n\n' cssClasses, newHtml = Converters().convertStyleToClass(html) self.assertEqual(newHtml, expected_html) self.assertEqual(cssClasses, expected_css)
def resource(self): """ Returns the unmarshalled object of the response body """ if 'content-type' in self.response[0]: contenttype = self.response[0]['content-type'].split(';')[0] else: contenttype = None converter = Converters.marshaller_for(contenttype) return converter.unmarshal(self.body)
from lxml import objectify from converters import Converters class LXMLConverter(object): def marshal(self, dictionary): '''from dictionary''' output = "" for key, value in dictionary.iteritems(): output += "<%s>" % key if isinstance(value, dict): output += self.marshal(value) else: output += str(value) output += "</%s>" % key return output def unmarshal(self, xml_content): '''to object''' BlankSlate = type('object', (object,), {}) result = BlankSlate() xml = objectify.fromstring(xml_content) child_tag = xml.iterchildren().next().tag setattr(result, xml.tag, getattr(xml, child_tag)) return result Converters.register("application/xml", XmlConverter())
class American(Converter[Base]): date: str = Converters.from_datetime("date", "%m/%d/%y %H:%M") start: str = Converters.from_datetime("range.start", "%m/%d/%y %H:%M") end: str = Converters.from_datetime("range.end", "%m/%d/%y %H:%M")
class International(Converter[Base]): date: str = Converters.from_datetime("date", "%d/%m/%y %H:%M") start: str = Converters.from_datetime("range.start", "%d/%m/%y %H:%M") end: str = Converters.from_datetime("range.end", "%d/%m/%y %H:%M")