Browse Source

modify summary_watcher to reduce cyclomatic complexity

tags/v0.2.0-alpha
luopengting 5 years ago
parent
commit
57dd459557
3 changed files with 65 additions and 21 deletions
  1. +9
    -0
      mindinsight/datavisual/common/exceptions.py
  2. +42
    -21
      mindinsight/datavisual/data_transform/summary_watcher.py
  3. +14
    -0
      mindinsight/datavisual/utils/tools.py

+ 9
- 0
mindinsight/datavisual/common/exceptions.py View File

@@ -81,3 +81,12 @@ class NodeNotInGraphError(MindInsightException):
super(NodeNotInGraphError, self).__init__(DataVisualErrors.NODE_NOT_IN_GRAPH_ERROR, super(NodeNotInGraphError, self).__init__(DataVisualErrors.NODE_NOT_IN_GRAPH_ERROR,
error_msg, error_msg,
http_code=400) http_code=400)


class MaxCountExceededException(MindInsightException):
"""Count is out of limit."""
def __init__(self):
error_msg = "Count is out of limit."
super(MaxCountExceededException, self).__init__(DataVisualErrors.NODE_NOT_IN_GRAPH_ERROR,
error_msg,
http_code=400)

+ 42
- 21
mindinsight/datavisual/data_transform/summary_watcher.py View File

@@ -21,6 +21,8 @@ from pathlib import Path
from mindinsight.datavisual.common.log import logger from mindinsight.datavisual.common.log import logger
from mindinsight.datavisual.common.validation import Validation from mindinsight.datavisual.common.validation import Validation
from mindinsight.datavisual.utils.tools import Counter
from mindinsight.utils.exceptions import ParamValueError
from mindinsight.utils.exceptions import FileSystemPermissionError from mindinsight.utils.exceptions import FileSystemPermissionError
@@ -42,6 +44,7 @@ class SummaryWatcher:
Args: Args:
summary_base_dir (str): Path of summary base directory. summary_base_dir (str): Path of summary base directory.
overall (bool): Limit the total num of scanning if overall is False.
Returns: Returns:
list, list of summary directory info, each of which including the following attributes. list, list of summary directory info, each of which including the following attributes.
@@ -67,7 +70,11 @@ class SummaryWatcher:
return [] return []
summary_dict = {} summary_dict = {}
scan_count = 0
if not overall:
counter = Counter(max_count=self.MAX_SCAN_COUNT)
else:
counter = Counter()
try: try:
entries = os.scandir(summary_base_dir) entries = os.scandir(summary_base_dir)
@@ -78,6 +85,12 @@ class SummaryWatcher:
for entry in entries: for entry in entries:
if len(summary_dict) == self.MAX_SUMMARY_DIR_COUNT: if len(summary_dict) == self.MAX_SUMMARY_DIR_COUNT:
break break
try:
counter.add()
except ParamValueError:
logger.info('Stop further scanning due to overall is False and '
'number of scanned files exceeds upper limit.')
break
relative_path = os.path.join('.', '') relative_path = os.path.join('.', '')
if entry.is_symlink(): if entry.is_symlink():
pass pass
@@ -85,30 +98,12 @@ class SummaryWatcher:
self._update_summary_dict(summary_dict, relative_path, entry) self._update_summary_dict(summary_dict, relative_path, entry)
elif entry.is_dir(): elif entry.is_dir():
full_path = os.path.realpath(os.path.join(summary_base_dir, entry.name)) full_path = os.path.realpath(os.path.join(summary_base_dir, entry.name))
try: try:
subdir_entries = os.scandir(full_path) subdir_entries = os.scandir(full_path)
except PermissionError: except PermissionError:
logger.warning('Path of %s under summary base directory is not accessible.', entry.name) logger.warning('Path of %s under summary base directory is not accessible.', entry.name)
else:
for subdir_entry in subdir_entries:
if len(summary_dict) == self.MAX_SUMMARY_DIR_COUNT:
break
subdir_relative_path = os.path.join('.', entry.name)
if subdir_entry.is_symlink():
pass
elif subdir_entry.is_file():
self._update_summary_dict(summary_dict, subdir_relative_path, subdir_entry)
scan_count += 1
if not overall and scan_count >= self.MAX_SCAN_COUNT:
break
scan_count += 1
if not overall and scan_count >= self.MAX_SCAN_COUNT:
logger.info('Stop further scanning due to overall is False and '
'number of scanned files exceeds upper limit.')
break
continue
self._scan_subdir_entries(summary_dict, subdir_entries, entry.name, counter)
directories = [{ directories = [{
'relative_path': key, 'relative_path': key,
@@ -121,6 +116,32 @@ class SummaryWatcher:
return directories return directories
def _scan_subdir_entries(self, summary_dict, subdir_entries, entry_name, counter):
"""
Scan subdir entries.
Args:
summary_dict (dict): Temporary data structure to hold summary directory info.
subdir_entries(DirEntry): Directory entry instance.
entry_name (str): Name of entry.
counter (Counter): An instance of CountLimiter.
"""
for subdir_entry in subdir_entries:
if len(summary_dict) == self.MAX_SUMMARY_DIR_COUNT:
break
try:
counter.add()
except ParamValueError:
logger.info('Stop further scanning due to overall is False and '
'number of scanned files exceeds upper limit.')
break
subdir_relative_path = os.path.join('.', entry_name)
if subdir_entry.is_symlink():
pass
elif subdir_entry.is_file():
self._update_summary_dict(summary_dict, subdir_relative_path, subdir_entry)
def _contains_null_byte(self, **kwargs): def _contains_null_byte(self, **kwargs):
""" """
Check if arg contains null byte. Check if arg contains null byte.


+ 14
- 0
mindinsight/datavisual/utils/tools.py View File

@@ -20,6 +20,7 @@ import os
from numbers import Number from numbers import Number
from urllib.parse import unquote from urllib.parse import unquote


from mindinsight.datavisual.common.exceptions import MaxCountExceededException
from mindinsight.utils import exceptions from mindinsight.utils import exceptions


_IMG_EXT_TO_MIMETYPE = { _IMG_EXT_TO_MIMETYPE = {
@@ -153,3 +154,16 @@ def if_nan_inf_to_none(name, value):
if math.isnan(value) or math.isinf(value): if math.isnan(value) or math.isinf(value):
value = None value = None
return value return value


class Counter:
"""Count accumulator with limit checking."""
def __init__(self, max_count=None, init_count=0):
self._count = init_count
self._max_count = max_count

def add(self, value=1):
"""Add value."""
if self._max_count is not None and self._count + value > self._max_count:
raise MaxCountExceededException()
self._count += value

Loading…
Cancel
Save