1.1 --- a/imiptools/profile.py Sat Oct 24 22:19:17 2015 +0200
1.2 +++ b/imiptools/profile.py Sun Oct 25 01:24:56 2015 +0200
1.3 @@ -19,30 +19,54 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 -from imiptools.config import PREFERENCES_DIR
1.8 +from imiptools import config
1.9 +from imiptools.dates import get_default_timezone
1.10 from imiptools.filesys import fix_permissions, FileBase
1.11 from os.path import exists, isdir
1.12 -from os import makedirs
1.13 +from os import listdir, makedirs
1.14
1.15 class Preferences(FileBase):
1.16
1.17 "A simple preferences file manager."
1.18
1.19 + # See: docs/preferences.txt
1.20 +
1.21 + known_keys = {
1.22 + "CN" : "",
1.23 + "LANG" : config.LANG,
1.24 + "TZID" : get_default_timezone(),
1.25 + "add_method_response" : config.ADD_RESPONSE_DEFAULT,
1.26 + "event_refreshing" : config.REFRESHING_DEFAULT,
1.27 + "freebusy_bundling" : config.BUNDLING_DEFAULT,
1.28 + "freebusy_messages" : config.NOTIFYING_DEFAULT,
1.29 + "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT,
1.30 + "freebusy_publishing" : config.PUBLISHING_DEFAULT,
1.31 + "freebusy_sharing" : config.SHARING_DEFAULT,
1.32 + "incoming" : config.INCOMING_DEFAULT,
1.33 + "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT,
1.34 + "participating" : config.PARTICIPATING_DEFAULT,
1.35 + "permitted_times" : None,
1.36 + }
1.37 +
1.38 def __init__(self, user, store_dir=None):
1.39 - FileBase.__init__(self, store_dir or PREFERENCES_DIR)
1.40 + FileBase.__init__(self, store_dir or config.PREFERENCES_DIR)
1.41 self.user = user
1.42
1.43 - def get(self, name, default=None):
1.44 + def get(self, name, default=None, config_default=False):
1.45
1.46 """
1.47 Return the value for 'name', with absent entries providing a default of
1.48 - None or any indicated 'default'.
1.49 + None or any indicated 'default' or, if 'config_default' is set to a true
1.50 + value, the default value from the config module.
1.51 """
1.52
1.53 try:
1.54 return self[name]
1.55 except KeyError:
1.56 - return default
1.57 + if config_default:
1.58 + return self.known_keys.get(name, default)
1.59 + else:
1.60 + return default
1.61
1.62 def get_all(self, names):
1.63
1.64 @@ -68,6 +92,30 @@
1.65 except KeyError:
1.66 return False
1.67
1.68 + def keys(self):
1.69 +
1.70 + "Return all entry names in the preferences."
1.71 +
1.72 + filename = self.get_object_in_store(self.user)
1.73 + if not filename or not isdir(filename):
1.74 + return []
1.75 +
1.76 + return listdir(filename)
1.77 +
1.78 + def items(self, all_known=False, default=None, config_default=False):
1.79 +
1.80 + """
1.81 + Return all entries in the preferences or all known entries if
1.82 + 'all_known' is set to a true value, with absent entries providing a
1.83 + default of None or any indicated 'default' or, if 'config_default' is
1.84 + set to a true value, the default value from the config module.
1.85 + """
1.86 +
1.87 + l = []
1.88 + for key in (all_known and self.known_keys or self).keys():
1.89 + l.append((key, self.get(key, default, config_default)))
1.90 + return l
1.91 +
1.92 def __getitem__(self, name):
1.93
1.94 "Return the value for 'name', raising a KeyError if absent."