mirror of
https://github.com/EDCD/EDMarketConnector.git
synced 2025-06-06 10:23:06 +03:00
config.py: Remove u-prefix from strings. More specific Exceptions
* u-prefix is un-necessary, remove it. * We should get ValueError in some cases, so catch it first. * Change debug log for if linux config file not yet present.
This commit is contained in:
parent
0274c98e50
commit
cb795b2230
27
config.py
27
config.py
@ -26,7 +26,7 @@ appcmdname = 'EDMC'
|
|||||||
appversion = '4.2.0-beta1' # -rc1+a872b5f'
|
appversion = '4.2.0-beta1' # -rc1+a872b5f'
|
||||||
# For some things we want appversion without (possible) +build metadata
|
# For some things we want appversion without (possible) +build metadata
|
||||||
appversion_nobuild = str(semantic_version.Version(appversion).truncate('prerelease'))
|
appversion_nobuild = str(semantic_version.Version(appversion).truncate('prerelease'))
|
||||||
copyright = u'© 2015-2019 Jonathan Harris, 2020-2021 EDCD'
|
copyright = '© 2015-2019 Jonathan Harris, 2020 EDCD'
|
||||||
|
|
||||||
update_feed = 'https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector.xml'
|
update_feed = 'https://raw.githubusercontent.com/EDCD/EDMarketConnector/releases/edmarketconnector.xml'
|
||||||
update_interval = 8*60*60
|
update_interval = 8*60*60
|
||||||
@ -193,6 +193,11 @@ class Config(object):
|
|||||||
"""Look up an integer configuration value."""
|
"""Look up an integer configuration value."""
|
||||||
try:
|
try:
|
||||||
return int(self.settings.get(key, 0)) # should already be int, but check by casting
|
return int(self.settings.get(key, 0)) # should already be int, but check by casting
|
||||||
|
|
||||||
|
except ValueError as e:
|
||||||
|
logger.error(f"Failed to int({key=})", exc_info=e)
|
||||||
|
return 0
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug('The exception type is ...', exc_info=e)
|
logger.debug('The exception type is ...', exc_info=e)
|
||||||
return 0
|
return 0
|
||||||
@ -321,7 +326,7 @@ class Config(object):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
elif key_type.value == REG_MULTI_SZ:
|
elif key_type.value == REG_MULTI_SZ:
|
||||||
return list(ctypes.wstring_at(buf, len(buf)-2).split(u'\x00'))
|
return list(ctypes.wstring_at(buf, len(buf)-2).split('\x00'))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
return str(buf.value)
|
return str(buf.value)
|
||||||
@ -358,7 +363,7 @@ class Config(object):
|
|||||||
|
|
||||||
elif isinstance(val, list):
|
elif isinstance(val, list):
|
||||||
# null terminated non-empty strings
|
# null terminated non-empty strings
|
||||||
string_val = u'\x00'.join([str(x) or u' ' for x in val] + [u''])
|
string_val = '\x00'.join([str(x) or ' ' for x in val] + [''])
|
||||||
buf = ctypes.create_unicode_buffer(string_val)
|
buf = ctypes.create_unicode_buffer(string_val)
|
||||||
RegSetValueEx(self.hkey, key, 0, REG_MULTI_SZ, buf, len(buf)*2)
|
RegSetValueEx(self.hkey, key, 0, REG_MULTI_SZ, buf, len(buf)*2)
|
||||||
|
|
||||||
@ -410,7 +415,7 @@ class Config(object):
|
|||||||
self.config.read_file(h)
|
self.config.read_file(h)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug('config section not yet present', exc_info=e)
|
logger.debug('Reading config failed, assuming we\'re making a new one...', exc_info=e)
|
||||||
self.config.add_section(self.SECTION)
|
self.config.add_section(self.SECTION)
|
||||||
|
|
||||||
if not self.get('outdir') or not isdir(self.get('outdir')):
|
if not self.get('outdir') or not isdir(self.get('outdir')):
|
||||||
@ -420,11 +425,11 @@ class Config(object):
|
|||||||
"""Look up a string configuration value."""
|
"""Look up a string configuration value."""
|
||||||
try:
|
try:
|
||||||
val = self.config.get(self.SECTION, key)
|
val = self.config.get(self.SECTION, key)
|
||||||
if u'\n' in val: # list
|
if '\n' in val: # list
|
||||||
# ConfigParser drops the last entry if blank,
|
# ConfigParser drops the last entry if blank,
|
||||||
# so we add a spurious ';' entry in set() and remove it here
|
# so we add a spurious ';' entry in set() and remove it here
|
||||||
assert val.split('\n')[-1] == ';', val.split('\n')
|
assert val.split('\n')[-1] == ';', val.split('\n')
|
||||||
return [self._unescape(x) for x in val.split(u'\n')[:-1]]
|
return [self._unescape(x) for x in val.split('\n')[:-1]]
|
||||||
else:
|
else:
|
||||||
return self._unescape(val)
|
return self._unescape(val)
|
||||||
|
|
||||||
@ -437,6 +442,10 @@ class Config(object):
|
|||||||
try:
|
try:
|
||||||
return self.config.getint(self.SECTION, key)
|
return self.config.getint(self.SECTION, key)
|
||||||
|
|
||||||
|
except ValueError as e:
|
||||||
|
logger.error(f"Failed to int({key=})", exc_info=e)
|
||||||
|
return 0
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.debug('And the exception type is...', exc_info=e)
|
logger.debug('And the exception type is...', exc_info=e)
|
||||||
return 0
|
return 0
|
||||||
@ -450,7 +459,7 @@ class Config(object):
|
|||||||
self.config.set(self.SECTION, key, self._escape(val))
|
self.config.set(self.SECTION, key, self._escape(val))
|
||||||
|
|
||||||
elif isinstance(val, list):
|
elif isinstance(val, list):
|
||||||
self.config.set(self.SECTION, key, u'\n'.join([self._escape(x) for x in val] + [u';']))
|
self.config.set(self.SECTION, key, '\n'.join([self._escape(x) for x in val] + [';']))
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
@ -485,7 +494,7 @@ class Config(object):
|
|||||||
|
|
||||||
def _escape(self, val):
|
def _escape(self, val):
|
||||||
"""Escape a string for storage."""
|
"""Escape a string for storage."""
|
||||||
return str(val).replace(u'\\', u'\\\\').replace(u'\n', u'\\n').replace(u';', u'\\;')
|
return str(val).replace('\\', '\\\\').replace('\n', '\\n').replace(';', '\\;')
|
||||||
|
|
||||||
def _unescape(self, val):
|
def _unescape(self, val):
|
||||||
"""Un-escape a string from storage."""
|
"""Un-escape a string from storage."""
|
||||||
@ -497,7 +506,7 @@ class Config(object):
|
|||||||
if chars[i] == 'n':
|
if chars[i] == 'n':
|
||||||
chars[i] = '\n'
|
chars[i] = '\n'
|
||||||
i += 1
|
i += 1
|
||||||
return u''.join(chars)
|
return ''.join(chars)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user