Skip to content

Commit

Permalink
prefix list stored in file
Browse files Browse the repository at this point in the history
  • Loading branch information
mullweisser committed Mar 31, 2024
1 parent da789fe commit d087411
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 46 deletions.
100 changes: 54 additions & 46 deletions WeeNu.py
Original file line number Diff line number Diff line change
@@ -1,75 +1,83 @@
import rumps
from datetime import datetime
import webbrowser
import os

def get_week_number():
"""
Calculate the current week number according to the ISO 8601 standard,
which is used in Sweden.
"""
today = datetime.now()
week_number = today.isocalendar()[1]
return week_number

class PrefixFormatter:
def __init__(self):
self.prefix_formats = self.load_prefixes_from_file()
self.current_format = self.load_format()

def format_with_week(self, week_number):
self.current_format_formatted = f"{self.current_format} {week_number}"
return self.current_format_formatted

def save_format(self, format_str):
try:
with open('user_settings', 'w') as f:
f.write(format_str)
self.current_format = format_str
except IOError as e:
rumps.alert(f"Error saving current prefix format: {e}")

def load_format(self):
if not os.path.exists('user_settings'):
return self.prefix_formats[0] # Return the first format if the file doesn't exist
try:
with open('user_settings', 'r') as f:
format_str = f.read().strip()
if format_str in self.prefix_formats:
return format_str
except IOError as e:
rumps.alert(f"Error loading prefix format: {e}")
return self.prefix_formats[0]

def load_prefixes_from_file(self):
try:
with open('prefix_list', 'r') as file:
prefixes = [line.strip() for line in file.readlines()]
except IOError as e:
raise Exception(f"Error reading file: 'prefix_list'")
return prefixes

class WeeNu(rumps.App):
def __init__(self):
super(WeeNu, self).__init__("WeeNu")
self.title_format = "w. {week_number}"
self.prefix_formatter = PrefixFormatter()

# Version info
self.menu = ["WeeNu v0.1", None]

# Creating a submenu for format toggling
self.settings_menu = rumps.MenuItem("Settings")
self.toggle_week_prefix_item = rumps.MenuItem("Show prefix", callback=self.toggle_format)

self.settings_menu.add(self.toggle_week_prefix_item)
self.menu = ["WeeNu v0.2", None]

# Initializing the menu item's state to True
self.toggle_week_prefix_item.state = True
self.format_menu = rumps.MenuItem("Prefix")
for format_option in self.prefix_formatter.prefix_formats:
menu_item = rumps.MenuItem(format_option, callback=self.change_prefix_format)
menu_item.state = format_option == self.prefix_formatter.current_format
self.format_menu.add(menu_item)

# Add a divider/separator here
self.menu = ["Manual update", self.settings_menu, None] # 'None' acts as a separator

# Add a GitHub link menu item
self.menu.add(self.format_menu)
self.menu.add(None)
self.github_link_item = rumps.MenuItem("About WeeNu", callback=self.open_github)
self.menu.add(self.github_link_item)

self.update_title()

@rumps.timer(60) # Update every 60 seconds
@rumps.timer(60)
def update_title(self, _=None):
"""
Updates the menu bar title with the current week number, according to the selected format.
"""
week_number = get_week_number()
self.title = self.title_format.format(week_number=week_number)
self.title = self.prefix_formatter.format_with_week(week_number)

@rumps.clicked("Manual update")
def on_update(self, _):
"""
Manually updates the week number when the 'Update' menu item is clicked.
"""
def change_prefix_format(self, sender):
literal_format = sender.title
self.prefix_formatter.save_format(literal_format)
for item in self.format_menu.values():
item.state = item.title == literal_format
self.update_title()

def toggle_format(self, sender):
"""
Toggles the title format and the checkmark state based on the current state.
"""
# Toggle the state and adjust the format based on the new state
sender.state = not sender.state
if sender.state:
self.title_format = "w. {week_number}"
sender.title = "Hide prefix"
else:
self.title_format = "{week_number}"
sender.title = "Show prefix"
self.update_title()

def open_github(self, _):
"""
Opens the GitHub repository URL in the default web browser.
"""
webbrowser.open('https://github.com/mullweisser/mac-weenu')

if __name__ == "__main__":
Expand Down
11 changes: 11 additions & 0 deletions prefix_list
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Week
WK.
Wk.
wk.
W.
w.
WK
Wk
wk
W
w
1 change: 1 addition & 0 deletions user_settings
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Week

0 comments on commit d087411

Please sign in to comment.