1
1
mirror of https://github.com/KenanZhu/AutoLibrary.git synced 2026-06-17 23:13:03 +08:00
Files
AutoLibrary/src/utils/JSONReader.py
T
KenanZhu 5e898180c7 refactor(style): 统一项目代码风格,整理导入顺序、间距规范与方法排列
- GUI 模块统一 QtCore → QtGui → QtWidgets 导入排列,各类独占一行按字母排序
- 统一类间两空行、类内方法间一空行、函数间一空行的间距规范
- 统一方法排列顺序:__init__ → setupUi → connectSignals → public → Slot → private
- 统一 _widgets 中 ConditionRowFrame/ActionStepFrame 方法命名(populate* / toScript / updateValueWidget)
- LibTimeSelector 迁入 operators/abs 抽象层

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-24 13:14:27 +08:00

82 lines
1.9 KiB
Python

# -*- coding: utf-8 -*-
"""
Copyright (c) 2026 KenanZhu.
All rights reserved.
This software is provided "as is", without any warranty of any kind.
You may use, modify, and distribute this file under the terms of the MIT License.
See the LICENSE file for details.
"""
import os
import json
class JSONReader:
"""
JSON reader class.
This class is used to read JSON file.
Args:
json_path (str): The path of JSON file.
Examples:
>>> print(open("config.json", "r", encoding="utf-8").read())
{
"key1": {
"key2": "value1"
}
}
>>> json_reader = JSONReader("config.json")
>>> data = json_reader.data()
>>> data["key1"]["key2"]
"value1"
"""
def __init__(
self,
json_path: str
):
self.__json_path = os.path.abspath(json_path)
self.__json_data = None
self.__read()
def __read(
self
):
try:
with open(self.__json_path, 'r', encoding='utf-8') as file:
self.__json_data = json.load(file)
except FileNotFoundError as e:
raise Exception(f"文件不存在: {self.__json_path}") from e
except PermissionError as e:
raise Exception(f"没有足够的权限读取文件: {self.__json_path}") from e
except json.JSONDecodeError as e:
raise Exception(f"JSON 解析错误: {self.__json_path}") from e
except Exception as e:
raise Exception(f"读取文件时发生未知错误: {e}") from e
def read(
self
) -> bool:
try:
self.__read()
except:
return False
return True
def data(
self
) -> dict:
return self.__json_data.copy()
def path(
self
) -> str:
return self.__json_path