Refactoring common/file_utils.py
- misleading name `load_files` for a method does not load file, renamed - exception already handled, unnecessary arg validation when calling removed - log exception to error for diagnostic - check directory existance before listing Change-Id: I35c19989338fd0411f253279412a88a608d4f131
This commit is contained in:
parent
0f9d08adcf
commit
29f5cf403b
|
@ -20,26 +20,30 @@ import yaml
|
|||
LOG = log.getLogger(__name__)
|
||||
|
||||
|
||||
def load_files(dir_path,
|
||||
def list_files(dir_path,
|
||||
suffix=None,
|
||||
with_pathname=False,
|
||||
with_exception=False):
|
||||
if not os.path.isdir(dir_path):
|
||||
return []
|
||||
|
||||
try:
|
||||
loaded_files = os.listdir(dir_path)
|
||||
except Exception:
|
||||
file_list = os.listdir(dir_path)
|
||||
except Exception as e:
|
||||
if with_exception:
|
||||
raise
|
||||
else:
|
||||
LOG.error("Fails to list files in %s: %s" % (dir_path, e))
|
||||
return []
|
||||
if suffix:
|
||||
loaded_files = [dir_path + '/' + f if with_pathname else f
|
||||
for f in loaded_files if f.endswith(suffix)]
|
||||
file_list = [os.path.join(dir_path, f) if with_pathname else f
|
||||
for f in file_list if f.endswith(suffix)]
|
||||
|
||||
return loaded_files
|
||||
return file_list
|
||||
|
||||
|
||||
def load_yaml_files(dir_path, with_exception=False):
|
||||
files = load_files(dir_path, '.yaml', with_pathname=True)
|
||||
files = list_files(dir_path, '.yaml', with_pathname=True)
|
||||
|
||||
yaml_files = []
|
||||
for f in files:
|
||||
|
|
|
@ -13,7 +13,6 @@
|
|||
# under the License.
|
||||
|
||||
import copy
|
||||
import os
|
||||
|
||||
from vitrage.common.constants import DatasourceProperties as DSProps
|
||||
from vitrage.common.constants import EventAction
|
||||
|
@ -52,15 +51,14 @@ class StaticPhysicalDriver(DriverBase):
|
|||
def _get_all_entities(self):
|
||||
static_entities = []
|
||||
|
||||
if os.path.isdir(self.cfg.static_physical.directory):
|
||||
files = file_utils.load_files(
|
||||
self.cfg.static_physical.directory, '.yaml')
|
||||
files = file_utils.list_files(
|
||||
self.cfg.static_physical.directory, '.yaml')
|
||||
|
||||
for file_ in files:
|
||||
full_path = self.cfg.static_physical.directory \
|
||||
+ '/' + file_
|
||||
static_entities += self._get_entities_from_file(file_,
|
||||
full_path)
|
||||
for file_ in files:
|
||||
full_path = self.cfg.static_physical.directory \
|
||||
+ '/' + file_
|
||||
static_entities += self._get_entities_from_file(file_,
|
||||
full_path)
|
||||
|
||||
return static_entities
|
||||
|
||||
|
@ -78,7 +76,7 @@ class StaticPhysicalDriver(DriverBase):
|
|||
def _get_changes_entities(self):
|
||||
|
||||
entities_updates = []
|
||||
files = file_utils.load_files(
|
||||
files = file_utils.list_files(
|
||||
self.cfg.static_physical.directory, '.yaml')
|
||||
|
||||
for file_ in files:
|
||||
|
|
|
@ -107,7 +107,7 @@ class DatasourceInfoMapper(object):
|
|||
valid_datasources_conf = {}
|
||||
erroneous_datasources_conf = []
|
||||
|
||||
files = file_utils.load_files(
|
||||
files = file_utils.list_files(
|
||||
self.conf.entity_graph.datasources_values_dir, '.yaml')
|
||||
|
||||
for file_name in files:
|
||||
|
|
Loading…
Reference in New Issue