import json
from mylinux.libs.decorator import ClassProperty
from mylinux import view
from mylinux.constants import error as ERR
from mylinux.libs import AppErr
[docs]class Info:
"""Class for project info.json file
Args:
path (str): Path to the info.json file.
Attributes:
__path (str): See args...
__data (dic): Data from info file on __path.
__requiredDataKeys (arr-str): What dic keys info file should have.
Other Parameters:
Update __data with info data.
"""
__requiredDataKeys = ['info', 'class', 'module']
@ClassProperty
@classmethod
def requiredDataKeys(cls):
return cls.__requiredDataKeys
def __init__(self, path):
self.__path = path
self.__data = None
self.update()
[docs] def __call__(self):
"""
Return:
__data
"""
return self.__data
[docs] def __getitem__(self, item):
"""
Args:
item (str)
Return:
__data[item]
"""
return self.__data[item]
@property
def path(self):
"""
Return:
__path
"""
return self.__path
[docs] def update(self):
"""Checking and updating __data with new data
Other Parameters:
Check if file on __path exist.
Check if json on __path is correct.
If pre check fails show error report.
"""
error = []
try:
with open(self.__path) as f:
try:
self.__data = json.load(f)
except Exception as err:
error.append(['ERROR', 'Json load', ' '.join(list(err.args))])
except Exception:
error.append(['FAIL', 'File open', 'No such file or directory'])
if not error:
for requiredDataKey in self.__requiredDataKeys:
if not requiredDataKey in self.__data:
error.append([
'ERROR',
'Json key',
ERR.Format.keyMissingIn(requiredDataKey, 'package info file')
])
if error:
view.Tli.raiseError(AppErr.model,self.__path,error)