diff --git a/LICENSE.html b/LICENSE.html new file mode 100644 index 0000000000000000000000000000000000000000..ae4224f716b35f2f2d497b01e432176dd2c6221e --- /dev/null +++ b/LICENSE.html @@ -0,0 +1,9 @@ +Copyright (c) [2024] [Xurui] +[grz] is licensed under Mulan PubL v2. +You can use this software according to the terms and conditions of the Mulan PubL v2. +You may obtain a copy of Mulan PubL v2 at: + http://license.coscl.org.cn/MulanPubL-2.0 +THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +See the Mulan PubL v2 for more details. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e27deb000fab0128138482ea8d7705de929656e4 --- /dev/null +++ b/README.md @@ -0,0 +1,193 @@ +# Grz - openGauss 自动巡检工具 + +## 项目概述 + +        **Grz** 是专为 openGauss 数据库开发的自动巡检工具,旨在简化和优化数据库的监控与管理操作。该工具参考了 Oracle 的 Orz 工具和达梦数据库的 Drz 工具,帮助用户通过简单的命令获取数据库的健康状况和性能指标。 + +        Grz 工具能够将用户输入的简单命令自动转换为底层复杂操作,如通过输入 `grz status` 获取数据库状态。此外,Grz 支持生成 HTML 格式的巡检报告,便于管理员对数据库状态和性能指标进行综合分析。 + +## 功能模块 + +Grz 工具采用模块化设计,包含以下主要模块: + +- **命令采集模块**:收集并验证用户输入的指令。 + +- **命令解析模块**:将简单命令映射为底层复杂操作。  + +- **命令执行模块**:使用 `subprocess` 模块执行解析后的复杂命令。  + +- **错误处理模块**:确保在执行过程中捕获并记录所有错误信息。 + +## 安装指南 + +### 环境要求 + +- Python 3.6+ +- openGauss 企业版数据库环境 +- 必需的 Python 库:`argparse`, `subprocess`, `logging` + +### 安装步骤 + +1. 确保openGauss 企业版数据库环境正确 + +2. 克隆或下载本项目代码: + +```bash +git clone https://gitee.com/cndba/orz.git +or +pip install grz==1.2.0 +``` + +## 使用方法 + +### 1. 常见命令操作 + +| 命令 | 描述 | +| ------------------------------------- | ------------------- | +| `grz help` | 显示所有命令的帮助信息 | +| `grz start` | 启动 openGauss | +| `grz stop` | 停止 openGauss | +| `grz restart` | 重启 openGauss | +| `grz status` | 查看 openGauss 状态 | +| `grz generateconf` | 生成配置文件 | +| `grz check` | 检查 openGauss 整体健康状态 | +| `grz checkos ` | 检查操作系统参数 | +| `grz checkperf` | 检查数据库性能 | +| `grz wdr ` | 生成 WDR 巡检报告 | +| `grz collect ` | 收集数据库运行日志 | + +### 2. 使用示例 + +        在使用之前,请确保当前环境已正确配置为企业版本的数据库。仅企业版本数据库提供所需的运维命令。Grz工具会将用户输入的简化命令映射到相应的运维命令,并执行这些命令以返回结果。以下为一些常用操作命令示例: + +1. **启动 openGauss**: + + ```bash + grz start + ``` + +2. **查看数据库状态**: + + ```bash + grz status + ``` + +3. **生成 WDR 巡检报告**: + + 请注意,访问WDR快照数据需要具有sysadmin或monadmin权限。因此,必须使用root账户或其他具有相应权限的账户来生成WDR诊断报告。在执行以下命令时,将在指定路径下创建HTML文件,并连接至系统库postgres,默认端口号为5432。接下来,进入gsql模式,用户可以选择不同的快照及输出格式,以生成HTML格式的WDR报告。 + + ```bash + grz wdr /file_path + ``` + +4. **收集运行日志**: + + 执行命令后根据界面输出提示,进入相应的日志收集目录,解压收集的日志,并检查数据库日志。 + + ```bash + grz collect "20240818 01:01" "20240818 23:59" + ``` + +### 3. 帮助信息 + +可以`help` 参数查看帮助信息: + +```bash +grz help +``` + +输出: + +```bash +❯ grz help + +Available commands: + +    start        - Start the openGauss database + +    stop         - Stop the openGauss database + +    restart      - Restart the openGauss database + +    ---------------------------------------------------------------------------- + +    status       - Show detailed status of the database cluster + +    generateconf - Generate and distribute configuration files + +    check        - Check cluster state with specified option + +    checkos      - Check operating system compatibility + +    checkperf    - Check system performance  + +    ---------------------------------------------------------------------------- + +    wdr          - Generate WDR performance report (requires --file_path) + +    collect      - Collect diagnostic logs (requires --start_time, --end_time) + +    help         - Show this help message +``` + +## 代码结构 + +``` +grz +├── LICENSE.html +├── README.md +├── dist +│   ├── grz-1.3.0-py3-none-any.whl +│   └── grz-1.3.0.tar.gz +├── pyproject.toml +├── setup.py +├── src +│   ├── grz +│   │   ├── CommandCollector +│   │   │   ├── Collector.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Collector.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   ├── CommandParser +│   │   │   ├── Parser.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Parser.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   ├── ErrorHandler +│   │   │   ├── Handler.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Handler.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   └── grz.py +│   └── grz.egg-info +│   ├── PKG-INFO +│   ├── SOURCES.txt +│   ├── dependency_links.txt +│   ├── entry_points.txt +│   └── top_level.txt +└── tests +12 directories, 24 files +``` + +## 错误处理 + +Grz 工具内置错误处理模块,能够在执行过程中捕获并记录所有错误信息,包括缺失参数、无效命令等。日志文件保存在项目根目录下,便于用户进行后续的审计和问题排查。 + +## 未来开发 + +Grz 工具后续计划包括: + +- **增加命令支持**:涵盖更多数据库监控、备份和恢复功能。 +- **增强错误处理**:增加自动恢复机制,减少手动干预。 +- **性能优化**:优化工具执行效率,降低资源消耗。 + +## 贡献 + +如果您有任何建议或改进意见,欢迎提交 Issue 或 Pull Request。您的贡献将帮助 Grz 工具更好地服务 openGauss 社区。 + +## 许可证 + +本项目使用 Mulan PSL v2 许可证 - 有关详细信息,请参见 `LICENSE` 文件。 diff --git a/dist/grz-1.2.0-py3-none-any.whl b/dist/grz-1.2.0-py3-none-any.whl new file mode 100644 index 0000000000000000000000000000000000000000..87f491f0c03b43a6a94b84587f98a8008992259b Binary files /dev/null and b/dist/grz-1.2.0-py3-none-any.whl differ diff --git a/dist/grz-1.2.0.tar.gz b/dist/grz-1.2.0.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..3243466b540afd16c23c5a82257f6ec1d5ae5ae8 Binary files /dev/null and b/dist/grz-1.2.0.tar.gz differ diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000000000000000000000000000000000000..39844a0b3857d175f75c888bba9d775f6b9ba5cd --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,28 @@ +[build-system] +requires = ["setuptools", "wheel"] # 构建项目所需依赖 +build-backend = "setuptools.build_meta" + +[project] +name = "grz" # 项目名称 +version = "1.2.0" # 项目版本 +description = "This is a widget designed for openGauss Database inspection Tool!" # 简要描述 +readme = "README.md" # 项目的说明文件 +requires-python = ">=3.6" # Python版本要求 +license = {text = "License: GPL version 3, excluding DRM provisions"} # 许可证 +authors = [{name = "RuiXu", email = "ruixu@std.uestc.edu.cn"}] +classifiers = [ + "Programming Language :: Python :: 3", + 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', +] + +dependencies = [ + # 示例依赖,如 'requests' +] + + +[tool.setuptools] +packages = ["grz", "grz.CommandCollector", "grz.CommandParser", "grz.ErrorHandler"] + +[project.scripts] # 定义命令行入口 +grz = "grz.grz:main" + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000000000000000000000000000000000000..c914d4e4e9f8a4451809a365a82af3997db3edda --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup, find_packages + +setup( + name='grz', + version='1.2.0', + description='This is a widget designed for openGauss Database inspection Tool!', + author='RuiXu', + author_email='ruixu@std.uestc.edu.cn', + license='Mulan PSL v2', # 或其他许可证 + packages=find_packages(where='src'), # 查找 src 目录中的包 + package_dir={'': 'src'}, # 指定包的根目录 + entry_points={ + 'console_scripts': [ + 'grz=grz.grz:main', # 将命令 `orz` 映射到 `orz/orz.py` 中的 `main` 函数 + ], + }, + python_requires='>=3.6', # Python 版本要求 +) \ No newline at end of file diff --git a/src/grz.egg-info/PKG-INFO b/src/grz.egg-info/PKG-INFO new file mode 100644 index 0000000000000000000000000000000000000000..93b4ba7cc7f0b34cba847f208e186ec80e7345c9 --- /dev/null +++ b/src/grz.egg-info/PKG-INFO @@ -0,0 +1,202 @@ +Metadata-Version: 2.1 +Name: grz +Version: 1.2.0 +Summary: This is a widget designed for openGauss Database inspection Tool! +Author: RuiXu +Author-email: RuiXu +License: License: GPL version 3, excluding DRM provisions +Classifier: Programming Language :: Python :: 3 +Classifier: License :: OSI Approved :: GNU General Public License v3 (GPLv3) +Requires-Python: >=3.6 +Description-Content-Type: text/markdown +License-File: LICENSE.html + +# Grz - openGauss 自动巡检工具 + +## 项目概述 + +        **Grz** 是专为 openGauss 数据库开发的自动巡检工具,旨在简化和优化数据库的监控与管理操作。该工具参考了 Oracle 的 Orz 工具和达梦数据库的 Drz 工具,帮助用户通过简单的命令获取数据库的健康状况和性能指标。 + +        Grz 工具能够将用户输入的简单命令自动转换为底层复杂操作,如通过输入 `grz status` 获取数据库状态。此外,Grz 支持生成 HTML 格式的巡检报告,便于管理员对数据库状态和性能指标进行综合分析。 + +## 功能模块 + +Grz 工具采用模块化设计,包含以下主要模块: + +- **命令采集模块**:收集并验证用户输入的指令。 + +- **命令解析模块**:将简单命令映射为底层复杂操作。  + +- **命令执行模块**:使用 `subprocess` 模块执行解析后的复杂命令。  + +- **错误处理模块**:确保在执行过程中捕获并记录所有错误信息。 + +## 安装指南 + +### 环境要求 + +- Python 3.6+ +- openGauss 企业版数据库环境 +- 必需的 Python 库:`argparse`, `subprocess`, `logging` + +### 安装步骤 + +1. 确保openGauss 企业版数据库环境正确 + +2. 克隆或下载本项目代码: + +```bash +git clone https://gitee.com/cndba/orz +or +pip install grz +``` + +## 使用方法 + +### 1. 常见命令操作 + +| 命令 | 描述 | +| ------------------------------------- | ------------------- | +| `grz help` | 显示所有命令的帮助信息 | +| `grz start` | 启动 openGauss | +| `grz stop` | 停止 openGauss | +| `grz restart` | 重启 openGauss | +| `grz status` | 查看 openGauss 状态 | +| `grz generateconf` | 生成配置文件 | +| `grz check` | 检查 openGauss 整体健康状态 | +| `grz checkos ` | 检查操作系统参数 | +| `grz checkperf` | 检查数据库性能 | +| `grz wdr ` | 生成 WDR 报告 | +| `grz collect ` | 收集数据库运行日志 | + +### 2. 使用示例 + +以下为一些常用操作命令示例: + +1. **启动 openGauss**: + + ```bash + grz start + ``` + +2. **查看数据库状态**: + + ```bash + grz status + ``` + +3. **生成 WDR 报告**: + + ```bash + grz wdr /file_path + ``` + +4. **收集运行日志**: + + ```bash + grz collect "20240818 01:01" "20240818 23:59" + ``` + +### 3. 帮助信息 + +可以`help` 参数查看帮助信息: + +```bash +grz help +``` + +输出: + +```bash +❯ grz help + +Available commands: + +    start        - Start the openGauss database + +    stop         - Stop the openGauss database + +    restart      - Restart the openGauss database + +    ---------------------------------------------------------------------------- + +    status       - Show detailed status of the database cluster + +    generateconf - Generate and distribute configuration files + +    check        - Check cluster state with specified option + +    checkos      - Check operating system compatibility + +    checkperf    - Check system performance  + +    ---------------------------------------------------------------------------- + +    wdr          - Generate WDR performance report (requires --file_path) + +    collect      - Collect diagnostic logs (requires --start_time, --end_time) + +    help         - Show this help message +``` + +## 代码结构 + +``` +grz +├── LICENSE.html +├── README.md +├── dist +│   ├── grz-1.3.0-py3-none-any.whl +│   └── grz-1.3.0.tar.gz +├── pyproject.toml +├── setup.py +├── src +│   ├── grz +│   │   ├── CommandCollector +│   │   │   ├── Collector.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Collector.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   ├── CommandParser +│   │   │   ├── Parser.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Parser.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   ├── ErrorHandler +│   │   │   ├── Handler.py +│   │   │   ├── __init__.py +│   │   │   └── __pycache__ +│   │   │   ├── Handler.cpython-310.pyc +│   │   │   └── __init__.cpython-310.pyc +│   │   └── grz.py +│   └── grz.egg-info +│   ├── PKG-INFO +│   ├── SOURCES.txt +│   ├── dependency_links.txt +│   ├── entry_points.txt +│   └── top_level.txt +└── tests +12 directories, 24 files +``` + +## 错误处理 + +Grz 工具内置错误处理模块,能够在执行过程中捕获并记录所有错误信息,包括缺失参数、无效命令等。日志文件保存在项目根目录下,便于用户进行后续的审计和问题排查。 + +## 未来开发 + +Grz 工具后续计划包括: + +- **增加命令支持**:涵盖更多数据库监控、备份和恢复功能。 +- **增强错误处理**:增加自动恢复机制,减少手动干预。 +- **性能优化**:优化工具执行效率,降低资源消耗。 + +## 贡献 + +如果您有任何建议或改进意见,欢迎提交 Issue 或 Pull Request。您的贡献将帮助 Grz 工具更好地服务 openGauss 社区。 + +## 许可证 + +本项目使用 Mulan PSL v2 许可证 - 有关详细信息,请参见 `LICENSE` 文件。 diff --git a/src/grz.egg-info/SOURCES.txt b/src/grz.egg-info/SOURCES.txt new file mode 100644 index 0000000000000000000000000000000000000000..6b17b290258996188f4f513fddbfda5d65ce8e17 --- /dev/null +++ b/src/grz.egg-info/SOURCES.txt @@ -0,0 +1,16 @@ +LICENSE.html +README.md +pyproject.toml +setup.py +src/grz/grz.py +src/grz.egg-info/PKG-INFO +src/grz.egg-info/SOURCES.txt +src/grz.egg-info/dependency_links.txt +src/grz.egg-info/entry_points.txt +src/grz.egg-info/top_level.txt +src/grz/CommandCollector/Collector.py +src/grz/CommandCollector/__init__.py +src/grz/CommandParser/Parser.py +src/grz/CommandParser/__init__.py +src/grz/ErrorHandler/Handler.py +src/grz/ErrorHandler/__init__.py \ No newline at end of file diff --git a/src/grz.egg-info/dependency_links.txt b/src/grz.egg-info/dependency_links.txt new file mode 100644 index 0000000000000000000000000000000000000000..8b137891791fe96927ad78e64b0aad7bded08bdc --- /dev/null +++ b/src/grz.egg-info/dependency_links.txt @@ -0,0 +1 @@ + diff --git a/src/grz.egg-info/entry_points.txt b/src/grz.egg-info/entry_points.txt new file mode 100644 index 0000000000000000000000000000000000000000..1f27e5280320eca416244d661f74fd5cd8636888 --- /dev/null +++ b/src/grz.egg-info/entry_points.txt @@ -0,0 +1,2 @@ +[console_scripts] +grz = grz.grz:main diff --git a/src/grz.egg-info/top_level.txt b/src/grz.egg-info/top_level.txt new file mode 100644 index 0000000000000000000000000000000000000000..969f4e7294e09f32de5e3f6067bbe8cae87441bb --- /dev/null +++ b/src/grz.egg-info/top_level.txt @@ -0,0 +1 @@ +grz diff --git a/src/grz/CommandCollector/Collector.py b/src/grz/CommandCollector/Collector.py new file mode 100644 index 0000000000000000000000000000000000000000..6baf960a4af6920e70c486fb4ab01ce7da7c7d2a --- /dev/null +++ b/src/grz/CommandCollector/Collector.py @@ -0,0 +1,36 @@ +# CommandCollector/Collector.py + +import argparse + +class CommandCollector: + def __init__(self): + self.parser = argparse.ArgumentParser(description="Command collector for openGauss operations") + self.subparsers = self.parser.add_subparsers(dest="command", required=True) + + # 定义各个命令 + self.subparsers.add_parser("start", help="启动 openGauss") + self.subparsers.add_parser("stop", help="停止 openGauss") + self.subparsers.add_parser("restart", help="重启 openGauss") + self.subparsers.add_parser("status", help="查看 openGauss 状态") + self.subparsers.add_parser("generateconf", help="生成配置文件") + self.subparsers.add_parser("check", help="检查 openGauss 整体健康状态") + + checkos_parser = self.subparsers.add_parser("checkos", help="检查操作系统参数") + checkos_parser.add_argument("option", help="检查选项") + + self.subparsers.add_parser("checkperf", help="检查性能") + + wdr_parser = self.subparsers.add_parser("wdr", help="生成 WDR 报告") + wdr_parser.add_argument("file_path", help="WDR 报告的文件路径") + + collect_parser = self.subparsers.add_parser("collect", help="收集数据库运行日志") + collect_parser.add_argument("start_time", help="日志收集的开始时间") + collect_parser.add_argument("end_time", help="日志收集的结束时间") + + self.subparsers.add_parser("help", help="显示所有命令的帮助信息") + + def set_command(self, command): + self.command = command + + def get_command(self): + return self.parser.parse_args() # 返回解析后的命令对象 diff --git a/src/grz/CommandCollector/__init__.py b/src/grz/CommandCollector/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/grz/CommandCollector/__pycache__/Collector.cpython-310.pyc b/src/grz/CommandCollector/__pycache__/Collector.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..ccd531942219070db9b063336042fb70b768fe40 Binary files /dev/null and b/src/grz/CommandCollector/__pycache__/Collector.cpython-310.pyc differ diff --git a/src/grz/CommandCollector/__pycache__/__init__.cpython-310.pyc b/src/grz/CommandCollector/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..06d7a4644f52dfc8cfb8397939bea8f79ce15013 Binary files /dev/null and b/src/grz/CommandCollector/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/grz/CommandParser/Parser.py b/src/grz/CommandParser/Parser.py new file mode 100644 index 0000000000000000000000000000000000000000..9d5ccc82e2c1b573d054b535ee4c5315bbc9bf4d --- /dev/null +++ b/src/grz/CommandParser/Parser.py @@ -0,0 +1,75 @@ +# CommandParser/Parser.py +import subprocess +from ..ErrorHandler.Handler import ErrorHandler + +def get_current_port(): + return 5432 + +def generate_help_message(): + help_text = """Available commands: + start - Start the openGauss database + stop - Stop the openGauss database + restart - Restart the openGauss database + ---------------------------------------------------------------------------- + status - Show detailed status of the database cluster + generateconf - Generate and distribute configuration files + check - Check cluster state with specified option + checkos - Check operating system compatibility + checkperf - Check system performance + ---------------------------------------------------------------------------- + wdr - Generate WDR performance report (requires --file_path) + collect - Collect diagnostic logs (requires --start_time, --end_time) + help - Show this help message""" + return help_text + +class CommandParser: + def __init__(self): + self.error_handler = ErrorHandler() + self.command_map = { + "start": "gs_om -t start", + "stop": "gs_om -t stop", + "restart": "gs_om -t restart", + "status": "gs_om -t status --detail", + "generateconf": "gs_om -t generateconf -X /opt/software/openGauss/clusterconfig.xml --distribute", + "check": "gs_check -U omm -i CheckClusterState", + "checkos": lambda option: f'gs_checkos -i {option}', + "checkperf": "gs_checkperf", + "wdr": lambda file_path: f'touch {file_path} && gsql -d postgres -p {get_current_port()} -r', + "collect": lambda start_time, end_time: f'gs_collector --begin-time="{start_time}" --end-time="{end_time}"', + "help": '' + } + + def parse_and_execute_command(self, args): + command = args.command.lower() + available_commands = list(self.command_map.keys()) + + if not self.error_handler.check_invalid_command(command, available_commands): + return + + required_args_map = { + "checkos": ["option"], + "wdr": ["file_path"], + "collect": ["start_time", "end_time"] + } + + if command in required_args_map: + required_args = required_args_map[command] + if not self.error_handler.check_missing_argument(args, required_args): + return + + if command == "help": + print(generate_help_message()) + return + + if callable(self.command_map[command]): + if command == "checkos": + full_command = self.command_map[command](args.option) + elif command == "wdr": + full_command = self.command_map[command](args.file_path) + elif command == "collect": + full_command = self.command_map[command](args.start_time, args.end_time) + else: + full_command = self.command_map[command] + + result = subprocess.run(full_command, shell=True, capture_output=True, text=True) + print(result.stdout if result.returncode == 0 else result.stderr) diff --git a/src/grz/CommandParser/__init__.py b/src/grz/CommandParser/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/grz/CommandParser/__pycache__/Parser.cpython-310.pyc b/src/grz/CommandParser/__pycache__/Parser.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..39f52ac6ec0c12cca6cf4aaa19f6c94c663b0f9d Binary files /dev/null and b/src/grz/CommandParser/__pycache__/Parser.cpython-310.pyc differ diff --git a/src/grz/CommandParser/__pycache__/__init__.cpython-310.pyc b/src/grz/CommandParser/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1eae8682fe78475300c95fc829536de09ce6d9a9 Binary files /dev/null and b/src/grz/CommandParser/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/grz/ErrorHandler/Handler.py b/src/grz/ErrorHandler/Handler.py new file mode 100644 index 0000000000000000000000000000000000000000..e54028cbf0f32c207339afb27c7e42d3eea85e99 --- /dev/null +++ b/src/grz/ErrorHandler/Handler.py @@ -0,0 +1,39 @@ +# ErrorHandler/Handler.py + +class ErrorHandler: + def handle_error(self, error): + if isinstance(error, ValueError): + print(f"[Error] Invalid input: {error}") + elif isinstance(error, FileNotFoundError): + print("[Error] File not found. Please check the file path.") + elif isinstance(error, PermissionError): + print("[Error] Permission denied. Please check your permissions.") + elif isinstance(error, KeyError): + print(f"[Error] Missing key: {error}") + else: + print(f"[Error] An unexpected error occurred: {error}") + + def check_missing_argument(self, args, required_args): + """ + 检查是否缺少必要的参数。 + :param args: 用户提供的参数。 + :param required_args: 当前命令所需的参数列表。 + :return: 缺少参数则返回False,否则返回True。 + """ + missing_args = [arg for arg in required_args if not getattr(args, arg, None)] + if missing_args: + print(f"[Error] Missing required arguments: {', '.join(missing_args)}") + return False + return True + + def check_invalid_command(self, command, available_commands): + """ + 检查命令是否有效。 + :param command: 用户输入的命令。 + :param available_commands: 支持的命令列表。 + :return: 若命令有效则返回True否则返回False。 + """ + if command not in available_commands: + print(f"[Error] Unknown command '{command}'. Type 'help' to see available commands.") + return False + return True diff --git a/src/grz/ErrorHandler/__init__.py b/src/grz/ErrorHandler/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/src/grz/ErrorHandler/__pycache__/Handler.cpython-310.pyc b/src/grz/ErrorHandler/__pycache__/Handler.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6cfa64296e5ded25b328c8bf51f1e700959207ab Binary files /dev/null and b/src/grz/ErrorHandler/__pycache__/Handler.cpython-310.pyc differ diff --git a/src/grz/ErrorHandler/__pycache__/__init__.cpython-310.pyc b/src/grz/ErrorHandler/__pycache__/__init__.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..5cb39f0830e487136454d4c76ac7cafac94c161c Binary files /dev/null and b/src/grz/ErrorHandler/__pycache__/__init__.cpython-310.pyc differ diff --git a/src/grz/grz.py b/src/grz/grz.py new file mode 100644 index 0000000000000000000000000000000000000000..72bad9717c9bc30d00c2e8fb2981225a10da0af4 --- /dev/null +++ b/src/grz/grz.py @@ -0,0 +1,21 @@ +# main.py + +import argparse +from .CommandCollector.Collector import CommandCollector +from .CommandParser.Parser import CommandParser +from .ErrorHandler.Handler import ErrorHandler + +def grz(): + collector = CommandCollector() + command_args = collector.get_command() # 获取解析后的命令对象 + error_handler = ErrorHandler() + + try: + # 直接使用解析后的命令对象 + CommandParser().parse_and_execute_command(command_args) # 使用解析的命令 + except Exception as e: + error_handler.handle_error(e) + +def main(): + grz() +