Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

316

317

318

319

320

321

322

323

324

325

326

327

328

329

330

331

332

333

334

335

336

337

338

339

340

341

342

343

344

345

346

347

348

349

350

351

352

353

354

355

356

357

358

359

360

361

362

363

364

365

366

367

368

369

370

371

372

373

374

375

376

377

378

379

380

381

from calendar import timegm 

from decimal import Decimal as MyDecimal, ROUND_HALF_EVEN 

from email.utils import formatdate 

from urllib.parse import urlparse, urlunparse 

 

from sanic_restful.marshal import marshal 

 

__all__ = ["String", "FormattedString", "Url", "DateTime", "Float", 

"Integer", "Arbitrary", "Nested", "List", "Raw", "Boolean", 

"Fixed", "Price"] 

 

 

class MarshallingException(Exception): 

""" 

This is an encapsulating Exception in case of marshalling error. 

""" 

 

def __init__(self, underlying_exception): 

# just put the contextual representation of the error to hint on what 

# went wrong without exposing internals 

super().__init__(str(underlying_exception)) 

 

 

def is_indexable_but_not_string(obj): 

return not hasattr(obj, "strip") and hasattr(obj, "__iter__") 

 

 

def get_value(key, obj, default=None): 

"""Helper for pulling a keyed value off various types of objects""" 

if isinstance(key, int): 

return _get_value_for_key(key, obj, default) 

elif callable(key): 

return key(obj) 

else: 

return _get_value_for_keys(key.split('.'), obj, default) 

 

 

def _get_value_for_keys(keys, obj, default): 

if len(keys) == 1: 

return _get_value_for_key(keys[0], obj, default) 

else: 

return _get_value_for_keys( 

keys[1:], _get_value_for_key(keys[0], obj, default), default) 

 

 

def _get_value_for_key(key, obj, default): 

if is_indexable_but_not_string(obj): 

try: 

return obj[key] 

except (IndexError, TypeError, KeyError): 

pass 

return getattr(obj, key, default) 

 

 

def to_marshallable_type(obj): 

"""Helper for converting an object to a dictionary only if it is not 

dictionary already or an indexable object nor a simple type""" 

if obj is None: 

return None # make it idempotent for None 

 

if hasattr(obj, '__marshallable__'): 

return obj.__marshallable__() 

 

if hasattr(obj, '__getitem__'): 

return obj # it is indexable it is ok 

 

return dict(obj.__dict__) 

 

 

class Raw(object): 

"""Raw provides a base field class from which others should extend. It 

applies no formatting by default, and should only be used in cases where 

data does not need to be formatted before being serialized. Fields should 

throw a :class:`MarshallingException` in case of parsing problem. 

 

:param default: The default value for the field, if no value is 

specified. 

:param attribute: If the public facing value differs from the internal 

value, use this to retrieve a different attribute from the response 

than the publicly named value. 

""" 

 

def __init__(self, default=None, attribute=None): 

self.attribute = attribute 

self.default = default 

 

def format(self, value): 

"""Formats a field's value. No-op by default - field classes that 

modify how the value of existing object keys should be presented should 

override this and apply the appropriate formatting. 

 

:param value: The value to format 

:exception MarshallingException: In case of formatting problem 

 

Ex:: 

 

class TitleCase(Raw): 

def format(self, value): 

return unicode(value).title() 

""" 

return value 

 

def output(self, key, obj): 

"""Pulls the value for the given key from the object, applies the 

field's formatting and returns the result. If the key is not found 

in the object, returns the default value. Field classes that create 

values which do not require the existence of the key in the object 

should override this and return the desired value. 

 

:exception MarshallingException: In case of formatting problem 

""" 

 

value = get_value(key 

if self.attribute is None else self.attribute, obj) 

 

if value is None: 

return self.default 

 

return self.format(value) 

 

 

class Nested(Raw): 

"""Allows you to nest one set of fields inside another. 

See :ref:`nested-field` for more information 

 

:param dict nested: The dictionary to nest 

:param bool allow_null: Whether to return None instead of a dictionary 

with null keys, if a nested dictionary has all-null keys 

:param kwargs: If ``default`` keyword argument is present, a nested 

dictionary will be marshaled as its value if nested dictionary is 

all-null keys (e.g. lets you return an empty JSON object instead of 

null) 

""" 

 

def __init__(self, nested, allow_null=False, **kwargs): 

self.nested = nested 

self.allow_null = allow_null 

super().__init__(**kwargs) 

 

def output(self, key, obj): 

value = get_value(key 

if self.attribute is None else self.attribute, obj) 

if value is None: 

if self.allow_null: 

return None 

elif self.default is not None: 

return self.default 

 

return marshal(value, self.nested) 

 

 

class List(Raw): 

""" 

Field for marshalling lists of other fields. 

 

See :ref:`list-field` for more information. 

 

:param cls_or_instance: The field type the list will contain. 

""" 

 

def __init__(self, cls_or_instance, **kwargs): 

super().__init__(**kwargs) 

error_msg = ("The type of the list elements must be a subclass of " 

"flask_restful.fields.Raw") 

if isinstance(cls_or_instance, type): 

if not issubclass(cls_or_instance, Raw): 

raise MarshallingException(error_msg) 

self.container = cls_or_instance() 

else: 

if not isinstance(cls_or_instance, Raw): 

raise MarshallingException(error_msg) 

self.container = cls_or_instance 

 

def format(self, value): 

# Convert all instances in typed list to container type 

if isinstance(value, set): 

value = list(value) 

 

return [ 

self.container.output( 

idx, val if (isinstance(val, dict) or 

(self.container.attribute 

and hasattr(val, self.container.attribute))) 

and not isinstance(self.container, Nested) 

and not type(self.container) is Raw else value) 

for idx, val in enumerate(value) 

] 

 

def output(self, key, data): 

value = get_value(key 

if self.attribute is None else self.attribute, data) 

# we cannot really test for external dict behavior 

if is_indexable_but_not_string(value) and not isinstance(value, dict): 

return self.format(value) 

 

if value is None: 

return self.default 

 

return [marshal(value, self.container.nested)] 

 

 

class String(Raw): 

""" 

Marshal a value as a string. Uses ``six.text_type`` so values will 

be converted to :class:`unicode` in python2 and :class:`str` in 

python3. 

""" 

def format(self, value): 

try: 

return str(value) 

except ValueError as ve: 

raise MarshallingException(ve) 

 

 

class Integer(Raw): 

""" Field for outputting an integer value. 

 

:param int default: The default value for the field, if no value is 

specified. 

""" 

def __init__(self, default=0, **kwargs): 

super(Integer, self).__init__(default=default, **kwargs) 

 

def format(self, value): 

try: 

if value is None: 

return self.default 

return int(value) 

except ValueError as ve: 

raise MarshallingException(ve) 

 

 

class Boolean(Raw): 

""" 

Field for outputting a boolean value. 

 

Empty collections such as ``""``, ``{}``, ``[]``, etc. will be converted to 

``False``. 

""" 

def format(self, value): 

return bool(value) 

 

 

class FormattedString(Raw): 

""" 

FormattedString is used to interpolate other values from 

the response into this field. The syntax for the source string is 

the same as the string :meth:`~str.format` method from the python 

stdlib. 

 

Ex:: 

 

fields = { 

'name': fields.String, 

'greeting': fields.FormattedString("Hello {name}") 

} 

data = { 

'name': 'Doug', 

} 

marshal(data, fields) 

""" 

def __init__(self, src_str): 

""" 

:param string src_str: the string to format with the other 

values from the response. 

""" 

super().__init__() 

self.src_str = str(src_str) 

 

def output(self, key, obj): 

try: 

data = to_marshallable_type(obj) 

return self.src_str.format(**data) 

except (TypeError, IndexError) as error: 

raise MarshallingException(error) 

 

 

class Float(Raw): 

""" 

A double as IEEE-754 double precision. 

ex : 3.141592653589793 3.1415926535897933e-06 3.141592653589793e+24 nan inf 

-inf 

""" 

 

def format(self, value): 

try: 

return float(value) 

except ValueError as ve: 

raise MarshallingException(ve) 

 

 

class Arbitrary(Raw): 

""" 

A floating point number with an arbitrary precision 

ex: 634271127864378216478362784632784678324.23432 

""" 

 

def format(self, value): 

return str(MyDecimal(value)) 

 

 

class DateTime(Raw): 

""" 

Return a formatted datetime string in UTC. Supported formats are RFC 822 

and ISO 8601. 

 

See :func:`email.utils.formatdate` for more info on the RFC 822 format. 

 

See :meth:`datetime.datetime.isoformat` for more info on the ISO 8601 

format. 

 

:param dt_format: ``'rfc822'`` or ``'iso8601'`` 

:type dt_format: str 

""" 

def __init__(self, dt_format='rfc822', **kwargs): 

super().__init__(**kwargs) 

self.dt_format = dt_format 

 

def format(self, value): 

try: 

if self.dt_format == 'rfc822': 

return _rfc822(value) 

elif self.dt_format == 'iso8601': 

return _iso8601(value) 

else: 

raise MarshallingException( 

'Unsupported date format %s' % self.dt_format 

) 

except AttributeError as ae: 

raise MarshallingException(ae) 

 

 

ZERO = MyDecimal() 

 

 

class Fixed(Raw): 

""" 

A decimal number with a fixed precision. 

""" 

def __init__(self, decimals=5, **kwargs): 

super().__init__(**kwargs) 

self.precision = MyDecimal('0.' + '0' * (decimals - 1) + '1') 

 

def format(self, value): 

dvalue = MyDecimal(value) 

if not dvalue.is_normal() and dvalue != ZERO: 

raise MarshallingException('Invalid Fixed precision number.') 

return str( 

dvalue.quantize(self.precision, rounding=ROUND_HALF_EVEN)) 

 

 

"""Alias for :class:`~fields.Fixed`""" 

Price = Fixed 

 

 

def _rfc822(dt): 

"""Turn a datetime object into a formatted date. 

 

Example:: 

 

fields._rfc822(datetime(2011, 1, 1)) => "Sat, 01 Jan 2011 00:00:00 -0000" 

 

:param dt: The datetime to transform 

:type dt: datetime 

:return: A RFC 822 formatted date string 

""" 

return formatdate(timegm(dt.utctimetuple())) 

 

 

def _iso8601(dt): 

"""Turn a datetime object into an ISO8601 formatted date. 

 

Example:: 

 

fields._iso8601(datetime(2012, 1, 1, 0, 0)) => "2012-01-01T00:00:00" 

 

:param dt: The datetime to transform 

:type dt: datetime 

:return: A ISO 8601 formatted date string 

""" 

return dt.isoformat()