Exemplo n.º 1
0
    def _loader(self, element):
        root = ElementTree(element).getroot()
        self.document.clear()
        cur = self.document_editor.textCursor()
        for child in root.getchildren():
            form = getattr(self.document_editor, 'format_{}'.format(child.tag), None)

            cur.insertText(child.text+'\n', form)

        return to_html_string(element)
Exemplo n.º 2
0
 def _parse_resource(self, sequence, group):
     if group is None:
         try:
             group = self._groups[None]
         except KeyError:
             group = self._groups[None] = ResourceGroup(self, None, None)
     rdef = Resource.parse_definition(sequence[0].text)
     desc, index = parse_description(
         sequence, 1, self._next_header_tag(sequence[0].tag), "ul")
     if len(sequence) <= index:
         if entities.report_warnings:
             sys.stderr.write("Skipping empty resource %s\n" % rdef[0])
         return
     desc_sections = False
     if sequence[index].tag in ("ul", "ol"):
         sections = []
         for s in sequence[index]:
             section = self._parse_section(None, s, rdef[0])
             if section is not None:
                 sections.append(section)
             else:
                 if desc is None:
                     desc = "<ul>\n"
                 desc += to_html_string(s) + "\n"
                 desc_sections = True
         index += 1
     else:
         sections = tuple()
     if desc_sections:
         desc += "</ul>"
     rdef += (desc,)
     kwargs = {s: None for s in Resource.NESTED_SECTIONS}
     kwargs.update({s.NESTED_SECTION_ID: s for s in sections})
     action_instead_of_resource = False
     try:
         r = Resource(group, *rdef, **kwargs)
     except TypeError as e:
         action_instead_of_resource = True
         r = Resource(group, *rdef, parameters=None, attributes=None,
                      model=None)
         if entities.report_warnings:
             sys.stderr.write("Invalid section in resource %s: %s\n" %
                              (r, e))
     else:
         if r.model is not None and r.name is not None:
             self._models[r.name] = r.model
     group._resources[r.id] = r
     if r.attributes is not None and r.name is not None:
         self._attributes[r.name] = r.attributes
     if len(sequence) <= index:
         if action_instead_of_resource:
             try:
                 act, _ = Action.parse_from_etree(r, sequence, 0)
                 act._name = r.name
                 act._request_method = r.request_method
                 act._uri_template = r.uri_template
                 r._actions[act.id] = act
                 if entities.report_warnings:
                     sys.stderr.write(
                         "Assumed single implicit action in %s\n" % r)
             except:
                 pass
         return
     while index < len(sequence) and self._is_header(sequence[index]):
         action, index = Action.parse_from_etree(r, sequence, index)
         if action.uri_template is None:
             action._uri_template = r.uri_template
         if action.request_method is None:
             action._request_method = r.request_method
         for rr in chain(action.requests.values(),
                         chain.from_iterable(action.responses.values())):
             if rr._reference is None:
                 continue
             if rr._reference not in self._models:
                 if entities.report_warnings:
                     sys.stderr.write("Bad reference: %s\n" %
                                      rr._reference)
             else:
                 rr._copy_from_payload(self._models[rr._reference])
         r._actions[action.id] = action
Exemplo n.º 3
0
 def _parse_resource(self, sequence, group):
     if group is None:
         try:
             group = self._groups[None]
         except KeyError:
             group = self._groups[None] = ResourceGroup(self, None, None)
     rdef = Resource.parse_definition(sequence[0].text)
     desc, index = parse_description(
         sequence, 1, self._next_header_tag(sequence[0].tag), "ul")
     if len(sequence) <= index:
         if entities.report_warnings:
             sys.stderr.write("Skipping empty resource %s\n" % rdef[0])
         return
     desc_sections = False
     if sequence[index].tag in ("ul", "ol"):
         sections = []
         for s in sequence[index]:
             section = self._parse_section(None, s, rdef[0])
             if section is not None:
                 sections.append(section)
             else:
                 if desc is None:
                     desc = "<ul>\n"
                 desc += to_html_string(s) + "\n"
                 desc_sections = True
         index += 1
     else:
         sections = tuple()
     if desc_sections:
         desc += "</ul>"
     rdef += (desc,)
     kwargs = {s: None for s in Resource.NESTED_SECTIONS}
     kwargs.update({s.NESTED_SECTION_ID: s for s in sections})
     action_instead_of_resource = False
     try:
         r = Resource(group, *rdef, **kwargs)
     except TypeError as e:
         action_instead_of_resource = True
         r = Resource(group, *rdef, parameters=None, attributes=None,
                      model=None)
         if entities.report_warnings:
             sys.stderr.write("Invalid section in resource %s: %s\n" %
                              (r, e))
     else:
         if r.model is not None and r.name is not None:
             self._models[r.name] = r.model
     group._resources[r.id] = r
     if r.attributes is not None and r.name is not None:
         self._attributes[r.name] = r.attributes
     if len(sequence) <= index:
         if action_instead_of_resource:
             try:
                 act, _ = Action.parse_from_etree(r, sequence, 0)
                 act._name = r.name
                 act._request_method = r.request_method
                 act._uri_template = r.uri_template
                 r._actions[act.id] = act
                 if entities.report_warnings:
                     sys.stderr.write(
                         "Assumed single implicit action in %s\n" % r)
             except:
                 pass
         return
     while index < len(sequence) and self._is_header(sequence[index]):
         action, index = Action.parse_from_etree(r, sequence, index)
         if action.uri_template is None:
             action._uri_template = r.uri_template
         if action.request_method is None:
             action._request_method = r.request_method
         for rr in chain(action.requests.values(),
                         chain.from_iterable(action.responses.values())):
             if rr._reference is None:
                 continue
             if rr._reference not in self._models:
                 if entities.report_warnings:
                     sys.stderr.write("Bad reference: %s\n" %
                                      rr._reference)
             else:
                 rr._copy_from_payload(self._models[rr._reference])
         r._actions[action.id] = action