mailmap.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # mailmap.py -- Mailmap reader
  2. # Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  5. # General Public License as public by the Free Software Foundation; version 2.0
  6. # or (at your option) any later version. You can redistribute it and/or
  7. # modify it under the terms of either of these two licenses.
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. # You should have received a copy of the licenses; if not, see
  16. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  17. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  18. # License, Version 2.0.
  19. #
  20. """Mailmap file reader."""
  21. def parse_identity(text):
  22. # TODO(jelmer): Integrate this with dulwich.fastexport.split_email and
  23. # dulwich.repo.check_user_identity
  24. (name, email) = text.rsplit(b"<", 1)
  25. name = name.strip()
  26. email = email.rstrip(b">").strip()
  27. if not name:
  28. name = None
  29. if not email:
  30. email = None
  31. return (name, email)
  32. def read_mailmap(f):
  33. """Read a mailmap.
  34. :param f: File-like object to read from
  35. :return: Iterator over
  36. ((canonical_name, canonical_email), (from_name, from_email)) tuples
  37. """
  38. for line in f:
  39. # Remove comments
  40. line = line.split(b'#')[0]
  41. line = line.strip()
  42. if not line:
  43. continue
  44. (canonical_identity, from_identity) = line.split(b'>', 1)
  45. canonical_identity += b">"
  46. if from_identity.strip():
  47. parsed_from_identity = parse_identity(from_identity)
  48. else:
  49. parsed_from_identity = None
  50. parsed_canonical_identity = parse_identity(canonical_identity)
  51. yield parsed_canonical_identity, parsed_from_identity
  52. class Mailmap(object):
  53. """Class for accessing a mailmap file."""
  54. def __init__(self, map=None):
  55. self._table = {}
  56. if map:
  57. for (canonical_identity, from_identity) in map:
  58. self.add_entry(canonical_identity, from_identity)
  59. def add_entry(self, canonical_identity, from_identity=None):
  60. """Add an entry to the mail mail.
  61. Any of the fields can be None, but at least one of them needs to be
  62. set.
  63. :param canonical_identity: The canonical identity (tuple)
  64. :param from_identity: The from identity (tuple)
  65. """
  66. if from_identity is None:
  67. from_name, from_email = None, None
  68. else:
  69. (from_name, from_email) = from_identity
  70. (canonical_name, canonical_email) = canonical_identity
  71. if from_name is None and from_email is None:
  72. self._table[canonical_name, None] = canonical_identity
  73. self._table[None, canonical_email] = canonical_identity
  74. else:
  75. self._table[from_name, from_email] = canonical_identity
  76. def lookup(self, identity):
  77. """Lookup an identity in this mailmail."""
  78. if not isinstance(identity, tuple):
  79. was_tuple = False
  80. identity = parse_identity(identity)
  81. else:
  82. was_tuple = True
  83. for query in [identity, (None, identity[1]), (identity[0], None)]:
  84. canonical_identity = self._table.get(query)
  85. if canonical_identity is not None:
  86. identity = (
  87. canonical_identity[0] or identity[0],
  88. canonical_identity[1] or identity[1])
  89. break
  90. if was_tuple:
  91. return identity
  92. else:
  93. return identity[0] + b" <" + identity[1] + b">"
  94. @classmethod
  95. def from_path(cls, path):
  96. with open(path, 'rb') as f:
  97. return cls(read_mailmap(f))