mailmap.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 collections.abc import Iterator
  23. from typing import IO, Optional, Union
  24. def parse_identity(text: bytes) -> tuple[Optional[bytes], Optional[bytes]]:
  25. # TODO(jelmer): Integrate this with dulwich.fastexport.split_email and
  26. # dulwich.repo.check_user_identity
  27. (name_str, email_str) = text.rsplit(b"<", 1)
  28. name_str = name_str.strip()
  29. email_str = email_str.rstrip(b">").strip()
  30. name: Optional[bytes] = name_str if name_str else None
  31. email: Optional[bytes] = email_str if email_str else None
  32. return (name, email)
  33. def read_mailmap(
  34. f: IO[bytes],
  35. ) -> Iterator[
  36. tuple[
  37. tuple[Optional[bytes], Optional[bytes]],
  38. Optional[tuple[Optional[bytes], Optional[bytes]]],
  39. ]
  40. ]:
  41. """Read a mailmap.
  42. Args:
  43. f: File-like object to read from
  44. Returns: Iterator over
  45. ((canonical_name, canonical_email), (from_name, from_email)) tuples
  46. """
  47. for line in f:
  48. # Remove comments
  49. line = line.split(b"#")[0]
  50. line = line.strip()
  51. if not line:
  52. continue
  53. (canonical_identity, from_identity) = line.split(b">", 1)
  54. canonical_identity += b">"
  55. if from_identity.strip():
  56. parsed_from_identity = parse_identity(from_identity)
  57. else:
  58. parsed_from_identity = None
  59. parsed_canonical_identity = parse_identity(canonical_identity)
  60. yield parsed_canonical_identity, parsed_from_identity
  61. class Mailmap:
  62. """Class for accessing a mailmap file."""
  63. def __init__(
  64. self,
  65. map: Optional[
  66. Iterator[
  67. tuple[
  68. tuple[Optional[bytes], Optional[bytes]],
  69. Optional[tuple[Optional[bytes], Optional[bytes]]],
  70. ]
  71. ]
  72. ] = None,
  73. ) -> None:
  74. self._table: dict[
  75. tuple[Optional[bytes], Optional[bytes]],
  76. tuple[Optional[bytes], Optional[bytes]],
  77. ] = {}
  78. if map:
  79. for canonical_identity, from_identity in map:
  80. self.add_entry(canonical_identity, from_identity)
  81. def add_entry(
  82. self,
  83. canonical_identity: tuple[Optional[bytes], Optional[bytes]],
  84. from_identity: Optional[tuple[Optional[bytes], Optional[bytes]]] = None,
  85. ) -> None:
  86. """Add an entry to the mail mail.
  87. Any of the fields can be None, but at least one of them needs to be
  88. set.
  89. Args:
  90. canonical_identity: The canonical identity (tuple)
  91. from_identity: The from identity (tuple)
  92. """
  93. if from_identity is None:
  94. from_name, from_email = None, None
  95. else:
  96. (from_name, from_email) = from_identity
  97. (canonical_name, canonical_email) = canonical_identity
  98. if from_name is None and from_email is None:
  99. self._table[canonical_name, None] = canonical_identity
  100. self._table[None, canonical_email] = canonical_identity
  101. else:
  102. self._table[from_name, from_email] = canonical_identity
  103. def lookup(
  104. self, identity: Union[bytes, tuple[Optional[bytes], Optional[bytes]]]
  105. ) -> Union[bytes, tuple[Optional[bytes], Optional[bytes]]]:
  106. """Lookup an identity in this mailmail."""
  107. if not isinstance(identity, tuple):
  108. was_tuple = False
  109. identity = parse_identity(identity)
  110. else:
  111. was_tuple = True
  112. for query in [identity, (None, identity[1]), (identity[0], None)]:
  113. canonical_identity = self._table.get(query)
  114. if canonical_identity is not None:
  115. identity = (
  116. canonical_identity[0] or identity[0],
  117. canonical_identity[1] or identity[1],
  118. )
  119. break
  120. if was_tuple:
  121. return identity
  122. else:
  123. name, email = identity
  124. if name is None:
  125. name = b""
  126. if email is None:
  127. email = b""
  128. return name + b" <" + email + b">"
  129. @classmethod
  130. def from_path(cls, path: str) -> "Mailmap":
  131. with open(path, "rb") as f:
  132. return cls(read_mailmap(f))