From 3cb82765504f08978a2477210c289f2d438c0ad2 Mon Sep 17 00:00:00 2001 From: Ben Finney Date: Oct 20 2023 21:32:41 +0000 Subject: Extract metadata manipulation functionality to ‘util.metadata’ module. --- diff --git a/test/test_util_metadata.py b/test/test_util_metadata.py new file mode 100644 index 0000000..40a913c --- /dev/null +++ b/test/test_util_metadata.py @@ -0,0 +1,62 @@ +# test_util_metadata.py +# Part of ‘python-daemon’, an implementation of PEP 3143. +# +# This is free software, and you are welcome to redistribute it under +# certain conditions; see the end of this file for copyright +# information, grant of license, and disclaimer of warranty. + +""" Unit test for ‘util.metadata’ packaging module. """ + +import testscenarios +import testtools + +import util.metadata + + +class parse_person_field_TestCase( + testscenarios.WithScenarios, testtools.TestCase): + """ Test cases for ‘get_latest_version’ function. """ + + scenarios = [ + ('simple', { + 'test_person': "Foo Bar ", + 'expected_result': ("Foo Bar", "foo.bar@example.com"), + }), + ('empty', { + 'test_person': "", + 'expected_result': (None, None), + }), + ('none', { + 'test_person': None, + 'expected_error': TypeError, + }), + ('no email', { + 'test_person': "Foo Bar", + 'expected_result': ("Foo Bar", None), + }), + ] + + def test_returns_expected_result(self): + """ Should return expected result. """ + if hasattr(self, 'expected_error'): + self.assertRaises( + self.expected_error, + util.metadata.parse_person_field, self.test_person) + else: + result = util.metadata.parse_person_field(self.test_person) + self.assertEqual(self.expected_result, result) + + +# Copyright © 2008–2023 Ben Finney +# +# This is free software: you may copy, modify, and/or distribute this work +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 3 of that license or any later version. +# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. + + +# Local variables: +# coding: utf-8 +# mode: python +# End: +# vim: fileencoding=utf-8 filetype=python : diff --git a/test/test_util_version.py b/test/test_util_version.py index 8aad39b..4e93cc7 100644 --- a/test/test_util_version.py +++ b/test/test_util_version.py @@ -305,40 +305,6 @@ class VersionInfoWriter_translate_TestCase(testtools.TestCase): self.assertEqual(expected_output, instance.output) -class parse_person_field_TestCase( - testscenarios.WithScenarios, testtools.TestCase): - """ Test cases for ‘get_latest_version’ function. """ - - scenarios = [ - ('simple', { - 'test_person': "Foo Bar ", - 'expected_result': ("Foo Bar", "foo.bar@example.com"), - }), - ('empty', { - 'test_person': "", - 'expected_result': (None, None), - }), - ('none', { - 'test_person': None, - 'expected_error': TypeError, - }), - ('no email', { - 'test_person': "Foo Bar", - 'expected_result': ("Foo Bar", None), - }), - ] - - def test_returns_expected_result(self): - """ Should return expected result. """ - if hasattr(self, 'expected_error'): - self.assertRaises( - self.expected_error, - version.parse_person_field, self.test_person) - else: - result = version.parse_person_field(self.test_person) - self.assertEqual(self.expected_result, result) - - class NoOpContextManager: """ A context manager with no effect. """ diff --git a/util/metadata.py b/util/metadata.py new file mode 100644 index 0000000..3f495d9 --- /dev/null +++ b/util/metadata.py @@ -0,0 +1,59 @@ +# util/metadata.py +# Part of ‘python-daemon’, an implementation of PEP 3143. +# +# This is free software, and you are welcome to redistribute it under +# certain conditions; see the end of this file for copyright +# information, grant of license, and disclaimer of warranty. + +""" functionality to work with project metadata. + + This module implements ways to derive various project metadata at build + time. + """ + +import collections +import re + + +rfc822_person_regex = re.compile( + r"^(?P[^<]+) <(?P[^>]+)>$") + +ParsedPerson = collections.namedtuple('ParsedPerson', ['name', 'email']) + + +def parse_person_field(value): + """ Parse a person field into name and email address. + + :param value: The text value specifying a person. + :return: A 2-tuple (name, email) for the person's details. + + If the `value` does not match a standard person with email + address, the `email` item is ``None``. + """ + result = ParsedPerson(None, None) + + match = rfc822_person_regex.match(value) + if len(value): + if match is not None: + result = ParsedPerson( + name=match.group('name'), + email=match.group('email')) + else: + result = ParsedPerson(name=value, email=None) + + return result + + +# Copyright © 2008–2023 Ben Finney +# +# This is free software: you may copy, modify, and/or distribute this work +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 3 of that license or any later version. +# No warranty expressed or implied. See the file ‘LICENSE.GPL-3’ for details. + + +# Local variables: +# coding: utf-8 +# mode: python +# End: +# vim: fileencoding=utf-8 filetype=python : diff --git a/util/packaging.py b/util/packaging.py index 4ad9af2..b8306fb 100644 --- a/util/packaging.py +++ b/util/packaging.py @@ -26,9 +26,9 @@ import setuptools.command.build_py import setuptools.command.egg_info import setuptools.dist +from .metadata import parse_person_field from .version import ( generate_version_info_from_changelog, - parse_person_field, serialise_version_info_from_mapping, ) diff --git a/util/version.py b/util/version.py index 999cd02..818b669 100644 --- a/util/version.py +++ b/util/version.py @@ -26,12 +26,13 @@ import datetime import functools import io import json -import re import sys import textwrap import packaging.version +from .metadata import rfc822_person_regex + def ensure_class_bases_begin_with(namespace, class_name, base_class): """ Ensure the named class's bases start with the base class. @@ -94,35 +95,6 @@ class VersionInfoWriter(object): self.output = visitor.astext() -rfc822_person_regex = re.compile( - r"^(?P[^<]+) <(?P[^>]+)>$") - -ParsedPerson = collections.namedtuple('ParsedPerson', ['name', 'email']) - - -def parse_person_field(value): - """ Parse a person field into name and email address. - - :param value: The text value specifying a person. - :return: A 2-tuple (name, email) for the person's details. - - If the `value` does not match a standard person with email - address, the `email` item is ``None``. - """ - result = ParsedPerson(None, None) - - match = rfc822_person_regex.match(value) - if len(value): - if match is not None: - result = ParsedPerson( - name=match.group('name'), - email=match.group('email')) - else: - result = ParsedPerson(name=value, email=None) - - return result - - class ChangeLogEntry: """ An individual entry from the ‘ChangeLog’ document. """