Пример #1
0
    def _to_decimal(self, value_txt, style_id):
        """Convert a given numeric value_text using the named style.

        TODO: From the style, get the number of decimal places, use that to
        build a string version of the float value.
        """
        fdp_attr = dom.QName(self.NUMBERS_NS["sf"], 'format-decimal-places')
        fs_attr = dom.QName(self.NUMBERS_NS["sf"], 'format-string')
        cell_style = self.cell_style.get(style_id)
        #print( "TO_DECIMAL", value_txt, style_id, "=", cell_style )

        fs = None  # cell_style.get(fs_attr) # Doesn't seem correct
        fdp = None  # cell_style.get(fdp_attr) # Doesn't seem correct

        # Transform fs into proper Python format, otherwise, use the number of
        # decimal places.

        if fs is not None:
            fmt = self._rewrite_fmt(fs)
            #print( "Decimal: {{0:{0}}}.format({1}) = ".format( fmt, value_txt ), end="" )
            value = decimal.Decimal("{:{fmt}}".format(float(value_txt),
                                                      fmt=fmt))
            #print( value )
            return value
        elif fdp is not None:
            #fmt= "{{0:.{0}f}}".format(fdp)
            value = decimal.Decimal("{:.{fdp}f}".format(float(value_txt),
                                                        fdp=fdp))
            #print( "Decimal: {0}.format({1}) = {2!r}".format( fmt, value_txt, value ) )
            return value
        else:
            value = decimal.Decimal(value_txt)
            #print( "Decimal: {0} = {1!r}".format( value_txt, value ) )
        return value
Пример #2
0
    def _get_styles(self, root):
        """Get the styles."""
        ID_attr = dom.QName(self.NUMBERS_NS["sfa"], 'ID')
        ident_attr = dom.QName(self.NUMBERS_NS["sf"], 'ident')
        parent_ident_attr = dom.QName(self.NUMBERS_NS["sf"], 'parent-ident')

        self.cell_style = {}
        for cs in root.findall('.//sf:cell-style', namespaces=self.NUMBERS_NS):
            #print( "STYLE", dom.tostring(cs) )
            ID = cs.get(ID_attr)
            ident = cs.get(ident_attr)
            parent_ident = cs.get(parent_ident_attr)
            property_number_format = cs.find(
                './/sf:SFTCellStylePropertyNumberFormat',
                namespaces=self.NUMBERS_NS)
            if property_number_format is None:
                if parent_ident is not None:
                    self.cell_style[ID] = self.cell_style[parent_ident]
            else:
                number_format = property_number_format.find(
                    'sf:number-format', namespaces=self.NUMBERS_NS)
                if number_format is None:
                    if parent_ident is not None:
                        self.cell_style[ID] = self.cell_style[parent_ident]
                else:
                    self.cell_style[ID] = number_format.attrib
                    if ident is not None:
                        self.cell_style[ident] = number_format.attrib
Пример #3
0
 def _decode_number(self, cell):
     """Decode a <n> tag's value, applying the style."""
     s_attr = dom.QName(self.NUMBERS_NS["sf"], 's')
     v_attr = dom.QName(self.NUMBERS_NS["sf"], 'v')
     s = cell.get(s_attr)
     cell_style = self.cell_style.get(s)
     try:
         value_txt = cell.attrib[v_attr]
         value = self._to_decimal(value_txt, s)
     except KeyError as ex:
         #self._cell_warning("Number with no value", cell)
         value = self._to_decimal('0', s)
     return value
Пример #4
0
    def rows_of(self, sheet):
        """Iterator over rows as a list of Cells for a named worksheet."""
        # 1. Map user name to member.
        rId = self.name_to_id[sheet.name]
        self.sheet_member_name = self.id_to_member[rId]
        # 2. Open member.
        sheet_zip = self.zip_archive.getinfo("xl/" + self.sheet_member_name)
        self.row = {}
        # 3. Assemble each row, allowing for missing cells.
        row_tag = dom.QName(self.XLSX_NS['main'], "row")
        cell_tag = dom.QName(self.XLSX_NS['main'], "c")
        value_tag = dom.QName(self.XLSX_NS['main'], "v")
        format_tag = dom.QName(self.XLSX_NS['main'], "f")

        for event, element in dom.iterparse(self.zip_archive.open(sheet_zip),
                                            events=('start', 'end')):
            logging.debug(element.tag, repr(element.text))
            if event == 'end' and element.tag == row_tag:
                # End of row: fill in missing cells
                if self.row.keys():
                    data = stingray.sheet.Row(
                        sheet,
                        *(self.row.get(i, stingray.cell.EmptyCell('', self))
                          for i in range(max(self.row.keys()) + 1)))
                    yield data
                else:
                    yield stingray.sheet.Row(sheet)
                self.row = {}
                element.clear()
            elif event == 'end' and element.tag == cell_tag:
                # End of cell: consolidate the final string
                self.row[self.row_col[1]] = self.value
                self.value = stingray.cell.EmptyCell('', self)
            elif event == 'start' and element.tag == cell_tag:
                # Start of cell: collect a string in pieces.
                self.cell_type = element.attrib.get('t', None)
                self.cell_id = element.attrib['r']
                id_match = self.cell_id_pat.match(self.cell_id)
                self.row_col = self.make_row_col(id_match.groups())
                self.value = stingray.cell.EmptyCell('', self)
            elif event == 'end' and element.tag == value_tag:
                # End of a value; what type was it?
                self.value = self.cell(element)

            elif event == 'end' and element.tag == format_tag:
                pass  # A format string
            else:
                pass
                logging.debug("Ignoring",
                              end="")  # Numerous bits of structure exposed.
                logging.debug(dom.tostring(element))
Пример #5
0
    def _locate_sheets(self):
        """Locate the name to id mapping and the id to member mapping.
        """
        # 1a. Open "workbook.xml" member.
        workbook_zip = self.zip_archive.getinfo("xl/workbook.xml")
        workbook_doc = dom.parse(self.zip_archive.open(workbook_zip))
        # 1b. Get a dict of sheet names and their rIdx values.
        key_attr_id = 'name'
        val_attr_id = dom.QName(self.XLSX_NS['r'], 'id')
        self.name_to_id = dict(
            (s.attrib[key_attr_id], s.attrib[val_attr_id])
            for s in workbook_doc.findall("*/main:sheet",
                                          namespaces=self.XLSX_NS))
        logging.debug(self.name_to_id)

        # 2a. Open the "_rels/workbook.xml.rels" member
        rels_zip = self.zip_archive.getinfo("xl/_rels/workbook.xml.rels")
        rels_doc = dom.parse(self.zip_archive.open(rels_zip))
        # 2b. Get a dict of rIdx to Target member name
        logging.debug(dom.tostring(rels_doc.getroot()))
        key_attr_id = 'Id'
        val_attr_id = 'Target'
        self.id_to_member = dict(
            (r.attrib[key_attr_id], r.attrib[val_attr_id])
            for r in rels_doc.findall("rel:Relationship",
                                      namespaces=self.XLSX_NS))
        logging.debug(self.id_to_member)
Пример #6
0
class TestNegativeParserTest(TestTestFromElement):
    tag = ElementTree.QName(TEST, 'NegativeParserTest')

    def test_is_negative_parser_test(self):
        self.assert_(isinstance(self.test, NegativeParserTest))

    def test_has_uri(self):
        self.assertEqual(self.test.uri, TESTS['rdf-charmod-literals/Manifest.rdf#error001'])

    def test_has_type(self):
        self.assertEqual(self.test.type, TEST.NegativeParserTest)

    def test_has_status(self):
        self.assertEqual(self.test.status, 'WITHDRAWN')

    def test_has_description(self):
        self.assertTextEqual(self.test.description,
            " Does the treatment of literals conform to charmod ? "
            " Test for failure for literal not in Normal Form C "
            " This test case has been WITHDRAWN in light of changes to NFC handling in concepts ")

    def test_has_input_document(self):
        self.assertEqual(self.test.input_document,
                         Document(TEST['RDF-XML-Document'],
                                  TESTS['rdf-charmod-literals/error001.rdf']))

    def test_warning_is_none(self):
        self.assertEqual(self.test.warning, None)
Пример #7
0
class TestPositiveParserTest(TestTestFromElement):
    tag = ElementTree.QName(TEST, 'PositiveParserTest')

    def test_is_positive_parser_test(self):
        self.assert_(isinstance(self.test, PositiveParserTest))

    def test_has_uri(self):
        self.assertEqual(self.test.uri,
                         TESTS['amp-in-url/Manifest.rdf#test001'])

    def test_has_type(self):
        self.assertEqual(self.test.type, TEST.PositiveParserTest)

    def test_has_status(self):
        self.assertEqual(self.test.status, 'APPROVED')

    def test_description_is_none(self):
        self.assertEqual(self.test.description, None)

    def test_has_input_documents(self):
        self.assertEqual(set(self.test.input_documents),
                         {Document(TEST['RDF-XML-Document'],
                                   TESTS['amp-in-url/test001.rdf'])})

    def test_has_output_document(self):
        self.assertEqual(self.test.output_document,
                         Document(TEST['NT-Document'],
                                  TESTS['amp-in-url/test001.nt']))

    def test_warning_is_none(self):
        self.assertEqual(self.test.warning, None)
Пример #8
0
class TestMiscellaneousTest(TestTestFromElement):
    tag = ElementTree.QName(TEST, 'MiscellaneousTest')

    def test_has_uri(self):
        self.assertEqual(self.test.uri,
            TESTS['rdfms-uri-substructure/Manifest.rdf#error001'])

    def test_has_type(self):
        self.assertEqual(self.test.type, TEST.MiscellaneousTest)

    def test_has_status(self):
        self.assertEqual(self.test.status, 'APPROVED') 

    def test_has_description(self):
        self.assertTextEqual(self.test.description,
            " An RDF/XML serlializer is recommended to produce an exception if "
            " asked to serialize the following graph since there is no way "
            " to represent it in the RDF/XML syntax. ")

    def test_has_warning(self):
        self.assertTextEqual(self.test.warning,
            " An RDF/XML serlializer is recommended to produce an exception if "
            " asked to serialize the following graph since there is no way "
            " to represent it in the RDF/XML syntax. ")

    def test_has_documents(self):
        self.assertEqual(set(self.test.documents),
            {Document(TEST['NT-Document'],
                      TESTS['rdfms-uri-substructure/error001.nt'])})
Пример #9
0
    def to_xml(self, parent, name):
        """
            Function to convert to xml from python representation.

            This is basic function and it is suitable for complex types.
            Primitive types must overload it.

            Parameters
            ----------
            parent : etree.Element
                Parent xml element to append this child to.
            name : str
                Full qualified (with namespace) name of this element.
        """
        # this level element
        element = etree.SubElement(parent, name)

        # add all children to the current level
        # note that children include also base classes, as they are propagated by
        # the metaclass below
        for child in self._children:
            child_name = child["name"]
            full_child_name = child["fullname"]
            # get the value of the argument
            val = getattr(self, child_name, None)

            # do constraints checking
            n = 0  # number of values for constraints checking
            if hasattr(val, "__iter__") and val.__class__.__name__ != "str":
                n = len(val)
            elif val is not None:
                n = 1
                val = [
                    val,
                ]

            if n < child["min"]:
                raise ValueError("Number of values for %s is less than "
                                 "min_occurs: %s" % (name, str(val)))
            if child["max"].__class__.__name__ == "int" and n > child["max"]:
                raise ValueError(
                    "Number of values for %s is more than max_occurs: %s" %
                    (name, str(val)))

            if n == 0:
                continue  # only nillables can get so far

            # conversion
            for single in val:
                if not (hasattr(single, "to_xml")):
                    single = child['type'](single)
                single.to_xml(element, full_child_name)
                if child["type"] is XMLAny or \
                        (isinstance(single, XMLType) and type(single) != child["type"]):
                    # append type information
                    element[-1].set(
                        "{%s}type" % xmlnamespace.NS_XSI,
                        etree.QName(
                            "{%s}%s" %
                            (single._namespace, single.__class__.__name__)))
Пример #10
0
 def __init__(self, form):
     self.form = form
     self.request_url = self._process_request()
     self.cache_filename = self._make_filename()
     # Get strings for the namespace-qualified tags
     if self.xmlns:
         for key, val in self.tags.items():
             self.tags[key] = etree.QName(self.xmlns, val).text
Пример #11
0
 def _decode_text(self, cell):
     """Decode a <t> tag's value."""
     sfa_s_attr = dom.QName(self.NUMBERS_NS["sfa"], 's')
     ct = cell.find('sf:ct', namespaces=self.NUMBERS_NS)
     value = ct.get(sfa_s_attr)
     if value is None:
         value = "\n".join(cell.itertext())
     return value
Пример #12
0
 def cell(self, cell_doc):
     logging.debug(dom.tostring(cell_doc))
     value_attr_id = dom.QName(self.ODS_NS['office'], 'value')
     date_attr_id = dom.QName(self.ODS_NS['office'], 'date-value')
     type_attr_id = dom.QName(self.ODS_NS['office'], 'value-type')
     # Get the type
     try:
         type_name = cell_doc.attrib[type_attr_id]
     except KeyError:
         return stingray.cell.EmptyCell('', self)
     value = None
     # Date value as attribute?
     if not value:
         try:
             value = cell_doc.attrib[date_attr_id]
         except KeyError:
             pass
     # Other value as attribute?
     if not value:
         try:
             value = cell_doc.attrib[value_attr_id]
         except KeyError:
             pass
     # No value attributes, get *all* the text content.
     if not value:
         value = "".join(x for x in cell_doc.itertext())
     if not value:
         # TODO: Proper warning.
         dom.dump(cell_doc)
     logging.debug(type_name, repr(value))
     if type_name == "string":
         return stingray.cell.TextCell(value, self)
     elif type_name == "float":
         return stingray.cell.NumberCell(float(value), self)
     elif type_name == "date":
         theDate = datetime.datetime.strptime(value,
                                              ODS_Workbook.date_format)
         return stingray.cell.FloatDateCell(theDate, self)
     elif type_name == "boolean":
         return stingray.cell.BooleanCell(float(value.upper() == 'TRUE'),
                                          self)
     elif type_name == "empty":
         return stingray.cell.EmptyCell('', self)
     else:
         raise Exception("Unknown cell {0}".format(dom.tostring(cell_doc)))
Пример #13
0
    def rows_of(self, sheet):
        """Iterator over rows.

        Two parallel traversals:

        Internal iterator over grid/datasource/* has d, t, n, pm, g, o and s
            yields individual cell values.

        Iterator over grid/rows/grid-row may have ``nc``, number of columns in that row.
            Each grid-row fetches a number of cell values to assemble a row.
            Row's may be variable length (sigh) but padded to the number of columns
            specified in the grid.
          
        :param sheet: a Sheet object to retrieve rows from.
        """
        self.log.debug("rows of {0}: {1}".format(sheet, sheet.name))
        ws_name, t_name = sheet.name
        ws, tables = self.workspace[ws_name]
        tabular_model = tables[t_name]

        grid = tabular_model.find('sf:grid', namespaces=self.NUMBERS_NS)
        numrows_attr = dom.QName(self.NUMBERS_NS["sf"], 'numrows')
        numcols_attr = dom.QName(self.NUMBERS_NS["sf"], 'numcols')
        numrows = int(grid.attrib[numrows_attr])
        numcols = int(grid.attrib[numcols_attr])

        nc_attr = dom.QName(self.NUMBERS_NS["sf"], 'nc')

        datasource = iter(self._datasource(grid))

        rows = grid.find('sf:rows', namespaces=self.NUMBERS_NS)
        for n, r in enumerate(
                rows.findall('sf:grid-row', namespaces=self.NUMBERS_NS)):
            #print( "ROW", dom.tostring(r) )
            self.debug_row = n
            # Is this really relevant for Numbers '09?
            nc = int(r.get(nc_attr, numcols))
            try:
                row = [next(datasource) for self.debug_col in range(nc)]
            except StopIteration as e:
                pass  # Last row will exhaust the datasource.
            if len(row) == numcols:
                yield row
            else:
                yield row + (numcols - nc) * [None]
Пример #14
0
    def import_metadata(self, elements, gpx_version=None):
        """Import information from GPX metadata

        :type elements: :class:`ET.Element`
        :param elements: GPX metadata subtree
        :type gpx_version: ``str``
        :param gpx_version: Specific GPX version entities to import

        """
        if gpx_version:
            try:
                accepted_gpx = {gpx_version: GPX_VERSIONS[gpx_version]}
            except KeyError:
                raise KeyError("Unknown GPX version `%s'" % gpx_version)
        else:
            accepted_gpx = GPX_VERSIONS

        for version, namespace in accepted_gpx.items():
            logging.info("Searching for GPX v%s entries" % version)
            metadata_elem = lambda name: ET.QName(namespace, name)

            for child in elements.getchildren():
                tag_ns, tag_name = child.tag[1:].split("}")
                if not tag_ns == namespace:
                    continue
                if tag_name in ("name", "desc", "keywords"):
                    setattr(self, tag_name, child.text)
                elif tag_name == "time":
                    self.time = utils.Timestamp.parse_isoformat(child.text)
                elif tag_name == "author":
                    self.author["name"] = child.findtext(metadata_elem("name"))
                    aemail = child.find(metadata_elem("email"))
                    if aemail:
                        self.author["email"] = "%s@%s" % (aemail.get("id"),
                                                          aemail.get("domain"))
                    self.author["link"] = child.findtext(metadata_elem("link"))
                elif tag_name == "bounds":
                    self.bounds = {
                        "minlat": child.get("minlat"),
                        "maxlat": child.get("maxlat"),
                        "minlon": child.get("minlon"),
                        "maxlon": child.get("maxlon"),
                    }
                elif tag_name == "extensions":
                    self.extensions = child.getchildren()
                elif tag_name == "copyright":
                    if child.get("author"):
                        self.copyright["name"] = child.get("author")
                    self.copyright["year"] = child.findtext(metadata_elem("year"))
                    self.copyright["license"] = child.findtext(metadata_elem("license"))
                elif tag_name == "link":
                    link = {
                        "href": link.get("href"),
                        "type": child.findtext(metadata_elem("type")),
                        "text": child.findtext(metadata_elem("text")),
                    }
                    self.link.append(link)
Пример #15
0
 def _get_shared_strings(self):
     """Build ``strings_dict`` with all shared strings.
     """
     self.strings_dict = defaultdict(str)
     count = 0
     text_tag = dom.QName(self.XLSX_NS['main'], "t")
     string_tag = dom.QName(self.XLSX_NS['main'], "si")
     # 1. Open the "xl/sharedStrings.xml" member
     sharedStrings_zip = self.zip_archive.getinfo("xl/sharedStrings.xml")
     for event, element in dom.iterparse(
             self.zip_archive.open(sharedStrings_zip), events=('end', )):
         logging.debug(event, element.tag)
         if element.tag == text_tag:
             self.strings_dict[count] += element.text
         elif element.tag == string_tag:
             count += 1
         element.clear()
     logging.debug(self.strings_dict)
Пример #16
0
 def _locate_sheets(self):
     """Create ``tables`` map from name to table."""
     workbook_zip = self.zip_archive.getinfo("content.xml")
     workbook_doc = dom.parse(self.zip_archive.open(workbook_zip))
     name_attr_id = dom.QName(self.ODS_NS["table"], "name")
     logging.debug(dom.tostring(workbook_doc.getroot()))
     self.tables = dict((t.attrib[name_attr_id], t)
                        for t in workbook_doc.findall(
                            "office:body/office:spreadsheet/table:table",
                            namespaces=self.ODS_NS))
Пример #17
0
 def getSchema():
     import xml.etree.cElementTree as ET
     xsd = ET.Element(mbsim.XS + 'sequence')
     xsd.append(
         ET.Element(
             mbsim.XS + 'element', {
                 'name': "stiffness",
                 'type': ET.QName(mbsim.PV + "stiffnessScalar")
             }))
     return xsd
Пример #18
0
    def _locate_sheets(self, root):
        """Create ``workspace_table`` map from name to workspace and table."""
        self.workspace = dict()

        ws_name_attr = dom.QName(self.NUMBERS_NS["ls"], 'workspace-name')
        name_attr = dom.QName(self.NUMBERS_NS["sf"], 'name')
        workspace_array = root.find("ls:workspace-array",
                                    namespaces=self.NUMBERS_NS)
        for workspace in workspace_array.findall('.//ls:workspace',
                                                 namespaces=self.NUMBERS_NS):
            # Populate tables within this workspace.
            tables = dict()
            page_info = workspace.find('ls:page-info',
                                       namespaces=self.NUMBERS_NS)
            for tabular_info in page_info.findall('.//sf:tabular-info',
                                                  namespaces=self.NUMBERS_NS):
                tabular_model = tabular_info.find('sf:tabular-model',
                                                  namespaces=self.NUMBERS_NS)
                tables[tabular_model.get(name_attr)] = tabular_model
            self.workspace[workspace.get(ws_name_attr)] = workspace, tables
Пример #19
0
    def __send_sms(self, phone_num, sms_content):
        params = {
            'method': 'Submit',
            'account': SMS_USER,
            'password': SMS_PASSWD,
            'mobile': phone_num,
            'content': sms_content
        }

        logger.debug(SMS_API+'?'+urllib.urlencode(params))

        try:
            resp = urllib2.urlopen(SMS_API+'?'+urllib.urlencode(params))
            root = ET.fromstring(resp.read())
            assert root.tag == str(ET.QName(SMS_XMLNS, 'SubmitResult'))
            code = int(root.find(str(ET.QName(SMS_XMLNS, 'code'))).text)
            if (code == 2):
                logger.debug('send sms successfully')
                return True
            else:
                msg = root.find(str(ET.QName(SMS_XMLNS, 'msg'))).text
                logger.error('send sms failed!! code:%d, msg:%s' %(code, msg))
        except urllib2.URLError, e:
            logger.error('URLError = ' + str(e.reason))
Пример #20
0
class TestNegativeEntailmentTest(TestTestFromElement):
    tag = ElementTree.QName(TEST, 'NegativeEntailmentTest')

    def test_is_negative_entailment_test(self):
        self.assert_(isinstance(self.test, NegativeEntailmentTest))

    def test_has_uri(self):
        self.assertEqual(self.test.uri,
            TESTS['datatypes-intensional/Manifest.rdf#xsd-integer-decimal-compatible'])

    def test_has_type(self):
        self.assertEqual(self.test.type, TEST.NegativeEntailmentTest)

    def test_has_status(self):
        self.assertEqual(self.test.status, 'APPROVED')

    def test_has_description(self):
        self.assertTextEqual(self.test.description,
            " The claim that xsd:integer is a subClassOF xsd:decimal is not "
            " incompatible with using the intensional semantics for datatypes. ")

    def test_has_entailment_rules(self):
        self.assertEqual(set(self.test.entailment_rules),
                         {RDF, RDFS, TESTS['datatypes#']})

    def test_has_datatype_support(self):
        self.assertEqual(set(self.test.datatype_support),
                         {XSD.integer, XSD.decimal})

    def test_has_premise_documents(self):
        self.assertEqual(set(self.test.premise_documents),
                         {Document(TEST['NT-Document'],
                                   TESTS['datatypes-intensional/test001.nt'])})

    def test_has_conclusion_document(self):
        self.assertEqual(self.test.conclusion_document,
                         Document(TEST['False-Document']))

    def test_warning_is_none(self):
        self.assertEqual(self.test.warning, None)
Пример #21
0
def alt_workbook_text(db, filepath):
    """
    Takes a xl/workbook.xml and returns a db altered text version of the xml

    :param pylightxl.Database db: pylightxl database that contains data to update xml file
    :param str filepath: file path for xl/workbook.xml
    :return str: returns the updated xml text
    """

    # extract text from existing app.xml
    ns = xml_namespace(filepath)
    for prefix, uri in ns.items():
        ET.register_namespace(prefix, uri)
    tree = ET.parse(filepath)
    root = tree.getroot()

    # remove existing sheets
    for element in root.findall('./default:sheets/default:sheet', ns):
        root.find('./default:sheets', ns).remove(element)

    # since all 'r' namespace tags are deleted, we have to properly use a linked namespace tag for r:id with qname
    qname_r_id = ET.QName(ns['r'], 'id')
    # write new sheets from db
    for sheet_num, sheet_name in enumerate(db.ws_names, 1):
        element = ET.Element('sheet')
        element.set(qname_r_id, 'rId{sheet_num}'.format(sheet_num=sheet_num))
        element.set('sheetId', '{sheet_num}'.format(sheet_num=sheet_num))
        element.set('name', '{sheet_name}'.format(sheet_name=sheet_name))

        root.findall('./default:sheets', ns)[0].append(element)

    # reset default namespace
    ET.register_namespace('', ns['default'])

    # roll up entire xml file as text
    text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n' + ET.tostring(
        root, encoding='unicode')

    return text
Пример #22
0
    def cell(self, cell):
        logging.debug(dom.tostring(cell))

        date_tag = dom.QName(self.NUMBERS_NS["sf"], 'd')
        date_attr = dom.QName(self.NUMBERS_NS["sf"], 'cell-date')
        formula_tag = dom.QName(self.NUMBERS_NS["sf"], 'f')
        s_attr = dom.QName(self.NUMBERS_NS["sf"], 's')
        v_attr = dom.QName(self.NUMBERS_NS["sf"], 'v')
        general_tag = dom.QName(self.NUMBERS_NS["sf"], 'g')
        number_tag = dom.QName(self.NUMBERS_NS["sf"], 'n')
        text_tag = dom.QName(self.NUMBERS_NS["sf"], 't')
        o_tag = dom.QName(self.NUMBERS_NS["sf"], 'o')
        span_tag = dom.QName(self.NUMBERS_NS["sf"], 's')
        bool_tag = dom.QName(self.NUMBERS_NS["sf"], 'b')
        popup_menu_tag = dom.QName(self.NUMBERS_NS["sf"], 'pm')
        IDREF_attr = dom.QName(self.NUMBERS_NS["sfa"], 'IDREF')
        ID_attr = dom.QName(self.NUMBERS_NS["sfa"], 'ID')
        fs_attr = dom.QName(self.NUMBERS_NS["sf"], "fs")

        if cell.tag == date_tag:
            seconds = int(cell.attrib[date_attr])
            epoch = datetime.datetime(2001, 1, 1)
            delta = datetime.timedelta(seconds=seconds)
            theDate = epoch + delta
            return stingray.cell.DateCell(theDate, self)

        elif cell.tag == formula_tag:  # formula or error.
            s = cell.get(s_attr)
            fo = cell.find('sf:fo', namespaces=self.NUMBERS_NS)
            # Numeric Result? What about non-numeric results?
            r = cell.find('sf:r', namespaces=self.NUMBERS_NS)
            if r:
                # Result:
                rn = r.find('sf:rn', namespaces=self.NUMBERS_NS)
                try:
                    value_txt = rn.attrib[v_attr]
                    value = self._to_decimal(value_txt, s)
                except KeyError as ex:
                    #self._cell_warning("Formula with no value", cell)
                    value = self._to_decimal('0', s)
                return stingray.cell.NumberCell(value, self)
            else:
                # Error:
                #self._cell_warning("Formula error", cell)
                value = "#Error in {0}".format(fo.get(fs_attr))
                return stingray.cell.ErrorCell(value, self)

        elif cell.tag == general_tag:  # General?
            return stingray.cell.EmptyCell('', self)
        elif cell.tag == number_tag:  # Number
            value = self._decode_number(cell)
            return stingray.cell.NumberCell(value, self)
        elif cell.tag == o_tag:  #??
            self._cell_warning("Unknown cell type", cell)
            return stingray.cell.EmptyCell('', self)
        elif cell.tag == span_tag:  # Span?
            self._cell_warning("Unknown cell type", cell)
            return stingray.cell.EmptyCell('', self)
        elif cell.tag == text_tag:  # Text
            value = self._decode_text(cell)
            return stingray.cell.TextCell(value, self)
        elif cell.tag == bool_tag:  # Boolean
            value = self._decode_number(cell)
            return stingray.cell.BooleanCell(value, self)
        elif cell.tag == popup_menu_tag:  # popup menu
            # TODO:: Better Xpath query: ``menu-choices/*[@ID='name']``
            value = None  # In case we can't find anything.
            selected = cell.find('sf:proxied-cell-ref',
                                 namespaces=self.NUMBERS_NS)
            name = selected.get(IDREF_attr)
            mc = cell.find('sf:menu-choices', namespaces=self.NUMBERS_NS)
            for t in mc:
                if t.get(ID_attr) == name:
                    # t's tag cold end in Could be "t", or "n".
                    if t.tag.endswith('t'):  # Text
                        value = self._decode_text(t)
                        return stingray.cell.TextCell(value, self)
                    elif t.tag.endswith('n'):  # Number
                        value = self._decode_number(t)
                        return stingray.cell.NumberCell(value, self)
                    else:
                        raise Exception("Unknown popup menu {0}".format(
                            dom.tostring(cell)))
        else:
            raise Exception("Unknown cell {0}".format(dom.tostring(cell)))
Пример #23
0
    ooow="http://openoffice.org/2004/writer",
    oooc="http://openoffice.org/2004/calc",
    dom="http://www.w3.org/2001/xml-events",
    xforms="http://www.w3.org/2002/xforms",
    xsd="http://www.w3.org/2001/XMLSchema",
    xsi="http://www.w3.org/2001/XMLSchema-instance",
    rpt="http://openoffice.org/2005/report",
    of="urn:oasis:names:tc:opendocument:xmlns:of:1.2",
    xhtml="http://www.w3.org/1999/xhtml",
    grddl="http://www.w3.org/2003/g/data-view#",
    tableooo="http://openoffice.org/2009/table",
    textooo="http://openoffice.org/2013/office",
    field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0",
)

DRAW_FRAME = XML.QName(ns['draw'], 'frame')
DRAW_OBJECT = XML.QName(ns['draw'], 'object')
DRAW_NAME = XML.QName(ns['draw'], 'name')
DRAW_IMAGE = XML.QName(ns['draw'], 'image')
XLINK_HREF = XML.QName(ns['xlink'], 'href')
SVG_DESC = XML.QName(ns['svg'], 'desc')


def show_tag(tag, base_directory):
    if tag.tag == DRAW_FRAME:
        print(":frame:`{0}`".format(tag.attrib[DRAW_NAME]), end='')
    if tag.tag == DRAW_IMAGE:
        print(":image:`{0}`".format(tag.attrib[XLINK_HREF]), end='')
    if tag.tag == DRAW_OBJECT:
        filename = tag.attrib[XLINK_HREF]
        d2 = parse_content_xml(os.path.join(base_directory, filename))
Пример #24
0
def REPO(tag):
    return ET.QName('http://linux.duke.edu/metadata/repo', tag).text
Пример #25
0
def RPM(tag):
    return ET.QName('http://linux.duke.edu/metadata/rpm', tag).text
Пример #26
0
    def import_locations(self, kml_file, kml_version=None):
        """Import KML data files

        ``import_locations()`` returns a dictionary with keys containing the
        section title, and values consisting of :class:`Placemark` objects.

        It expects data files in KML format, as specified in `KML Reference`_,
        which is XML such as::

            <?xml version="1.0" encoding="utf-8"?>
            <kml xmlns="http://earth.google.com/kml/2.1">
                <Document>
                    <Placemark id="Home">
                        <name>Home</name>
                        <Point>
                            <coordinates>-0.221,52.015,60</coordinates>
                        </Point>
                    </Placemark>
                    <Placemark id="Cambridge">
                        <name>Cambridge</name>
                        <Point>
                            <coordinates>0.390,52.167</coordinates>
                        </Point>
                    </Placemark>
                </Document>
            </kml>

        The reader uses the :mod:`ElementTree` module, so should be very fast
        when importing data.  The above file processed by ``import_locations()``
        will return the following ``dict`` object::

            {"Home": Placemark(52.015, -0.221, 60),
             "Cambridge": Placemark(52.167, 0.390, None)}

        >>> locations = Placemarks(open("kml"))
        >>> for value in sorted(locations.values(),
        ...                     key=lambda x: x.name.lower()):
        ...     print(value)
        Cambridge (52°10'01"N, 000°23'24"E)
        Home (52°00'54"N, 000°13'15"W alt 60m)

        The ``kml_version`` parameter allows the caller to specify the specific
        KML version that should be processed, this allows the caller to process
        inputs which contain entries in more than one namespace without
        duplicates resulting from entries in being specified with different
        namespaces.

        :type kml_file: ``file``, ``list`` or ``str``
        :param kml_file: KML data to read
        :type kml_version: ``str``
        :param kml_version: Specific KML version entities to import
        :rtype: ``dict``
        :return: Named locations with optional comments

        .. _KML Reference:
           http://code.google.com/apis/kml/documentation/kml_tags_21.html

        """
        self._kml_file = kml_file
        data = utils.prepare_xml_read(kml_file)

        if kml_version:
            try:
                accepted_kml = {kml_version: KML_VERSIONS[kml_version]}
            except KeyError:
                raise KeyError("Unknown KML version `%s'" % kml_version)
        else:
            accepted_kml = KML_VERSIONS

        for version, namespace in accepted_kml.items():
            logging.info("Searching for KML v%s entries" % version)
            kml_elem = lambda name: ET.QName(namespace, name).text
            placemark_elem = "//" + kml_elem("Placemark")
            name_elem = kml_elem("name")
            coords_elem = kml_elem("Point") + "/" + kml_elem("coordinates")
            desc_elem = kml_elem("description")

            for place in data.findall(placemark_elem):
                name = place.findtext(name_elem)
                coords = place.findtext(coords_elem)
                if coords is None:
                    logging.info("No coordinates found for `%s' entry" % name)
                    continue
                coords = coords.split(",")
                if len(coords) == 2:
                    longitude, latitude = coords
                    altitude = None
                elif len(coords) == 3:
                    longitude, latitude, altitude = coords
                else:
                    raise ValueError(
                        "Unable to handle coordinates value `%s'" % coords)
                description = place.findtext(desc_elem)
                self[name] = Placemark(latitude, longitude, altitude, name,
                                       description)
Пример #27
0
def findtext(element, tag, alt='', ns='http://maven.apache.org/POM/4.0.0'):
    fulltag = str(ET.QName(ns, tag))
    return element.findtext(fulltag, alt)
Пример #28
0
def ns_attr(attr):
    return str(
        cElementTree.QName("http://www.w3.org/2001/XMLSchema-instance", attr))
Пример #29
0
    def _parse_ome_xml(self, xml):
        try:  # bioformats seem to copy some (wrongly encoded) strings verbatim
            root = ElementTree.fromstring(xml)
        except ElementTree.ParseError:
            root = ElementTree.fromstring(
                xml.decode('iso-8859-1').encode('utf-8'))

        self.xml = root

        self.ns = ns = root.tag.split('}')[0][1:]

        # sometimes string properties creep up, but then we don't care as we don't plan on using them ...
        def float_or_int(s):
            """

            :param s:
            :return:
            """
            try:
                if '.' in s:
                    return float(s)
                else:
                    return int(s)
            except ValueError:
                return s

        keep_pa = {
            'SizeZ', 'SizeY', 'SizeX', 'SignificantBits', 'PhysicalSizeX',
            'PhysicalSizeY', 'SizeC', 'SizeT'
        }

        images = {}

        if self.treat_z_as_mp:  # handling for mal-encoded files
            image_nodes = [
                n for n in root if n.tag == ElementTree.QName(ns, 'Image')
            ]
            # there will be only one image node
            imn = image_nodes[0]

            pixels = [
                n for n in imn if n.tag == ElementTree.QName(ns, 'Pixels')
            ][0]

            pa = pixels.attrib

            self.pixel_attrib_sanity_check(pa)

            pai = list({k: v for k, v in pa.items() if k in keep_pa}.items())

            tiff_data = {(n.attrib['FirstC'], n.attrib['FirstT'],
                          n.attrib['FirstZ']): n.attrib
                         for n in pixels
                         if n.tag == ElementTree.QName(ns, 'TiffData')}
            planes = [
                dict(
                    list(n.attrib.items()) +
                    list(tiff_data[(n.attrib['TheC'], n.attrib['TheT'],
                                    n.attrib['TheZ'])].items()) + pai)
                for n in pixels if n.tag == ElementTree.QName(ns, 'Plane')
            ]

            planes = [{k: float_or_int(v)
                       for k, v in p.items()} for p in planes]
            multipoints = range(planes[0]['SizeZ'])
            images = {
                mp: [p for p in planes if p['TheZ'] == mp]
                for mp in multipoints
            }

            # more fixing

            def _correct_attributes(inner_p, inner_planes):
                inner_p['PositionX'] = inner_planes[0]['PositionX']
                inner_p['PositionY'] = inner_planes[0]['PositionY']
                inner_p['PositionZ'] = inner_planes[0]['PositionZ']
                inner_p['TheZ'] = 0
                return inner_p

            images = {
                mp: [_correct_attributes(p, planes) for p in planes]
                for mp, planes in images.items()
            }

        else:
            image_nodes = [
                n for n in root if n.tag == ElementTree.QName(ns, 'Image')
            ]
            for n, imn in enumerate(image_nodes):
                pixels = [
                    n for n in imn if n.tag == ElementTree.QName(ns, 'Pixels')
                ][0]

                pa = pixels.attrib

                self.pixel_attrib_sanity_check(pa)

                pai = list({k: v
                            for k, v in pa.items() if k in keep_pa}.items())

                tiff_data = {(n.attrib['FirstC'], n.attrib['FirstT'],
                              n.attrib['FirstZ']): n.attrib
                             for n in pixels
                             if n.tag == ElementTree.QName(ns, 'TiffData')}
                planes = [
                    dict(
                        list(n.attrib.items()) +
                        list(tiff_data[(n.attrib['TheC'], n.attrib['TheT'],
                                        n.attrib['TheZ'])].items()) + pai)
                    for n in pixels if n.tag == ElementTree.QName(ns, 'Plane')
                ]

                planes = [{k: float_or_int(v)
                           for k, v in p.items()} for p in planes]

                images[n] = planes

        return images
def ns_tag(tag):
    return str(
        cElementTree.QName(
            'http://schemas.datacontract.org/2004/07/ArtDatabanken.WebService.Data',
            tag))