Skip to content

Review Config.load() #20

@Zvorky

Description

@Zvorky

It works and make sure the config file is loaded correctly, but there's too many "Try Excepts" without any comment.
I wrote it and I can't read sh!#

    def load(self, config = ''):
        if(config):
            self.name = config

        try:
            with open(str(Config.dir) + '/' + str(self.name) + '.json') as config:
                configjson = json.load(config)
                loaded     = 0

                if(self == configjson):
                    config.close()
                    return True

                if(self.log):
                    self.log << '\nConfig File: ' + json.dumps(configjson, indent = 4)

                try:
                    self.dict['SleepTime'] = configjson['SleepTime']
                    loaded += 1
                except KeyError:
                    if(self.log):
                        self.log.warn(self.name + '.json does not have a "SleepTime" key', False)
                
                try:
                    self.dict['File'] = configjson['File']
                    loaded += 1
                except KeyError:
                    if(self.log):
                        self.log.warn(self.name + '.json does not have a "File" key', False)
                
                try:
                    self.dict['Directory'] = configjson['Directory']
                    loaded += 1
                except KeyError:
                    if(self.log):
                        self.log.warn(self.name + '.json does not have a "Directory" key', False)
                
                try:
                    self.dict['Filter'] = configjson['Filter']
                    loaded += 1
                except KeyError:
                    if(self.log):
                        self.log.warn(self.name + '.json does not have a "Filter" key', False)

                if(not loaded):
                    if(self.log):
                        self.log.error("Configuration Couldn't be Loaded", True)
                elif(loaded < 4):
                    if(self.log):
                        self.log.warn('Configuration Partially Loaded, SleepTime: ' + str(self.sleepTime()) + 's.', True)
                else:
                    if(self.log):
                        self.log.info('Configuration Loaded, SleepTime: ' + str(self.sleepTime()) + 's.', False)

                config.close()
                return loaded == 4

        except FileNotFoundError:
            if(self.log):
                self.log.warn(self.name + '.json not found', False)
            return False```

Metadata

Metadata

Assignees

No one assigned

    Labels

    invalidThis doesn't seem right

    Projects

    Status

    Scheduled

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions