From 3ab46b943befdd480b24c3d459555011dd04e120 Mon Sep 17 00:00:00 2001 From: Zac Medico Date: Tue, 1 Jul 2008 10:11:10 +0000 Subject: [PATCH] Add a "prefix" keyword parameter to slot_dict_class() which controls the prefix used when mapping attribute names from keys. Use this to change the syntax from files["foo"] to files.foo (it's fewer characters to look at). svn path=/main/trunk/; revision=10869 --- pym/_emerge/__init__.py | 58 +++++++++++++++++------------------ pym/portage/cache/mappings.py | 24 +++++++++------ 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/pym/_emerge/__init__.py b/pym/_emerge/__init__.py index 9aea4415f..51bb2fddc 100644 --- a/pym/_emerge/__init__.py +++ b/pym/_emerge/__init__.py @@ -1459,7 +1459,7 @@ class EbuildFetcherAsync(SlotObject): "pid", "returncode", "files") _file_names = ("fetcher", "out") - _files_dict = slot_dict_class(_file_names) + _files_dict = slot_dict_class(_file_names, prefix="") _bufsize = 4096 def start(self): @@ -1477,7 +1477,7 @@ class EbuildFetcherAsync(SlotObject): files = self.files if log_file is not None: - files["out"] = open(log_file, "a") + files.out = open(log_file, "a") portage.util.apply_secpass_permissions(log_file, uid=portage.portage_uid, gid=portage.portage_gid, mode=0660) @@ -1488,7 +1488,7 @@ class EbuildFetcherAsync(SlotObject): if fd == sys.stderr.fileno(): sys.stderr.flush() - files["out"] = os.fdopen(os.dup(fd_pipes[1]), 'w') + files.out = os.fdopen(os.dup(fd_pipes[1]), 'w') master_fd, slave_fd = os.pipe() @@ -1526,22 +1526,22 @@ class EbuildFetcherAsync(SlotObject): self.pid = retval[0] os.close(slave_fd) - files["fetcher"] = os.fdopen(master_fd, 'r') - self.register(files["fetcher"].fileno(), + files.fetcher = os.fdopen(master_fd, 'r') + self.register(files.fetcher.fileno(), select.POLLIN, self._output_handler) def _output_handler(self, fd, event): files = self.files buf = array.array('B') try: - buf.fromfile(files["fetcher"], self._bufsize) + buf.fromfile(files.fetcher, self._bufsize) except EOFError: pass if buf: - buf.tofile(files["out"]) - files["out"].flush() + buf.tofile(files.out) + files.out.flush() else: - self.unregister(files["fetcher"].fileno()) + self.unregister(files.fetcher.fileno()) for f in files.values(): f.close() @@ -1710,7 +1710,7 @@ class EbuildPhase(SlotObject): "pid", "returncode", "files") _file_names = ("log", "stdout", "ebuild") - _files_dict = slot_dict_class(_file_names) + _files_dict = slot_dict_class(_file_names, prefix="") _bufsize = 4096 def start(self): @@ -1785,26 +1785,26 @@ class EbuildPhase(SlotObject): if logfile: os.close(slave_fd) - files["log"] = open(logfile, 'a') - files["stdout"] = os.fdopen(os.dup(fd_pipes_orig[1]), 'w') - files["ebuild"] = os.fdopen(master_fd, 'r') - self.register(files["ebuild"].fileno(), + files.log = open(logfile, 'a') + files.stdout = os.fdopen(os.dup(fd_pipes_orig[1]), 'w') + files.ebuild = os.fdopen(master_fd, 'r') + self.register(files.ebuild.fileno(), select.POLLIN, self._output_handler) def _output_handler(self, fd, event): files = self.files buf = array.array('B') try: - buf.fromfile(files["ebuild"], self._bufsize) + buf.fromfile(files.ebuild, self._bufsize) except EOFError: pass if buf: - buf.tofile(files["stdout"]) - files["stdout"].flush() - buf.tofile(files["log"]) - files["log"].flush() + buf.tofile(files.stdout) + files.stdout.flush() + buf.tofile(files.log) + files.log.flush() else: - self.unregister(files["ebuild"].fileno()) + self.unregister(files.ebuild.fileno()) for f in files.values(): f.close() @@ -2002,7 +2002,7 @@ class BinpkgFetcherAsync(SlotObject): "locked", "files", "pid", "pkg_path", "returncode", "_lock_obj") _file_names = ("fetcher", "out") - _files_dict = slot_dict_class(_file_names) + _files_dict = slot_dict_class(_file_names, prefix="") _bufsize = 4096 def __init__(self, **kwargs): @@ -2029,7 +2029,7 @@ class BinpkgFetcherAsync(SlotObject): files = self.files if log_file is not None: - files["out"] = open(log_file, "a") + files.out = open(log_file, "a") portage.util.apply_secpass_permissions(log_file, uid=portage.portage_uid, gid=portage.portage_gid, mode=0660) @@ -2041,7 +2041,7 @@ class BinpkgFetcherAsync(SlotObject): if fd == sys.stderr.fileno(): sys.stderr.flush() - files["out"] = os.fdopen(os.dup(fd_pipes[1]), 'w') + files.out = os.fdopen(os.dup(fd_pipes[1]), 'w') master_fd, slave_fd = os.pipe() fcntl.fcntl(master_fd, fcntl.F_SETFL, @@ -2100,22 +2100,22 @@ class BinpkgFetcherAsync(SlotObject): self.pid = retval[0] os.close(slave_fd) - files["fetcher"] = os.fdopen(master_fd, 'r') - self.register(files["fetcher"].fileno(), + files.fetcher = os.fdopen(master_fd, 'r') + self.register(files.fetcher.fileno(), select.POLLIN, self._output_handler) def _output_handler(self, fd, event): files = self.files buf = array.array('B') try: - buf.fromfile(files["fetcher"], self._bufsize) + buf.fromfile(files.fetcher, self._bufsize) except EOFError: pass if buf: - buf.tofile(files["out"]) - files["out"].flush() + buf.tofile(files.out) + files.out.flush() else: - self.unregister(files["fetcher"].fileno()) + self.unregister(files.fetcher.fileno()) for f in files.values(): f.close() if self.locked: diff --git a/pym/portage/cache/mappings.py b/pym/portage/cache/mappings.py index 2cddd8147..2ccc96b05 100644 --- a/pym/portage/cache/mappings.py +++ b/pym/portage/cache/mappings.py @@ -104,14 +104,17 @@ class LazyLoad(UserDict.DictMixin): _slot_dict_classes = weakref.WeakValueDictionary() -def slot_dict_class(keys): +def slot_dict_class(keys, prefix="_val_"): """ Generates mapping classes that behave similar to a dict but store values as object attributes that are allocated via __slots__. Instances of these objects have a smaller memory footprint than a normal dict object. @param keys: Fixed set of allowed keys - @type keys: iterable + @type keys: Iterable + @param prefix: a prefix to use when mapping + attribute names from keys + @type prefix: String @rtype: SlotDict @returns: A class that constructs SlotDict instances having the specified keys. @@ -120,14 +123,15 @@ def slot_dict_class(keys): keys_set = keys else: keys_set = frozenset(keys) - v = _slot_dict_classes.get(keys_set) + v = _slot_dict_classes.get((keys_set, prefix)) if v is None: class SlotDict(object): allowed_keys = keys_set + _prefix = prefix __slots__ = ("__weakref__",) + \ - tuple("_val_" + k for k in allowed_keys) + tuple(prefix + k for k in allowed_keys) def __iter__(self): for k, v in self.iteritems(): @@ -145,7 +149,7 @@ def slot_dict_class(keys): def iteritems(self): for k in self.allowed_keys: try: - yield (k, getattr(self, "_val_" + k)) + yield (k, getattr(self, self._prefix + k)) except AttributeError: pass @@ -161,12 +165,12 @@ def slot_dict_class(keys): def __delitem__(self, k): try: - delattr(self, "_val_" + k) + delattr(self, self._prefix + k) except AttributeError: raise KeyError(k) def __setitem__(self, k, v): - setattr(self, "_val_" + k, v) + setattr(self, self._prefix + k, v) def setdefault(self, key, default=None): try: @@ -186,7 +190,7 @@ def slot_dict_class(keys): def __getitem__(self, k): try: - return getattr(self, "_val_" + k) + return getattr(self, self._prefix + k) except AttributeError: raise KeyError(k) @@ -197,7 +201,7 @@ def slot_dict_class(keys): return default def __contains__(self, k): - return hasattr(self, "_val_" + k) + return hasattr(self, self._prefix + k) def has_key(self, k): return k in self @@ -232,7 +236,7 @@ def slot_dict_class(keys): def clear(self): for k in self.allowed_keys: try: - delattr(self, "_val_" + k) + delattr(self, self._prefix + k) except AttributeError: pass -- 2.26.2