From 97929938006be5405be632ee1181b2e1a30f9620 Mon Sep 17 00:00:00 2001 From: Alec Warner Date: Fri, 7 Sep 2007 10:17:26 +0000 Subject: [PATCH] Fix minor issues with loaders in the static file loader, fix quoting and import style in news.py. Add TextFileLoader and EnvLoader loaders svn path=/main/trunk/; revision=7753 --- pym/portage/env/loaders.py | 35 ++++++++++++++++++++++++++++++++++- pym/portage/news.py | 35 ++++++++++++++++++----------------- pym/portage/sets/files.py | 11 ++++++----- 3 files changed, 58 insertions(+), 23 deletions(-) diff --git a/pym/portage/env/loaders.py b/pym/portage/env/loaders.py index ae9579a86..e7f5289a7 100644 --- a/pym/portage/env/loaders.py +++ b/pym/portage/env/loaders.py @@ -67,6 +67,40 @@ class DataLoader(object): """ raise NotImplementedError("Please override in a subclass") +class EnvLoader(DataLoader): + """ Class to access data in the environment """ + def __init__(self, validator): + DataLoader.__init__(self, validator) + + def load(self): + return os.environ + +class TestTextLoader(DataLoader): + """ You give it some data, it 'loads' it for you, no filesystem access + """ + def __init__(self, validator): + DataLoader.__init__(self, validator) + self.data = {} + self.errors = {} + + def setData(self, text): + """Explicitly set the data field + Args: + text - a dict of data typical of Loaders + Returns: + None + """ + if isinstance(text, dict): + self.data = text + else: + raise ValueError("setData requires a dict argument") + + def setErrors(self, errors): + self.errors = errors + + def load(self): + return (self.data, self.errors) + class FileLoader(DataLoader): """ Class to access data in files """ @@ -234,4 +268,3 @@ class KeyValuePairFileLoader(FileLoader): data[key].append(value) else: data[key] = value - diff --git a/pym/portage/news.py b/pym/portage/news.py index 702a377a5..dff66e3da 100644 --- a/pym/portage/news.py +++ b/pym/portage/news.py @@ -3,12 +3,13 @@ # Distributed under the terms of the GNU General Public License v2 # $Id$ +import os +import re from portage.const import INCREMENTALS, PROFILE_PATH, NEWS_LIB_PATH -from portage.util import ensure_dirs, apply_permissions +from portage.util import ensure_dirs, apply_permissions, normalize_path from portage.data import portage_gid from portage.locks import lockfile, unlockfile, lockdir, unlockdir from portage.exception import FileNotFound -import os, re class NewsManager(object): """ @@ -23,7 +24,7 @@ class NewsManager(object): """ - TIMESTAMP_FILE = "news-timestamp" + TIMESTAMP_FILE = 'news-timestamp' def __init__(self, portdb, vardb, NEWS_PATH, UNREAD_PATH, LANGUAGE_ID='en'): self.NEWS_PATH = NEWS_PATH @@ -37,8 +38,7 @@ class NewsManager(object): self.portdb = portdb portdir = portdb.porttree_root - profiles_base = os.path.join(portdir, "profiles") + os.path.sep - from portage.util import normalize_path + profiles_base = os.path.join(portdir, 'profiles') + os.path.sep profile_path = normalize_path( os.path.realpath(portdb.mysettings.profile_path)) if profile_path.startswith(profiles_base): @@ -89,7 +89,7 @@ class NewsManager(object): updates.append(item) del path - path = os.path.join(self.UNREAD_PATH, "news-"+repoid+".unread") + path = os.path.join(self.UNREAD_PATH, 'news-' + repoid + '.unread') try: unread_lock = lockfile(path) if not os.path.exists(path): @@ -97,9 +97,9 @@ class NewsManager(object): open(path, "w") # Ensure correct perms on the unread file. apply_permissions( filename=path, - uid=int(self.config["PORTAGE_INST_UID"]), gid=portage_gid, mode=0664) + uid=int(self.config['PORTAGE_INST_UID']), gid=portage_gid, mode=0664) # Make sure we have the correct permissions when created - unread_file = open(path, "a") + unread_file = open(path, 'a') for item in updates: unread_file.write(item.name + "\n") @@ -108,7 +108,7 @@ class NewsManager(object): unlockfile(unread_lock) # Touch the timestamp file - f = open(timestamp_file, "w") + f = open(timestamp_file, 'w') f.close() def getUnreadItems(self, repoid, update=False): @@ -122,13 +122,13 @@ class NewsManager(object): if update: self.updateItems(repoid) - unreadfile = os.path.join(self.UNREAD_PATH, "news-"+repoid+".unread") + unreadfile = os.path.join(self.UNREAD_PATH, 'news-' + repoid + '.unread') try: try: unread_lock = lockfile(unreadfile) # Set correct permissions on the news-repoid.unread file apply_permissions(filename=unreadfile, - uid=int(self.config["PORTAGE_INST_UID"]), gid=portage_gid, mode=0664) + uid=int(self.config['PORTAGE_INST_UID']), gid=portage_gid, mode=0664) if os.path.exists(unreadfile): unread = open(unreadfile).readlines() @@ -181,9 +181,10 @@ class NewsItem(object): if not len(self.restrictions): return True # no restrictions to match means everyone should see it - kwargs = { 'vardb' : vardb, - 'config' : config, - 'profile' : profile } + kwargs = \ + { 'vardb' : vardb, + 'config' : config, + 'profile' : profile } for restriction in self.restrictions: if restriction.checkRestriction(**kwargs): @@ -197,7 +198,7 @@ class NewsItem(object): for line in lines: #Optimization to ignore regex matchines on lines that #will never match - if not line.startswith("D"): + if not line.startswith('D'): continue restricts = { _installedRE : DisplayInstalledRestriction, _profileRE : DisplayProfileRestriction, @@ -224,7 +225,7 @@ class DisplayRestriction(object): """ def checkRestriction(self, **kwargs): - raise NotImplementedError("Derived class should over-ride this method") + raise NotImplementedError('Derived class should over-ride this method') class DisplayProfileRestriction(DisplayRestriction): """ @@ -250,7 +251,7 @@ class DisplayKeywordRestriction(DisplayRestriction): self.keyword = keyword def checkRestriction(self, **kwargs): - if kwargs['config']["ARCH"] == self.keyword: + if kwargs['config']['ARCH'] == self.keyword: return True return False diff --git a/pym/portage/sets/files.py b/pym/portage/sets/files.py index a3875ada8..05a44e7bd 100644 --- a/pym/portage/sets/files.py +++ b/pym/portage/sets/files.py @@ -17,13 +17,14 @@ from portage.env.validators import PackagesFileValidator, ValidAtomValidator class StaticFileSet(EditablePackageSet): _operations = ["merge", "unmerge"] - def __init__(self, name, filename, loader=None): + def __init__(self, name, filename, data = None): super(StaticFileSet, self).__init__(name) self._filename = filename self._mtime = None self.description = "Package set loaded from file %s" % self._filename - if loader is None: - self.loader = ConfigLoaderKlass(ItemFileLoader(filename=self._filename, + self.data = data + if data is None: + self.data = ConfigLoaderKlass(ItemFileLoader(filename=self._filename, validator=PackagesFileValidator)) metadata = grabfile(self._filename + ".metadata") key = None @@ -54,12 +55,12 @@ class StaticFileSet(EditablePackageSet): mtime = None if (not self._loaded or self._mtime != mtime): try: - self.loader.load() + self.data.load() except EnvironmentError, e: if e.errno != errno.ENOENT: raise del e - self._setAtoms(self.loader.keys()) + self._setAtoms(self.data.keys()) self._mtime = mtime class ConfigFileSet(PackageSet): -- 2.26.2