1 #!/usr/bin/env python 2 3 """ 4 User profile management. 5 6 Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 from imiptools import config 23 from imiptools.dates import get_default_timezone 24 from imiptools.filesys import fix_permissions, FileBase 25 from os.path import exists, isdir 26 from os import listdir, makedirs 27 28 class Preferences(FileBase): 29 30 "A simple preferences file manager." 31 32 # See: docs/preferences.txt 33 34 known_keys = { 35 "CN" : "", 36 "LANG" : config.LANG, 37 "TZID" : get_default_timezone(), 38 "add_method_response" : config.ADD_RESPONSE_DEFAULT, 39 "event_refreshing" : config.REFRESHING_DEFAULT, 40 "freebusy_bundling" : config.BUNDLING_DEFAULT, 41 "freebusy_messages" : config.NOTIFYING_DEFAULT, 42 "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT, 43 "freebusy_publishing" : config.PUBLISHING_DEFAULT, 44 "freebusy_sharing" : config.SHARING_DEFAULT, 45 "incoming" : config.INCOMING_DEFAULT, 46 "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT, 47 "participating" : config.PARTICIPATING_DEFAULT, 48 "permitted_times" : None, 49 } 50 51 def __init__(self, user, store_dir=None): 52 FileBase.__init__(self, store_dir or config.PREFERENCES_DIR) 53 self.user = user 54 55 def get(self, name, default=None, config_default=False): 56 57 """ 58 Return the value for 'name', with absent entries providing a default of 59 None or any indicated 'default' or, if 'config_default' is set to a true 60 value, the default value from the config module. 61 """ 62 63 try: 64 return self[name] 65 except KeyError: 66 if config_default: 67 return self.known_keys.get(name, default) 68 else: 69 return default 70 71 def get_all(self, names): 72 73 """ 74 Return a dictionary containing values for entries having the given 75 'names'. Absent entries for names are omitted without error. 76 """ 77 78 d = {} 79 for name in names: 80 value = self.get(name) 81 if value is not None: 82 d[name] = value 83 return d 84 85 def has_key(self, name): 86 87 "Return whether an entry exists for 'name'." 88 89 try: 90 self[name] 91 return True 92 except KeyError: 93 return False 94 95 def keys(self): 96 97 "Return all entry names in the preferences." 98 99 filename = self.get_object_in_store(self.user) 100 if not filename or not isdir(filename): 101 return [] 102 103 return listdir(filename) 104 105 def items(self, all_known=False, default=None, config_default=False): 106 107 """ 108 Return all entries in the preferences or all known entries if 109 'all_known' is set to a true value, with absent entries providing a 110 default of None or any indicated 'default' or, if 'config_default' is 111 set to a true value, the default value from the config module. 112 """ 113 114 l = [] 115 for key in (all_known and self.known_keys or self).keys(): 116 l.append((key, self.get(key, default, config_default))) 117 return l 118 119 def __getitem__(self, name): 120 121 "Return the value for 'name', raising a KeyError if absent." 122 123 filename = self.get_object_in_store(self.user, name) 124 if not filename or not exists(filename): 125 raise KeyError, name 126 127 f = open(filename) 128 try: 129 return f.read().strip() 130 finally: 131 f.close() 132 133 def __setitem__(self, name, value): 134 135 "Set for 'name' the given 'value'." 136 137 filename = self.get_object_in_store(self.user, name) 138 if not filename: 139 return False 140 141 f = open(filename, "w") 142 try: 143 f.write(value) 144 finally: 145 f.close() 146 fix_permissions(filename) 147 148 return True 149 150 # vim: tabstop=4 expandtab shiftwidth=4