# RhodeCode VCSServer provides access to different vcs backends via network. # Copyright (C) 2014-2020 RhodeCode GmbH # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # 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 General Public License # along with this program; if not, write to the Free Software Foundation, # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA import logging import hashlib log = logging.getLogger(__name__) def safe_int(val, default=None): """ Returns int() of val if val is not convertable to int use default instead :param val: :param default: """ try: val = int(val) except (ValueError, TypeError): val = default return val def safe_str(str_, to_encoding=None) -> str: """ safe str function. Does few trick to turn unicode_ into string :param str_: str to encode :param to_encoding: encode to this type UTF8 default :rtype: str :returns: str object """ if isinstance(str_, str): return str_ # if it's bytes cast to str if not isinstance(str_, bytes): return str(str_) to_encoding = to_encoding or ['utf8'] if not isinstance(to_encoding, (list, tuple)): to_encoding = [to_encoding] for enc in to_encoding: try: return str(str_, enc) except UnicodeDecodeError: pass return str(str_, to_encoding[0], 'replace') def safe_bytes(str_, from_encoding=None) -> bytes: """ safe bytes function. Does few trick to turn str_ into bytes string: :param str_: string to decode :param from_encoding: encode from this type UTF8 default :rtype: unicode :returns: unicode object """ if isinstance(str_, bytes): return str_ if not isinstance(str_, str): raise ValueError('safe_bytes cannot convert other types than str: got: {}'.format(type(str_))) from_encoding = from_encoding or ['utf8'] if not isinstance(from_encoding, (list, tuple)): from_encoding = [from_encoding] for enc in from_encoding: try: return str_.encode(enc) except UnicodeDecodeError: pass return unicode(str_, from_encoding[0], 'replace') class AttributeDict(dict): def __getattr__(self, attr): return self.get(attr, None) __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ def sha1(val): return hashlib.sha1(val).hexdigest()