From 5589e95fb26bfb9bf2232f5f4f46c4bf70a5dc38 Mon Sep 17 00:00:00 2001 From: Ilia Miheev Date: Tue, 30 Sep 2025 21:03:11 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=D0=B0=D0=BB=D0=B8=D0=B4=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F=20=D0=B2=20ParsManager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ipars/JsonManagerCode.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/ipars/JsonManagerCode.py b/ipars/JsonManagerCode.py index c24f990..e203a7a 100644 --- a/ipars/JsonManagerCode.py +++ b/ipars/JsonManagerCode.py @@ -1,5 +1,6 @@ import json from pprint import pprint +from cerberus import Validator class JsonManager: '''Класс для работы с json файлами во время парсинга''' @@ -8,6 +9,13 @@ class JsonManager: '''Конструктор encoding: кодировка открываемого файла''' + schema = { + 'encoding': {'type': 'string'} + } + v = Validator(schema) + if not v.validate({'encoding': encoding}): + raise ValueError(v.errors) + self.encoding = encoding def pprint(self, data: any) -> None: @@ -20,6 +28,14 @@ class JsonManager: '''Получаем данные из json файла pathToJsonFile: путь до json файла''' + + schema = { + 'pathToJsonFile': {'type': 'string'} + } + v = Validator(schema) + if not v.validate({'pathToJsonFile': pathToJsonFile}): + raise ValueError(v.errors) + with open(pathToJsonFile, encoding=self.encoding) as jsonFile: src = json.load(jsonFile) return src @@ -29,5 +45,13 @@ class JsonManager: pathToJsonFile: путь до json файла data: данные которые надо записать''' + + schema = { + 'pathToJsonFile': {'type': 'string'} + } + v = Validator(schema) + if not v.validate({'pathToJsonFile': pathToJsonFile}): + raise ValueError(v.errors) + with open(pathToJsonFile, 'w', encoding=self.encoding) as jsonFile: - json.dump(data, jsonFile, indent=4, ensure_ascii=0) \ No newline at end of file + json.dump(data, jsonFile, indent=4, ensure_ascii=0)