reports: Add BaseODS.set_common_properties() method.

This commit is contained in:
Brett Smith 2020-07-29 17:22:09 -04:00
parent 04be991e19
commit 837fcec8f0
3 changed files with 47 additions and 0 deletions

View file

@ -22,11 +22,15 @@ import enum
import itertools
import operator
import re
import shlex
import sys
import urllib.parse as urlparse
import babel.core # type:ignore[import]
import babel.numbers # type:ignore[import]
import git # type:ignore[import]
import odf.config # type:ignore[import]
import odf.element # type:ignore[import]
import odf.meta # type:ignore[import]
@ -1052,6 +1056,22 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
### Properties
def set_common_properties(self,
repo: Optional[git.Repo]=None,
command: Optional[Sequence[str]]=sys.argv,
) -> None:
if repo is None:
git_shahex = '<none>'
git_dirty = True
else:
git_shahex = repo.head.commit.hexsha
git_dirty = repo.is_dirty()
self.set_custom_property('GitSHA', git_shahex)
self.set_custom_property('GitDirty', git_dirty, 'boolean')
if command is not None:
command_s = ' '.join(shlex.quote(s) for s in command)
self.set_custom_property('ReportCommand', command_s)
def set_custom_property(self,
name: str,
value: Any,

View file

@ -748,3 +748,21 @@ def test_ods_writer_set_custom_property(ods_writer, value, exptype):
else:
expected = str(value)
assert cprop.text == expected
def test_ods_writer_set_common_properties(ods_writer):
ods_writer.set_common_properties()
get_child(ods_writer.document.meta, odf.meta.UserDefined, name='ReportCommand')
def test_ods_writer_common_repo_properties(ods_writer):
repo = testutil.TestRepo('abcd1234', False)
ods_writer.set_common_properties(repo)
meta = ods_writer.document.meta
sha_prop = get_child(meta, odf.meta.UserDefined, name='GitSHA')
assert sha_prop.text == 'abcd1234'
dirty_prop = get_child(meta, odf.meta.UserDefined, name='GitDirty')
assert dirty_prop.text == 'false'
def test_ods_writer_common_command(ods_writer):
ods_writer.set_common_properties(command=['testcmd', 'testarg*'])
cmd_prop = get_child(ods_writer.document.meta, odf.meta.UserDefined, name='ReportCommand')
assert cmd_prop.text == 'testcmd \'testarg*\''

View file

@ -17,12 +17,14 @@
import datetime
import itertools
import re
import unittest.mock
import beancount.core.amount as bc_amount
import beancount.core.data as bc_data
import beancount.loader as bc_loader
import beancount.parser.options as bc_options
import git
import odf.element
import odf.opendocument
import odf.table
@ -288,6 +290,13 @@ class TestConfig:
return self._rt_wrapper
def TestRepo(head_hexsha='abcd1234', dirty=False):
retval = unittest.mock.Mock(spec=git.Repo)
retval.is_dirty.return_value = dirty
retval.head.commit.hexsha = head_hexsha
return retval
class _TicketBuilder:
MESSAGE_ATTACHMENTS = [
('(Unnamed)', 'multipart/alternative', '0b'),