Files @ ef03893bfed3
Branch filter:

Location: NPO-Accounting/conservancy_beancount/conservancy_beancount/reports/query.py

Brett Smith
query: Convert query functions that return List to Set.

Beancount's built-in renderers expect this and are better equipped for it.
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
"""query.py - Report arbitrary queries with advanced loading and formatting

This tool extends Beancount's bean-query with the following:

* ``META_DOCS("meta-key")`` function: Given the name of metadata with
  documentation in it, returns a list of the documentation links.

* ``STR_META("meta-key")`` function: Looks up metadata like ANY_META, then
  returns the result as a nicely formatted string, to make sorting easier or
  just provide prettier reports.

* ``SET()`` function: Returns only unique arguments over a GROUP BY.

* ODS output format, with proper formatting of currency and documentation links.

* Loads books from your configuration file, with options available to specify
  which year(s) to load.

* Can load rewrite rules just like other reports. Your queries will show the
  transformed entries.

* Improved error reporting.

Run it like bean-query, except instead of specifying a books filename, use
date options like ``--begin``, ``--end``, ``--fy``, and ``--cy`` to specify
the year(s) you want to load. Run ``query-report --help`` for details about
those.

Start an interactive shell::

    query-report [year options]

Write a spreadsheet with results for one query::

    query-report [year options] [-O OUTPUT.ods] <query string>

    query-report [year options] [-O OUTPUT.ods] < QUERY_FILE.bql

query-report also accepts all the same options as bean-query, like ``--format``
and ``--numberify``.
"""
# Copyright © 2021  Brett Smith
# License: AGPLv3-or-later WITH Beancount-Plugin-Additional-Permission-1.0
#
# Full copyright and licensing details can be found at toplevel file
# LICENSE.txt in the repository.

import argparse
import contextlib
import datetime
import enum
import functools
import itertools
import logging
import sys

from typing import (
    cast,
    Any,
    Callable,
    ClassVar,
    Dict,
    Hashable,
    Iterable,
    Iterator,
    List,
    Mapping,
    MutableMapping,
    NamedTuple,
    Optional,
    Sequence,
    Set,
    TextIO,
    Tuple,
    Type,
    Union,
)
from ..beancount_types import (
    MetaKey,
    MetaValue,
    OptionsMap,
    Posting,
    Transaction,
)

from decimal import Decimal
from pathlib import Path
from beancount.core.amount import _Amount as BeancountAmount
from beancount.core.inventory import Inventory
from beancount.core.position import _Position as Position

import beancount.query.numberify as bc_query_numberify
import beancount.query.query_compile as bc_query_compile
import beancount.query.query_env as bc_query_env
import beancount.query.query_execute as bc_query_execute
import beancount.query.query_parser as bc_query_parser
import beancount.query.query_render as bc_query_render
import beancount.query.shell as bc_query_shell
import odf.table  # type:ignore[import]
import rt

from . import core
from . import rewrite
from .. import books
from .. import cliutil
from .. import config as configmod
from .. import data
from .. import rtutil

PROGNAME = 'query-report'
SENTINEL = object()
logger = logging.getLogger('conservancy_beancount.reports.query')

CellFunc = Callable[[Any], odf.table.TableCell]
EnvironmentFunctions = Dict[
    # The real key type is something like:
    #   Union[str, Tuple[str, Type, ...]]
    # but two issues with that. One, you can't use Ellipses in a Tuple like
    # that, so there's no short way to declare this. Second, Beancount doesn't
    # declare it anyway, and mypy infers it as Sequence[object]. So just use
    # that.
    Sequence[object],
    Type[bc_query_compile.EvalFunction],
]
RowTypes = Sequence[Tuple[str, Type]]
Rows = Sequence[NamedTuple]
RTResult = Optional[Mapping[Any, Any]]
Store = List[Any]
QueryExpression = Union[
    bc_query_parser.Column,
    bc_query_parser.Constant,
    bc_query_parser.Function,
    bc_query_parser.UnaryOp,
]
QueryStatement = Union[
    bc_query_parser.Balances,
    bc_query_parser.Journal,
    bc_query_parser.Select,
]

# This class annotates the types that Beancount's RowContexts have when they're
# passed to EvalFunction.__call__(). These types get set across
# create_row_context and execute_query.
class PostingContext:
    posting: Posting
    entry: Transaction
    balance: Inventory
    options_map: OptionsMap
    account_types: Mapping
    open_close_map: Mapping
    commodity_map: Mapping
    price_map: Mapping
    # Dynamically set by execute_query
    store: Store


class MetaDocs(bc_query_env.AnyMeta):
    """Return a list of document links from metadata."""
    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
        super(bc_query_env.AnyMeta, self).__init__(operands, set)
        # The second argument is our return type.
        # It should match the annotated return type of __call__.

    def __call__(self, context: PostingContext) -> Set[str]:
        raw_value = super().__call__(context)
        seq = raw_value.split() if isinstance(raw_value, str) else ''
        return set(seq)


class RTField(NamedTuple):
    key: str
    parse: Optional[Callable[[str], object]]
    unset_value: Optional[str] = None

    def load(self, rt_ticket: RTResult) -> object:
        value = rt_ticket.get(self.key) if rt_ticket else None
        if not value or value == self.unset_value:
            return None
        elif self.parse is None:
            return value
        else:
            return self.parse(value)


class RTTicket(bc_query_compile.EvalFunction):
    """Look up a field from RT ticket(s) mentioned in metadata documentation"""
    __intypes__ = [str, str, int]
    FIELDS = {key: RTField(key, None) for key in [
        'AdminCc',
        'Cc',
        'Creator',
        'Owner',
        'Queue',
        'Status',
        'Subject',
        'Requestors',
    ]}
    FIELDS.update((key, RTField(key, int, '0')) for key in [
        'numerical_id',
        'FinalPriority',
        'InitialPriority',
        'Priority',
        'TimeEstimated',
        'TimeLeft',
        'TimeWorked',
    ])
    FIELDS.update((key, RTField(key, rtutil.RTDateTime, 'Not set')) for key in [
        'Created',
        'Due',
        'LastUpdated',
        'Resolved',
        'Started',
        'Starts',
        'Told',
    ])
    FIELDS.update({key.lower(): value for key, value in FIELDS.items()})
    FIELDS['id'] = FIELDS['numerical_id']
    FIELDS['AdminCC'] = FIELDS['AdminCc']
    FIELDS['CC'] = FIELDS['Cc']
    RT_CLIENT: ClassVar[rt.Rt]
    # _CACHES holds all of the caches for different RT instances that have
    # been passed through RTTicket.with_client().
    _CACHES: ClassVar[Dict[Hashable, MutableMapping[str, RTResult]]] = {}
    # _rt_cache is the cache specific to this RT_CLIENT.
    _rt_cache: ClassVar[MutableMapping[str, RTResult]] = {}

    @classmethod
    def with_client(cls, client: rt.Rt, cache_key: Hashable) -> Type['RTTicket']:
        return type(cls.__name__, (cls,), {
            'RT_CLIENT': client,
            '_rt_cache': cls._CACHES.setdefault(cache_key, {}),
        })

    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
        if not hasattr(self, 'RT_CLIENT'):
            raise RuntimeError("no RT client available - cannot use rt_ticket()")
        rt_op, meta_op, *rest = operands
        # We have to evaluate the RT and meta keys on each call, because they
        # might themselves be dynamic. In the common case they're constants.
        # In that case, check for typos so we can report an error to the user
        # before execution even begins.
        if isinstance(rt_op, bc_query_compile.EvalConstant):
            self._rt_key(rt_op.value)
        if isinstance(meta_op, bc_query_compile.EvalConstant):
            self._meta_key(meta_op.value)
        if not rest:
            operands.append(bc_query_compile.EvalConstant(sys.maxsize))
        super().__init__(operands, set)

    def _rt_key(self, key: str) -> RTField:
        try:
            return self.FIELDS[key]
        except KeyError:
            raise ValueError(f"unknown RT ticket field {key!r}") from None

    def _meta_key(self, key: str) -> str:
        if key in data.LINK_METADATA:
            return key
        else:
            raise ValueError(f"metadata key {key!r} does not contain documentation links")

    def __call__(self, context: PostingContext) -> Set[object]:
        rt_key: str
        meta_key: str
        limit: int
        rt_key, meta_key, limit = self.eval_args(context)
        rt_field = self._rt_key(rt_key)
        meta_key = self._meta_key(meta_key)
        if context.posting.meta is None:
            meta_value: Any = SENTINEL
        else:
            meta_value = context.posting.meta.get(meta_key, SENTINEL)
        if meta_value is SENTINEL:
            meta_value = context.entry.meta.get(meta_key)
        if not isinstance(meta_value, str) or limit < 1:
            meta_value = ''
        ticket_ids: Set[str] = set()
        for link_s in meta_value.split():
            rt_id = rtutil.RT.parse(link_s)
            if rt_id is not None:
                ticket_ids.add(rt_id[0])
                if len(ticket_ids) >= limit:
                    break
        retval: Set[object] = set()
        for ticket_id in ticket_ids:
            try:
                rt_ticket = self._rt_cache[ticket_id]
            except KeyError:
                rt_ticket = self.RT_CLIENT.get_ticket(ticket_id)
                self._rt_cache[ticket_id] = rt_ticket
            field_value = rt_field.load(rt_ticket)
            if field_value is None:
                pass
            elif isinstance(field_value, list):
                retval.update(field_value)
            else:
                retval.add(field_value)
        return retval


class StrMeta(bc_query_env.AnyMeta):
    """Looks up metadata like AnyMeta, then always returns a string."""
    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
        super(bc_query_env.AnyMeta, self).__init__(operands, str)

    def __call__(self, context: PostingContext) -> str:
        raw_value = super().__call__(context)
        if raw_value is None:
            return ''
        else:
            return str(raw_value)


class AggregateSet(bc_query_compile.EvalAggregator):
    """Filter argument values that aren't unique."""
    __intypes__ = [object]

    def __init__(self, operands: List[bc_query_compile.EvalNode]) -> None:
       super().__init__(operands, set)

    def allocate(self, allocator: bc_query_execute.Allocator) -> None:
        """Allocate and save an index handle into result storage."""
        self.handle = allocator.allocate()

    def initialize(self, store: Store) -> None:
        """Prepare result storage for a new aggregation."""
        store[self.handle] = self.dtype()
        # self.dtype() is our return type, aka the second argument to __init__
        # above, aka the annotated return type of __call__.

    def update(self, store: Store, context: PostingContext) -> None:
        """Update existing storage with new result data."""
        value, = self.eval_args(context)
        if isinstance(value, Sequence) and not isinstance(value, (str, tuple)):
            store[self.handle].update(value)
        else:
            store[self.handle].add(value)

    def __call__(self, context: PostingContext) -> set:
        """Return the result for an aggregation."""
        return context.store[self.handle]  # type:ignore[no-any-return]


class _EnvironmentMixin:
    functions: EnvironmentFunctions

    @classmethod
    def with_rt_client(
            cls,
            rt_client: Optional[rt.Rt],
            cache_key: Hashable,
    ) -> Type['_EnvironmentMixin']:
        if rt_client is None:
            rt_ticket = RTTicket
        else:
            rt_ticket = RTTicket.with_client(rt_client, cache_key)
        functions = cls.functions.copy()
        functions[('rt_ticket', str, str)] = rt_ticket
        functions[('rt_ticket', str, str, int)] = rt_ticket
        return type(cls.__name__, (cls,), {'functions': functions})


class FilterPostingsEnvironment(bc_query_env.FilterPostingsEnvironment, _EnvironmentMixin):
    functions: EnvironmentFunctions = bc_query_env.FilterPostingsEnvironment.functions.copy()  # type:ignore[assignment]
    functions['meta_docs'] = MetaDocs
    functions['str_meta'] = StrMeta


class TargetsEnvironment(bc_query_env.TargetsEnvironment, _EnvironmentMixin):
    functions: EnvironmentFunctions = FilterPostingsEnvironment.functions.copy()  # type:ignore[assignment]
    functions.update(bc_query_env.AGGREGATOR_FUNCTIONS)
    functions['set'] = AggregateSet


class BooksLoader:
    """Closure to load books with a zero-argument callable

    This matches the load interface that BQLShell expects.
    """
    def __init__(
            self,
            books_loader: Optional[books.Loader],
            start_date: Optional[datetime.date]=None,
            stop_date: Optional[datetime.date]=None,
            rewrite_rules: Sequence[rewrite.RewriteRuleset]=(),
    ) -> None:
        self.books_loader = books_loader
        self.start_date = start_date
        self.stop_date = stop_date
        self.rewrite_rules = rewrite_rules

    def __call__(self) -> books.LoadResult:
        logger.debug("BooksLoader called")
        result = books.Loader.dispatch(self.books_loader, self.start_date, self.stop_date)
        logger.debug("books loaded from Beancount")
        if self.rewrite_rules:
            for index, entry in enumerate(result.entries):
                # entry might not be a Transaction; we catch that later.
                # The type ignores are because the underlying Beancount type isn't
                # type-checkable.
                postings = data.Posting.from_txn(entry)  # type:ignore[arg-type]
                for ruleset in self.rewrite_rules:
                    postings = ruleset.rewrite(postings)
                try:
                    result.entries[index] = entry._replace(postings=list(postings))  # type:ignore[call-arg]
                except AttributeError:
                    pass
            logger.debug("rewrite rules applied")
        return result


class BQLShell(bc_query_shell.BQLShell):
    def __init__(
            self,
            config: configmod.Config,
            is_interactive: bool,
            loadfun: Callable[[], books.LoadResult],
            outfile: TextIO,
            default_format: str='text',
            do_numberify: bool=False,
    ) -> None:
        super().__init__(is_interactive, loadfun, outfile, default_format, do_numberify)
        rt_credentials = config.rt_credentials()
        rt_key = rt_credentials.idstr()
        rt_client = config.rt_client(rt_credentials)
        self.env_postings = FilterPostingsEnvironment.with_rt_client(rt_client, rt_key)()
        self.env_targets = TargetsEnvironment.with_rt_client(rt_client, rt_key)()
        self.ods = QueryODS(config.rt_wrapper(rt_credentials))
        self.last_line_parsed = ''

    def run_parser(
            self,
            line: str,
            default_close_date: Optional[datetime.datetime]=None,
    ) -> None:
        self.last_line_parsed = line
        super().run_parser(line, default_close_date)

    @functools.wraps(bc_query_shell.BQLShell.on_Select, ('__doc__',))
    def on_Select(self, statement: QueryStatement) -> None:
        output_format: str = self.vars['format']
        try:
            render_func = getattr(self, f'_render_{output_format}')
        except AttributeError:
            logger.error("unknown output format %r", output_format)
            return

        try:
            logger.debug("compiling query")
            compiled_query = bc_query_compile.compile(
                statement, self.env_targets, self.env_postings, self.env_entries,
            )
            logger.debug("executing query")
            row_types, rows = bc_query_execute.execute_query(
                compiled_query, self.entries, self.options_map,
            )
            if self.vars['numberify']:
                logger.debug("numberifying query")
                row_types, rows = bc_query_numberify.numberify_results(
                    row_types, rows, self.options_map['dcontext'].build(),
                )
        except Exception as error:
            logger.error(str(error), exc_info=logger.isEnabledFor(logging.DEBUG))
            if (isinstance(error, TypeError)
                and error.args
                and ' not supported between instances ' in error.args[0]):
                logger.info(
                    "HINT: Are you using ORDER BY or comparisons with metadata "
                    "that isn't consistently set?\n  "
                    "Try looking up that metadata with str_meta() instead to "
                    "ensure your comparisons use a consistent data type.",
                )
            return

        if not rows and output_format != 'ods':
            print("(empty)", file=self.outfile)
        else:
            logger.debug("rendering query as %s", output_format)
            render_func(statement, row_types, rows)

    def _render_csv(self, statement: QueryStatement, row_types: RowTypes, rows: Rows) -> None:
        bc_query_render.render_csv(
            row_types,
            rows,
            self.options_map['dcontext'],
            self.outfile,
            self.vars['expand'],
        )

    def _render_ods(self, statement: QueryStatement, row_types: RowTypes, rows: Rows) -> None:
        self.ods.write_query(statement, row_types, rows, self.last_line_parsed)
        logger.info(
            "%s rows of results saved in sheet %s",
            len(rows),
            self.ods.sheet.getAttribute('name'),
        )

    def _render_text(self, statement: QueryStatement, row_types: RowTypes, rows: Rows) -> None:
        with contextlib.ExitStack() as stack:
            if self.is_interactive:
                output = stack.enter_context(self.get_pager())
            else:
                output = self.outfile
            bc_query_render.render_text(
                row_types,
                rows,
                self.options_map['dcontext'],
                output,
                self.vars['expand'],
                self.vars['boxed'],
                self.vars['spaced'],
            )


class QueryODS(core.BaseODS[NamedTuple, None]):
    META_FNAMES = frozenset([
        # Names of functions, as defined in Environments, that look up
        # posting metadata that could contain documentation links
        'any_meta',
        'entry_meta',
        'meta',
        'meta_docs',
        'str_meta',
    ])

    def is_empty(self) -> bool:
        return not self.sheet.childNodes

    def section_key(self, row: NamedTuple) -> None:
        return None

    def _generic_cell(self, value: Any) -> odf.table.TableCell:
        if isinstance(value, Iterable) and not isinstance(value, (str, tuple)):
            return self.multiline_cell(value)
        else:
            return self.string_cell('' if value is None else str(value))

    def _inventory_cell(self, value: Inventory) -> odf.table.TableCell:
        return self.balance_cell(core.Balance(pos.units for pos in value))

    def _link_string_cell(self, value: str) -> odf.table.TableCell:
        return self.meta_links_cell(value.split())

    def _metadata_cell(self, value: MetaValue) -> odf.table.TableCell:
        return self._cell_type(type(value))(value)

    def _position_cell(self, value: Position) -> odf.table.TableCell:
        return self.currency_cell(value.units)

    def _cell_type(self, row_type: Type) -> CellFunc:
        """Return a function to create a cell, for non-metadata row types."""
        if issubclass(row_type, Inventory):
            return self._inventory_cell
        elif issubclass(row_type, Position):
            return self._position_cell
        elif issubclass(row_type, BeancountAmount):
            return self.currency_cell
        elif issubclass(row_type, (int, float, Decimal)):
            return self.float_cell
        elif issubclass(row_type, datetime.date):
            return self.date_cell
        elif issubclass(row_type, str):
            return self.string_cell
        else:
            return self._generic_cell

    def _link_cell_type(self, row_type: Type) -> CellFunc:
        """Return a function to create a cell from metadata with documentation links."""
        if issubclass(row_type, str):
            return self._link_string_cell
        elif issubclass(row_type, tuple):
            return self._generic_cell
        elif issubclass(row_type, Iterable):
            return self.meta_links_cell
        else:
            return self._generic_cell

    def _meta_target(self, target: QueryExpression) -> Optional[MetaKey]:
        """Return the metadata key looked up by this target, if any

        This function takes a parsed target (i.e., what we're SELECTing) and
        recurses it to see whether it's looking up any metadata. If so, it
        returns the key of that metadata. Otherwise it returns None.
        """
        if isinstance(target, bc_query_parser.UnaryOp):
            return self._meta_target(target.operand)
        elif not isinstance(target, bc_query_parser.Function):
            return None
        try:
            operand = target.operands[0]
        except IndexError:
            return None
        if (target.fname in self.META_FNAMES
            and isinstance(operand, bc_query_parser.Constant)):
            return operand.value  # type:ignore[no-any-return]
        else:
            for operand in target.operands:
                retval = self._meta_target(operand)
                if retval is not None:
                    break
            return retval

    def _cell_types(self, statement: QueryStatement, row_types: RowTypes) -> Iterator[CellFunc]:
        """Return functions to create table cells from result rows

        Given a parsed query and the types of return rows, yields a function
        to create a cell for each column in the row, in order. The returned
        functions vary in order to provide the best available formatting for
        different data types.
        """
        if (isinstance(statement, bc_query_parser.Select)
            and isinstance(statement.targets, Sequence)):
            targets = [t.expression for t in statement.targets]
        else:
            # Synthesize something that makes clear we're not loading metadata.
            targets = [bc_query_parser.Column(name) for name, _ in row_types]
        for target, (_, row_type) in zip(targets, row_types):
            meta_key = self._meta_target(target)
            if meta_key is None:
                yield self._cell_type(row_type)
            elif meta_key in data.LINK_METADATA:
                yield self._link_cell_type(row_type)
            else:
                yield self._metadata_cell

    def write_query(
            self,
            statement: QueryStatement,
            row_types: RowTypes,
            rows: Rows,
            query_string: Optional[str]=None,
    ) -> None:
        if self.is_empty():
            self.sheet.setAttribute('name', "Query 1")
        else:
            self.use_sheet(f"Query {len(self.document.spreadsheet.childNodes) + 1}")
        for name, row_type in row_types:
            if issubclass(row_type, datetime.date):
                col_width = 1.0
            elif issubclass(row_type, (BeancountAmount, Inventory, Position)):
                col_width = 1.5
            else:
                col_width = 2.0
            col_style = self.column_style(col_width)
            self.sheet.addElement(odf.table.TableColumn(stylename=col_style))
        self.add_row(*(
            self.string_cell(data.Metadata.human_name(name), stylename=self.style_bold)
            for name, _ in row_types
        ))
        self.lock_first_row()
        if query_string:
            self.add_annotation(query_string, parent=self.sheet.lastChild.firstChild)
        cell_funcs = list(self._cell_types(statement, row_types))
        for row in rows:
            self.add_row(*(
                cell_func(value)
                for cell_func, value in zip(cell_funcs, row)
            ))


class ReportFormat(enum.Enum):
    TEXT = 'text'
    TXT = TEXT
    CSV = 'csv'
    ODS = 'ods'


class SetCYDates(argparse.Action):
    def __call__(self,
                 parser: argparse.ArgumentParser,
                 namespace: argparse.Namespace,
                 values: Union[Sequence[Any], str, None]=None,
                 option_string: Optional[str]=None,
    ) -> None:
        value = cliutil.year_or_date_arg(str(values))
        if isinstance(value, int):
            value = datetime.date(value, 1, 1)
        namespace.start_date = value
        namespace.stop_date = cliutil.diff_year(value, 1)


class SetFYDates(argparse.Action):
    def __call__(self,
                 parser: argparse.ArgumentParser,
                 namespace: argparse.Namespace,
                 values: Union[Sequence[Any], str, None]=None,
                 option_string: Optional[str]=None,
    ) -> None:
        value = cliutil.year_or_date_arg(str(values))
        namespace.start_date = value
        # The configuration hasn't been loaded, so we don't know the boundaries
        # of a fiscal year yet. But that's okay, because we just need to set
        # enough so that when these arguments are passed to a BooksLoader,
        # it'll load the right fiscal year.
        if isinstance(value, int):
            namespace.stop_date = value
        else:
            namespace.stop_date = value + datetime.timedelta(days=1)


def parse_arguments(arglist: Optional[Sequence[str]]=None) -> argparse.Namespace:
    parser = argparse.ArgumentParser(prog=PROGNAME)
    cliutil.add_version_argument(parser)
    cliutil.add_loglevel_argument(parser)
    parser.add_argument(
        '--begin', '--start', '-b',
        dest='start_date',
        metavar='YEAR',
        type=cliutil.year_or_date_arg,
        help="""Begin loading entries from this fiscal year. You can specify a
full date, and %(prog)s will use the fiscal year for that date.
""")
    parser.add_argument(
        '--end', '--stop', '-e',
        dest='stop_date',
        metavar='YEAR',
        type=cliutil.year_or_date_arg,
        help="""End loading entries at this fiscal year. You can specify a
full date, and %(prog)s will use the fiscal year for that date.
""")
    parser.add_argument(
        '--calendar-year', '--cy',
        action=SetCYDates,
        metavar='YEAR',
        help="""Shortcut to set --begin and --end to load a single calendar year.
You can specify a full date, or just a year to start from January 1.
""")
    parser.add_argument(
        '--fiscal-year', '--fy',
        action=SetFYDates,
        metavar='YEAR',
        help="""Shortcut to set --begin and --end to load a single fiscal year.
You can specify a full date, and %(prog)s will use the fiscal year for that date.
""")
    cliutil.add_rewrite_rules_argument(parser)
    format_arg = cliutil.EnumArgument(ReportFormat)
    parser.add_argument(
        '--report-type', '--format', '-t', '-f',
        metavar='TYPE',
        type=format_arg.enum_type,
        help="""Format of report to generate. Choices are
{format_arg.choices_str()}. Default is guessed from your output filename
extension. If that fails, default is 'text' for interactive output, and 'ods'
otherwise.
""")
    parser.add_argument(
        '--numberify', '-m',
        action='store_true',
        help="""Separate currency amounts into numeric columns by currency
""")
    parser.add_argument(
        '--output-file', '-O', '-o',
        metavar='PATH',
        type=Path,
        help="""Write the report to this file, or stdout when PATH is `-`.
The default is stdout for text and CSV reports, and a generated filename for
ODS reports.
""")
    parser.add_argument(
        'query',
        nargs=argparse.ZERO_OR_MORE,
        default=[],
        help="""Query to run non-interactively. If none is provided, and
standard input is not a terminal, reads the query from stdin instead.
""")
    return parser.parse_args(arglist)

def main(arglist: Optional[Sequence[str]]=None,
         stdout: TextIO=sys.stdout,
         stderr: TextIO=sys.stderr,
         config: Optional[configmod.Config]=None,
) -> int:
    args = parse_arguments(arglist)
    cliutil.set_loglevel(logger, args.loglevel)
    if config is None:
        config = configmod.Config()
        config.load_file()

    query = ' '.join(args.query).strip()
    if not query and not sys.stdin.isatty():
        query = sys.stdin.read().strip()
    if args.report_type is None:
        try:
            args.report_type = ReportFormat[args.output_file.suffix[1:].upper()]
        except (AttributeError, KeyError):
            args.report_type = ReportFormat.ODS if query else ReportFormat.TEXT

    load_func = BooksLoader(
        config.books_loader(),
        args.start_date,
        args.stop_date,
        [rewrite.RewriteRuleset.from_yaml(path) for path in args.rewrite_rules],
    )
    shell = BQLShell(
        config,
        not query,
        load_func,
        stdout,
        args.report_type.value,
        args.numberify,
    )
    shell.on_Reload()
    if query:
        shell.onecmd(query)
    else:
        shell.cmdloop()

    if not shell.ods.is_empty():
        shell.ods.set_common_properties(config.books_repo())
        if args.output_file is None:
            out_dir_path = config.repository_path() or Path()
            args.output_file = out_dir_path / 'QueryResults_{}.ods'.format(
                datetime.datetime.now().isoformat(timespec='seconds'),
            )
            logger.info("Writing spreadsheet to %s", args.output_file)
        ods_file = cliutil.bytes_output(args.output_file, stdout)
        shell.ods.save_file(ods_file)

    return cliutil.ExitCode.OK

entry_point = cliutil.make_entry_point(__name__, PROGNAME)

if __name__ == '__main__':
    exit(entry_point())