1 2# 3# spyne - Copyright (C) Spyne contributors. 4# 5# This library is free software; you can redistribute it and/or 6# modify it under the terms of the GNU Lesser General Public 7# License as published by the Free Software Foundation; either 8# version 2.1 of the License, or (at your option) any later version. 9# 10# This library is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13# Lesser General Public License for more details. 14# 15# You should have received a copy of the GNU Lesser General Public 16# License along with this library; if not, write to the Free Software 17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 18# 19 20import logging 21logger = logging.getLogger(__name__) 22 23from spyne.util import six 24 25from spyne.util.coopmt import keepfirst 26from spyne.util.coopmt import coroutine 27from spyne.util.coopmt import Break 28 29from spyne.util.memo import memoize 30from spyne.util.memo import memoize_first 31from spyne.util.memo import memoize_ignore 32from spyne.util.memo import memoize_ignore_none 33from spyne.util.memo import memoize_id 34 35from spyne.util.attrdict import AttrDict 36from spyne.util.attrdict import AttrDictColl 37from spyne.util.attrdict import DefaultAttrDict 38 39from spyne.util._base import utctime 40from spyne.util._base import get_version 41 42 43try: 44 import thread 45 46 from urllib import splittype, splithost, quote, urlencode 47 from urllib2 import urlopen, Request, HTTPError 48 49except ImportError: # Python 3 50 import _thread as thread 51 52 from urllib.parse import splittype, splithost, quote, urlencode 53 from urllib.request import urlopen, Request 54 from urllib.error import HTTPError 55 56 57def split_url(url): 58 """Splits a url into (uri_scheme, host[:port], path)""" 59 scheme, remainder = splittype(url) 60 host, path = splithost(remainder) 61 return scheme.lower(), host, path 62 63 64def sanitize_args(a): 65 try: 66 args, kwargs = a 67 if isinstance(args, tuple) and isinstance(kwargs, dict): 68 return args, dict(kwargs) 69 70 except (TypeError, ValueError): 71 args, kwargs = (), {} 72 73 if a is not None: 74 if isinstance(a, dict): 75 args = tuple() 76 kwargs = a 77 78 elif isinstance(a, tuple): 79 if isinstance(a[-1], dict): 80 args, kwargs = a[0:-1], a[-1] 81 else: 82 args = a 83 kwargs = {} 84 85 return args, kwargs 86 87 88if six.PY2: 89 def _bytes_join(val, joiner=''): 90 return joiner.join(val) 91else: 92 def _bytes_join(val, joiner=b''): 93 return joiner.join(val) 94 95 96def utf8(s): 97 if isinstance(s, bytes): 98 return s.decode('utf8') 99 100 if isinstance(s, list): 101 return [utf8(ss) for ss in s] 102 103 if isinstance(s, tuple): 104 return tuple([utf8(ss) for ss in s]) 105 106 if isinstance(s, set): 107 return {utf8(ss) for ss in s} 108 109 if isinstance(s, frozenset): 110 return frozenset([utf8(ss) for ss in s]) 111 112 return s 113