From 80c3dd7572329b4352b5637767b9d1786ce41011 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 6 Jul 2017 13:30:57 +0200 Subject: [PATCH 1/2] Ignore directory entries that begin with dot('.'). --- cdist/core/__init__.py | 1 + cdist/core/cdist_object.py | 2 +- cdist/core/cdist_type.py | 9 +++++---- cdist/core/util.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 cdist/core/util.py diff --git a/cdist/core/__init__.py b/cdist/core/__init__.py index 41e00a3a..8c384b3c 100644 --- a/cdist/core/__init__.py +++ b/cdist/core/__init__.py @@ -27,3 +27,4 @@ from cdist.core.cdist_object import IllegalObjectIdError from cdist.core.explorer import Explorer from cdist.core.manifest import Manifest from cdist.core.code import Code +from cdist.core.util import listdir diff --git a/cdist/core/cdist_object.py b/cdist/core/cdist_object.py index 262db8bf..3167792b 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -107,7 +107,7 @@ class CdistObject(object): @classmethod def list_type_names(cls, object_base_path): """Return a list of type names""" - return os.listdir(object_base_path) + return cdist.core.listdir(object_base_path) @staticmethod def split_name(object_name): diff --git a/cdist/core/cdist_type.py b/cdist/core/cdist_type.py index 14865386..9e2446f4 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -23,6 +23,7 @@ import os import cdist +import cdist.core class NoSuchTypeError(cdist.Error): @@ -75,7 +76,7 @@ class CdistType(object): @classmethod def list_type_names(cls, base_path): """Return a list of type names""" - return os.listdir(base_path) + return cdist.core.listdir(base_path) _instances = {} @@ -117,8 +118,8 @@ class CdistType(object): """Return a list of available explorers""" if not self.__explorers: try: - self.__explorers = os.listdir(os.path.join(self.absolute_path, - "explorer")) + self.__explorers = cdist.core.listdir( + os.path.join(self.absolute_path, "explorer")) except EnvironmentError: # error ignored self.__explorers = [] @@ -222,7 +223,7 @@ class CdistType(object): defaults_dir = os.path.join(self.absolute_path, "parameter", "default") - for name in os.listdir(defaults_dir): + for name in cdist.core.listdir(defaults_dir): try: with open(os.path.join(defaults_dir, name)) as fd: defaults[name] = fd.read().strip() diff --git a/cdist/core/util.py b/cdist/core/util.py new file mode 100644 index 00000000..a8216ef9 --- /dev/null +++ b/cdist/core/util.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# +# 2017 Darko Poljak (darko.poljak at gmail.com) +# +# This file is part of cdist. +# +# cdist is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# cdist is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with cdist. If not, see . +# +# + +import os + + +def listdir(path='.', include_dot=False): + """os.listdir but do not include entries whose names begin with a dot('.') + if include_dot is False. + """ + return [x for x in os.listdir(path) if include_dot or not _ishidden(x)] + + +def _ishidden(path): + return path[0] in ('.', b'.'[0]) From f9b632eebb90658a92b84420ef1b388d266c3151 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 6 Jul 2017 17:22:25 +0200 Subject: [PATCH 2/2] Improve listdir. --- cdist/core/util.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cdist/core/util.py b/cdist/core/util.py index a8216ef9..ca48c4c8 100644 --- a/cdist/core/util.py +++ b/cdist/core/util.py @@ -26,7 +26,10 @@ def listdir(path='.', include_dot=False): """os.listdir but do not include entries whose names begin with a dot('.') if include_dot is False. """ - return [x for x in os.listdir(path) if include_dot or not _ishidden(x)] + if include_dot: + return os.listdir(path) + else: + return [x for x in os.listdir(path) if not _ishidden(x)] def _ishidden(path):