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 410dfafc..2d92aa41 100644 --- a/cdist/core/cdist_object.py +++ b/cdist/core/cdist_object.py @@ -104,7 +104,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 339d5995..c2d45cdd 100644 --- a/cdist/core/cdist_type.py +++ b/cdist/core/cdist_type.py @@ -22,6 +22,7 @@ import os import cdist +import cdist.core class NoSuchTypeError(cdist.Error): @@ -74,7 +75,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 = {} @@ -116,8 +117,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 = [] @@ -221,7 +222,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..ca48c4c8 --- /dev/null +++ b/cdist/core/util.py @@ -0,0 +1,36 @@ +# -*- 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. + """ + if include_dot: + return os.listdir(path) + else: + return [x for x in os.listdir(path) if not _ishidden(x)] + + +def _ishidden(path): + return path[0] in ('.', b'.'[0])