options.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2023/11/01 00:01:04
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : 命令行参数,或配置文件
  8. """
  9. import argparse
  10. import os
  11. import shlex
  12. import dotenv
  13. from collections import OrderedDict
  14. from .utils.str_util import preferredencoding
  15. from .utils.frozen_dir import get_app_path
  16. def parser_args(overrideArguments=None):
  17. """解析参数"""
  18. argparser = argparse.ArgumentParser()
  19. argparser.add_argument('-c', '--config', help='config file', default='config.ini')
  20. # argparser.add_argument(
  21. # 'command',
  22. # help='command: ',
  23. # choices=['clone', 'push', 'delete', 'list', 'serve', 'server'],
  24. # )
  25. argparser.add_argument('-l', '--log', help='log file', default='log.txt')
  26. argparser.add_argument('-d', '--debug', help='debug mode', action='store_true')
  27. argparser.add_argument('-user', '--user', help='set a user')
  28. argparser.add_argument(
  29. '-p',
  30. '--platform',
  31. help='set a platform',
  32. choices=['github', 'gitee', 'gitlab', 'gogs', 'gitea', 'bitbucket', 'coding'],
  33. default='github',
  34. )
  35. argparser.add_argument('-token', '--token', help='set a token')
  36. argparser.add_argument('-host', '--host', help='set a host')
  37. argparser.add_argument(
  38. '-repo_path', '--repo_path', help='set a repo'
  39. ) # , default=os.getcwd())
  40. argparser.add_argument('-repo_name', '--repo_name', help='set a repo name')
  41. argparser.add_argument('-repo_id', '--repo_id', help='set a repo id')
  42. args = argparser.parse_args()
  43. # remove None
  44. command_line_conf = OrderedDict(
  45. {k: v for k, v in args.__dict__.items() if v is not None}
  46. )
  47. system_conf = user_conf = custom_conf = OrderedDict()
  48. user_conf = _read_user_conf()
  49. if args.config:
  50. custom_conf = _read_custom_conf(args.config)
  51. system_conf.update(user_conf)
  52. system_conf.update(command_line_conf)
  53. # if args.command == None and args.extractor == None:
  54. # raise 'Error, please input cmd and extractor params11'
  55. app_path = get_app_path()
  56. system_conf["app_path"] = app_path
  57. return system_conf
  58. def _read_custom_conf(config_path: str) -> OrderedDict:
  59. """读取自定义配置文件 config.yaml"""
  60. def compat_shlex_split(s, comments=False, posix=True):
  61. if isinstance(s, str):
  62. s = s.encode('utf-8')
  63. return list(map(lambda s: s.decode('utf-8'), shlex.split(s, comments, posix)))
  64. try:
  65. with open(config_path, 'r', encoding=preferredencoding()) as f:
  66. contents = f.read()
  67. res = compat_shlex_split(contents, comments=True)
  68. except Exception as e:
  69. return []
  70. return res
  71. def _read_user_conf() -> OrderedDict:
  72. """读取用户配置文件: .env 文件"""
  73. user_conf = OrderedDict()
  74. dotenv_path = '.env'
  75. if os.path.exists(dotenv_path):
  76. user_conf = dotenv.dotenv_values(dotenv_path)
  77. return OrderedDict(user_conf)