summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNejc Habjan <nejc.habjan@siemens.com>2022-01-09 03:06:40 +0100
committerGitHub <noreply@github.com>2022-01-09 03:06:40 +0100
commitbdc19b162ca75c4a2eac70f3f9814ab31de97f7c (patch)
treec70282a8a15c9040692fed634051090c6d2cd706
parentac812727c26c9bde4ee5c1115029f2ff4ab1964b (diff)
parentd69ba0479a4537bbc7a53f342661c1984382f939 (diff)
downloadgitlab-bdc19b162ca75c4a2eac70f3f9814ab31de97f7c.tar.gz
Merge pull request #1812 from python-gitlab/jlvillal/pprint
chore: add `pprint()` and `pformat()` methods to RESTObject
-rw-r--r--docs/api-usage.rst14
-rw-r--r--gitlab/base.py9
-rw-r--r--tests/unit/test_base.py30
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 == ""