[2acc11]: script.module.brightcove / lib / brightcove / core.py  Maximize  Restore  History

Download this file

158 lines (119 with data), 4.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'''Contains core classes and functions for creating and manipulating API
objects.
'''
import urllib
from datetime import datetime
def get_item(resp, cls):
'''Converts a response dictionary to a class instance.
Returns cls(**resp).
'''
try:
params = dict((str(key), val) for key, val in resp.items())
except AttributeError:
# Don't have a dict
return cls(resp)
return cls(**params)
class Connection(object):
'''Augments requests to the API with the auth token.'''
def __init__(self, token):
self.token = token
def _request(self, url, data=None):
'''Returns a response for the given url and data.'''
conn = urllib.urlopen(url, data)
resp = conn.read()
conn.close()
return resp
def get_request(self, url, params=None):
'''Returns a response for a given url and an optional dictionary of
query string parameters.
This method calls urlencode on the params and augments params with the
auth token.
'''
if params is None:
params = {}
params.update({'token': self.token})
data = urllib.urlencode(params)
url = '%s?%s' % (url, data)
return self._request(url)
class Field(object):
'''Basic API Field subclass.'''
def __init__(self, help=None):
self.help = help
def to_python(self, value):
return value
from_python = to_python
class DateTimeField(Field):
'''Handles conversion from milliseconds to python datetime.'''
def to_python(self, value):
return datetime.fromtimestamp(int(value) / 1000)
def from_python(self, value):
return str(int(value.strftime('%s')) * 1000)
class ListField(Field):
'''A field composed of a python list of another Field instance.'''
def __init__(self, item_cls, help=None):
self.help = help
self.item_cls = item_cls
def to_python(self, value):
return [get_item(item, self.item_cls) for item in value]
def from_python(self, value):
return [item.from_python() for item in value]
class EnumField(Field):
def __init__(self, enum_cls, help=None):
self.help = help
self.enum_cls = enum_cls
def to_python(self, value):
for field in self.enum_cls._fields:
if field == value:
return value
raise Exception('Invalid Enum: %s' % field)
def from_python(self, value):
return value
class APIObjectMeta(type):
def __new__(cls, name, bases, attrs):
super_new = super(APIObjectMeta, cls).__new__
_meta = dict([(attr_name, attr_value)
for attr_name, attr_value in attrs.items()
if isinstance(attr_value, Field)])
# Now create Field objects for everything listed in _fields but not
# already explicity declared as a field
if '_fields' in attrs.keys():
_meta.update(dict((attr_name, Field()) for attr_name in
attrs['_fields'] if attr_name not in _meta.keys()))
del attrs['_fields']
attrs['_meta'] = _meta
attributes = _meta.keys()
attrs.update(dict([(attr_name, None)
for attr_name in attributes]))
def _contribute_method(name, func):
func.func_name = name
attrs[name] = func
def constructor(self, **kwargs):
for attr_name, attr_value in kwargs.items():
attr = self._meta.get(attr_name)
if attr:
setattr(self, attr_name, attr.to_python(attr_value))
else:
setattr(self, attr_name, attr_value)
_contribute_method("__init__", constructor)
def from_python(self):
ret = {}
for attr_name, attr_value in vars(self).items():
if attr_value is None:
continue
attr = self._meta.get(attr_name)
if attr:
ret[attr_name] = attr.from_python(attr_value)
else:
ret[attr_name] = attr_value
return ret
_contribute_method('from_python', from_python)
def iterate(self):
not_empty = lambda e: e[1] is not None
return iter(filter(not_empty, vars(self).items()))
_contribute_method("__iter__", iterate)
result_cls = super_new(cls, name, bases, attrs)
#result_cls.__doc__ = doc_generator(result_cls.__doc__, _meta)
return result_cls
## API Objects
class APIObject(object):
__metaclass__ = APIObjectMeta

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks