Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load COL_SCHEMEs from json files #166

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions hangups/ui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sys
import urwid
import readlike
import json

import hangups
from hangups.ui.notify import Notifier
Expand Down Expand Up @@ -829,13 +830,48 @@ def main():
default_log_path = os.path.join(dirs.user_log_dir, 'hangups.log')
default_token_path = os.path.join(dirs.user_cache_dir, 'refresh_token.txt')
default_config_path = os.path.join(dirs.user_config_dir, 'hangups.conf')
default_colors_path = os.path.join(dirs.user_config_dir, 'colors')

# Create a default empty config file if does not exist.
dir_maker(default_config_path)
if not os.path.isfile(default_config_path):
with open(default_config_path, 'a') as cfg:
cfg.write("")

# Create empty colors directory if there is none
dir_maker(default_colors_path)

# Read .col files
attributes = {'active_tab', 'inactive_tab', 'msg_date', 'msg_sender', 'msg_text', 'status_line', 'tab_background'}
for root, dirs, files in os.walk(default_colors_path):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a waste to load all the colour schemes when we already know which one the user wants. Can you make it just load the file that's specified?

for filename in files:
splitName = os.path.splitext(filename)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variable names should be lowercase with underscores, eg. spilt_name.

if splitName[1] == '.json':
obj = []
usedAttrs = []

name = splitName[0]

with open(os.path.join(root, filename), "r") as f:
try:
data = json.load(f)
except:
print("Error loading JSON file (" + filename + ")")
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's always better to catch a specific exceptions, and you can use sys.exit to both exit with status 1 and print the message to stderr:

except ValueError:
    sys.exit('Error loading JSON file: {}'.format(filename))

for key, subdict in data.items():
retList = [key,'','']
if 'foreground' in subdict:
retList[1] = subdict['foreground']
if 'background' in subdict:
retList[2] = subdict['background']
usedAttrs.append(key)
obj.append(tuple(retList))

for attr in (attributes - set(usedAttrs)):
obj.append(tuple([attr, '', '']))

COL_SCHEMES[name] = set(obj)

parser = configargparse.ArgumentParser(
prog='hangups', default_config_files=[default_config_path],
formatter_class=configargparse.ArgumentDefaultsHelpFormatter,
Expand Down