Module geode.lib.common
Expand source code
# ------------------------------------------------------------------------------
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
# ------------------------------------------------------------------------------
# API
from geode.conf import Configuration
# Filesystem
from os.path import expanduser
from pathlib import Path
# ------------------------------------------------------------------------------
# Class
# ------------------------------------------------------------------------------
class CommonObject(object):
def __init__(self, api, filepath):
""" Constructor
Parameters
----------
api : geode.api.API
API instance
filepath : pathlib.Path
Object configuration file
Raises
------
TypeError
when api parameter is not a geode API instance
when filepath parameter is not a string
when object name is not define
"""
# ----------------------------------------
# Check api parameters
# ----------------------------------------
if type(api) is None:
raise TypeError("Wrong type for api, expected geode.api.API")
self.api = api
# ----------------------------------------
# Check configuration parameters
# ----------------------------------------
if type(filepath) is None:
raise TypeError("Wrong type for filepath, expected pathlib.Path")
# Generate configuration instance
self.__configuration = Configuration(str(filepath))
# ----------------------------------------
# Check variables
# ----------------------------------------
# Set uniq identifier
self.id = filepath.stem
for section, option, value in self.attributes:
# ----------------------------------------
# Manage specific variable
# ----------------------------------------
if type(value) is bool:
value = self.__configuration.getboolean(
section, option, fallback=value)
elif type(value) is list:
value = self.__configuration.get(
section, option, fallback=value).split()
else:
value = self.__configuration.get(
section, option, fallback=value)
# ----------------------------------------
# Manage specific constant
# ----------------------------------------
if value is not None and section in ("path", "pattern"):
for key, new_value in self.api.get_paths():
if new_value is not None and "<%s>" % key in value:
value = value.replace("<%s>" % key, str(new_value))
if len(value) > 0:
value = Path(expanduser(value))
setattr(self, option, value)
# Check if a name is unavailable
if self.name is None or len(self.name) == 0:
raise TypeError("Missing name attribute")
# ----------------------------------------
# Environment keys
# ----------------------------------------
# Store environment keys
self.environment = dict()
if self.__configuration.has_section("environment"):
for option, value in self.__configuration.items("environment"):
self.environment[option] = value
def __str__(self):
""" Print object as string
Returns
-------
str
Formatted object data as string
"""
return self.id
def get_path(self):
""" Retrieve object configuration file
Returns
-------
pathlib.Path
Object configuration file
"""
return self.__configuration.path
def save(self):
""" Save object instance
"""
# Metadata
for section, option, value in self.attributes:
self.__configuration.modify(
section, option, getattr(self, option, value))
# Environment keys
if len(self.environment) > 0:
for key, value in self.environment.items():
self.__configuration.modify("environment", key, value)
else:
self.__configuration.remove("environment")
self.__configuration.update()
Classes
class CommonObject (api, filepath)
-
Constructor
Parameters
api
:API
- API instance
filepath
:pathlib.Path
- Object configuration file
Raises
TypeError
- when api parameter is not a geode API instance when filepath parameter is not a string when object name is not define
Expand source code
class CommonObject(object): def __init__(self, api, filepath): """ Constructor Parameters ---------- api : geode.api.API API instance filepath : pathlib.Path Object configuration file Raises ------ TypeError when api parameter is not a geode API instance when filepath parameter is not a string when object name is not define """ # ---------------------------------------- # Check api parameters # ---------------------------------------- if type(api) is None: raise TypeError("Wrong type for api, expected geode.api.API") self.api = api # ---------------------------------------- # Check configuration parameters # ---------------------------------------- if type(filepath) is None: raise TypeError("Wrong type for filepath, expected pathlib.Path") # Generate configuration instance self.__configuration = Configuration(str(filepath)) # ---------------------------------------- # Check variables # ---------------------------------------- # Set uniq identifier self.id = filepath.stem for section, option, value in self.attributes: # ---------------------------------------- # Manage specific variable # ---------------------------------------- if type(value) is bool: value = self.__configuration.getboolean( section, option, fallback=value) elif type(value) is list: value = self.__configuration.get( section, option, fallback=value).split() else: value = self.__configuration.get( section, option, fallback=value) # ---------------------------------------- # Manage specific constant # ---------------------------------------- if value is not None and section in ("path", "pattern"): for key, new_value in self.api.get_paths(): if new_value is not None and "<%s>" % key in value: value = value.replace("<%s>" % key, str(new_value)) if len(value) > 0: value = Path(expanduser(value)) setattr(self, option, value) # Check if a name is unavailable if self.name is None or len(self.name) == 0: raise TypeError("Missing name attribute") # ---------------------------------------- # Environment keys # ---------------------------------------- # Store environment keys self.environment = dict() if self.__configuration.has_section("environment"): for option, value in self.__configuration.items("environment"): self.environment[option] = value def __str__(self): """ Print object as string Returns ------- str Formatted object data as string """ return self.id def get_path(self): """ Retrieve object configuration file Returns ------- pathlib.Path Object configuration file """ return self.__configuration.path def save(self): """ Save object instance """ # Metadata for section, option, value in self.attributes: self.__configuration.modify( section, option, getattr(self, option, value)) # Environment keys if len(self.environment) > 0: for key, value in self.environment.items(): self.__configuration.modify("environment", key, value) else: self.__configuration.remove("environment") self.__configuration.update()
Subclasses
Methods
def get_path(self)
-
Retrieve object configuration file
Returns
pathlib.Path
- Object configuration file
Expand source code
def get_path(self): """ Retrieve object configuration file Returns ------- pathlib.Path Object configuration file """ return self.__configuration.path
def save(self)
-
Save object instance
Expand source code
def save(self): """ Save object instance """ # Metadata for section, option, value in self.attributes: self.__configuration.modify( section, option, getattr(self, option, value)) # Environment keys if len(self.environment) > 0: for key, value in self.environment.items(): self.__configuration.modify("environment", key, value) else: self.__configuration.remove("environment") self.__configuration.update()