def encode_inner_components(self, name, property_values, encoding='utf-8'): lines = [] for property_value in property_values: record = self.get_record(property_value.value) if record is not None: seq = 0 c_type = record.type for version in record: line = 'BEGIN:%s\n' % c_type lines.append(Unicode.encode(line)) # Properties names = version.keys() names.sort() for name in names: if name in ('id', 'ts', 'type'): continue elif name == 'DTSTAMP': value = version['ts'] else: value = version[name] if name == 'SEQUENCE': value.value += seq else: name = name.upper() line = self.encode_property(name, value) lines.extend(line) line = 'END:%s\n' % c_type lines.append(Unicode.encode(line)) seq += 1 return lines
def to_ical(self): """Serialize as an ical file, generally named .ics """ lines = [] line = 'BEGIN:VCALENDAR\n' lines.append(Unicode.encode(line)) # Calendar properties properties = (('VERSION', u'2.0'), ('PRODID', u'-//itaapy.com/NONSGML ikaaro icalendar V1.0//EN')) for name, value in properties: value = Property(value) line = self.encode_property(name, value) lines.append(line[0]) # Calendar components for record in self.records: if record is not None: seq = 0 c_type = record.type # Ignore some components (like DAYLIGHT, STANDARD, ...) # keeping only VEVENT, VTIMEZONE, V.., and x-name ones if not c_type.startswith('V') and not c_type.startswith('X'): continue for version in record: line = 'BEGIN:%s\n' % c_type lines.append(Unicode.encode(line)) line = '' # Properties names = version.keys() names.sort() for name in names: if name in ('id', 'ts', 'type'): continue elif name == 'DTSTAMP': value = version['ts'] else: value = version[name] if name == 'SEQUENCE': value.value += seq # Insert inner components elif name == 'inner': line = self.encode_inner_components(name, value) else: name = name.upper() line = self.encode_property(name, value) lines.extend(line) line = 'END:%s\n' % c_type lines.append(Unicode.encode(line)) seq += 1 line = 'END:VCALENDAR\n' lines.append(Unicode.encode(line)) return ''.join(lines)
def to_ical(self): """Serialize as an ical file, generally named .ics """ lines = [] line = 'BEGIN:VCALENDAR\n' lines.append(Unicode.encode(line)) # Calendar properties properties = ( ('VERSION', u'2.0'), ('PRODID', u'-//itaapy.com/NONSGML ikaaro icalendar V1.0//EN')) for name, value in properties: value = Property(value) line = self.encode_property(name, value) lines.append(line[0]) # Calendar components for record in self.records: if record is not None: seq = 0 c_type = record.type # Ignore some components (like DAYLIGHT, STANDARD, ...) # keeping only VEVENT, VTIMEZONE, V.., and x-name ones if not c_type.startswith('V') and not c_type.startswith('X'): continue for version in record: line = 'BEGIN:%s\n' % c_type lines.append(Unicode.encode(line)) line = '' # Properties names = version.keys() names.sort() for name in names: if name in ('id', 'ts', 'type'): continue elif name == 'DTSTAMP': value = version['ts'] else: value = version[name] if name == 'SEQUENCE': value.value += seq # Insert inner components elif name == 'inner': line = self.encode_inner_components(name, value) else: name = name.upper() line = self.encode_property(name, value) lines.extend(line) line = 'END:%s\n' % c_type lines.append(Unicode.encode(line)) seq += 1 line = 'END:VCALENDAR\n' lines.append(Unicode.encode(line)) return ''.join(lines)
def to_str(self, encoding='UTF-8'): lines = [] lines.append('BEGIN:VCALENDAR\n') # Calendar properties for key in self.properties: value = self.properties[key] line = self.encode_property(key, value, encoding) lines.extend(line) # Calendar components for uid in self.components: component = self.components[uid] c_type = component.c_type for sequence in component.get_sequences(): version = component.versions[sequence] # Begin line = u'BEGIN:%s\n' % c_type lines.append(Unicode.encode(line)) # UID, SEQUENCE lines.append('UID:%s\n' % uid) lines.append('SEQUENCE:%s\n' % sequence) # Properties for key in version: value = version[key] line = self.encode_property(key, value, encoding) lines.extend(line) # Insert inner components for c_inner_component in component.c_inner_components: c_inner_type = c_inner_component.c_inner_type # sequence not supported into inner components version = c_inner_component.versions[0] # Begin line = u'BEGIN:%s\n' % c_inner_type lines.append(Unicode.encode(line)) # Properties for key in version: value = version[key] line = self.encode_property(key, value, encoding) lines.extend(line) # End line = u'END:%s\n' % c_inner_type lines.append(Unicode.encode(line)) # End line = u'END:%s\n' % c_type lines.append(Unicode.encode(line)) lines.append('END:VCALENDAR\n') return ''.join(lines)
def to_str(self, encoding='UTF-8', separator=',', newline='\n'): def escape(data): return '"%s"' % data.replace('"', '""') lines = [] # Header if self.has_header: line = [ escape(Unicode.encode(x, encoding)) for x in self.header ] line = separator.join(line) lines.append(line) # When schema or columns (or both) are None there is plain # string to string conversion schema = self.schema columns = self.columns if schema and columns: datatypes = [ (i, schema[x]) for i, x in enumerate(columns) ] for row in self.get_rows(): line = [] for i, datatype in datatypes: try: data = datatype.encode(row[i], encoding=encoding) except TypeError: data = datatype.encode(row[i]) line.append(escape(data)) lines.append(separator.join(line)) else: for row in self.get_rows(): line = [ escape(x) for x in row ] lines.append(separator.join(line)) return newline.join(lines)
def split_reference(ref): """Return the reference associated to the path, the path and the optional view without query/fragment. ref: Reference path: Path view: string """ # XXX specific case for the menu # Be robust if the path is multilingual type_ref = type(ref) if type_ref is unicode: ref = Unicode.encode(ref) if type_ref is not Reference: ref = get_reference(ref) # Split path and view path = ref.path view = '' name = path.get_name() # Strip the view if name and name[0] == ';': view = '/' + name path = path[:-1] return ref, path, view
def test_Unicode(self): x = u'العربيه 中文 Español Français' data = Unicode.encode(x) self.assertEqual(x, Unicode.decode(data))
def encode(cls, value): if value is None: return None return Unicode.encode(value['value'])