chore(sync-up): synced libs/settings maker from vcsserver

This commit is contained in:
RhodeCode Admin 2024-03-05 15:36:11 +01:00
parent d00bf18b2d
commit ca042238f9
2 changed files with 34 additions and 2 deletions

View file

@ -23,6 +23,7 @@ import functools
import logging
import tempfile
import logging.config
from rhodecode.lib.type_utils import str2bool, aslist
log = logging.getLogger(__name__)
@ -34,13 +35,16 @@ set_keys = {
}
class SettingsMaker(object):
class SettingsMaker:
def __init__(self, app_settings):
self.settings = app_settings
@classmethod
def _bool_func(cls, input_val):
if isinstance(input_val, bytes):
# decode to str
input_val = input_val.decode('utf8')
return str2bool(input_val)
@classmethod
@ -61,12 +65,25 @@ class SettingsMaker(object):
input_val = input_val.lower()
return input_val
@classmethod
def _string_no_quote_func(cls, input_val, lower=True):
"""
Special case string function that detects if value is set to empty quote string
e.g.
core.binar_dir = ""
"""
input_val = cls._string_func(input_val, lower=lower)
if input_val in ['""', "''"]:
return ''
@classmethod
def _dir_func(cls, input_val, ensure_dir=False, mode=0o755):
# ensure we have our dir created
if not os.path.isdir(input_val) and ensure_dir:
os.makedirs(input_val, mode=mode)
os.makedirs(input_val, mode=mode, exist_ok=True)
if not os.path.isdir(input_val):
raise Exception(f'Dir at {input_val} does not exist')
@ -150,6 +167,7 @@ class SettingsMaker(object):
'list:newline': functools.partial(self._list_func, sep='/n'),
'list:spacesep': functools.partial(self._list_func, sep=' '),
'string': functools.partial(self._string_func, lower=lower),
'string:noquote': functools.partial(self._string_no_quote_func, lower=lower),
'dir': self._dir_func,
'dir:ensured': functools.partial(self._dir_func, ensure_dir=True),
'file': self._file_path_func,

View file

@ -167,3 +167,17 @@ def convert_special_chars(str_) -> str:
value = safe_str(str_)
converted_value = unidecode(value)
return converted_value
def splitnewlines(text: bytes):
"""
like splitlines, but only split on newlines.
"""
lines = [_l + b'\n' for _l in text.split(b'\n')]
if lines:
if lines[-1] == b'\n':
lines.pop()
else:
lines[-1] = lines[-1][:-1]
return lines