def load(self, fin): ''' Load the calling instance from fin. fin can either be a file to open or a file-like object to be read from. ''' f = open(fin, 'rb') if is_a_string(fin) else fin ln = f.readline() if not ln: raise SelfSerializerEmptyHeaderError('Empty header.') H = ln.split() if (len(H) != 3) or (H[0] != SelfSerializer._record_sep): raise SelfSerializerError('Incorrect record separator.') class_name, indicated_len = H[1], int(H[2]) if self.__class__.__name__ != class_name: raise SelfSerializerError('Incorrect name: %s' % class_name) if indicated_len < 0: raise SelfSerializerError('Invalid length: %d' % indicated_len) tmp_dict = dict() for i in xrange(indicated_len): name, val = self._deserialize(f) tmp_dict[name] = val if len(tmp_dict) != indicated_len: raise SelfSerializerError('Incorrect number of items: %d != %d' % (len(tmp_dict), indicated_len)) if is_a_string(fin): f.close() self.__dict__ = tmp_dict
def load(self, fin): ''' Load the calling instance from fin. fin can either be a file to open or a file-like object to be read from. ''' f = open(fin, 'rb') if is_a_string(fin) else fin ln = f.readline() if not ln: raise SelfSerializerEmptyHeaderError('Empty header.') H = ln.split() if (len(H) != 3) or (H[0] != SelfSerializer._record_sep): raise SelfSerializerError('Incorrect record separator.') class_name, indicated_len = H[1], int(H[2]) if self.__class__.__name__ != class_name: raise SelfSerializerError('Incorrect name: %s' % class_name) if indicated_len < 0: raise SelfSerializerError('Invalid length: %d' % indicated_len) tmp_dict = dict() for i in xrange(indicated_len): name, val = self._deserialize(f) tmp_dict[name] = val if len(tmp_dict) != indicated_len: raise SelfSerializerError( 'Incorrect number of items: %d != %d' % ( len(tmp_dict), indicated_len)) if is_a_string(fin): f.close() self.__dict__ = tmp_dict
def __setattr__(self, name, value): if self.frozen: raise Enumeration.ConstError('Enumeration is frozen.') sd = self.__dict__ if name in sd: raise Enumeration.ConstError('Cannot rebind %s.' % name) i, desc = self._next_val, None if is_a_string(value): desc = value elif is_an_integer(value): i = value else: i, desc = value if self.unique and i in self._values: raise ValueError('Value already used: %d.' % i) sa = self._super_setattr sa(name, i) self._names.add(name) self._values.add(i) sa('_next_val', i + 1) if desc: sdd = self._descriptions sdd[name] = desc sdd[i] = desc
def zip_from_config(target, source, env): """Produce a zip file `target` from the config file in `source`. See the documentation for :func:`tar_gz_from_config` for the format of the config file. """ t = str(target[0]) base = os.path.basename(os.path.splitext(t)[0]) add_base = lambda name: _add_base(base, name) join = lambda x, y: '/'.join([x, y]) cfg = _load_archive_config(source[0]) with zipfile.ZipFile(t, 'w', zipfile.ZIP_DEFLATED) as z: for f in cfg.archive_config: if is_a_string(f): fn, an = f, add_base(f) else: fn, an = f[0], add_base(f[1]) z.write(fn, an) # It would be nice if you could add directories recursively. Oh # well... if os.path.isdir(fn): toadd = os.listdir(fn) while toadd: entry = toadd.pop(0) fne = join(fn, entry) z.write(fne, join(an, entry)) if os.path.isdir(fne): toadd.extend(join(entry, x) for x in os.listdir(fne))
def zip_from_config(target, source, env): """Produce a zip file `target` from the config file in `source`. See the documentation for :func:`tar_gz_from_config` for the format of the config file. """ t = str(target[0]) base = os.path.basename(os.path.splitext(t)[0]) add_base = lambda name: _add_base(base, name) join = lambda x, y: '/'.join([x,y]) cfg = _load_archive_config(source[0]) with zipfile.ZipFile(t, 'w', zipfile.ZIP_DEFLATED) as z: for f in cfg.archive_config: if is_a_string(f): fn, an = f, add_base(f) else: fn, an = f[0], add_base(f[1]) z.write(fn, an) # It would be nice if you could add directories recursively. Oh # well... if os.path.isdir(fn): toadd = os.listdir(fn) while toadd: entry = toadd.pop(0) fne = join(fn, entry) z.write(fne, join(an, entry)) if os.path.isdir(fne): toadd.extend(join(entry, x) for x in os.listdir(fne))
def dump(self, fout, append = True): ''' Dump the calling instance to fout. fout can either be a file to open or a file-like object to be written to. A to-be-opened file is appended to by default. Make append false to truncate a possibly-extant file. ''' if not self._is_serializable(): raise SelfSerializerError('Cannot serialize.') d = self.__dict__ keys = d.keys() keys.sort() f = open(fout, 'ab' if append else 'wb') if is_a_string(fout) else fout f.write('%s %s %d\n' % ( SelfSerializer._record_sep, self.__class__.__name__, len(keys))) for k in keys: self._serialize(f, k, d[k]) if is_a_string(fout): f.close()
def dump(self, fout, append=True): ''' Dump the calling instance to fout. fout can either be a file to open or a file-like object to be written to. A to-be-opened file is appended to by default. Make append false to truncate a possibly-extant file. ''' if not self._is_serializable(): raise SelfSerializerError('Cannot serialize.') d = self.__dict__ keys = d.keys() keys.sort() f = open(fout, 'ab' if append else 'wb') if is_a_string(fout) else fout f.write( '%s %s %d\n' % (SelfSerializer._record_sep, self.__class__.__name__, len(keys))) for k in keys: self._serialize(f, k, d[k]) if is_a_string(fout): f.close()
def __init__(self, start_addr, end_addr, name, type_='CONST'): if start_addr > end_addr: raise ValueError('Start address > end address: {:x} {:x}'.format( start_addr, end_addr)) if not name or not is_a_string(name): raise ValueError('Invalid name: {}'.format(name)) seg_types = 'CODE CONST DATA'.split() if type_ not in seg_types: raise ValueError('Invalid segment type: {}'.format(type_)) self.start_addr = start_addr self.end_addr = end_addr self.name = name self.type_ = type_
def __init__(self, start_addr, end_addr, name, type_ = 'CONST'): if start_addr > end_addr: raise ValueError( 'Start address > end address: {:x} {:x}'.format( start_addr, end_addr)) if not name or not is_a_string(name): raise ValueError('Invalid name: {}'.format(name)) seg_types = 'CODE CONST DATA'.split() if type_ not in seg_types: raise ValueError('Invalid segment type: {}'.format(type_)) self.start_addr = start_addr self.end_addr = end_addr self.name = name self.type_ = type_
def tar_gz_from_config(target, source, env): ''' This function produces a gzipped tar archive target from the config file given in source. The config file is simply a Python module that should have an iterable called archive_config which produces items that are either names or name/archive-name pairs (that can be indexed). The name/archive-name pairs are used to specify the name in the archive for the given file/directory being added. The target name is used to arrive at a top-level directory name for the archive. Directories are added recursively. The following config file produces the following archive structure (assuming that the files are extant, etc. and that the target name is x.tgz): archive_config = ['a', 'b/c', ['d', 'e']] x/a x/b/c x/e ''' t = str(target[0]) if t.endswith('.tgz'): base = t[:-4] elif t.endswith('.tar.gz'): base = t[:-7] else: base = os.path.splitext(t)[0] base = os.path.basename(base) add_base = lambda name: _add_base(base, name) cfg = _load_archive_config(source[0]) with contextlib.closing(tarfile.open(name=t, mode='w:gz')) as tf: for f in cfg.archive_config: if is_a_string(f): tf.add(f, add_base(f)) else: tf.add(f[0], add_base(f[1]))
def tar_gz_from_config(target, source, env): ''' This function produces a gzipped tar archive target from the config file given in source. The config file is simply a Python module that should have an iterable called archive_config which produces items that are either names or name/archive-name pairs (that can be indexed). The name/archive-name pairs are used to specify the name in the archive for the given file/directory being added. The target name is used to arrive at a top-level directory name for the archive. Directories are added recursively. The following config file produces the following archive structure (assuming that the files are extant, etc. and that the target name is x.tgz): archive_config = ['a', 'b/c', ['d', 'e']] x/a x/b/c x/e ''' t = str(target[0]) if t.endswith('.tgz'): base = t[:-4] elif t.endswith('.tar.gz'): base = t[:-7] else: base = os.path.splitext(t)[0] base = os.path.basename(base) add_base = lambda name: _add_base(base, name) cfg = _load_archive_config(source[0]) with contextlib.closing(tarfile.open(name = t, mode = 'w:gz')) as tf: for f in cfg.archive_config: if is_a_string(f): tf.add(f, add_base(f)) else: tf.add(f[0], add_base(f[1]))