# Path: pyilper/userconfig.py
# class cls_userconfig:
# 
#    def __init__(self,progname,filename,configversion,instance,production):
# #
# #  determine config file name
# #
#       self.__configfile__,self.__configpath__=buildconfigfilename(progname,filename,configversion,instance,production)
# 
# #
# #  read configuration, if no configuration exists write default configuration
# #
#    def read(self,default):
#       if not os.path.isfile(self.__configfile__):
#          if not os.path.exists(self.__configpath__):
#             try:
#                os.makedirs(self.__configpath__)
#             except OSError as e:
#                raise ConfigError("Cannot create path for configuration file", e.strerror)
#          try:
#             self.write(default)
#          except OSError as e:
#             raise ConfigError("Cannot write default configuration file", e.strerror)
#          return default
#       f=None
#       try:
#          f= open(self.__configfile__,"r")
#          config= json.load(f)
#       except json.JSONDecodeError as e:
#          add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno)
#          raise ConfigError("Cannot decode configuration data",add_msg)
#       except OSError as e:
#          raise ConfigError("Cannot read configuration file", e.strerror)
#       finally:
#          if f is not None:
#             f.close()
# 
#       return config
# #
# #  Store configuration, create file if it does not exist
# #
#    def write(self,config):
#       f=None
#       try:
#          f= open(self.__configfile__,"w")
#          json.dump(config,f,sort_keys=True,indent=3)
#       except json.JSONDecodeError as e:
#          add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno)
#          raise ConfigError("Cannot encode configuration data",add_msg)
#       except OSError as e:
#          raise ConfigError("Cannot write to configuration file", e.strerror)
#       finally:
#          if f is not None:
#             f.close()
# 
# class ConfigError(Exception):
#    def __init__(self,msg,add_msg= None):
#       self.msg= msg
#       self.add_msg = add_msg


from .userconfig import cls_userconfig, ConfigError
# - added the clean parameter to the open method
# 12.12.2021 jsi
# - add configversion parameter to open method
#


class PilConfigError(Exception):
   def __init__(self,msg,add_msg= None):
      self.msg= msg
      if add_msg is None:
         self.add_msg=""
      else:
         self.add_msg = add_msg


class cls_pilconfig:
#
#  initialize: create instance
#
   def __init__(self):
      self.__config__= { } 
      self.__userconfig__ = None
      return

#
#  open: read in the configuration file into the dictionary
#  if the configuration file does not exist, an empty file is created
#  If clean is true do not read the config file
#
   def open(self,name,configversion,instance,production,clean):

# Path: pyilper/userconfig.py
# class cls_userconfig:
# 
#    def __init__(self,progname,filename,configversion,instance,production):
# #
# #  determine config file name
# #
#       self.__configfile__,self.__configpath__=buildconfigfilename(progname,filename,configversion,instance,production)
# 
# #
# #  read configuration, if no configuration exists write default configuration
# #
#    def read(self,default):
#       if not os.path.isfile(self.__configfile__):
#          if not os.path.exists(self.__configpath__):
#             try:
#                os.makedirs(self.__configpath__)
#             except OSError as e:
#                raise ConfigError("Cannot create path for configuration file", e.strerror)
#          try:
#             self.write(default)
#          except OSError as e:
#             raise ConfigError("Cannot write default configuration file", e.strerror)
#          return default
#       f=None
#       try:
#          f= open(self.__configfile__,"r")
#          config= json.load(f)
#       except json.JSONDecodeError as e:
#          add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno)
#          raise ConfigError("Cannot decode configuration data",add_msg)
#       except OSError as e:
#          raise ConfigError("Cannot read configuration file", e.strerror)
#       finally:
#          if f is not None:
#             f.close()
# 
#       return config
# #
# #  Store configuration, create file if it does not exist
# #
#    def write(self,config):
#       f=None
#       try:
#          f= open(self.__configfile__,"w")
#          json.dump(config,f,sort_keys=True,indent=3)
#       except json.JSONDecodeError as e:
#          add_msg="File: "+self.__configfile__+". Error: "+e.msg+" at line: "+str(e.lineno)
#          raise ConfigError("Cannot encode configuration data",add_msg)
#       except OSError as e:
#          raise ConfigError("Cannot write to configuration file", e.strerror)
#       finally:
#          if f is not None:
#             f.close()
# 
# class ConfigError(Exception):
#    def __init__(self,msg,add_msg= None):
#       self.msg= msg
#       self.add_msg = add_msg


# Path: pyilper/pilconfig.py
from .userconfig import cls_userconfig, ConfigError

# - added the clean parameter to the open method
# 12.12.2021 jsi
# - add configversion parameter to open method
#


class PilConfigError(Exception):
   def __init__(self,msg,add_msg= None):
      self.msg= msg
      if add_msg is None:
         self.add_msg=""
      else:
         self.add_msg = add_msg


class cls_pilconfig:
#
#  initialize: create instance
#
   def __init__(self):
      self.__config__= { } 
      self.__userconfig__ = None
      return

#
#  open: read in the configuration file into the dictionary
#  if the configuration file does not exist, an empty file is created
#  If clean is true do not read the config file
#
   def open(self,name,configversion,instance,production,clean):

      self.__userconfig__= cls_userconfig(name,name,configversion,instance,production)