_policybase.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. """Policy framework for the email package.
  2. Allows fine grained feature control of how the package parses and emits data.
  3. """
  4. from __future__ import unicode_literals
  5. from __future__ import print_function
  6. from __future__ import division
  7. from __future__ import absolute_import
  8. from future.builtins import super
  9. from future.builtins import str
  10. from future.utils import with_metaclass
  11. import abc
  12. from future.backports.email import header
  13. from future.backports.email import charset as _charset
  14. from future.backports.email.utils import _has_surrogates
  15. __all__ = [
  16. 'Policy',
  17. 'Compat32',
  18. 'compat32',
  19. ]
  20. class _PolicyBase(object):
  21. """Policy Object basic framework.
  22. This class is useless unless subclassed. A subclass should define
  23. class attributes with defaults for any values that are to be
  24. managed by the Policy object. The constructor will then allow
  25. non-default values to be set for these attributes at instance
  26. creation time. The instance will be callable, taking these same
  27. attributes keyword arguments, and returning a new instance
  28. identical to the called instance except for those values changed
  29. by the keyword arguments. Instances may be added, yielding new
  30. instances with any non-default values from the right hand
  31. operand overriding those in the left hand operand. That is,
  32. A + B == A(<non-default values of B>)
  33. The repr of an instance can be used to reconstruct the object
  34. if and only if the repr of the values can be used to reconstruct
  35. those values.
  36. """
  37. def __init__(self, **kw):
  38. """Create new Policy, possibly overriding some defaults.
  39. See class docstring for a list of overridable attributes.
  40. """
  41. for name, value in kw.items():
  42. if hasattr(self, name):
  43. super(_PolicyBase,self).__setattr__(name, value)
  44. else:
  45. raise TypeError(
  46. "{!r} is an invalid keyword argument for {}".format(
  47. name, self.__class__.__name__))
  48. def __repr__(self):
  49. args = [ "{}={!r}".format(name, value)
  50. for name, value in self.__dict__.items() ]
  51. return "{}({})".format(self.__class__.__name__, ', '.join(args))
  52. def clone(self, **kw):
  53. """Return a new instance with specified attributes changed.
  54. The new instance has the same attribute values as the current object,
  55. except for the changes passed in as keyword arguments.
  56. """
  57. newpolicy = self.__class__.__new__(self.__class__)
  58. for attr, value in self.__dict__.items():
  59. object.__setattr__(newpolicy, attr, value)
  60. for attr, value in kw.items():
  61. if not hasattr(self, attr):
  62. raise TypeError(
  63. "{!r} is an invalid keyword argument for {}".format(
  64. attr, self.__class__.__name__))
  65. object.__setattr__(newpolicy, attr, value)
  66. return newpolicy
  67. def __setattr__(self, name, value):
  68. if hasattr(self, name):
  69. msg = "{!r} object attribute {!r} is read-only"
  70. else:
  71. msg = "{!r} object has no attribute {!r}"
  72. raise AttributeError(msg.format(self.__class__.__name__, name))
  73. def __add__(self, other):
  74. """Non-default values from right operand override those from left.
  75. The object returned is a new instance of the subclass.
  76. """
  77. return self.clone(**other.__dict__)
  78. def _append_doc(doc, added_doc):
  79. doc = doc.rsplit('\n', 1)[0]
  80. added_doc = added_doc.split('\n', 1)[1]
  81. return doc + '\n' + added_doc
  82. def _extend_docstrings(cls):
  83. if cls.__doc__ and cls.__doc__.startswith('+'):
  84. cls.__doc__ = _append_doc(cls.__bases__[0].__doc__, cls.__doc__)
  85. for name, attr in cls.__dict__.items():
  86. if attr.__doc__ and attr.__doc__.startswith('+'):
  87. for c in (c for base in cls.__bases__ for c in base.mro()):
  88. doc = getattr(getattr(c, name), '__doc__')
  89. if doc:
  90. attr.__doc__ = _append_doc(doc, attr.__doc__)
  91. break
  92. return cls
  93. class Policy(with_metaclass(abc.ABCMeta, _PolicyBase)):
  94. r"""Controls for how messages are interpreted and formatted.
  95. Most of the classes and many of the methods in the email package accept
  96. Policy objects as parameters. A Policy object contains a set of values and
  97. functions that control how input is interpreted and how output is rendered.
  98. For example, the parameter 'raise_on_defect' controls whether or not an RFC
  99. violation results in an error being raised or not, while 'max_line_length'
  100. controls the maximum length of output lines when a Message is serialized.
  101. Any valid attribute may be overridden when a Policy is created by passing
  102. it as a keyword argument to the constructor. Policy objects are immutable,
  103. but a new Policy object can be created with only certain values changed by
  104. calling the Policy instance with keyword arguments. Policy objects can
  105. also be added, producing a new Policy object in which the non-default
  106. attributes set in the right hand operand overwrite those specified in the
  107. left operand.
  108. Settable attributes:
  109. raise_on_defect -- If true, then defects should be raised as errors.
  110. Default: False.
  111. linesep -- string containing the value to use as separation
  112. between output lines. Default '\n'.
  113. cte_type -- Type of allowed content transfer encodings
  114. 7bit -- ASCII only
  115. 8bit -- Content-Transfer-Encoding: 8bit is allowed
  116. Default: 8bit. Also controls the disposition of
  117. (RFC invalid) binary data in headers; see the
  118. documentation of the binary_fold method.
  119. max_line_length -- maximum length of lines, excluding 'linesep',
  120. during serialization. None or 0 means no line
  121. wrapping is done. Default is 78.
  122. """
  123. raise_on_defect = False
  124. linesep = '\n'
  125. cte_type = '8bit'
  126. max_line_length = 78
  127. def handle_defect(self, obj, defect):
  128. """Based on policy, either raise defect or call register_defect.
  129. handle_defect(obj, defect)
  130. defect should be a Defect subclass, but in any case must be an
  131. Exception subclass. obj is the object on which the defect should be
  132. registered if it is not raised. If the raise_on_defect is True, the
  133. defect is raised as an error, otherwise the object and the defect are
  134. passed to register_defect.
  135. This method is intended to be called by parsers that discover defects.
  136. The email package parsers always call it with Defect instances.
  137. """
  138. if self.raise_on_defect:
  139. raise defect
  140. self.register_defect(obj, defect)
  141. def register_defect(self, obj, defect):
  142. """Record 'defect' on 'obj'.
  143. Called by handle_defect if raise_on_defect is False. This method is
  144. part of the Policy API so that Policy subclasses can implement custom
  145. defect handling. The default implementation calls the append method of
  146. the defects attribute of obj. The objects used by the email package by
  147. default that get passed to this method will always have a defects
  148. attribute with an append method.
  149. """
  150. obj.defects.append(defect)
  151. def header_max_count(self, name):
  152. """Return the maximum allowed number of headers named 'name'.
  153. Called when a header is added to a Message object. If the returned
  154. value is not 0 or None, and there are already a number of headers with
  155. the name 'name' equal to the value returned, a ValueError is raised.
  156. Because the default behavior of Message's __setitem__ is to append the
  157. value to the list of headers, it is easy to create duplicate headers
  158. without realizing it. This method allows certain headers to be limited
  159. in the number of instances of that header that may be added to a
  160. Message programmatically. (The limit is not observed by the parser,
  161. which will faithfully produce as many headers as exist in the message
  162. being parsed.)
  163. The default implementation returns None for all header names.
  164. """
  165. return None
  166. @abc.abstractmethod
  167. def header_source_parse(self, sourcelines):
  168. """Given a list of linesep terminated strings constituting the lines of
  169. a single header, return the (name, value) tuple that should be stored
  170. in the model. The input lines should retain their terminating linesep
  171. characters. The lines passed in by the email package may contain
  172. surrogateescaped binary data.
  173. """
  174. raise NotImplementedError
  175. @abc.abstractmethod
  176. def header_store_parse(self, name, value):
  177. """Given the header name and the value provided by the application
  178. program, return the (name, value) that should be stored in the model.
  179. """
  180. raise NotImplementedError
  181. @abc.abstractmethod
  182. def header_fetch_parse(self, name, value):
  183. """Given the header name and the value from the model, return the value
  184. to be returned to the application program that is requesting that
  185. header. The value passed in by the email package may contain
  186. surrogateescaped binary data if the lines were parsed by a BytesParser.
  187. The returned value should not contain any surrogateescaped data.
  188. """
  189. raise NotImplementedError
  190. @abc.abstractmethod
  191. def fold(self, name, value):
  192. """Given the header name and the value from the model, return a string
  193. containing linesep characters that implement the folding of the header
  194. according to the policy controls. The value passed in by the email
  195. package may contain surrogateescaped binary data if the lines were
  196. parsed by a BytesParser. The returned value should not contain any
  197. surrogateescaped data.
  198. """
  199. raise NotImplementedError
  200. @abc.abstractmethod
  201. def fold_binary(self, name, value):
  202. """Given the header name and the value from the model, return binary
  203. data containing linesep characters that implement the folding of the
  204. header according to the policy controls. The value passed in by the
  205. email package may contain surrogateescaped binary data.
  206. """
  207. raise NotImplementedError
  208. @_extend_docstrings
  209. class Compat32(Policy):
  210. """+
  211. This particular policy is the backward compatibility Policy. It
  212. replicates the behavior of the email package version 5.1.
  213. """
  214. def _sanitize_header(self, name, value):
  215. # If the header value contains surrogates, return a Header using
  216. # the unknown-8bit charset to encode the bytes as encoded words.
  217. if not isinstance(value, str):
  218. # Assume it is already a header object
  219. return value
  220. if _has_surrogates(value):
  221. return header.Header(value, charset=_charset.UNKNOWN8BIT,
  222. header_name=name)
  223. else:
  224. return value
  225. def header_source_parse(self, sourcelines):
  226. """+
  227. The name is parsed as everything up to the ':' and returned unmodified.
  228. The value is determined by stripping leading whitespace off the
  229. remainder of the first line, joining all subsequent lines together, and
  230. stripping any trailing carriage return or linefeed characters.
  231. """
  232. name, value = sourcelines[0].split(':', 1)
  233. value = value.lstrip(' \t') + ''.join(sourcelines[1:])
  234. return (name, value.rstrip('\r\n'))
  235. def header_store_parse(self, name, value):
  236. """+
  237. The name and value are returned unmodified.
  238. """
  239. return (name, value)
  240. def header_fetch_parse(self, name, value):
  241. """+
  242. If the value contains binary data, it is converted into a Header object
  243. using the unknown-8bit charset. Otherwise it is returned unmodified.
  244. """
  245. return self._sanitize_header(name, value)
  246. def fold(self, name, value):
  247. """+
  248. Headers are folded using the Header folding algorithm, which preserves
  249. existing line breaks in the value, and wraps each resulting line to the
  250. max_line_length. Non-ASCII binary data are CTE encoded using the
  251. unknown-8bit charset.
  252. """
  253. return self._fold(name, value, sanitize=True)
  254. def fold_binary(self, name, value):
  255. """+
  256. Headers are folded using the Header folding algorithm, which preserves
  257. existing line breaks in the value, and wraps each resulting line to the
  258. max_line_length. If cte_type is 7bit, non-ascii binary data is CTE
  259. encoded using the unknown-8bit charset. Otherwise the original source
  260. header is used, with its existing line breaks and/or binary data.
  261. """
  262. folded = self._fold(name, value, sanitize=self.cte_type=='7bit')
  263. return folded.encode('ascii', 'surrogateescape')
  264. def _fold(self, name, value, sanitize):
  265. parts = []
  266. parts.append('%s: ' % name)
  267. if isinstance(value, str):
  268. if _has_surrogates(value):
  269. if sanitize:
  270. h = header.Header(value,
  271. charset=_charset.UNKNOWN8BIT,
  272. header_name=name)
  273. else:
  274. # If we have raw 8bit data in a byte string, we have no idea
  275. # what the encoding is. There is no safe way to split this
  276. # string. If it's ascii-subset, then we could do a normal
  277. # ascii split, but if it's multibyte then we could break the
  278. # string. There's no way to know so the least harm seems to
  279. # be to not split the string and risk it being too long.
  280. parts.append(value)
  281. h = None
  282. else:
  283. h = header.Header(value, header_name=name)
  284. else:
  285. # Assume it is a Header-like object.
  286. h = value
  287. if h is not None:
  288. parts.append(h.encode(linesep=self.linesep,
  289. maxlinelen=self.max_line_length))
  290. parts.append(self.linesep)
  291. return ''.join(parts)
  292. compat32 = Compat32()