diff --git a/src/_ardaas.py b/src/_ardaas.py new file mode 100644 index 0000000..1e264a5 --- /dev/null +++ b/src/_ardaas.py @@ -0,0 +1,407 @@ +# ------------------------------------------------------------------------------ +# _ardaas.py - Ardaas generator +# +# June 2023, Gurkiran Singh +# +# Copyright (c) 2023 +# All rights reserved. +# ------------------------------------------------------------------------------ + +"""Handler for ardaas subparser.""" + +from __future__ import annotations + +__all__ = ["parse"] + +import argparse + +import _cmn + +_log = _cmn.Logger("ardaas") + + +def _amrit_vela(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to request to be woken up for amrit vela. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + f"{_pluralise('dws', multiple)} nUM sie rihq Aqy pUrw Srdw bKSo jI[ ", + f"kl svyry nUM, kl AMimRq vyly iv~c {_pluralise('dws', multiple)} " + + "nUM AMimRq vylw iv~c jgW ky auTw ky gurbwnI pVwau[ ", + f"{_pluralise('dws', multiple)} nUM AMimRq vylw dI dwn bKSo[ ", + ] + + +def _anand_sahib(add: bool) -> list[str]: + """Add lines to the ardaas to state that 6 pauri Anand Sahib was read/sung. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "Cy pauVI AnMd swihb hoey[ ", + ] + + +def _degh(parshaad: bool, langar: bool) -> list[str]: + """Add lines to the ardaas to state that bhog of parshaad and/or langar was + done. + + :param parshaad: Parshaad is present + :param langar: Langar is present + :return: list of lines to add to the ardaas. + """ + if not (parshaad or langar): + return [] + + anik_prakar = "Aink pRkwr Bojn bhu kIey bhu ibMjn imstwey] \ +krI pwkswl soc pivqRw huix lwvhu Bogu hir rwey] " + + if parshaad and langar: + degh = "kVwh pRswd dI dyG Aqy lMgr" + elif parshaad: + degh = "kVwh pRswd dI dyG" + elif langar: + degh = "lMgr" + else: + raise ValueError("Either parshaad or langar must be True") + + return [ + f"{degh} swjky hwzr hn[ ", + anik_prakar, + f"prvwn kIqw {degh} swD sMgq dw rsnw dy lwiek hox[ ", + "jo jI C~ky so qyrw hI nwm jpy[ ", + ] + + +def _generate(ctx: argparse.Namespace) -> None: + """Handler for all generate requests to the Ardaas CLI. + + :param ctx: context about the original instruction. + """ + start_unicode = ["Awp jI dy hzUr Ardws byNqI jodVI hY[ "] + end_unicode = [ + "A~Kr vwDw Gwtw Bul cuk mwP krnI[ ", + "srb~q dy kwrj rws krny[ ", + "seI ipAwry myl ijnHW imilAW qyrw nwm icq Awvy[ ", + "nwnk nwm cVHdI klw[ ", + "qyry Bwny srb~q dw Blw[ ", + ] + + ardaas_unicode = [] + + ardaas_unicode.extend(_read_specific_banis(ctx.read_bani)) + ardaas_unicode.extend(_sukhmani(ctx.sukhmani)) + ardaas_unicode.extend(_kirtan(ctx.kirtan)) + ardaas_unicode.extend(_anand_sahib(ctx.anand_sahib)) + ardaas_unicode.extend(_sukhaasan_post(ctx.sukhaasan_post)) + # For any banis that were read: + ardaas_unicode.extend( + _read_banis( + ctx.read_bani + or ctx.sukhmani + or ctx.kirtan + or ctx.anand_sahib + or ctx.sukhaasan_post, + ctx.multiple, + ) + ) + + ardaas_unicode.extend(_katha(ctx.katha)) + + ardaas_unicode.extend(_akhand_paath_arambh(ctx.akhand_paath_arambh)) + ardaas_unicode.extend(_akhand_paath_bhog(ctx.akhand_paath_bhog)) + ardaas_unicode.extend( + _sehaj_paath_arambh(ctx.sehaj_paath_arambh, ctx.multiple) + ) + ardaas_unicode.extend(_sehaj_paath_madh(ctx.sehaj_paath_madh, ctx.multiple)) + ardaas_unicode.extend(_sehaj_paath_bhog(ctx.sehaj_paath_bhog, ctx.multiple)) + + ardaas_unicode.extend(_birthday(ctx.birthday)) + + ardaas_unicode.extend(_hukamnama(ctx.hukamnama, ctx.multiple)) + + ardaas_unicode.extend(_degh(ctx.parshaad, ctx.langar)) + + ardaas_unicode.extend(_sukhaasan_pre(ctx.sukhaasan_pre)) + + ardaas_unicode.extend( + [ + f"{_pluralise('Awpxy', ctx.multiple)} Axjwx " + + f"{_pluralise('b~cy', ctx.multiple)} dy isr qy myhr BirAw h~Q " + + "r~Kxw[ ", + f"{_pluralise('Awpxy', ctx.multiple)} " + + f"{_pluralise('b~cy', ctx.multiple)} nUM kwm kRoD loB moh AhMkwr " + + "ausq~q inMidAw cuglIAw qoN bcwA ky r~Kxw[ ", + ] + ) + + ardaas_unicode.extend(_amrit_vela(ctx.amrit_vela, ctx.multiple)) + + ardaas_unicode = start_unicode + ardaas_unicode + end_unicode + ardaas = "".join(ardaas_unicode) + + # Now that the ardaas is fully generated, translate it + if ctx.romanised: + ardaas = _cmn.gurbani_unicode_to_romanised(ardaas) + + print(ardaas) + + +def _akhand_paath_arambh(add: bool) -> list[str]: + """Add lines to the ardaas to state that an akhand paath is about to start. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "Awp jI dy dwsW nUM AwigAw bKSo AKMf pwT dw AwrMBqw krn leI[ ", + ] + + +def _akhand_paath_bhog(add: bool) -> list[str]: + """Add lines to the ardaas to state that an akhand paath has been done. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "Awp jI dy dwsW ny AKMf pwT riKAw[ ", + ] + + +def _birthday(add: bool) -> list[str]: + """Add lines to the ardaas to state that today is someone's birthday. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return ["A~j ... dw jnmidn hY[ ", "ienUM gurisKI jIvn bKSo jI[ "] + + +def _hukamnama(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to state that a hukamnama was taken. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + jagat_jalandhaa = ( + "jgqu jlµdw riK lY AwpxI ikrpw Dwir] " + + "ijqu duAwrY aubrY iqqY lYhu aubwir] " + + "siqguir suKu vyKwilAw scw sbdu bIcwir] " + + "nwnk Avru n suJeI hir ibnu bKsxhwru] " + ) + + hukamanama_benti = ( + f"{_pluralise('dws', multiple)} nUM awp jI dw pwvn pivqr hukmnwmw bKSo " + + "jI qy hukmnwmy dy c~lx dI smr~Qw bKSo[ " + ) + + return [jagat_jalandhaa, hukamanama_benti] + + +def _katha(add: bool) -> list[str]: + """Add lines to the ardaas to state that katha was done. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "Awp jI dw Ak~Q k~Qw kr idAw[ ", + ] + + +def _kirtan(add: bool) -> list[str]: + """Add lines to the ardaas to state that kirtan was done. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "Sbd kIrqn hoey[ ", + ] + + +def parse(ctx: argparse.Namespace) -> None: + """Main handler for Ardaas CLI. This is the API called by the main + Gurbani Analysis CLI. + + :param ctx: context received from the Gurbani Analysis CLI. Namespace object + containing the args received by the CLI. + """ + _log.set_level(ctx.verbosity) + _generate(ctx) + + return _cmn.RC.SUCCESS + + +def _pluralise(word: str, plural: bool) -> str: + """Take in a punjabi work and return the pluralised version of it, if + `plural` is True. Else, return the original word. + + :param word: word to pluralise. + :param plural: whether to pluralise the word or not. + :return: pluralised word, if `plural` is True. Else, return the original + """ + if not plural: + return word + + special_cases: dict[str, str] = {} + + if word in special_cases: + return special_cases[word] + + if word[-1] == "y": + pluralised = ( + word[:-2] + "i" + word[-2] + "AW" + ) # change laav to sihaari + aera + kannaa bindi + else: + pluralised = word + "W" # add kannaa + return pluralised + + +def _read_banis(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to state that bani was read. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + f"{_pluralise('dws', multiple)} ny Awp jI dy crnw kmlw pws Su~D Aqy " + + "sp~St bwnI pVI, suxI Aqy ivcwr kIqw[ ", + "ies bwnI dw Bwv, ies bwnI dw P~l sMgqw dy ihrdy ivc vswauxw[ ", + "bwnI pVHn, suxn Aqy ivcwr krn dy ivc AnkyW prkwr dIAw glqIAW hoieAw[ ", + "Bul cu`k mwP krnI[ ", + ] + + +def _read_specific_banis(add: bool) -> list[str]: + """Add lines to the ardaas to state that specific banis were read. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return ["Awp jI ... dw jwp krvwieAw[ "] + + +def _sehaj_paath_arambh(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to state that a sehaj paath raul is about to start. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + f"Awp jI dy {_pluralise('dws', multiple)} nUM AwigAw bKSo shj pwT dw " + + "rOl dw AwrMBqw krn leI[ ", + ] + + +def _sehaj_paath_madh(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to state that an sehaj paath raul has been done. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + f"Awp jI dy {_pluralise('dws', multiple)} ny shj pwT ivc mD ivc AweI hY[ ", + ] + + +def _sehaj_paath_bhog(add: bool, multiple: bool) -> list[str]: + """Add lines to the ardaas to state that an sehaj paath raul has been done. + + :param add: whether to add the lines or not. + :param multiple: set to True if multiple people are in the sangat. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + f"Awp jI dy {_pluralise('dws', multiple)} ny shj pwT dw rOl riKAw[ ", + ] + + +def _sukhaasan_pre(add: bool) -> list[str]: + """Add lines to the ardaas to request permission to do sukhaasan. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return ["Awp jI dI suKwsn syvw krn dw AwigAw bKSnw[ "] + + +def _sukhaasan_post(add: bool) -> list[str]: + """Add lines to the ardaas to state that sukhaasan was done. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + return [ + "AwigAw iml ky kIrqn soihlw dw pwT pVI geI Aqy Awp jI dI suKwsn syvw hoeI[ " + ] + + +def _sukhmani(add: bool) -> list[str]: + """Add lines to the ardaas to state that Sukhmani Sahib was read. + + :param add: whether to add the lines or not. + :return: list of lines to add to the ardaas. + """ + if not add: + return [] + + sukhmani = "suKmnI suK AMimRq pRB nwmu] Bgq jnw kY min ibsRwm] " + + sukhmani_ardaas = "suKmnI swihb dw jwp how[ " + + return [sukhmani, sukhmani_ardaas] diff --git a/src/_cmn.py b/src/_cmn.py index 267f523..6dab1a7 100644 --- a/src/_cmn.py +++ b/src/_cmn.py @@ -13,6 +13,7 @@ __all__ = [ "Error", + "gurbani_unicode_to_romanised", "Logger", "NotImplementedException", "RC", @@ -20,7 +21,7 @@ "Verbosity", ] -from typing import Any, Optional +from typing import Any, Optional, Union import enum import logging @@ -39,6 +40,7 @@ def __init__( self.rc = rc self.msg = msg self.suggested_steps = suggested_steps + Exception.__init__(self, msg) def __str__(self) -> str: output = "\nError: " @@ -153,3 +155,177 @@ class Verbosity(enum.Enum): STANDARD = 20 VERBOSE = 15 VERY_VERBOSE = 10 + + +class _LetterType(enum.Enum): + """Special cases of letters""" + + ADHAK = "adhak" + + +def gurbani_unicode_to_romanised( # pylint: disable=too-many-branches + unicode: str, +) -> str: + """Converts unicode Gurmukhi to romanised Gurmukhi. + + :param unicode: unicode Gurmukhi string. + :return: romanised Gurmukhi string. + """ + grammar_mapping = { + " ": " ", # space + "[": ".", # full stop + "]": ".", # double full stop + ".": ".", # full stop (probably used in elipses) + ",": ",", # comma + } + oora_aera_eeri_mapping = { + # For oora aera and eeri, add mukta sound. Presence of a vowel will + # modify this further. + "a": "", # oora + "A": "a", # aera + "e": "", # eeri + } + consonant_mapping = { + # Oora, aera and eeri are in the vowels mapping as they follow that + # pattern better. + "s": "s", # sassa + "h": "h", # haha + "k": "k", # kakka + "K": "kh", # khakha + "g": "g", # gagga + "G": "gh", # ghagha + "|": "ng", # nganga + "c": "ch", # chacha + "C": "chh", # chhachha + "j": "j", # jaja + "J": "jh", # jhajha + "\\": "nj", # njanja + "t": "tt", # tainka + "T": "tth", # ttattha + "f": "dd", # ddadda + "F": "ddh", # ddhaddha + "x": "ṉ", # nana + "q": "t", # tata + "Q": "th", # thatha + "d": "d", # dada + "D": "dh", # dhadha + "n": "n", # nana + "p": "p", # pappa + "P": "ph", # phapha + "b": "b", # babba + "B": "bh", # bhabha + "m": "m", # mamma + "X": "y", # yaya + "r": "r", # rara + "l": "l", # lala + "v": "v", # vava + "V": "ṙ", # rrarra + "S": "sh", # shasha + "Z": "ghh", # ghhaghha + "@@@1": "", # khhakhha @@@ + "z": "z", # zazza + "@@@2": "", # faffa @@@ + "L": "ḷ", # lalla pair bindi + } + pairee_mapping = { + "H": "h", # haha + "R": "r", # rara + } + vowel_mapping = { + # True vowels. + "w": "aa", # kannaa + "W": "aaṅ", # kannaa bindi + "i": "i", # sihaari + "I": "ee", # bihaari + "u": "u", # aunkar + "U": "oo", # dulainkar + "o": "o", # horaa + "O": "ou", # kanhaura + "y": "ae", # laav + "Y": "ai", # dulaav + } + semi_vowel_mapping = { + "N": "ṅ", # bindee + "M": "ṅ", # tippee + "µ": "ṅ", # tippee + } + special_mapping = { + "~": _LetterType.ADHAK, # adhak + "`": _LetterType.ADHAK, # adhak + } + + mapping: dict[str, Union[str, _LetterType]] = {} + mapping.update(grammar_mapping) + mapping.update(oora_aera_eeri_mapping) + mapping.update(consonant_mapping) + mapping.update(pairee_mapping) + mapping.update(vowel_mapping) + mapping.update(semi_vowel_mapping) + mapping.update(special_mapping) + + romanised: list[str] = [] + buf = "" + adhak = False + + for i, char in enumerate(unicode): + mapped_char = mapping[char] + if ( + char in special_mapping + and special_mapping[char] is _LetterType.ADHAK + ): # adhak + adhak = True + continue + + assert isinstance(mapped_char, str) + + if romanised and char == " ": + # If the last character is a sihaari without an eeri, or an aunkar + # without an oora, add brackets around the romanised sound + if len(unicode) > 2 and ( + (unicode[i - 2] == "i" and unicode[i - 1] != "e") + or (unicode[i - 1] == "u" and unicode[i - 2] != "a") + ): + romanised = romanised[:-1] + ["(" + romanised[-1] + ")"] + if char == "i": # sihaari + buf = mapped_char + elif char in pairee_mapping: + if romanised[-1] == "i": + romanised = romanised[:-2] + [mapped_char, romanised[-1]] + else: + romanised.append(mapped_char) + else: + if ( + romanised + and romanised[-1].lower() == "a" + and "aa" in mapped_char + ): + romanised[-1] = "" + # For consecutive consonants, and for vowels after oora aera eeri, + # add mukta sound between them + elif romanised and ( + ( + romanised[-1].lower() in consonant_mapping.values() + and char + in list(oora_aera_eeri_mapping) + + list(consonant_mapping) + + list(pairee_mapping) + ) + or ( + romanised[-1].lower() in oora_aera_eeri_mapping.values() + and char in vowel_mapping + ) + ): + romanised[-1] += "a" + if mapped_char: + if adhak: + mapped_char = mapped_char.upper() + adhak = False + romanised.append(mapped_char) + if buf: + romanised.append(buf) + buf = "" + + return "".join(romanised) + + +gurbani_unicode_to_romanised("ijnI nwmu iDAwieAw gey mskiq Gwil ]") diff --git a/src/_hukamnama.py b/src/_hukamnama.py index cb92266..0c906fd 100644 --- a/src/_hukamnama.py +++ b/src/_hukamnama.py @@ -649,7 +649,7 @@ def _load_webpage_data(url: str) -> str: return html -def parse(ctx: argparse.Namespace) -> None: +def parse(ctx: argparse.Namespace) -> int: """Main handler for Hukamnama CLI. This is the API called by the main Gurbani Analysis CLI. @@ -660,6 +660,8 @@ def parse(ctx: argparse.Namespace) -> None: if ctx.function == Function.DATA.value: _data(ctx) + return _cmn.RC.SUCCESS + def _separate_manglacharan( shabad_lines: MutableSequence[str], diff --git a/src/main.py b/src/main.py index 5c7b034..6bcd97e 100755 --- a/src/main.py +++ b/src/main.py @@ -24,6 +24,7 @@ import traceback import _cmn +import _ardaas import _hukamnama @@ -41,50 +42,106 @@ def _exit(rc: _cmn.RC, exc: Optional[_cmn.Error] = None) -> None: sys.exit(int(rc.value)) -def main() -> None: - """Main handler for Gurbani Analysis CLI. Calls callbacks based on the - subcommand received. +def _add_ardaas_parser(subparser: argparse._SubParsersAction) -> None: + """Add parser for ardaas subcommand. + + :param subparser: subparser to add to. """ - rc = _cmn.RC.SUCCESS - _log.set_level(_cmn.Verbosity.SUPPRESSED) + ardaas = subparser.add_parser( + "ardaas", description="Generate the customised portion of ardaas." + ) - # Main parser - parser = argparse.ArgumentParser( - description="Collect and analyse data about Gurbani." + # Ardaas -> Language + language_group = ardaas.add_mutually_exclusive_group() + language_group.add_argument( + "--unicode", + action="store_true", + help="Give output in unicode encoding", + ) + language_group.add_argument( + "--romanised", + action="store_true", + help="Give output in romanised font", ) - # Common parsing - verbosity_parser = argparse.ArgumentParser(add_help=False) - verbosity_group = verbosity_parser.add_mutually_exclusive_group() - verbosity_group.add_argument( - "-s", - "--suppress-output", - action="store_const", - const=_cmn.Verbosity.SUPPRESSED, - dest="verbosity", - help="Suppress logging output", + # Ardaas -> Contexts + ardaas.add_argument( + "-m", + "--multiple", + action="store_true", + help="Ardaas for multiple people", ) - verbosity_group.add_argument( - "-v", - "--verbose", - action="store_const", - const=_cmn.Verbosity.VERBOSE, - dest="verbosity", - help="Suppress logging output", + akhand_paath = ardaas.add_mutually_exclusive_group() + akhand_paath.add_argument( + "--akhand-paath-arambh", + action="store_true", + help="Starting an akhand paath", ) - verbosity_group.add_argument( - "-V", - "--very-verbose", - action="store_const", - const=_cmn.Verbosity.VERY_VERBOSE, - dest="verbosity", - help="Suppress logging output", + akhand_paath.add_argument( + "--akhand-paath-bhog", + action="store_true", + help="Finishing an akhand paath", + ) + sehaj_paath = ardaas.add_mutually_exclusive_group() + sehaj_paath.add_argument( + "--sehaj-paath-arambh", + action="store_true", + help="Starting a sehaj paath", + ) + sehaj_paath.add_argument( + "--sehaj-paath-madh", + action="store_true", + help="Reached the halfway point of a sehaj paath", + ) + sehaj_paath.add_argument( + "--sehaj-paath-bhog", + action="store_true", + help="Finishing a sehaj paath", + ) + ardaas.add_argument( + "--read-bani", action="store_true", help="Specific banis were read" + ) + ardaas.add_argument("--sukhmani", "--sukhmani-sahib", action="store_true") + ardaas.add_argument("--kirtan", action="store_true", help="Kirtan was sung") + ardaas.add_argument("--katha", action="store_true", help="Katha was done") + ardaas.add_argument( + "--anand-sahib", action="store_true", help="6 pauri Anand Sahib" + ) + sukhaasan = ardaas.add_mutually_exclusive_group() + sukhaasan.add_argument( + "--sukhaasan-pre", action="store_true", help="Aagya to do sukhaasan" + ) + sukhaasan.add_argument( + "--sukhaasan-post", action="store_true", help="Sukhaasan was done" ) - composition = parser.add_subparsers(dest="composition") + ardaas.add_argument( + "--hukamnama", action="store_true", help="Hukamnama is to be taken" + ) + ardaas.add_argument( + "--parshaad", action="store_true", help="Doing bhog of Karah Parshaad" + ) + ardaas.add_argument( + "--langar", action="store_true", help="Doing bhog of Langar" + ) + ardaas.add_argument( + "--amrit-vela", action="store_true", help="Wake up at Amrit vela" + ) + + ardaas.add_argument( + "--birthday", action="store_true", help="Birthday of a person" + ) + + +def _add_hukamnama_parser( + subparser: argparse._SubParsersAction, + common_parser: argparse.ArgumentParser, +) -> None: + """Add parser for hukamnama subcommand. - # Hukamnama - hukamnama = composition.add_parser( + :param subparser: subparser to add to. + """ + hukamnama = subparser.add_parser( "hukamnama", description="Data regarding hukamnama archives." ) hukamnama_subparser = hukamnama.add_subparsers(dest="function") @@ -93,7 +150,7 @@ def main() -> None: data = hukamnama_subparser.add_parser( _hukamnama.Function.DATA.value, description="Update database from archives.", - parents=[verbosity_parser], + parents=[common_parser], ) update_method = data.add_mutually_exclusive_group(required=True) update_method.add_argument( @@ -124,34 +181,88 @@ def main() -> None: "without any data (does not overwrites existing data)", ) + +def main() -> None: + """Main handler for Gurbani Analysis CLI. Calls callbacks based on the + subcommand received. + """ + rc = _cmn.RC.SUCCESS + _log.set_level(_cmn.Verbosity.SUPPRESSED) + + # Main parser + parser = argparse.ArgumentParser( + description="Collect and analyse data about Gurbani.", add_help=True + ) + + # Common parsing + common_parser = argparse.ArgumentParser(add_help=False) + verbosity_group = parser.add_mutually_exclusive_group() + verbosity_group.add_argument( + "-s", + "--suppress-output", + action="store_const", + const=_cmn.Verbosity.SUPPRESSED, + dest="verbosity", + help="Suppress logging output", + ) + verbosity_group.add_argument( + "-v", + "--verbose", + action="store_const", + const=_cmn.Verbosity.VERBOSE, + dest="verbosity", + help="Suppress logging output", + ) + verbosity_group.add_argument( + "-V", + "--very-verbose", + action="store_const", + const=_cmn.Verbosity.VERY_VERBOSE, + dest="verbosity", + help="Suppress logging output", + ) + + composition = parser.add_subparsers(dest="composition") + + _add_ardaas_parser(composition) + _add_hukamnama_parser(composition, common_parser) + args = parser.parse_args() # Defaults if not hasattr(args, "verbosity") or args.verbosity is None: args.verbosity = _cmn.Verbosity.STANDARD + _log.set_level(args.verbosity) + + _log.very_verbose(args) + exception = None if rc.is_ok(): - if args.composition == "hukamnama": + subparser = None + if args.composition == "ardaas": + subparser = _ardaas + elif args.composition == "hukamnama": subparser = _hukamnama - - try: - rc = subparser.parse(args) - except KeyboardInterrupt: - pass - except NotImplementedError: - rc = _cmn.RC.NOT_IMPLEMENTED - exception = _cmn.NotImplementedException(traceback.format_exc()) - except _cmn.Error as exc: - rc = exc.rc - exception = exc - except Exception: - rc = _cmn.RC.UNHANDLED_ERROR - exception = _cmn.UnhandledExceptionError(traceback.format_exc()) else: parser.print_help() + try: + if subparser: + rc = subparser.parse(args) + except KeyboardInterrupt: + pass + except NotImplementedError: + rc = _cmn.RC.NOT_IMPLEMENTED + exception = _cmn.NotImplementedException(traceback.format_exc()) + except _cmn.Error as exc: + rc = exc.rc + exception = exc + except Exception: + rc = _cmn.RC.UNHANDLED_ERROR + exception = _cmn.UnhandledExceptionError(traceback.format_exc()) + _exit(rc, exception)