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

Add preferences to main app class #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion rumps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@
__copyright__ = 'Copyright 2017 Jared Suttles'

from .rumps import (separator, debug_mode, alert, notification, application_support, timers, quit_application, timer,
clicked, notifications, MenuItem, Timer, Window, App)
clicked, notifications, MenuItem, Timer, Window, App, Preferences)
37 changes: 36 additions & 1 deletion rumps/rumps.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@

import inspect
import os
import pickle
import sys
import traceback
import weakref

try:
import cPickle as pickle
except ImportError:
import pickle as pickle

from collections import Mapping, Iterable
from .utils import ListDict
from .compat import text_type, string_types, iteritems
Expand Down Expand Up @@ -212,6 +216,13 @@ def application_support(name):
return app_support_path


def preferences_path(name):
home_dir = os.path.join(os.path.expanduser("~"), name)
if not os.path.exists(home_dir):
os.makedirs(home_dir)
return os.path.join(home_dir, "{}.prefs".format(name.lower().replace(" ", "").strip()))


def timers():
"""Return a list of all :class:`rumps.Timer` objects. These can be active or inactive."""
return list(_TIMERS)
Expand Down Expand Up @@ -1026,6 +1037,29 @@ def callback_(cls, nsmenuitem):
_log(traceback.format_exc())


class Preferences(object):
"""
Provides access to a folder in the user's home directory, as well as a pickled key-value file.
:param name: the name of the application.
:param path: a path to the folder being used to store the preferences file.
"""

def __init__(self, name, path=None):
self._path = path if path else preferences_path(name)
self._preferences = {}
if os.path.exists(self._path):
with open(self._path, "rb") as file:
self._preferences = pickle.load(file)

def get(self, key, default=None):
return self._preferences.get(key, default)

def set(self, key, value):
self._preferences[key] = value
with open(self._path, "wb") as file:
pickle.dump(self._preferences, file, 2)
print("Set preference %s to %s" % (key, repr(value)))
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
print("Set preference %s to %s" % (key, repr(value)))
_log("Set preference %s to %s" % (key, repr(value)))


class App(object):
"""Represents the statusbar application.

Expand Down Expand Up @@ -1064,6 +1098,7 @@ def __init__(self, name, title=None, icon=None, template=None, menu=None, quit_b
if menu is not None:
self.menu = menu
self._application_support = application_support(self._name)
self.preferences = Preferences(self._name)

# Properties
#- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down