commotion_client.utils.extension_manager.ConfigManager Class Reference
Inheritance diagram for commotion_client.utils.extension_manager.ConfigManager:

Public Member Functions

def __init__
 
def has_configs
 
def find
 
def get_paths
 
def get
 
def load
 

Public Attributes

 log
 
 translate
 
 configs
 
 directory
 
 paths
 

Detailed Description

A object for loading config data from a library.

This object should only be used to load configs and saving/checking those values against the users settings. Any value checking should take place in the users settings.

Constructor & Destructor Documentation

def commotion_client.utils.extension_manager.ConfigManager.__init__ (   self,
  path = None 
)
Args:
  path (string): The path to an extension library.

References commotion_client.utils.extension_manager.ConfigManager.configs, commotion_client.utils.extension_manager.ConfigManager.directory, commotion_client.utils.extension_manager.ConfigManager.get(), commotion_client.utils.extension_manager.ConfigManager.get_paths(), commotion_client.GUI.system_tray.TrayIcon.log, commotion_client.GUI.welcome_page.ViewPort.log, commotion_client.GUI.menu_bar.MenuBar.log, commotion_client.GUI.crash_report.CrashReport.log, commotion_client.extensions.config_editor.main.ViewPort.log, commotion_client.GUI.main_window.MainWindow.log, commotion_client.GUI.toolbar_builder.ToolBar.log, commotion_client.GUI.toolbar.ToolBar.log, commotion_client.GUI.extension_toolbar.ExtensionToolBar.log, commotion_client.utils.extension_manager.ExtensionManager.log, commotion_client.commotion_client.HoldStateDuringRestart.log, commotion_client.GUI.extension_toolbar.MenuItem.log, commotion_client.GUI.crash_report.ReportGatherer.log, commotion_client.commotion_client.CommotionClientApplication.log, commotion_client.utils.extension_manager.ConfigManager.log, commotion_client.utils.extension_manager.ConfigManager.paths, commotion_client.GUI.menu_bar.MenuBar.translate, commotion_client.extensions.config_editor.main.ViewPort.translate, commotion_client.GUI.main_window.MainWindow.translate, commotion_client.GUI.toolbar_builder.ToolBar.translate, commotion_client.GUI.extension_toolbar.ExtensionToolBar.translate, commotion_client.GUI.toolbar.ToolBar.translate, commotion_client.utils.extension_manager.ExtensionManager.translate, commotion_client.GUI.extension_toolbar.MenuItem.translate, commotion_client.commotion_client.CommotionClientApplication.translate, and commotion_client.utils.extension_manager.ConfigManager.translate.

623  def __init__(self, path=None):
624  """
625  Args:
626  path (string): The path to an extension library.
627  """
628  #set function logger
629  self.log = logging.getLogger("commotion_client."+__name__)
630  self.translate = QtCore.QCoreApplication.translate
631  self.log.debug(self.translate("logs", "Initalizing ConfigManager"))
632  self.configs = []
633  self.directory = None
634  self.paths = []
635  if path:
636  self.directory = path
637  try:
638  self.paths = self.get_paths(path)
639  except TypeError:
640  self.log.debug(self.translate("logs", "No extensions found in the {0} directory. You must first populate the folder with extensions to init a ConfigManager in that folder. You can create a ConfigManager without a location specified, but you will have to add extensions before getting paths.".format(path)))
641  raise ValueError(self.translate("logs", "The path {0} is empty. ConfigManager could not be created".format(path)))
642  else:
643  self.log.info(self.translate("logs", "Extensions found in the {0} directory. Attempting to load extension configs.".format(path)))
644  self.configs = list(self.get())
directory
Definition: extension_manager.py:632
configs
Definition: extension_manager.py:631
log
Definition: extension_manager.py:628
def get
Definition: extension_manager.py:719
paths
Definition: extension_manager.py:633
translate
Definition: extension_manager.py:629
def __init__
Definition: extension_manager.py:622
def get_paths
Definition: extension_manager.py:677

Member Function Documentation

def commotion_client.utils.extension_manager.ConfigManager.find (   self,
  name = None 
)
Function used to obtain a config file from the ConfigManager.

@param name optional The name of the configuration file if known
@param path string The absolute path to the folder to check for extension configs.

@return list of tuples containing a config name and its config.

References commotion_client.utils.extension_manager.ConfigManager.configs, commotion_client.GUI.menu_bar.MenuBar.translate, commotion_client.extensions.config_editor.main.ViewPort.translate, commotion_client.GUI.main_window.MainWindow.translate, commotion_client.GUI.toolbar_builder.ToolBar.translate, commotion_client.GUI.extension_toolbar.ExtensionToolBar.translate, commotion_client.GUI.toolbar.ToolBar.translate, commotion_client.utils.extension_manager.ExtensionManager.translate, commotion_client.GUI.extension_toolbar.MenuItem.translate, commotion_client.commotion_client.CommotionClientApplication.translate, and commotion_client.utils.extension_manager.ConfigManager.translate.

657  def find(self, name=None):
658  """
659  Function used to obtain a config file from the ConfigManager.
660 
661  @param name optional The name of the configuration file if known
662  @param path string The absolute path to the folder to check for extension configs.
663 
664  @return list of tuples containing a config name and its config.
665  """
666  if not self.configs:
667  self.log.warning(self.translate("logs", "No configs have been loaded. Please load configs first.".format(name)))
668  return False
669  if not name:
670  return self.configs
671  elif name != None:
672  for conf in self.configs:
673  if conf["name"] and conf["name"] == name:
674  return conf
675  self.log.error(self.translate("logs", "No config of the chosed type named {0} found".format(name)))
676  return False
configs
Definition: extension_manager.py:631
def find
Definition: extension_manager.py:656
translate
Definition: extension_manager.py:629
def commotion_client.utils.extension_manager.ConfigManager.get (   self,
  paths = None 
)
Generator to retreive config files for the paths passed to it

@param a list of paths of the configuration file to retreive
@return config file as a dictionary

References commotion_client.utils.extension_manager.ConfigManager.load(), commotion_client.utils.extension_manager.ConfigManager.paths, commotion_client.GUI.menu_bar.MenuBar.translate, commotion_client.extensions.config_editor.main.ViewPort.translate, commotion_client.GUI.main_window.MainWindow.translate, commotion_client.GUI.toolbar_builder.ToolBar.translate, commotion_client.GUI.toolbar.ToolBar.translate, commotion_client.GUI.extension_toolbar.ExtensionToolBar.translate, commotion_client.utils.extension_manager.ExtensionManager.translate, commotion_client.GUI.extension_toolbar.MenuItem.translate, commotion_client.commotion_client.CommotionClientApplication.translate, and commotion_client.utils.extension_manager.ConfigManager.translate.

Referenced by commotion_client.utils.extension_manager.ConfigManager.__init__().

720  def get(self, paths=None):
721  """
722  Generator to retreive config files for the paths passed to it
723 
724  @param a list of paths of the configuration file to retreive
725  @return config file as a dictionary
726  """
727  #load config file
728  if not paths:
729  self.log.debug(self.translate("logs", "No paths found. Attempting to load all extension manager paths list."))
730  paths = self.paths
731  self.log.debug(self.translate("logs", "Found paths:{0}.".format(paths)))
732  for path in paths:
733  if fs_utils.is_file(path):
734  config = self.load(path)
735  if config:
736  yield config
737  else:
738  self.log.warning(self.translate("logs", "Config file {0} does not exist and therefore cannot be loaded.".format(path)))
def get
Definition: extension_manager.py:719
paths
Definition: extension_manager.py:633
def load
Definition: extension_manager.py:739
translate
Definition: extension_manager.py:629
def commotion_client.utils.extension_manager.ConfigManager.get_paths (   self,
  directory 
)
Returns the paths to all extensions with config files within a directory.

Args:
  directory (string): The path to the folder that extension's are within. Extensions can be up to one level below the directory given.

Returns:
  config_files (array): An array of paths to all extension objects with config files that were found.

Raises:
  TypeError: If no extensions exist within the directory requested.
  AssertionError: If the directory path does not exist.

References commotion_client.GUI.menu_bar.MenuBar.translate, commotion_client.extensions.config_editor.main.ViewPort.translate, commotion_client.GUI.main_window.MainWindow.translate, commotion_client.GUI.toolbar_builder.ToolBar.translate, commotion_client.GUI.toolbar.ToolBar.translate, commotion_client.GUI.extension_toolbar.ExtensionToolBar.translate, commotion_client.utils.extension_manager.ExtensionManager.translate, commotion_client.GUI.extension_toolbar.MenuItem.translate, commotion_client.commotion_client.CommotionClientApplication.translate, and commotion_client.utils.extension_manager.ConfigManager.translate.

Referenced by commotion_client.utils.extension_manager.ConfigManager.__init__().

678  def get_paths(self, directory):
679  """Returns the paths to all extensions with config files within a directory.
680 
681  Args:
682  directory (string): The path to the folder that extension's are within. Extensions can be up to one level below the directory given.
683 
684  Returns:
685  config_files (array): An array of paths to all extension objects with config files that were found.
686 
687  Raises:
688  TypeError: If no extensions exist within the directory requested.
689  AssertionError: If the directory path does not exist.
690 
691  """
692  #Check the directory and raise value error if not there
693  dir_obj = QtCore.QDir(str(directory))
694  if not dir_obj.exists(dir_obj.absolutePath()):
695  raise ValueError(self.translate("logs", "Folder at path {0} does not exist. No Config files loaded.".format(str(directory))))
696  else:
697  path = dir_obj.absolutePath()
698 
699  config_files = []
700  try:
701  for root, dirs, files in fs_utils.walklevel(path):
702  for file_name in files:
703  if zipfile.is_zipfile(os.path.join(root, file_name)):
704  ext_zip = zipfile.ZipFile(os.path.join(root, file_name), 'r')
705  ext_names = ext_zip.namelist()
706  for member_name in ext_names:
707  if member_name.endswith(".conf"):
708  config_files.append(os.path.join(root, file_name))
709  except AssertionError:
710  self.log.warn(self.translate("logs", "Extension library at path {0} does not exist. No Config files identified.".format(path)))
711  raise
712  except TypeError:
713  self.log.warn(self.translate("logs", "No extensions found at path {0}. No Config files identified.".format(path)))
714  raise
715  if config_files:
716  return config_files
717  else:
718  raise TypeError(self.translate("logs", "No config files found at path {0}. No Config files loaded.".format(path)))
translate
Definition: extension_manager.py:629
def get_paths
Definition: extension_manager.py:677
def commotion_client.utils.extension_manager.ConfigManager.has_configs (   self)
Provides the status of a ConfigManagers config files.

Returns:
  bool: True, if there are configs. False, if there are no configs currently.

References commotion_client.utils.extension_manager.ConfigManager.configs.

646  def has_configs(self):
647  """Provides the status of a ConfigManagers config files.
648 
649  Returns:
650  bool: True, if there are configs. False, if there are no configs currently.
651  """
652  if self.configs:
653  return True
654  else:
655  return False
configs
Definition: extension_manager.py:631
def has_configs
Definition: extension_manager.py:645
def commotion_client.utils.extension_manager.ConfigManager.load (   self,
  path 
)
This function loads the formatted config file and returns it.

long description

Args:
path (string): The path to a config file

Returns:
  (dictionary) On success returns a dictionary containing the config file values.
  (bool): On failure returns False

References commotion_client.GUI.menu_bar.MenuBar.translate, commotion_client.extensions.config_editor.main.ViewPort.translate, commotion_client.GUI.main_window.MainWindow.translate, commotion_client.GUI.toolbar_builder.ToolBar.translate, commotion_client.GUI.toolbar.ToolBar.translate, commotion_client.GUI.extension_toolbar.ExtensionToolBar.translate, commotion_client.utils.extension_manager.ExtensionManager.translate, commotion_client.GUI.extension_toolbar.MenuItem.translate, commotion_client.commotion_client.CommotionClientApplication.translate, and commotion_client.utils.extension_manager.ConfigManager.translate.

Referenced by commotion_client.utils.extension_manager.ConfigManager.get().

740  def load(self, path):
741  """This function loads the formatted config file and returns it.
742 
743  long description
744 
745  Args:
746  path (string): The path to a config file
747 
748  Returns:
749  (dictionary) On success returns a dictionary containing the config file values.
750  (bool): On failure returns False
751 
752  """
753  config = None
754  data = None
755  myfile = QtCore.QFile(str(path))
756  if not myfile.exists():
757  return False
758  if not zipfile.is_zipfile(str(path)):
759  return False
760  with zipfile.ZipFile(path, 'r') as zip_ext:
761  for file_name in zip_ext.namelist():
762  if file_name.endswith(".conf"):
763  config = zip_ext.read(file_name)
764  self.log.debug(self.translate("logs", "Config found in extension {0}.".format(path)))
765  if config:
766  try:
767  data = json.loads(config.decode('utf-8'))
768  self.log.info(self.translate("logs", "Successfully loaded {0}'s config file.".format(path)))
769  except ValueError:
770  self.log.warning(self.translate("logs", "Failed to load {0} due to a non-json or otherwise invalid file type".format(path)))
771  return False
772  if data:
773  self.log.debug(self.translate("logs", "Config file loaded.".format(path)))
774  return data
775  else:
776  self.log.debug(self.translate("logs", "Failed to load config file.".format(path)))
777  return False
def load
Definition: extension_manager.py:739
translate
Definition: extension_manager.py:629

The documentation for this class was generated from the following file: