def get_highscores(n,gametype="normal-game"): if not os.path.exists(highscores_file): return [] with open(highscores_file,"r") as hs: try: highscores = yaml.load(hs.read())[gametype] except KeyError: return [] highscores.sort(reverse=True) return highscores[0:n]
def args_overwrite_settings(settings): setting_changed = False for arg in sys.argv[1:]: match = re.match(settings_regex, arg) if match: groups = match.group(1, 2, 3, 4, 5) failed_setting = lambda _setting: "\tFAILED! Parsing error: '{0}'".format( _setting) non_existent_setting = lambda _setting: "WARNING! No such setting: {0}".format( _setting) if groups[0]: # subsetting matched try: settings[groups[0]][groups[1]] except KeyError: print( non_existent_setting("{0}.{1}".format( groups[0], groups[1]))) print("Temporarily changing setting {0}.{1}\t-->\t{2}".format( groups[0], groups[1], groups[2])) try: settings[groups[0]][groups[1]] = yaml.load(groups[2]) setting_changed = True except ParserError: print(failed_setting(groups[2])) elif groups[3]: # setting matched try: settings[groups[3]] except KeyError: print(non_existent_setting(groups[3])) print("Temporarily changing setting {0}\t-->\t{1}".format( groups[3], groups[4])) try: settings[groups[3]] = yaml.load(groups[4]) setting_changed = True except ParserError: print(failed_setting(groups[4])) return settings, setting_changed
def submit_score(score, gametype="normal-game"): if os.path.exists(highscores_file): f = open(highscores_file,"r") highscores = f.read() highscores = yaml.load(highscores) f.close() else: highscores = {gametype:[]} with open(highscores_file,"w") as hs: try: highscores[gametype].append(score) except KeyError: highscores[gametype] = [score] hs.write(yaml.dump(highscores))
def args_overwrite_settings(settings): setting_changed = False for arg in sys.argv[1:]: match = re.match(settings_regex,arg) if match: groups = match.group(1,2,3, 4,5) failed_setting = lambda _setting: "\tFAILED! Parsing error: '{0}'".format(_setting) non_existent_setting = lambda _setting: "WARNING! No such setting: {0}".format(_setting) if groups[0]: # subsetting matched try: settings[groups[0]][groups[1]] except KeyError: print(non_existent_setting("{0}.{1}".format(groups[0],groups[1]))) print("Temporarily changing setting {0}.{1}\t-->\t{2}".format(groups[0],groups[1],groups[2])) try: settings[groups[0]][groups[1]] = yaml.load(groups[2]) setting_changed = True except ParserError: print(failed_setting(groups[2])) elif groups[3]: # setting matched try: settings[groups[3]] except KeyError: print(non_existent_setting(groups[3])) print("Temporarily changing setting {0}\t-->\t{1}".format(groups[3],groups[4])) try: settings[groups[3]] = yaml.load(groups[4]) setting_changed = True except ParserError: print(failed_setting(groups[4])) return settings, setting_changed
settings[groups[3]] except KeyError: print(non_existent_setting(groups[3])) print("Temporarily changing setting {0}\t-->\t{1}".format(groups[3],groups[4])) try: settings[groups[3]] = yaml.load(groups[4]) setting_changed = True except ParserError: print(failed_setting(groups[4])) return settings, setting_changed settings_file = open("settings.yaml","r").read() settings = yaml.load(settings_file) settings, setting_changed = args_overwrite_settings(settings) def nice_print(args): if not settings['log-to-console']: return base = time.strftime("%H:%M:%S: ") for arg in args: base += '{0:<33}'.format(arg) print(base) def open_help_in_browser(): abs_path = os.path.abspath('.') #Absolute path of current working directory filename = os.path.join(abs_path,"help", 'help.html') nice_print('Trying to open ' + filename) webbrowser.open(filename)
settings[groups[3]] except KeyError: print(non_existent_setting(groups[3])) print("Temporarily changing setting {0}\t-->\t{1}".format( groups[3], groups[4])) try: settings[groups[3]] = yaml.load(groups[4]) setting_changed = True except ParserError: print(failed_setting(groups[4])) return settings, setting_changed settings_file = open("settings.yaml", "r").read() settings = yaml.load(settings_file) settings, setting_changed = args_overwrite_settings(settings) def nice_print(args): if not settings['log-to-console']: return base = time.strftime("%H:%M:%S: ") for arg in args: base += '{0:<33}'.format(arg) print(base) def open_help_in_browser(): abs_path = os.path.abspath( '.') #Absolute path of current working directory