from __future__ import absolute_import
import os
from mylinux.libs.decorator import ClassProperty
from mylinux.model.config.Info import Info
from mylinux.model.config.Script import Script
from mylinux import view
from mylinux.libs import AppErr
from mylinux.constants import error as ERR
[docs]class Package:
"""Class for handeling all package data.
Args:
name (str): Package name.
path (str): Absolute path to the package folder.
Attributes:
__requiredScriptNames (str): What scripts must package scripts folder have?
__relScriptsPath (str): Relative path to the scripts folder.
__relInfoPath (str): Relative path to the info package file.
__name (str): Name of the package.
__path (str): See args info.
__info (cls): Info class instance.
__scripts (arr-cls): Every package script instances.
"""
__requiredScriptNames = [
'config-install',
'config-purge',
'package-install',
'package-purge',
'test.py'
]
__relScriptsPath = '/scripts'
__relInfoPath = '/info.json'
@ClassProperty
@classmethod
def requiredScriptNames(cls):
return cls.__requiredScriptNames
@ClassProperty
@classmethod
def relScriptsPath(cls):
return cls.__relScriptsPath
@ClassProperty
@classmethod
def relInfoPath(cls):
return cls.__relInfoPath
def __init__(self, name, path):
self.__name = name
self.__path = path
self.__info = None
self.__scripts = None
self.__setInfo()
self.__setScripts()
@property
def scriptsPath(self):
"""
Return:
Absolute scripts path
"""
return self.__path + Package.__relScriptsPath
@property
def infoPath(self):
"""
Return:
Absolute info path
"""
return self.__path + Package.__relInfoPath
@property
def name(self):
"""
Return:
__name
"""
return self.__name
@property
def path(self):
"""
Return:
__path
"""
return self.__path
@property
def info(self):
"""
Return:
__info
"""
return self.__info
@property
def scripts(self):
"""
Return:
__scripts
"""
return self.__scripts
def __setInfo(self):
"""Set __info variable with Info class instance.
"""
self.__info = Info(path=self.infoPath)
def __setScripts(self):
"""Set __scripts variable with all Scripts class instance.
Other Parameters:
Get all scripts file names in scripts path.
Check if every script name is inrequired Script variable.
If pre check fails show error report.
Fill __scripts variable with Script instances.
"""
self.__scripts = []
setted = False
error = []
for root, dirs, files in os.walk(self.scriptsPath):
for scriptName in Package.__requiredScriptNames:
if not scriptName in files:
error.append(
['ERROR', 'Package script', ERR.Format.notFoundIn(scriptName, Package.__relScriptsPath)])
if error:
view.Tli.raiseError(AppErr.model,self.__path,error)
for fileName in files:
setted = True
script = Script(name=fileName, path=self.scriptsPath + '/' + fileName)
self.__scripts.append(script)
break
if not setted:
view.Tli.raiseError(
AppErr.model,
self.__path,
[
'ERROR',
'Package script folder',
ERR.Format.notFoundIn(Package.__relScriptsPath, 'package folder')
]
)
[docs] def getScript(self, name):
"""
Args:
name (str): Script name
Return:
Script instance
Other Parameters:
Check if script name match.
If check fails show error report.
"""
for script in self.__scripts:
if script.name == name:
return script
view.Tli.raiseError(
AppErr.model,
self.__path,
['ERROR', 'Package script', ERR.Format.notFoundIn(name, Package.__relScriptsPath)]
)