Package restkit :: Module util
[hide private]
[frames] | no frames]

Source Code for Module restkit.util

  1  # -*- coding: utf-8 - 
  2  # 
  3  # This file is part of restkit released under the MIT license.  
  4  # See the NOTICE for more information. 
  5   
  6  import os 
  7  import time 
  8  import urllib 
  9  import warnings 
 10   
 11  weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] 
 12  monthname = [None, 
 13               'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
 14               'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] 
 15                
16 -def http_date(timestamp=None):
17 """Return the current date and time formatted for a message header.""" 18 if timestamp is None: 19 timestamp = time.time() 20 year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp) 21 s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( 22 weekdayname[wd], 23 day, monthname[month], year, 24 hh, mm, ss) 25 return s
26
27 -def to_bytestring(s):
28 if not isinstance(s, basestring): 29 raise TypeError("value should be a str or unicode") 30 31 if isinstance(s, unicode): 32 return s.encode('utf-8') 33 return s
34
35 -def url_quote(s, charset='utf-8', safe='/:'):
36 """URL encode a single string with a given encoding.""" 37 if isinstance(s, unicode): 38 s = s.encode(charset) 39 elif not isinstance(s, str): 40 s = str(s) 41 return urllib.quote(s, safe=safe)
42 43
44 -def url_encode(obj, charset="utf8", encode_keys=False):
45 items = [] 46 if isinstance(obj, dict): 47 for k, v in list(obj.items()): 48 items.append((k, v)) 49 else: 50 items = list(items) 51 52 tmp = [] 53 for k, v in items: 54 if encode_keys: 55 k = encode(k, charset) 56 57 if not isinstance(v, (tuple, list)): 58 v = [v] 59 60 for v1 in v: 61 if v1 is None: 62 v1 = '' 63 elif callable(v1): 64 v1 = encode(v1(), charset) 65 else: 66 v1 = encode(v1, charset) 67 tmp.append('%s=%s' % (urllib.quote(k), urllib.quote_plus(v1))) 68 return '&'.join(tmp)
69
70 -def encode(v, charset="utf8"):
71 if isinstance(v, unicode): 72 v = v.encode(charset) 73 else: 74 v = str(v) 75 return v
76
77 -class deprecated_property(object):
78 """ 79 Wraps a decorator, with a deprecation warning or error 80 """
81 - def __init__(self, decorator, attr, message, warning=True):
82 self.decorator = decorator 83 self.attr = attr 84 self.message = message 85 self.warning = warning
86
87 - def __get__(self, obj, type=None):
88 if obj is None: 89 return self 90 self.warn() 91 return self.decorator.__get__(obj, type)
92
93 - def __set__(self, obj, value):
94 self.warn() 95 self.decorator.__set__(obj, value)
96
97 - def __delete__(self, obj):
98 self.warn() 99 self.decorator.__delete__(obj)
100
101 - def __repr__(self):
102 return '<Deprecated attribute %s: %r>' % ( 103 self.attr, 104 self.decorator)
105
106 - def warn(self):
107 if not self.warning: 108 raise DeprecationWarning( 109 'The attribute %s is deprecated: %s' % (self.attr, self.message)) 110 else: 111 warnings.warn( 112 'The attribute %s is deprecated: %s' % (self.attr, self.message), 113 DeprecationWarning, 114 stacklevel=3)
115 116 try:#python 2.6, use subprocess 117 import subprocess 118 subprocess.Popen # trigger ImportError early 119 closefds = os.name == 'posix' 120
121 - def popen3(cmd, mode='t', bufsize=0):
122 p = subprocess.Popen(cmd, shell=True, bufsize=bufsize, 123 stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, 124 close_fds=closefds) 125 p.wait() 126 return (p.stdin, p.stdout, p.stderr)
127 except ImportError: 128 subprocess = None 129 popen3 = os.popen3 130
131 -def locate_program(program):
132 if os.path.isabs(program): 133 return program 134 if os.path.dirname(program): 135 program = os.path.normpath(os.path.realpath(program)) 136 return program 137 138 default = program 139 paths = os.getenv('PATH') 140 if not paths: 141 return False 142 for path in paths.split(os.pathsep): 143 filename = os.path.join(path, program) 144 if os.access(filename, os.X_OK): 145 return filename 146 return False
147