Esempio n. 1
0
    def get_user_defined_metadata(self):
        """Return a dict of unicode/value mapping.

        Value types can be: Decimal, date, time, boolean or unicode.
        """

        result = {}
        for meta in self.get_element_list('//meta:user-defined'):
            # Read the values
            name = meta.get_attribute('meta:name')
            value_type = meta.get_attribute('meta:value-type')
            if value_type is None:
                value_type = 'string'
            value = meta.get_text()
            # Interpretation
            if value_type == 'boolean':
                result[name] = Boolean.decode(value)
            elif value_type in ('float', 'percentage', 'currency'):
                result[name] = Decimal(value)
            elif value_type == 'date':
                if 'T' in value:
                    result[name] = DateTime.decode(value)
                else:
                    result[name] = Date.decode(value)
            elif value_type == 'string':
                result[name] = value
            elif value_type == 'time':
                result[name] = Duration.decode(value)
        return result
Esempio n. 2
0
 def set_user_defined_metadata(self, name, value):
     if type(value) is bool:
         value_type = 'boolean'
         value = u'true' if value else u'false'
     elif isinstance(value, (int, float, Decimal)):
         value_type = 'float'
         value = unicode(value)
     elif type(value) is date:
         value_type = 'date'
         value = unicode(Date.encode(value))
     elif type(value) is datetime:
         value_type = 'date'
         value = unicode(DateTime.encode(value))
     elif type(value) is str:
         value_type = 'string'
         value = unicode(value)
     elif type(value) is unicode:
         value_type = 'string'
     elif type(value) is timedelta:
         value_type = 'time'
         value = unicode(Duration.encode(value))
     else:
         raise TypeError, 'unexpected type "%s" for value' % type(value)
     # Already the same element ?
     for metadata in self.get_element_list('//meta:user-defined'):
         if metadata.get_attribute('meta:name') == name:
             break
     else:
         metadata = odf_create_element('meta:user-defined')
         metadata.set_attribute('meta:name', name)
         self.get_meta_body().append(metadata)
     metadata.set_attribute('meta:value-type', value_type)
     metadata.set_text(value)
Esempio n. 3
0
    def get_user_defined_metadata(self):
        """Return a dict of unicode/value mapping.

        Value types can be: Decimal, date, time, boolean or unicode.
        """

        result = {}
        for meta in self.get_elements('//meta:user-defined'):
            # Read the values
            name = meta.get_attribute('meta:name')
            value_type = meta.get_attribute('meta:value-type')
            if value_type is None:
                value_type = 'string'
            value = meta.get_text()
            # Interpretation
            if value_type == 'boolean':
                result[name] = Boolean.decode(value)
            elif value_type in  ('float', 'percentage', 'currency'):
                result[name] = Decimal(value)
            elif value_type == 'date':
                if 'T' in value:
                    result[name] = DateTime.decode(value)
                else:
                    result[name] = Date.decode(value)
            elif value_type == 'string':
                result[name] = value
            elif value_type == 'time':
                result[name] = Duration.decode(value)
        return result
Esempio n. 4
0
 def _get_meta_value(element, full=False):
     """get_value deicated to the meta data part, for one meta element.
     """
     name = element.get_attribute('meta:name')
     value_type = element.get_attribute('meta:value-type')
     if value_type is None:
         value_type = 'string'
     text = element.get_text()
     # Interpretation
     if value_type == 'boolean':
         value = Boolean.decode(text)
     elif value_type in  ('float', 'percentage', 'currency'):
         value = Decimal(text)
     elif value_type == 'date':
         if 'T' in text:
             value = DateTime.decode(text)
         else:
             value = Date.decode(text)
     elif value_type == 'string':
         value = text
     elif value_type == 'time':
         value = Duration.decode(text)
     if full:
         return (value, value_type, text)
     else:
         return value
Esempio n. 5
0
 def set_user_defined_metadata(self, name, value):
     if type(value) is bool:
         value_type = 'boolean'
         value = u'true' if value else u'false'
     elif isinstance(value, (int, float, Decimal)):
         value_type = 'float'
         value = unicode(value)
     elif type(value) is date:
         value_type = 'date'
         value = unicode(Date.encode(value))
     elif type(value) is datetime:
         value_type = 'date'
         value = unicode(DateTime.encode(value))
     elif type(value) is str:
         value_type = 'string'
         value = unicode(value)
     elif type(value) is unicode:
         value_type = 'string'
     elif type(value) is timedelta:
         value_type = 'time'
         value = unicode(Duration.encode(value))
     else:
         raise TypeError, 'unexpected type "%s" for value' % type(value)
     # Already the same element ?
     for metadata in self.get_elements('//meta:user-defined'):
         if metadata.get_attribute('meta:name') == name:
             break
     else:
         metadata = odf_create_element('meta:user-defined')
         metadata.set_attribute('meta:name', name)
         self.get_meta_body().append(metadata)
     metadata.set_attribute('meta:value-type', value_type)
     metadata.set_text(value)
Esempio n. 6
0
 def _get_meta_value(element, full=False):
     """get_value deicated to the meta data part, for one meta element.
     """
     name = element.get_attribute('meta:name')
     value_type = element.get_attribute('meta:value-type')
     if value_type is None:
         value_type = 'string'
     text = element.get_text()
     # Interpretation
     if value_type == 'boolean':
         value = Boolean.decode(text)
     elif value_type in ('float', 'percentage', 'currency'):
         value = Decimal(text)
     elif value_type == 'date':
         if 'T' in text:
             value = DateTime.decode(text)
         else:
             value = Date.decode(text)
     elif value_type == 'string':
         value = text
     elif value_type == 'time':
         value = Duration.decode(text)
     if full:
         return (value, value_type, text)
     else:
         return value
Esempio n. 7
0
def get_value(element, value_type=None, try_get_text=True, get_type=False):
    """Only for "with office:value-type" elements
    """
    if value_type is None:
        value_type = element.get_attribute('office:value-type')
    if value_type == 'boolean':
        value = element.get_attribute('office:boolean-value')
        if get_type:
            return (Boolean.decode(value), value_type)
        return Boolean.decode(value)
    elif value_type in  ('float', 'percentage', 'currency'):
        value = dec(element.get_attribute('office:value'))
        # Return 3 instead of 3.0 if possible
        if int(value) == value:
            if get_type:
                return (int(value), value_type)
            return int(value)
        if get_type:
                return (value, value_type)
        return value
    elif value_type == 'date':
        value = element.get_attribute('office:date-value')
        if 'T' in value:
            if get_type:
                return (DateTime.decode(value), value_type)
            return DateTime.decode(value)
        else:
            if get_type:
                return (Date.decode(value), value_type)
            return Date.decode(value)
    elif value_type == 'string':
        value = element.get_attribute('office:string-value')
        if value is not None:
            if get_type:
                return (unicode(value), value_type)
            return unicode(value)
        if try_get_text:
            value = []
            for para in element.get_elements('text:p'):
                value.append(para.get_text(recursive=True))
            if value:
                if get_type:
                    return (u"\n".join(value), value_type)
                return u"\n".join(value)
        if get_type:
            return (None, value_type)
        return None
    elif value_type == 'time':
        value = Duration.decode(element.get_attribute('office:time-value'))
        if get_type:
            return (value, value_type)
        return value
    elif value_type is None:
        if get_type:
            return (None, None)
        return None

    raise ValueError, 'unexpected value type "%s"' % value_type
Esempio n. 8
0
def get_value(element, value_type=None, try_get_text=True, get_type=False):
    """Only for "with office:value-type" elements
    """
    if value_type is None:
        value_type = element.get_attribute('office:value-type')
    if value_type == 'boolean':
        value = element.get_attribute('office:boolean-value')
        if get_type:
            return (Boolean.decode(value), value_type)
        return Boolean.decode(value)
    elif value_type in ('float', 'percentage', 'currency'):
        value = dec(element.get_attribute('office:value'))
        # Return 3 instead of 3.0 if possible
        if int(value) == value:
            if get_type:
                return (int(value), value_type)
            return int(value)
        if get_type:
            return (value, value_type)
        return value
    elif value_type == 'date':
        value = element.get_attribute('office:date-value')
        if 'T' in value:
            if get_type:
                return (DateTime.decode(value), value_type)
            return DateTime.decode(value)
        else:
            if get_type:
                return (Date.decode(value), value_type)
            return Date.decode(value)
    elif value_type == 'string':
        value = element.get_attribute('office:string-value')
        if value is not None:
            if get_type:
                return (unicode(value), value_type)
            return unicode(value)
        if try_get_text:
            value = []
            for para in element.get_elements('text:p'):
                value.append(para.get_text(recursive=True))
            if value:
                if get_type:
                    return (u"\n".join(value), value_type)
                return u"\n".join(value)
        if get_type:
            return (None, value_type)
        return None
    elif value_type == 'time':
        value = Duration.decode(element.get_attribute('office:time-value'))
        if get_type:
            return (value, value_type)
        return value
    elif value_type is None:
        if get_type:
            return (None, None)
        return None

    raise ValueError, 'unexpected value type "%s"' % value_type
Esempio n. 9
0
    def get_editing_duration(self):
        """Get the time the document was edited, as reported by the
        generator.

        Return: timedelta (or None if inexistant)
        """
        element = self.get_element('//meta:editing-duration')
        if element is None:
            return None
        duration = element.get_text()
        return Duration.decode(duration)
Esempio n. 10
0
    def get_editing_duration(self):
        """Get the time the document was edited, as reported by the
        generator.

        Return: timedelta (or None if inexistant)
        """
        element = self.get_element('//meta:editing-duration')
        if element is None:
            return None
        duration = element.get_text()
        return Duration.decode(duration)
Esempio n. 11
0
def odf_create_time_variable(time, fixed=False, data_style=None, text=None, time_adjust=None):
    element = odf_create_element("text:time")
    element.set_attribute("text:time-value", DateTime.encode(time))
    if fixed:
        element.set_attribute("text:fixed", "true")
    if data_style is not None:
        element.set_style_attribute("style:data-style-name", data_style)
    if text is None:
        text = time.strftime("%H:%M:%S")
    element.set_text(text)
    if time_adjust is not None:
        element.set_attribute("text:time-adjust", Duration.encode(time_adjust))
    return element
Esempio n. 12
0
def odf_create_time_variable(time, fixed=False, data_style=None, text=None,
        time_adjust=None):
    data = '<text:time text:time-value="%s"/>'
    element = odf_create_element(data % DateTime.encode(time))
    if fixed:
        element.set_attribute('text:fixed', 'true')
    if data_style is not None:
        element.set_attribute('style:data-style-name', data_style)
    if text is None:
        text = time.strftime('%H:%M:%S')
    element.set_text(text)
    if time_adjust is not None:
        element.set_attribute('text:time-adjust',
                               Duration.encode(time_adjust))
    return element
Esempio n. 13
0
    def set_editing_duration(self, duration):
        """Set the time the document was edited.

        Arguments:

            duration -- timedelta
        """
        if type(duration) is not timedelta:
            raise TypeError, u"duration must be a timedelta"
        element = self.get_element('//meta:editing-duration')
        if element is None:
            element = odf_create_element('meta:editing-duration')
            self.get_meta_body().append(element)
        duration = Duration.encode(duration)
        element.set_text(duration)
Esempio n. 14
0
def odf_create_time_variable(time, fixed=False, data_style=None, text=None,
        time_adjust=None):
    element = odf_create_element('text:time')
    element.set_attribute('text:time-value', DateTime.encode(time))
    if fixed:
        element.set_attribute('text:fixed', 'true')
    if data_style is not None:
        element.set_style_attribute('style:data-style-name', data_style)
    if text is None:
        text = time.strftime('%H:%M:%S')
    element.set_text(text)
    if time_adjust is not None:
        element.set_attribute('text:time-adjust',
                               Duration.encode(time_adjust))
    return element
Esempio n. 15
0
def odf_create_date_variable(date, fixed=False, data_style=None, text=None,
        date_adjust=None):
    element = odf_create_element('text:date')
    element.set_attribute('text:date-value', DateTime.encode(date))
    if fixed:
        element.set_attribute('text:fixed', 'true')
    if data_style is not None:
        element.set_style_attribute('style:data-style-name', data_style)
    if text is None:
        text = Date.encode(date)
    element.set_text(text)
    if date_adjust is not None:
        element.set_attribute('text:date-adjust',
                               Duration.encode(date_adjust))
    return element
Esempio n. 16
0
    def set_editing_duration(self, duration):
        """Set the time the document was edited.

        Arguments:

            duration -- timedelta
        """
        if type(duration) is not timedelta:
            raise TypeError, u"duration must be a timedelta"
        element = self.get_element('//meta:editing-duration')
        if element is None:
            element = odf_create_element('meta:editing-duration')
            self.get_meta_body().append(element)
        duration = Duration.encode(duration)
        element.set_text(duration)
Esempio n. 17
0
def get_value(element, value_type=None, try_get_text=True):
    """Only for "with office:value-type" elements
    """

    if value_type is None:
        value_type = element.get_attribute('office:value-type')

    if value_type == 'boolean':
        value = element.get_attribute('office:boolean-value')
        return Boolean.decode(value)
    elif value_type in ('float', 'percentage', 'currency'):
        value = dec(element.get_attribute('office:value'))
        # Return 3 instead of 3.0 if possible
        if int(value) == value:
            return int(value)
        return value
    elif value_type == 'date':
        value = element.get_attribute('office:date-value')
        if 'T' in value:
            return DateTime.decode(value)
        else:
            return Date.decode(value)
    elif value_type == 'string':
        value = element.get_attribute('office:string-value')
        if value is not None:
            return unicode(value)
        # XXX: get_text or get_formatted_text ???
        if try_get_text:
            value = element.get_text(recursive=True)
            if value != '':
                return value
        return None

    elif value_type == 'time':
        value = element.get_attribute('office:time-value')
        return Duration.decode(value)
    elif value_type is None:
        return None

    raise ValueError, 'unexpected value type "%s"' % value_type
Esempio n. 18
0
def get_value(element, value_type=None, try_get_text=True):
    """Only for "with office:value-type" elements
    """

    if value_type is None:
        value_type = element.get_attribute('office:value-type')

    if value_type == 'boolean':
        value = element.get_attribute('office:boolean-value')
        return Boolean.decode(value)
    elif value_type in  ('float', 'percentage', 'currency'):
        value = dec(element.get_attribute('office:value'))
        # Return 3 instead of 3.0 if possible
        if int(value) == value:
            return int(value)
        return value
    elif value_type == 'date':
        value = element.get_attribute('office:date-value')
        if 'T' in value:
            return DateTime.decode(value)
        else:
            return Date.decode(value)
    elif value_type == 'string':
        value = element.get_attribute('office:string-value')
        if value is not None:
            return unicode(value)
        # XXX: get_text or get_formatted_text ???
        if try_get_text:
            value = element.get_text(recursive=True)
            if value != '':
                return value
        return None

    elif value_type == 'time':
        value = element.get_attribute('office:time-value')
        return Duration.decode(value)
    elif value_type is None:
        return None

    raise ValueError, 'unexpected value type "%s"' % value_type
Esempio n. 19
0
def _set_value_and_type(element, value=None, value_type=None, text=None,
        currency=None):
    # Remove possible previous value and type
    for name in ('office:value-type', 'office:boolean-value',
            'office:value', 'office:date-value', 'office:string-value',
            'office:time-value'):
        try:
            element.del_attribute(name)
        except KeyError:
            pass
    if type(value) is bool:
        if value_type is None:
            value_type = 'boolean'
        if text is None:
            text = u'true' if value else u'false'
        value = Boolean.encode(value)
    elif isinstance(value, (int, float, dec)):
        if value_type is None:
            value_type = 'float'
        if text is None:
            text = unicode(value)
        value = str(value)
    elif type(value) is date:
        if value_type is None:
            value_type = 'date'
        if text is None:
            text = unicode(Date.encode(value))
        value = Date.encode(value)
    elif type(value) is datetime:
        if value_type is None:
            value_type = 'date'
        if text is None:
            text = unicode(DateTime.encode(value))
        value = DateTime.encode(value)
    elif type(value) is str:
        if value_type is None:
            value_type = 'string'
        if text is None:
            text = unicode(value)
    elif type(value) is unicode:
        if value_type is None:
            value_type = 'string'
        if text is None:
            text = value
    elif type(value) is timedelta:
        if value_type is None:
            value_type = 'time'
        if text is None:
            text = unicode(Duration.encode(value))
        value = Duration.encode(value)
    elif value is not None:
        raise TypeError, 'type "%s" is unknown' % type(value)

    if value_type is not None:
        element.set_attribute('office:value-type', value_type)

    if value_type == 'boolean':
        element.set_attribute('office:boolean-value', value)
    elif value_type == 'currency':
        element.set_attribute('office:value', value)
        element.set_attribute('office:currency', currency)
    elif value_type == 'date':
        element.set_attribute('office:date-value', value)
    elif value_type in ('float', 'percentage'):
        element.set_attribute('office:value', value)
    elif value_type == 'string':
        element.set_attribute('office:string-value', value)
    elif value_type == 'time':
        element.set_attribute('office:time-value', value)

    return text
Esempio n. 20
0
def _set_value_and_type(element,
                        value=None,
                        value_type=None,
                        text=None,
                        currency=None):
    # Remove possible previous value and type
    for name in ('office:value-type', 'office:boolean-value', 'office:value',
                 'office:date-value', 'office:string-value',
                 'office:time-value', 'table:formula'):
        try:
            element.del_attribute('office:currency')
        except KeyError:
            pass
    if value is None:
        try:
            element.del_attribute(name)
        except KeyError:
            pass
        element._erase_text_content()
        return text
    if type(value) is bool:
        if value_type is None:
            value_type = 'boolean'
        if text is None:
            text = u'true' if value else u'false'
        value = Boolean.encode(value)
    elif isinstance(value, (int, float, long, dec)):
        if value_type is 'percentage':
            text = "%d %%" % int(value * 100)
        if value_type is None:
            value_type = 'float'
        if text is None:
            text = unicode(value)
        value = str(value)
    elif type(value) is date:
        if value_type is None:
            value_type = 'date'
        if text is None:
            text = unicode(Date.encode(value))
        value = Date.encode(value)
    elif type(value) is datetime:
        if value_type is None:
            value_type = 'date'
        if text is None:
            text = unicode(DateTime.encode(value))
        value = DateTime.encode(value)
    elif type(value) is str:
        if value_type is None:
            value_type = 'string'
        if text is None:
            text = unicode(value)
    elif type(value) is unicode:
        if value_type is None:
            value_type = 'string'
        if text is None:
            text = value
    elif type(value) is timedelta:
        if value_type is None:
            value_type = 'time'
        if text is None:
            text = unicode(Duration.encode(value))
        value = Duration.encode(value)
    elif value is not None:
        raise TypeError, 'type "%s" is unknown' % type(value)

    if value_type is not None:
        element.set_attribute('office:value-type', value_type)

    if value_type == 'boolean':
        element.set_attribute('office:boolean-value', value)
    elif value_type == 'currency':
        element.set_attribute('office:value', value)
        element.set_attribute('office:currency', currency)
    elif value_type == 'date':
        element.set_attribute('office:date-value', value)
    elif value_type in ('float', 'percentage'):
        element.set_attribute('office:value', value)
    elif value_type == 'string':
        element.set_attribute('office:string-value', value)
    elif value_type == 'time':
        element.set_attribute('office:time-value', value)

    return text