Commit 0211ded8 authored by Reiner Herrmann's avatar Reiner Herrmann

Refactor configuration to use Python properties

parent 334ecba6
......@@ -92,9 +92,9 @@ class ListToolsAction(argparse.Action):
def main():
parser = create_parser()
parsed_args = parser.parse_args(sys.argv[1:])
Config.setMaxDiffBlockLines(parsed_args.max_diff_block_lines)
Config.setMaxDiffInputLines(parsed_args.max_diff_input_lines)
Config.setMaxReportSize(parsed_args.max_report_size)
Config.config().max_diff_block_lines = parsed_args.max_diff_block_lines
Config.config().max_diff_input_lines = parsed_args.max_diff_input_lines
Config.config().max_report_size = parsed_args.max_report_size
if parsed_args.debug:
logger.setLevel(logging.DEBUG)
set_locale()
......
......@@ -19,34 +19,43 @@
class Config(object):
MAX_DIFF_BLOCK_LINES = 50
MAX_DIFF_INPUT_LINES = 100000 # GNU diff cannot process arbitrary large files :(
MAX_REPORT_SIZE = 2000 * 2 ** 10 # 2000 kB
_config = None
@classmethod
def setMaxDiffBlockLines(cls, lines):
if lines:
cls.MAX_DIFF_BLOCK_LINES = lines
@classmethod
def setMaxDiffInputLines(cls, lines):
if lines:
cls.MAX_DIFF_INPUT_LINES = lines
@classmethod
def setMaxReportSize(cls, bytes):
if bytes:
cls.MAX_REPORT_SIZE = bytes
@classmethod
def maxReportSize(cls):
return cls.MAX_REPORT_SIZE
@classmethod
def maxDiffBlockLines(cls):
return cls.MAX_DIFF_BLOCK_LINES
def __init__(self):
self._max_diff_block_lines = 50
self._max_diff_input_lines = 100000 # GNU diff cannot process arbitrary large files :(
self._max_report_size = 2000 * 2 ** 10 # 2000 kB
@classmethod
def maxDiffInputLines(cls):
return cls.MAX_DIFF_INPUT_LINES
def config(cls):
if not cls._config:
cls._config = Config()
return cls._config
@property
def max_diff_block_lines(self):
return self._max_diff_block_lines
@property
def max_diff_input_lines(self):
return self._max_diff_input_lines
@property
def max_report_size(self):
return self._max_report_size
@max_diff_block_lines.setter
def max_diff_block_lines(self, value):
if value:
self._max_diff_block_lines = value
@max_diff_input_lines.setter
def max_diff_input_lines(self, value):
if value:
self._max_diff_input_lines = value
@max_report_size.setter
def max_report_size(self, value):
if value:
self._max_report_size = value
......@@ -104,7 +104,7 @@ class DiffParser(object):
self._diff.write(line)
if line[0] in ('-', '+') and line[0] == self._direction:
self._block_len += 1
max_lines = Config.maxDiffBlockLines()
max_lines = Config.config().max_diff_block_lines
if max_lines > 0 and self._block_len >= max_lines:
return self.skip_block
else:
......@@ -114,7 +114,7 @@ class DiffParser(object):
def skip_block(self, line):
if self._remaining_hunk_lines == 0 or line[0] != self._direction:
self._diff.write('%s[ %d lines removed ]\n' % (self._direction, self._block_len - Config.maxDiffBlockLines()))
self._diff.write('%s[ %d lines removed ]\n' % (self._direction, self._block_len - Config.config().max_diff_block_lines))
return self.read_hunk(line)
self._block_len += 1
self._remaining_hunk_lines -= 1
......@@ -229,7 +229,7 @@ def make_feeder_from_file(in_file, filter=lambda buf: buf.encode('utf-8')):
for buf in in_file.readlines():
line_count += 1
out_file.write(filter(buf))
max_lines = Config.maxDiffInputLines()
max_lines = Config.config().max_diff_input_lines
if max_lines > 0 and line_count >= max_lines:
out_file.write('[ Too much input for diff ]%s\n' % (' ' * out_file.fileno()))
end_nl = True
......
......@@ -515,7 +515,7 @@ def output_header(css_url, print_func):
def output_html(difference, css_url=None, print_func=None):
if print_func is None:
print_func = print
print_func = create_limited_print_func(print_func, Config.maxReportSize())
print_func = create_limited_print_func(print_func, Config.config().max_report_size)
try:
output_header(css_url, print_func)
output_difference(difference, print_func, [])
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment