##// END OF EJS Templates
deps: bumped rhodecode-tools==4.0.0
deps: bumped rhodecode-tools==4.0.0

File last commit:

r5096:a0018795 default
r5592:2f9a9e11 default
Show More
type_utils.py
91 lines | 2.5 KiB | text/x-python | PythonLexer
copyrights: updated for 2023
r5088 # Copyright (C) 2011-2023 RhodeCode GmbH
core: break down some utils for better imports
r4915 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import logging
log = logging.getLogger(__name__)
def str2bool(str_):
"""
returns True/False value from given string, it tries to translate the
string into boolean
:param str_: string value to translate into boolean
:rtype: boolean
:returns: boolean from given string
"""
if str_ is None:
return False
if str_ in (True, False):
return str_
str_ = str(str_).strip().lower()
return str_ in ('t', 'true', 'y', 'yes', 'on', '1')
core: multiple fixes to unicode vs str usage...
r5065 def aslist(obj, sep=None, strip=True) -> list:
core: break down some utils for better imports
r4915 """
Returns given string separated by sep as list
:param obj:
:param sep:
:param strip:
"""
if isinstance(obj, str):
core: multiple fixes to unicode vs str usage...
r5065 if obj in ['', ""]:
return []
core: break down some utils for better imports
r4915 lst = obj.split(sep)
if strip:
lst = [v.strip() for v in lst]
return lst
elif isinstance(obj, (list, tuple)):
return obj
elif obj is None:
return []
else:
return [obj]
core: multiple fixes to unicode vs str usage...
r5065
class AttributeDictBase(dict):
def __getstate__(self):
odict = self.__dict__ # get attribute dictionary
return odict
def __setstate__(self, dict):
self.__dict__ = dict
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
class StrictAttributeDict(AttributeDictBase):
"""
Strict Version of Attribute dict which raises an Attribute error when
requested attribute is not set
"""
def __getattr__(self, attr):
try:
return self[attr]
except KeyError:
raise AttributeError(f'{self.__class__} object has no attribute {attr}')
class AttributeDict(AttributeDictBase):
def __getattr__(self, attr):
return self.get(attr, None)