File indexing completed on 2024-05-12 05:43:36

0001 #!/usr/bin/env python3
0002 
0003 """
0004 Base module for the lab package
0005 """
0006 
0007 # SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
0008 #
0009 # SPDX-License-Identifier: GPL-2.0-or-later
0010 
0011 import argparse
0012 import traceback
0013 from typing import List, Any
0014 
0015 from git.exc import GitCommandError
0016 
0017 from lab import (
0018     mergerequestcreator,
0019     mergerequestcheckout,
0020     mergerequestlist,
0021     feature,
0022     login,
0023     search,
0024     pipelines,
0025     fork,
0026     issues,
0027     issue,
0028     snippet,
0029     workflow,
0030     rewrite_remote,
0031 )
0032 from lab.utils import Utils, LogType
0033 
0034 
0035 class Parser:  # pylint: disable=R0903
0036     """
0037     Global parser, will instantiate subparser for each commands
0038     """
0039 
0040     def __init__(self) -> None:
0041         self.parser = argparse.ArgumentParser(description="The arcanist of GitLab.")
0042         self.subparsers = self.parser.add_subparsers(dest="subcommand")
0043 
0044         # init all subcommand
0045         command_list: List[Any] = [
0046             mergerequestcreator,
0047             mergerequestcheckout,
0048             mergerequestlist,
0049             feature,
0050             login,
0051             search,
0052             pipelines,
0053             fork,
0054             issue,
0055             issues,
0056             snippet,
0057             workflow,
0058             rewrite_remote,
0059         ]
0060         for command in command_list:
0061             parser = command.parser(self.subparsers)
0062             # if no default runner set fallback to default runner, run from command module
0063             if not parser.get_default(dest="runner"):
0064                 parser.set_defaults(runner=command.run)
0065 
0066     def parse(self) -> None:
0067         """
0068         parse args and run command
0069         """
0070         args: argparse.Namespace = self.parser.parse_args()
0071         if hasattr(args, "runner"):
0072             args.runner(args)
0073         else:
0074             self.parser.print_help()
0075 
0076 
0077 def main() -> None:
0078     """
0079     Entry point
0080     """
0081     parser: Parser = Parser()
0082 
0083     try:
0084         parser.parse()
0085     except GitCommandError as git_error:
0086         Utils.log(LogType.ERROR, str(git_error))
0087     except SystemExit:
0088         pass
0089     except KeyboardInterrupt:
0090         pass
0091     except:  # noqa: E722
0092         print()
0093         Utils.log(LogType.ERROR, "git-lab crashed. This should not happen.")
0094         print(
0095             "Please help us to fix it by opening an issue on",
0096             "https://invent.kde.org/sdk/git-lab/-/issues.",
0097             "Make sure to include the information below:",
0098             "\n```\n",
0099             traceback.format_exc(),
0100             "```",
0101         )
0102 
0103 
0104 if __name__ == "__main__":
0105     main()