Skip to content

Commit

Permalink
fix: prevent no account error (#156)
Browse files Browse the repository at this point in the history
  • Loading branch information
iholston committed Nov 27, 2023
1 parent e1706f8 commit d620048
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 28 deletions.
2 changes: 2 additions & 0 deletions lolbot/bot/launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def __init__(self) -> None:
def launch_league(self, username: str, password: str) -> None:
"""Runs setup logic and starts launch sequence"""
self.set_game_config()
if not username or not password:
self.log.warning('No account set. Add accounts on account page')
self.username = username
self.password = password
self.launch_loop()
Expand Down
13 changes: 7 additions & 6 deletions lolbot/common/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_account(self) -> Account:
pass

@abstractmethod
def get_all_accounts(self) -> dict:
def get_all_accounts(self) -> list:
pass

@abstractmethod
Expand Down Expand Up @@ -66,8 +66,9 @@ def get_account(self, max_level: int) -> Account:
for account in data['accounts']:
if account['level'] < max_level:
return Account(account['username'], account['password'], account['level'])
return Account('', '', 0)

def add_account(self, account: Account):
def add_account(self, account: Account) -> None:
"""Writes account to JSON, will not write duplicates"""
with open(Constants.ACCOUNT_PATH, 'r+') as f:
data = json.load(f)
Expand All @@ -77,7 +78,7 @@ def add_account(self, account: Account):
with open(Constants.ACCOUNT_PATH, 'r+') as outfile:
outfile.write(json.dumps(data, indent=4))

def edit_account(self, og_uname: str, account: Account):
def edit_account(self, og_uname: str, account: Account) -> None:
"""Edit an account"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
Expand All @@ -92,21 +93,21 @@ def edit_account(self, og_uname: str, account: Account):
with open(Constants.ACCOUNT_PATH, 'w') as outfile:
outfile.write(json.dumps(data, indent=4))

def delete_account(self, account: Account):
def delete_account(self, account: Account) -> None:
"""Deletes account"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
data['accounts'].remove(asdict(account))
with open(Constants.ACCOUNT_PATH, 'w') as outfile:
outfile.write(json.dumps(data, indent=4))

def get_all_accounts(self) -> dict:
def get_all_accounts(self) -> list:
"""Returns all accounts as dictionary"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
return data['accounts']

def set_account_as_leveled(self, account: Account, max_level: int):
def set_account_as_leveled(self, account: Account, max_level: int) -> None:
"""Sets account level to user configured max level in the JSON file"""
with open(Constants.ACCOUNT_PATH, 'r') as f:
data = json.load(f)
Expand Down
33 changes: 11 additions & 22 deletions lolbot/view/logs_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,28 +58,6 @@ def create_log_table(self) -> None:
f = open(f, "r")
dpg.add_input_text(multiline=True, default_value=f.read(), height=300, width=600, tab_input=True)

@staticmethod
def sorted_dir_creation_time(directory: str) -> list:
"""Sorts directory by creation time. recent first"""
def get_creation_time(item):
item_path = os.path.join(directory, item)
return os.path.getctime(item_path)

items = os.listdir(directory)
sorted_items = list(reversed(sorted(items, key=get_creation_time)))
return sorted_items

@staticmethod
def clear_bkps() -> None:
folder = Constants.LOG_DIR
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if filename.endswith('.1'):
os.unlink(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))

def clear_logs(self) -> None:
"""Empties the log folder"""
dpg.configure_item("DeleteFiles", show=False)
Expand All @@ -94,3 +72,14 @@ def clear_logs(self) -> None:
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
self.create_log_table()

@staticmethod
def sorted_dir_creation_time(directory: str) -> list:
"""Sorts directory by creation time. recent first"""
def get_creation_time(item):
item_path = os.path.join(directory, item)
return os.path.getctime(item_path)

items = os.listdir(directory)
sorted_items = list(reversed(sorted(items, key=get_creation_time)))
return sorted_items

0 comments on commit d620048

Please sign in to comment.