Skip to content

Commit

Permalink
config: allow empty & ws values
Browse files Browse the repository at this point in the history
  • Loading branch information
SilenZcience committed Jan 30, 2024
1 parent 9144d0e commit b4ea10f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
15 changes: 10 additions & 5 deletions cat_win/persistence/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Config:
DKW.DEFAULT_FILE_ENCODING: 'utf-8',
DKW.LARGE_FILE_SIZE: 1024 * 1024 * 100, # 100 Megabytes
DKW.STRIP_COLOR_ON_PIPE: True,
DKW.EDITOR_INDENTATION: '\t',
}

elements = list(default_dic.keys())
Expand Down Expand Up @@ -54,6 +55,7 @@ def convert_config_element(element: str, element_type: type):
(element_type):
whatever the element got converted to
"""
element = element[1:-1] # strip the quotes
if element_type == bool:
if element.upper() in ['FALSE', 'NO', 'N', '0']:
return False
Expand All @@ -77,7 +79,7 @@ def is_valid_value(value: str, value_type: type) -> bool:
(bool):
indicates whether the value is valid.
"""
if value == '':
if value is None:
return False
try:
value_type(value)
Expand Down Expand Up @@ -168,19 +170,22 @@ def save_config(self) -> None:
keyword = self.elements[int(keyword)-1] if (
0 < int(keyword) <= len(self.elements)) else keyword
print(f"Successfully selected element '{keyword}'")
print(f"The current value of '{keyword}' is '{self.const_dic[keyword]}'")
c_value_rep = repr(self.const_dic[keyword])
if c_value_rep[0] not in ['"', "'"]:
c_value_rep = f"'{c_value_rep}'"
print(f"The current value of '{keyword}' is {c_value_rep}")

value = ''
value = None
while not Config.is_valid_value(value, type(self.default_dic[keyword])):
if value != '':
print(f"Something went wrong. Invalid option '{value}'.")
print(f"Something went wrong. Invalid option: {repr(value)}.")
try:
value = input('Input new value: ')
except EOFError:
print('\nAborting due to End-of-File character...', file=sys.stderr)
return

self.config_parser['CONSTS'][keyword] = value
self.config_parser['CONSTS'][keyword] = f'"{value}"'
try:
with open(self.config_file, 'w', encoding='utf-8') as conf:
self.config_parser.write(conf)
Expand Down
20 changes: 10 additions & 10 deletions cat_win/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ class TestConfig(TestCase):
maxDiff = None

def test_convert_config_element(self):
self.assertEqual(Config.convert_config_element('15', int), 15)
self.assertEqual(Config.convert_config_element('7.4', float), 7.4)
self.assertEqual(Config.convert_config_element('15', list), ['1', '5'])
self.assertEqual(Config.convert_config_element('false', bool), False)
self.assertEqual(Config.convert_config_element('FALSE', bool), False)
self.assertEqual(Config.convert_config_element('no', bool), False)
self.assertEqual(Config.convert_config_element('n', bool), False)
self.assertEqual(Config.convert_config_element('0', bool), False)
self.assertEqual(Config.convert_config_element('nfalse', bool), True)
self.assertEqual(Config.convert_config_element('1', bool), True)
self.assertEqual(Config.convert_config_element('"15"', int), 15)
self.assertEqual(Config.convert_config_element('"7.4"', float), 7.4)
self.assertEqual(Config.convert_config_element('"15"', list), ['1', '5'])
self.assertEqual(Config.convert_config_element('"false"', bool), False)
self.assertEqual(Config.convert_config_element('"FALSE"', bool), False)
self.assertEqual(Config.convert_config_element('"no"', bool), False)
self.assertEqual(Config.convert_config_element('"n"', bool), False)
self.assertEqual(Config.convert_config_element('"0"', bool), False)
self.assertEqual(Config.convert_config_element('"nfalse"', bool), True)
self.assertEqual(Config.convert_config_element('"1"', bool), True)

def test_is_valid_value(self):
self.assertEqual(Config.is_valid_value('', int), False)
Expand Down

0 comments on commit b4ea10f

Please sign in to comment.