Commit 18f5ac15 authored by Jérémy Bobbio's avatar Jérémy Bobbio

Use a class property for global config

It's just a bit nicer.
parent 6c9206de
......@@ -92,9 +92,9 @@ class ListToolsAction(argparse.Action):
def main():
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
Config.general.max_diff_block_lines = parsed_args.max_diff_block_lines
Config.general.max_diff_input_lines = parsed_args.max_diff_input_lines
Config.general.max_report_size = parsed_args.max_report_size
if parsed_args.debug:
logger.setLevel(logging.DEBUG)
set_locale()
......
......@@ -18,19 +18,24 @@
# along with diffoscope. If not, see <http://www.gnu.org/licenses/>.
class Config(object):
_config = None
# From http://stackoverflow.com/a/7864317
# Credits to kylealanhale
class classproperty(property):
def __get__(self, cls, owner):
return classmethod(self.fget).__get__(None, owner)()
class Config(object):
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 config(cls):
if not cls._config:
cls._config = Config()
return cls._config
@classproperty
def general(cls):
if not hasattr(cls, '_general_config'):
cls._general_config = Config()
return cls._general_config
@property
def max_diff_block_lines(self):
......
......@@ -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.config().max_diff_block_lines
max_lines = Config.general.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.config().max_diff_block_lines))
self._diff.write('%s[ %d lines removed ]\n' % (self._direction, self._block_len - Config.general.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.config().max_diff_input_lines
max_lines = Config.general.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.config().max_report_size)
print_func = create_limited_print_func(print_func, Config.general.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