mailmap.py 4.1 KB

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