class HwpFileHeader(object): def __init__(self, item): self.open = item.open def to_dict(self): from hwp5.bintype import read_type f = self.open() try: return read_type(FileHeader, dict(), f) finally: f.close() value = cached_property(to_dict) def get_version(self): return self.value['version'] version = cached_property(get_version) def get_signature(self): return self.value['signature'] signature = cached_property(get_signature) def get_flags(self): return FileHeader.Flags(self.value['flags']) flags = cached_property(get_flags) def open_text(self): d = FileHeader.Flags.dictvalue(self.value['flags']) d['signature'] = self.value['signature'][:len('HWP Document File')] d['version'] = '%d.%d.%d.%d' % self.value['version'] out = StringIO() for k, v in sorted(d.items()): print >> out, '%s: %s' % (k, v) out.seek(0) return out def other_formats(self): return {'.txt': self.open_text}
class OleStorageItem(object): def __init__(self, olefile, path, parent=None): self.olefile = olefile self.path = path # path DOES NOT end with '/' def get_name(self): if self.path == '': return None segments = self.path.split('/') return segments[-1] name = cached_property(get_name)
class PreviewText(object): def __init__(self, item): self.open = item.open def other_formats(self): return {'.utf8': self.open_utf8} def open_utf8(self): transcode = transcoder('utf-16le', 'utf-8') return transcode(self.open()) def get_utf8(self): f = self.open_utf8() try: return f.read() finally: f.close() utf8 = cached_property(get_utf8) def __str__(self): return self.utf8
class Hwp5FileBase(ItemConversionStorage): ''' Base of an Hwp5File. Hwp5FileBase checks basic validity of an HWP format v5 and provides `fileheader` property. :param stg: an OLE2 structured storage. :type stg: an instance of storage, OleFileIO or filename :raises InvalidHwp5FileError: `stg` is not a valid HWP format v5 document. ''' def __init__(self, stg): from hwp5.errors import InvalidOleStorageError from hwp5.errors import InvalidHwp5FileError from hwp5.storage import is_storage from hwp5.storage.ole import OleStorage if not is_storage(stg): try: stg = OleStorage(stg) except InvalidOleStorageError: raise InvalidHwp5FileError('Not an OLE2 Compound Binary File.') if not storage_is_hwp5file(stg): errormsg = 'Not an HWP Document format v5 storage.' raise InvalidHwp5FileError(errormsg) ItemConversionStorage.__init__(self, stg) def resolve_conversion_for(self, name): if name == 'FileHeader': return HwpFileHeader def get_fileheader(self): return self['FileHeader'] fileheader = cached_property(get_fileheader) header = fileheader
class HwpSummaryInfo(VersionSensitiveItem): def other_formats(self): return {'.txt': self.open_text} def to_dict(self): f = self.open() from hwp5.msoleprops import MSOLEPropertySet try: context = dict(version=self.version) summaryinfo = MSOLEPropertySet.read(f, context) return summaryinfo finally: f.close() value = cached_property(to_dict) @property def byteorder(self): return self.value['byteorder'] @property def format(self): return self.value['format'] @property def os(self): return self.value['os'] @property def osversion(self): return self.value['osversion'] @property def clsid(self): return self.value['clsid'] @property def sections(self): for section in self.value['sections']: formatid = section['formatid'] properties = section['properties'] yield formatid, properties UNKNOWN_FMTID = '9fa2b660-1061-11d4-b4c6-006097c09d8c' @property def propertyset(self): from uuid import UUID for formatid, properties in self.sections: if formatid == UUID(self.UNKNOWN_FMTID): return properties @property def plaintext_lines(self): os_names = {0: 'win16', 1: 'macos', 2: 'win32'} yield 'byteorder: 0x%x' % self.byteorder yield 'clsid: %s' % self.clsid yield 'format: %d' % self.format yield 'os: %s %s' % (self.os, os_names.get(self.os, '')) yield 'osversion: %d' % self.osversion for formatid, properties in self.sections: yield ('-- Section %s --' % formatid) for prop_id, prop in properties.items(): prop_name = prop.get('name') or prop_id prop_value = prop.get('value') prop_str = u'%s: %s' % (prop_name, prop_value) yield prop_str.encode('utf-8') def open_text(self): out = StringIO() for line in self.plaintext_lines: out.write(line + '\n') out.seek(0) return out