summaryrefslogtreecommitdiffstats
path: root/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
diff options
context:
space:
mode:
authorChris St. Pierre <chris.a.st.pierre@gmail.com>2012-10-03 15:28:54 -0400
committerChris St. Pierre <chris.a.st.pierre@gmail.com>2012-10-03 15:28:54 -0400
commit3a16a7cb5a7c4142890af4b32841e15dbaa40556 (patch)
tree6df5d73662e35082a4729a841b71f008ad6f2250 /src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
parent5b6e588af64e89eeafbe54df137416a9be102482 (diff)
downloadbcfg2-3a16a7cb5a7c4142890af4b32841e15dbaa40556.tar.gz
bcfg2-3a16a7cb5a7c4142890af4b32841e15dbaa40556.tar.bz2
bcfg2-3a16a7cb5a7c4142890af4b32841e15dbaa40556.zip
Cfg: fixed regexes for handling and ignoring files with different Cfg handlers
Diffstat (limited to 'src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py')
-rw-r--r--src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py56
1 files changed, 17 insertions, 39 deletions
diff --git a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
index 0ab7fd98e..82b792fe0 100644
--- a/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
+++ b/src/lib/Bcfg2/Server/Plugins/Cfg/__init__.py
@@ -44,11 +44,11 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
#: indicators).
__extensions__ = []
- #: This handler ignores files with the listed extensions. A file
- #: that is ignored by a handler will not be handled by any other
- #: handlers; that is, a file is ignored if any handler ignores it.
- #: Ignoring a file is not simply a means to defer handling of that
- #: file to another handler.
+ #: This handler ignores all files with the listed extensions. A
+ #: file that is ignored by a handler will not be handled by any
+ #: other handlers; that is, a file is ignored if any handler
+ #: ignores it. Ignoring a file is not simply a means to defer
+ #: handling of that file to another handler.
__ignore__ = []
#: Whether or not the files handled by this handler are permitted
@@ -63,7 +63,7 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
Bcfg2.Server.Plugin.SpecificData.__init__(self, name, specific,
encoding)
self.encoding = encoding
- self.regex = self.__class__.get_regex(basename=name)
+ self.regex = self.__class__.get_regex([name])
__init__.__doc__ = Bcfg2.Server.Plugin.SpecificData.__init__.__doc__ + \
"""
.. -----
@@ -73,7 +73,7 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
.. autoattribute:: Bcfg2.Server.Plugins.Cfg.CfgBaseFileMatcher.__specific__"""
@classmethod
- def get_regex(cls, basename=None, extensions=None):
+ def get_regex(cls, basenames):
""" Get a compiled regular expression to match filenames (not
full paths) that this handler handles.
@@ -83,23 +83,15 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
directory that contains the files the regex
will be applied to)
:type basename: string
- :param extensions: Override the default list of
- :attr:`Bcfg2.Server.Plugins.Cfg.CfgBaseFileMatcher.__extensions__`
- to include in the regex.
- :type extensions: list of strings
:returns: compiled regex
"""
- if extensions is None:
- extensions = cls.__extensions__
- if cls.__basenames__:
- basename = '|'.join(cls.__basenames__)
-
- components = ['^(?P<basename>%s)' % basename]
+ components = ['^(?P<basename>%s)' % '|'.join(basenames)]
if cls.__specific__:
components.append('(|\\.H_(?P<hostname>\S+?)|' +
- '.G(?P<prio>\d+)_(?P<group>\S+?))')
- if extensions:
- components.append('\\.(?P<extension>%s)' % '|'.join(extensions))
+ '\.G(?P<prio>\d+)_(?P<group>\S+?))')
+ if cls.__extensions__:
+ components.append('\\.(?P<extension>%s)' %
+ '|'.join(cls.__extensions__))
components.append('$')
return re.compile("".join(components))
@@ -124,15 +116,12 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
if cls.__basenames__:
basenames = cls.__basenames__
else:
- basenames = [basename]
+ basenames = [os.path.basename(basename)]
- return any(
- cls.get_regex(
- basename=os.path.basename(bname)).match(event.filename)
- for bname in basenames)
+ return bool(cls.get_regex(basenames).match(event.filename))
@classmethod
- def ignore(cls, event, basename=None):
+ def ignore(cls, event, basename=None): # pylint: disable=W0613
""" Return True if this handler ignores the file described by
``event``. See
:attr:`Bcfg2.Server.Plugins.Cfg.CfgBaseFileMatcher.__ignore__`
@@ -149,18 +138,7 @@ class CfgBaseFileMatcher(Bcfg2.Server.Plugin.SpecificData):
:returns: bool - True if this handler handles the file listed
in the event, False otherwise.
"""
- if not cls.__ignore__:
- return False
-
- if cls.__basenames__:
- basenames = cls.__basenames__
- else:
- basenames = [basename]
-
- return any(
- cls.get_regex(basename=os.path.basename(bname),
- extensions=cls.__ignore__).match(event.filename)
- for bname in basenames)
+ return any(event.filename.endswith("." + e) for e in cls.__ignore__)
def __str__(self):
return "%s(%s)" % (self.__class__.__name__, self.name)
@@ -423,7 +401,7 @@ class CfgEntrySet(Bcfg2.Server.Plugin.EntrySet):
if hdlr.__specific__:
Bcfg2.Server.Plugin.EntrySet.entry_init(
self, event, entry_type=hdlr,
- specific=hdlr.get_regex(os.path.basename(self.path)))
+ specific=hdlr.get_regex([os.path.basename(self.path)]))
else:
if event.filename in self.entries:
LOGGER.warn("Got duplicate add for %s" % event.filename)