Skip to content

Commit

Permalink
Merge pull request #8 from julienbordet/feat-store-prefs
Browse files Browse the repository at this point in the history
Feat store prefs
  • Loading branch information
julienbordet authored Feb 10, 2022
2 parents 96b1933 + caf8e87 commit 0721cff
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
45 changes: 37 additions & 8 deletions menuping.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import rumps
from ping3 import ping
import os
from os import path
from os.path import expanduser
import shutil
from appdirs import user_data_dir
import configparser

#rumps.debug_mode(True)
# rumps.debug_mode(True)

# Global variables

Expand All @@ -16,7 +19,10 @@
plist_model_filename = "com.zejames.MenuPing-model.plist"
plist_filename = "com.zejames.MenuPing.plist"

target_dir = expanduser("~") + '/Library/LaunchAgents'
launch_dir = expanduser("~") + '/Library/LaunchAgents'
pref_dir = user_data_dir("MenuPing")
pref_file = "menuping.ini"


class MenuPingApp(rumps.App):

Expand All @@ -25,12 +31,15 @@ def __init__(self):

# Check if we are already persistant
self.persistant_menu = rumps.MenuItem(make_persistant_menu, self.manage_persistant)
if os.path.isfile(target_dir + '/' + plist_filename):
if os.path.isfile(launch_dir + '/' + plist_filename):
self.is_persistant = True
self.persistant_menu.state = True
else:
self.is_persistant = False

# Load preferences
self.load_preferences()

self.menu = [
rumps.MenuItem("Change target", self.change_target),
None,
Expand All @@ -39,8 +48,6 @@ def __init__(self):
rumps.MenuItem("About", self.about)
]

self.target_url = "www.google.fr"

self.timer = rumps.Timer(self.on_tick, 1)
self.timer.start()

Expand All @@ -51,7 +58,7 @@ def manage_persistant(self, sender):

if self.is_persistant:
# Delete LaunchAgents file
os.remove(target_dir + '/' + plist_filename)
os.remove(launch_dir + '/' + plist_filename)
else:
# Prepare plist file
with open(plist_dir + '/' + plist_model_filename, 'r') as file:
Expand All @@ -63,7 +70,7 @@ def manage_persistant(self, sender):
file.write(filedata)

# Copy file to ~/Library/LaunchAgents
shutil.copy(plist_dir + '/' + plist_filename, target_dir)
shutil.copy(plist_dir + '/' + plist_filename, launch_dir)

sender.state = not sender.state
self.is_persistant = not self.is_persistant
Expand All @@ -80,7 +87,7 @@ def change_target(self, sender):
if delay is False:
rumps.alert(title='MenuPing', message="Unable to ping the entered address. Please enter new one")
else:
self.target_url = new_target_url
self.update_target_url(new_target_url)

def on_tick(self, sender):
delay = ping(self.target_url)
Expand All @@ -102,6 +109,28 @@ def about(self, sender):
""",
ok=None, cancel=None)

def update_target_url(self, new_target_url):
if 'menuping' not in self.config.sections():
self.config['menuping'] = {}

self.config['menuping']['target_url'] = new_target_url
with open(pref_dir + '/' + pref_file, 'w') as file:
self.config.write(file)

self.target_url = new_target_url

def load_preferences(self):
if not path.isdir(pref_dir):
os.mkdir(pref_dir)

self.config = configparser.ConfigParser()
self.config.read(pref_dir + '/' + pref_file)

if 'menuping' in self.config.sections() and self.config['menuping']['target_url']:
self.target_url = self.config['menuping']['target_url']
else:
self.update_target_url("www.google.com")


if __name__ == "__main__":
app = MenuPingApp()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
altgraph==0.17.2
appdirs==1.4.4
macholib==1.15.2
modulegraph==0.19.2
ping3==3.0.2
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'CFBundleIdentifier': 'info.bordet.menuping',
'LSUIElement': True,
},
'packages': ['rumps', 'ping3'],
'packages': ['rumps', 'ping3', 'appdirs'],
}

setup(
Expand Down

0 comments on commit 0721cff

Please sign in to comment.