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
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
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
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
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