| 335 | | class FileDict(dict): |
|---|
| 336 | | """ |
|---|
| 337 | | A dictionary used to hold uploaded file contents. The only special feature |
|---|
| 338 | | here is that repr() of this object won't dump the entire contents of the |
|---|
| 339 | | file to the output. A handy safeguard for a large file upload. |
|---|
| 340 | | """ |
|---|
| 341 | | def __repr__(self): |
|---|
| 342 | | if 'content' in self: |
|---|
| 343 | | d = dict(self, content='<omitted>') |
|---|
| 344 | | return dict.__repr__(d) |
|---|
| 345 | | return dict.__repr__(self) |
|---|
| | 335 | class ImmutableList(tuple): |
|---|
| | 336 | """ |
|---|
| | 337 | A tuple-like object that raises useful errors when it is asked to mutate. |
|---|
| | 338 | |
|---|
| | 339 | Example:: |
|---|
| | 340 | |
|---|
| | 341 | >>> a = ImmutableList(range(5), warning="You cannot mutate this.") |
|---|
| | 342 | >>> a[3] = '4' |
|---|
| | 343 | Traceback (most recent call last): |
|---|
| | 344 | ... |
|---|
| | 345 | AttributeError: You cannot mutate this. |
|---|
| | 346 | """ |
|---|
| | 347 | |
|---|
| | 348 | def __new__(cls, *args, **kwargs): |
|---|
| | 349 | if 'warning' in kwargs: |
|---|
| | 350 | warning = kwargs['warning'] |
|---|
| | 351 | del kwargs['warning'] |
|---|
| | 352 | else: |
|---|
| | 353 | warning = 'ImmutableList object is immutable.' |
|---|
| | 354 | self = tuple.__new__(cls, *args, **kwargs) |
|---|
| | 355 | self.warning = warning |
|---|
| | 356 | return self |
|---|
| | 357 | |
|---|
| | 358 | def complain(self, *wargs, **kwargs): |
|---|
| | 359 | if isinstance(self.warning, Exception): |
|---|
| | 360 | raise self.warning |
|---|
| | 361 | else: |
|---|
| | 362 | raise AttributeError, self.warning |
|---|
| | 363 | |
|---|
| | 364 | # All list mutation functions complain. |
|---|
| | 365 | __delitem__ = complain |
|---|
| | 366 | __delslice__ = complain |
|---|
| | 367 | __iadd__ = complain |
|---|
| | 368 | __imul__ = complain |
|---|
| | 369 | __setitem__ = complain |
|---|
| | 370 | __setslice__ = complain |
|---|
| | 371 | append = complain |
|---|
| | 372 | extend = complain |
|---|
| | 373 | insert = complain |
|---|
| | 374 | pop = complain |
|---|
| | 375 | remove = complain |
|---|
| | 376 | sort = complain |
|---|
| | 377 | reverse = complain |
|---|