# HG changeset patch # User Paul Boddie # Date 1445729096 -7200 # Node ID 9e88e5e276aa38b0e656cc6a0301a0c37990b83b # Parent 8b4665be51eada3eb50469c003b96ae3018eda5d Added support for obtaining collections of preferences including all known preferences, and for obtaining the configuration default for each setting if indicated. Made a language configuration default. diff -r 8b4665be51ea -r 9e88e5e276aa imiptools/config.py --- a/imiptools/config.py Sat Oct 24 22:19:17 2015 +0200 +++ b/imiptools/config.py Sun Oct 25 01:24:56 2015 +0200 @@ -61,6 +61,10 @@ # allowing users to choose more appropriate settings themselves. # See: docs/preferences.txt +# Default language for messages. + +LANG = "en" + # Do users participate in the calendar system by default? PARTICIPATING_DEFAULT = "participate" diff -r 8b4665be51ea -r 9e88e5e276aa imiptools/profile.py --- a/imiptools/profile.py Sat Oct 24 22:19:17 2015 +0200 +++ b/imiptools/profile.py Sun Oct 25 01:24:56 2015 +0200 @@ -19,30 +19,54 @@ this program. If not, see . """ -from imiptools.config import PREFERENCES_DIR +from imiptools import config +from imiptools.dates import get_default_timezone from imiptools.filesys import fix_permissions, FileBase from os.path import exists, isdir -from os import makedirs +from os import listdir, makedirs class Preferences(FileBase): "A simple preferences file manager." + # See: docs/preferences.txt + + known_keys = { + "CN" : "", + "LANG" : config.LANG, + "TZID" : get_default_timezone(), + "add_method_response" : config.ADD_RESPONSE_DEFAULT, + "event_refreshing" : config.REFRESHING_DEFAULT, + "freebusy_bundling" : config.BUNDLING_DEFAULT, + "freebusy_messages" : config.NOTIFYING_DEFAULT, + "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT, + "freebusy_publishing" : config.PUBLISHING_DEFAULT, + "freebusy_sharing" : config.SHARING_DEFAULT, + "incoming" : config.INCOMING_DEFAULT, + "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT, + "participating" : config.PARTICIPATING_DEFAULT, + "permitted_times" : None, + } + def __init__(self, user, store_dir=None): - FileBase.__init__(self, store_dir or PREFERENCES_DIR) + FileBase.__init__(self, store_dir or config.PREFERENCES_DIR) self.user = user - def get(self, name, default=None): + def get(self, name, default=None, config_default=False): """ Return the value for 'name', with absent entries providing a default of - None or any indicated 'default'. + None or any indicated 'default' or, if 'config_default' is set to a true + value, the default value from the config module. """ try: return self[name] except KeyError: - return default + if config_default: + return self.known_keys.get(name, default) + else: + return default def get_all(self, names): @@ -68,6 +92,30 @@ except KeyError: return False + def keys(self): + + "Return all entry names in the preferences." + + filename = self.get_object_in_store(self.user) + if not filename or not isdir(filename): + return [] + + return listdir(filename) + + def items(self, all_known=False, default=None, config_default=False): + + """ + Return all entries in the preferences or all known entries if + 'all_known' is set to a true value, with absent entries providing a + default of None or any indicated 'default' or, if 'config_default' is + set to a true value, the default value from the config module. + """ + + l = [] + for key in (all_known and self.known_keys or self).keys(): + l.append((key, self.get(key, default, config_default))) + return l + def __getitem__(self, name): "Return the value for 'name', raising a KeyError if absent." diff -r 8b4665be51ea -r 9e88e5e276aa imipweb/resource.py --- a/imipweb/resource.py Sat Oct 24 22:19:17 2015 +0200 +++ b/imipweb/resource.py Sun Oct 25 01:24:56 2015 +0200 @@ -184,7 +184,7 @@ def get_user_locale(self): if not self.locale: - self.locale = self.get_preferences().get("LANG", "en") + self.locale = self.get_preferences().get("LANG", "en", True) return self.locale # Prettyprinting of dates and times.