From 5402130fbf31eaeecca2706d50f665594262b39c Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Wed, 27 Apr 2022 13:49:55 -0400 Subject: [PATCH] upgrade ability - if more keys are expected in the config.ini file and when updating from a previous version, defaults can be inserted if the particular keys do not already exist --- config/config.ini.example | 4 +-- src/run.py | 59 ++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/config/config.ini.example b/config/config.ini.example index 8e6b504..5a9d7ef 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -6,8 +6,8 @@ pinned = true # minimum number of points in your account before entering into giveaways minimum_points = 50 # max number of entries in a giveaway for it to be considered -max_entries = 900 +max_entries = 2000 # time left in minutes of a giveaway for it to be considered -max_time_left = 180 +max_time_left = 300 # the minimum point value for a giveaway to be considered minimum_game_points = 5 \ No newline at end of file diff --git a/src/run.py b/src/run.py index c709e13..99174f8 100644 --- a/src/run.py +++ b/src/run.py @@ -1,10 +1,10 @@ import configparser from configparser import ConfigParser +from random import randint import log logger = log.get_logger(__name__) -config = configparser.ConfigParser() class MyException(Exception): @@ -16,25 +16,51 @@ def value_range(min, max): class MyConfig(ConfigParser): + required_values = { + 'DEFAULT': { + 'gift_types': ('All', 'Wishlist', 'Recommended', 'Copies', 'DLC', 'New'), + 'pinned': ('true', 'false'), + 'minimum_points': '%s' % (value_range(0, 400)), + 'max_entries': '%s' % (value_range(0, 100000)), + 'max_time_left': '%s' % (value_range(0, 21600)), + 'minimum_game_points': '%s' % (value_range(0, 50)) + } + } + default_values = { + 'DEFAULT': { + 'cookie': '', + 'gift_types': 'All', + 'pinned': 'true', + 'minimum_points': f"{randint(20, 100)}", + 'max_entries': f"{randint(1000, 2500)}", + 'max_time_left': f"{randint(180,500)}", + 'minimum_game_points': '1' + } + } + def __init__(self, config_file): super(MyConfig, self).__init__() - self.read(config_file) + modified = self.create_defaults() + if modified: + with open(config_file, 'w+') as file: + self.write(file) self.validate_config() - def validate_config(self): - required_values = { - 'DEFAULT': { - 'gift_types': ('All', 'Wishlist', 'Recommended', 'Copies', 'DLC', 'New'), - 'pinned': ('true', 'false'), - 'minimum_points': '%s' % (value_range(0,400)), - 'max_entries': '%s' % (value_range(0,100000)), - 'max_time_left': '%s' % (value_range(0,21600)), - 'minimum_game_points': '%s' % (value_range(0,50)) - } - } + def create_defaults(self): + modified = False + for section, keys in self.default_values.items(): + if section not in self: + self.add_section(section) + modified = True + for key, value in keys.items(): + if key not in self[section]: + self.set(section, key, value) + modified = True + return modified - for section, keys in required_values.items(): + def validate_config(self): + for section, keys in self.required_values.items(): if section not in self: raise MyException( 'Missing section %s in the config file' % section) @@ -56,10 +82,9 @@ def run(): from main import SteamGifts as SG file_name = '../config/config.ini' + config = None try: - with open(file_name) as f: - config.read_file(f) - MyConfig(file_name) + config = MyConfig(file_name) except IOError: txt = f"{file_name} doesn't exist. Rename {file_name}.example to {file_name} and fill out." logger.warning(txt)