1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- from configparser import ConfigParser
- import os
- class Config:
- DEFAULT_XML_REFERENCE_TYPE = 'http://www.gzs.si/shemas/eslog/racun/1.6#Racun'
- DEFAULT_CONFIG_DIR = os.path.join(os.path.expanduser('~'), '.config')
- def __init__(self, config_file):
- self.config_file = os.path.join(self.DEFAULT_CONFIG_DIR, config_file)
- self.config = ConfigParser()
- self.config.read(self.config_file)
- self.errors = []
- def exists(self):
- return 'settings' in self.config and 'xmlschema' in self.config['settings'] and\
- 'certificate' in self.config['settings']
- def get_data(self):
- return {
- 'xmlreftype': self.config.get('settings', 'xmlreftype'),
- 'xmlschema': self.config.get('settings', 'xmlschema'),
- 'certificate': self.config.get('settings', 'certificate')
- }
- def verify_data(self):
- if not self.exists():
- self.errors.append('Config wasn\'t set up yet.')
- return False
- if not os.path.isfile(self.config['settings']['xmlschema']):
- self.errors.append('XML Schema file doesn\'t exist.')
- if not os.path.isfile(self.config['settings']['certificate']):
- self.errors.append('Certificate file doesn\'t exist.')
- return len(self.errors) == 0
- def get_errors(self):
- errors = self.errors.copy()
- self.errors.clear()
- return errors
- def save_data(self, config):
- if 'settings' not in self.config:
- self.config.add_section('settings')
- self.config.set('settings', 'xmlreftype', self.DEFAULT_XML_REFERENCE_TYPE)
- self.config.set('settings', 'xmlschema', config['xmlschema'])
- self.config.set('settings', 'certificate', config['certificate'])
- try:
- if not os.path.isfile(self.config_file):
- os.makedirs(self.DEFAULT_CONFIG_DIR, exist_ok=True)
- with open(self.config_file, 'w') as f:
- self.config.write(f)
- except:
- raise ConfigException('Cannot write config.')
- class ConfigException(Exception):
- pass
|