1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import re
20
21 import logging
22 try:
23 from logging import NullHandler
24 except ImportError:
26 - def emit(self, record):
28
29
30 __all__ = [
31 'DuplicateTag',
32 'get_default_logger',
33 'InvalidTagSpec',
34 'InvalidTagValueList',
35 'parse_tag_value',
36 ]
37
38
41
42
45
46
49
50
52 """Parse a DKIM Tag=Value list.
53
54 Interprets the syntax specified by RFC6376 section 3.2.
55 Assumes that folding whitespace is already unfolded.
56
57 @param tag_list: A bytes string containing a DKIM Tag=Value list.
58 """
59 tags = {}
60 tag_specs = tag_list.strip().split(b';')
61
62 if not tag_specs[-1]:
63 tag_specs.pop()
64 for tag_spec in tag_specs:
65 try:
66 key, value = [x.strip() for x in tag_spec.split(b'=', 1)]
67 except ValueError:
68 raise InvalidTagSpec(tag_spec)
69 if re.match(br'^[a-zA-Z](\w)*', key) is None:
70 raise InvalidTagSpec(tag_spec)
71 if key in tags:
72 raise DuplicateTag(key)
73 tags[key] = value
74 return tags
75
76
78 """Get the default dkimpy logger."""
79 logger = logging.getLogger('dkimpy')
80 if not logger.handlers:
81 logger.addHandler(NullHandler())
82 return logger
83