Changeset - 837fcec8f0db
[Not reviewed]
0 3 0
Brett Smith - 4 years ago 2020-07-29 21:22:09
brettcsmith@brettcsmith.org
reports: Add BaseODS.set_common_properties() method.
3 files changed with 47 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/core.py
Show inline comments
...
 
@@ -19,17 +19,21 @@ import collections
 
import copy
 
import datetime
 
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]
 
import odf.number  # type:ignore[import]
 
import odf.opendocument  # type:ignore[import]
 
import odf.style  # type:ignore[import]
...
 
@@ -1049,12 +1053,28 @@ class BaseODS(BaseSpreadsheet[RT, ST], metaclass=abc.ABCMeta):
 
            aligned_style.setAttribute('family', 'table-cell')
 
            aligned_style.addElement(odf.style.ParagraphProperties(textalign=textalign))
 
            setattr(self, f'style_{textalign}text', aligned_style)
 

	
 
    ### 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,
 
                            valuetype: Optional[str]=None,
 
    ) -> odf.meta.UserDefined:
 
        if valuetype is None:
tests/test_reports_spreadsheet.py
Show inline comments
...
 
@@ -745,6 +745,24 @@ def test_ods_writer_set_custom_property(ods_writer, value, exptype):
 
        expected = str(value).lower()
 
    elif exptype == 'date':
 
        expected = value.isoformat()
 
    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*\''
tests/testutil.py
Show inline comments
...
 
@@ -14,18 +14,20 @@
 
# You should have received a copy of the GNU Affero General Public License
 
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
 

	
 
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
 

	
 
from decimal import Decimal
 
from pathlib import Path
...
 
@@ -285,12 +287,19 @@ class TestConfig:
 
        return self._rt_client
 

	
 
    def rt_wrapper(self):
 
        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'),
 
        ('(Unnamed)', 'text/plain', '1.2k'),
 
        ('(Unnamed)', 'text/html', '1.4k'),
 
    ]
0 comments (0 inline, 0 general)