Changeset - 5893d6a59a2b
[Not reviewed]
0 1 0
Brett Smith - 3 years ago 2021-03-09 15:39:12
brettcsmith@brettcsmith.org
query: Add --calendar-year and --fiscal-year shortcuts.
1 file changed with 43 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/reports/query.py
Show inline comments
...
 
@@ -419,6 +419,35 @@ class ReportFormat(enum.Enum):
 
    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
 
        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)
...
 
@@ -438,6 +467,20 @@ full date, and %(prog)s will use the fiscal year for that date.
 
        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)
0 comments (0 inline, 0 general)