diff options
author | John L. Villalovos <john@sodarock.com> | 2022-01-08 17:56:26 -0800 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2022-01-08 17:56:26 -0800 |
commit | d69ba0479a4537bbc7a53f342661c1984382f939 (patch) | |
tree | b36dd5c0b5f777ecf4f3642b6208a390dd5abf45 | |
parent | 989634055b0c5ab622ac7774b546928a564a31ef (diff) | |
download | gitlab-d69ba0479a4537bbc7a53f342661c1984382f939.tar.gz |
chore: add `pprint()` and `pformat()` methods to RESTObject
This is useful in debugging and testing. As can easily print out the
values from an instance in a more human-readable form.
-rw-r--r-- | docs/api-usage.rst | 14 | ||||
-rw-r--r-- | gitlab/base.py | 9 | ||||
-rw-r--r-- | tests/unit/test_base.py | 30 |
3 files changed, 53 insertions, 0 deletions
diff --git a/docs/api-usage.rst b/docs/api-usage.rst index 66e5887..8befc56 100644 --- a/docs/api-usage.rst +++ b/docs/api-usage.rst @@ -179,6 +179,20 @@ resources. For example: project = gl.projects.get(1) project.star() +You can print a Gitlab Object. For example: + +.. code-block:: python + + project = gl.projects.get(1) + print(project) + + # Or in a prettier format. + project.pprint() + + # Or explicitly via `pformat()`. This is equivalent to the above. + print(project.pformat()) + + Base types ========== diff --git a/gitlab/base.py b/gitlab/base.py index 50f09c5..6a6e992 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -16,6 +16,7 @@ # along with this program. If not, see <http://www.gnu.org/licenses/>. import importlib +import pprint import textwrap from types import ModuleType from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type @@ -147,6 +148,14 @@ class RESTObject(object): data.update(self._updated_attrs) return f"{type(self)} => {data}" + def pformat(self) -> str: + data = self._attrs.copy() + data.update(self._updated_attrs) + return f"{type(self)} => \n{pprint.pformat(data)}" + + def pprint(self) -> None: + print(self.pformat()) + def __repr__(self) -> str: if self._id_attr: return f"<{self.__class__.__name__} {self._id_attr}:{self.get_id()}>" diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index 3ca0206..fa9f6aa 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -201,3 +201,33 @@ class TestRESTObject: obj1 = FakeObject(fake_manager, {"attr1": "foo"}) obj2 = FakeObject(fake_manager, {"attr1": "bar"}) assert obj1 != obj2 + + def test_dunder_str(self, fake_manager): + fake_object = FakeObject(fake_manager, {"attr1": "foo"}) + assert str(fake_object) == ( + "<class 'tests.unit.test_base.FakeObject'> => {'attr1': 'foo'}" + ) + + def test_pformat(self, fake_manager): + fake_object = FakeObject( + fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15} + ) + assert fake_object.pformat() == ( + "<class 'tests.unit.test_base.FakeObject'> => " + "\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n" + " 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}" + ) + + def test_pprint(self, capfd, fake_manager): + fake_object = FakeObject( + fake_manager, {"attr1": "foo" * 10, "ham": "eggs" * 15} + ) + result = fake_object.pprint() + assert result is None + stdout, stderr = capfd.readouterr() + assert stdout == ( + "<class 'tests.unit.test_base.FakeObject'> => " + "\n{'attr1': 'foofoofoofoofoofoofoofoofoofoo',\n" + " 'ham': 'eggseggseggseggseggseggseggseggseggseggseggseggseggseggseggs'}\n" + ) + assert stderr == "" |