Skip to content

Resource Registry

resRegistry

resRegistry()

Registry class for managing jps resources.

Attributes:

Name Type Description
resReg dict

resource registry dictionary

Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def __init__(self):
    """
    Constructs the registry object. If regsitry file does not exists, it creates one on the fly.
    """
    try:
        self.resReg = json.load(pkg_resources.resource_stream(__name__, os.path.join('..','resources',_RES_REG_FILE)))
    except FileNotFoundError:
        self.resReg = {'resources':{}}
        self._updateRegFile()

addResToReg

addResToReg(resName, resLoc, resMainJarFile=None)

Adds a resource to the registry.

Parameters:

Name Type Description Default
resName str

resource name

required
resLoc str

path to the resource directory

required
resMainJarFile str

name of the main jar file, if not provided the first jar found in the resource dir will be selected, if no jars are present it is set to None

None
Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def addResToReg(self, resName, resLoc, resMainJarFile=None):
    """
    Adds a resource to the registry.

    Args:
        resName (str): resource name
        resLoc (str): path to the resource directory
        resMainJarFile (str): name of the main jar file, if not provided the first jar found
            in the resource dir will be selected, if no jars are present it is set to None
    """
    if not self._isResInReg(resName):
        print("Info: Adding the {0} resource...".format(resName))
        self._checkResName(resName)

        # TODO default resources
        #======================
        #if self._isResInDefReg(resName):
        #    print("Info: {0} is a default resource. Retrieving the resource metadata...".format(resName))
        #    resMeta = self._getResDefMeta(resName)
        #    resLoc = resMeta['getFrom']
        #    resMainJarFile = resMeta['mainFile']
        ##======================
        self._addResRegEntry(resName)
        if resMainJarFile is None:
            resMainJarFile = self._checkForMainJar(resLoc)
        self._updateResRegEntry(resName, {'mainJarFile':resMainJarFile})
        self._addResFiles(resName, resLoc)
        self._addResMetaFile(resName)
        self._updateRegFile()
    else:
        print('Info: Resource already exist.')

removeResFromReg

removeResFromReg(resName)

Removes a resource from the registry.

Note, it removes all the resource files as well.

Parameters:

Name Type Description Default
resName str

resource name

required
Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def removeResFromReg(self, resName):
    """
    Removes a resource from the registry.
    > Note, it removes all the resource files as well.

    Args:
        resName (str): resource name
    """
    if self._isResInReg(resName):
        print("Info: Removing {0} resource...".format(resName))
        self._removeResRegEntry(resName)
        self._removeResFiles(resName)
        self._updateRegFile()
    else:
        print("Info: {0} resource is not on the registry!".format(resName))

listRes

listRes()

Lists all currently installed resources.

Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def listRes(self):
    """
    Lists all currently installed resources.
    """
    print('\n'.join(list(self.resReg['resources'].keys())))

cleanReg

cleanReg()

Cleans the registry.

Use with caution. The command removes:

  • all registry entries with no corresponding resource files

  • all resource files with no corresponding registry entries

Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def cleanReg(self):
    """
    Cleans the registry.

    > Use with caution. The command removes:

    > - all registry entries with no corresponding resource files

    > - all resource files with no corresponding registry entries
    """

    print("Info: Removing resources that do not exist on the registry...")
    for f in os.scandir(_RES_DIR):
        if f.is_dir():
            if not self._isResInReg(f.name):
                print("Info: Found {} directory that is not on the registry. Removing...".format(f.name))
                shutil.rmtree(f.path)
    print("Info: Removing resources complete.")
    print("Info: Removing registry entries that have no corresponding resource files...")
    if not self._isEmpty():
        for resName in self.resReg['resources']:
            if not os.path.exists(self._getResPath(resName)):
                print("Info: Found {} registry entry with no resource files.".format(resName))
                self._removeResRegEntry(resName)
    print("Info: Removing registry entries complete.")

getResMainFilePath

getResMainFilePath(resName)

Returns an absolute path to the resource main jar file.

Parameters:

Name Type Description Default
resName str

resource name

required
Source code in JPS_BASE_LIB/python_wrapper/twa/resRegistry/resRegistry.py
def getResMainFilePath(self, resName):
    """
    Returns an absolute path to the resource main jar file.

    Args:
        resName (str): resource name
    """
    if resName in self.resReg['resources']:
        mainFile = self.resReg['resources'][resName]['mainJarFile']
        return os.path.join(self._getResPath(resName), mainFile)
    else:
        return None