diff --git a/main.py b/main.py index d784861..bc28b04 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,11 @@ from src.prompt_helper import PromptHelper # Commandline options support import argparse +# basic attempt at making program faster +import datetime +from src.timing import timeit +from cachier import cachier +cachier = cachier(stale_after=datetime.timedelta(days=1), cache_dir='/tmp/.cache') class TableItem: @@ -90,14 +95,23 @@ class SteamGamePathTool: else: console.print("No game found") + @timeit + @cachier + def prepare_all_games(self): + game_rend = [Panel(self.get_game_content(game), expand=True) for game in self.games] + return game_rend + + def print_all_games(self): + game_rend = self.prepare_all_games() + console = Console() + console.print(Columns(game_rend)) + def prompt_user(self): game = self.prompter.prompt_game(text="Input (game name | appid | all | q/quit): ") if game is None: return elif game == 'fetch_all_games': - console = Console() - game_rend = [Panel(self.get_game_content(game), expand=True) for game in self.games] - console.print(Columns(game_rend)) + self.print_all_games() else: console = Console() console.print(Panel(self.get_game_content(game), expand=True)) @@ -106,6 +120,7 @@ class SteamGamePathTool: def sort_games(self, games): return sorted(games, key=lambda x: x['name']) + @cachier def get_game_content(self, game): """ Takes a game dictionary and returns a string that formats game information into a string renderable by the console in the rich library. diff --git a/src/prompt_helper.py b/src/prompt_helper.py index a27bb1d..72b6a8b 100644 --- a/src/prompt_helper.py +++ b/src/prompt_helper.py @@ -1,12 +1,13 @@ import prompt_toolkit as pt from prompt_toolkit.completion import WordCompleter, FuzzyCompleter import sys -from cachier import cachier import datetime +from src.timing import timeit +from cachier import cachier cachier = cachier(stale_after=datetime.timedelta(days=1), cache_dir='/tmp/.cache') - +@timeit @cachier def generate_completer(game_list): g_list = [x['name'] for x in game_list] + [x['appid'] for x in game_list] @@ -20,12 +21,14 @@ class PromptHelper: self.game_list = game_list self.completer = generate_completer(game_list) + @timeit @cachier def find_game_str(self, game_name) -> dict|None: for game in self.game_list: if game['name'] == game_name: return game + @timeit @cachier def find_game_num(self, appid: str) -> dict|None: for game in self.game_list: diff --git a/src/timing.py b/src/timing.py new file mode 100644 index 0000000..6bace23 --- /dev/null +++ b/src/timing.py @@ -0,0 +1,16 @@ +from functools import wraps +import time + +debug = True + +def timeit(func): + @wraps(func) + def timeit_wrapper(*args, **kwargs): + start_time = time.perf_counter() + result = func(*args, **kwargs) + end_time = time.perf_counter() + total_time = end_time - start_time + if debug: + print(f'Function {func.__name__} Took {total_time:.4f} seconds.') + return result + return timeit_wrapper diff --git a/src/vdfparser.py b/src/vdfparser.py index df66e16..ba80d59 100644 --- a/src/vdfparser.py +++ b/src/vdfparser.py @@ -2,9 +2,11 @@ import os import vdf from cachier import cachier import datetime +from src.timing import timeit cachier = cachier(stale_after=datetime.timedelta(days=1), cache_dir='/tmp/.cache') @cachier +@timeit def parse_vdf(steam_vdf_path) -> dict: """ Reads the Steam vdf file at the given path and returns the corresponding dict.