paul@147 | 1 | #!/usr/bin/env python |
paul@147 | 2 | |
paul@147 | 3 | """ |
paul@147 | 4 | User profile management. |
paul@147 | 5 | |
paul@147 | 6 | Copyright (C) 2015 Paul Boddie <paul@boddie.org.uk> |
paul@147 | 7 | |
paul@147 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@147 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@147 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@147 | 11 | version. |
paul@147 | 12 | |
paul@147 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@147 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@147 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@147 | 16 | details. |
paul@147 | 17 | |
paul@147 | 18 | You should have received a copy of the GNU General Public License along with |
paul@147 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@147 | 20 | """ |
paul@147 | 21 | |
paul@921 | 22 | from imiptools import config |
paul@921 | 23 | from imiptools.dates import get_default_timezone |
paul@147 | 24 | from imiptools.filesys import fix_permissions, FileBase |
paul@147 | 25 | from os.path import exists, isdir |
paul@921 | 26 | from os import listdir, makedirs |
paul@923 | 27 | import pytz |
paul@923 | 28 | |
paul@923 | 29 | def identity_dict(l): |
paul@923 | 30 | return dict([(i, i) for i in l]) |
paul@147 | 31 | |
paul@147 | 32 | class Preferences(FileBase): |
paul@147 | 33 | |
paul@147 | 34 | "A simple preferences file manager." |
paul@147 | 35 | |
paul@921 | 36 | # See: docs/preferences.txt |
paul@921 | 37 | |
paul@921 | 38 | known_keys = { |
paul@921 | 39 | "CN" : "", |
paul@921 | 40 | "LANG" : config.LANG, |
paul@921 | 41 | "TZID" : get_default_timezone(), |
paul@921 | 42 | "add_method_response" : config.ADD_RESPONSE_DEFAULT, |
paul@921 | 43 | "event_refreshing" : config.REFRESHING_DEFAULT, |
paul@921 | 44 | "freebusy_bundling" : config.BUNDLING_DEFAULT, |
paul@921 | 45 | "freebusy_messages" : config.NOTIFYING_DEFAULT, |
paul@921 | 46 | "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT, |
paul@921 | 47 | "freebusy_publishing" : config.PUBLISHING_DEFAULT, |
paul@921 | 48 | "freebusy_sharing" : config.SHARING_DEFAULT, |
paul@921 | 49 | "incoming" : config.INCOMING_DEFAULT, |
paul@921 | 50 | "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT, |
paul@921 | 51 | "participating" : config.PARTICIPATING_DEFAULT, |
paul@921 | 52 | "permitted_times" : None, |
paul@921 | 53 | } |
paul@921 | 54 | |
paul@923 | 55 | known_key_choices = { |
paul@923 | 56 | "TZID" : identity_dict(pytz.all_timezones), |
paul@923 | 57 | "add_method_response" : { |
paul@923 | 58 | "add" : "Add events", |
paul@923 | 59 | "ignore" : "Ignore requests", |
paul@923 | 60 | "refresh" : "Ask for refreshed event details" |
paul@923 | 61 | }, |
paul@923 | 62 | "event_refreshing" : { |
paul@923 | 63 | "never" : "Do not respond", |
paul@923 | 64 | "always" : "Always respond" |
paul@923 | 65 | }, |
paul@923 | 66 | "freebusy_bundling" : { |
paul@923 | 67 | "never" : "Never", |
paul@923 | 68 | "always" : "Always" |
paul@923 | 69 | }, |
paul@923 | 70 | "freebusy_messages" : { |
paul@923 | 71 | "none" : "Do not notify", |
paul@923 | 72 | "notify" : "Notify" |
paul@923 | 73 | }, |
paul@923 | 74 | "freebusy_publishing" : { |
paul@923 | 75 | "publish" : "Publish", |
paul@923 | 76 | "no" : "Do not publish" |
paul@923 | 77 | }, |
paul@923 | 78 | "freebusy_sharing" : { |
paul@923 | 79 | "share" : "Share", |
paul@923 | 80 | "no" : "Do not share" |
paul@923 | 81 | }, |
paul@923 | 82 | "incoming" : { |
paul@923 | 83 | "message-only" : "Original message only", |
paul@923 | 84 | "message-then-summary" : "Original message followed by a separate summary message", |
paul@923 | 85 | "summary-then-message" : "Summary message followed by the original message", |
paul@923 | 86 | "summary-only" : "Summary message only", |
paul@923 | 87 | "summary-wraps-message" : "Summary message wrapping the original message" |
paul@923 | 88 | }, |
paul@923 | 89 | "organiser_replacement" : { |
paul@923 | 90 | "any" : "Anyone", |
paul@923 | 91 | "attendee" : "Existing attendees only", |
paul@923 | 92 | "never" : "Never allow organiser replacement" |
paul@923 | 93 | }, |
paul@923 | 94 | "participating" : { |
paul@923 | 95 | "participate" : "Participate", |
paul@923 | 96 | "no" : "Do not participate" |
paul@923 | 97 | } |
paul@923 | 98 | } |
paul@923 | 99 | |
paul@639 | 100 | def __init__(self, user, store_dir=None): |
paul@921 | 101 | FileBase.__init__(self, store_dir or config.PREFERENCES_DIR) |
paul@147 | 102 | self.user = user |
paul@147 | 103 | |
paul@921 | 104 | def get(self, name, default=None, config_default=False): |
paul@693 | 105 | |
paul@693 | 106 | """ |
paul@693 | 107 | Return the value for 'name', with absent entries providing a default of |
paul@921 | 108 | None or any indicated 'default' or, if 'config_default' is set to a true |
paul@921 | 109 | value, the default value from the config module. |
paul@693 | 110 | """ |
paul@693 | 111 | |
paul@147 | 112 | try: |
paul@147 | 113 | return self[name] |
paul@147 | 114 | except KeyError: |
paul@921 | 115 | if config_default: |
paul@921 | 116 | return self.known_keys.get(name, default) |
paul@921 | 117 | else: |
paul@921 | 118 | return default |
paul@147 | 119 | |
paul@791 | 120 | def get_all(self, names): |
paul@791 | 121 | |
paul@791 | 122 | """ |
paul@791 | 123 | Return a dictionary containing values for entries having the given |
paul@791 | 124 | 'names'. Absent entries for names are omitted without error. |
paul@791 | 125 | """ |
paul@791 | 126 | |
paul@791 | 127 | d = {} |
paul@791 | 128 | for name in names: |
paul@791 | 129 | value = self.get(name) |
paul@791 | 130 | if value is not None: |
paul@791 | 131 | d[name] = value |
paul@791 | 132 | return d |
paul@791 | 133 | |
paul@791 | 134 | def has_key(self, name): |
paul@791 | 135 | |
paul@791 | 136 | "Return whether an entry exists for 'name'." |
paul@791 | 137 | |
paul@791 | 138 | try: |
paul@791 | 139 | self[name] |
paul@791 | 140 | return True |
paul@791 | 141 | except KeyError: |
paul@791 | 142 | return False |
paul@791 | 143 | |
paul@921 | 144 | def keys(self): |
paul@921 | 145 | |
paul@921 | 146 | "Return all entry names in the preferences." |
paul@921 | 147 | |
paul@921 | 148 | filename = self.get_object_in_store(self.user) |
paul@921 | 149 | if not filename or not isdir(filename): |
paul@921 | 150 | return [] |
paul@921 | 151 | |
paul@921 | 152 | return listdir(filename) |
paul@921 | 153 | |
paul@921 | 154 | def items(self, all_known=False, default=None, config_default=False): |
paul@921 | 155 | |
paul@921 | 156 | """ |
paul@921 | 157 | Return all entries in the preferences or all known entries if |
paul@921 | 158 | 'all_known' is set to a true value, with absent entries providing a |
paul@921 | 159 | default of None or any indicated 'default' or, if 'config_default' is |
paul@921 | 160 | set to a true value, the default value from the config module. |
paul@923 | 161 | |
paul@923 | 162 | Each entry will have the form (key, value). |
paul@921 | 163 | """ |
paul@921 | 164 | |
paul@921 | 165 | l = [] |
paul@921 | 166 | for key in (all_known and self.known_keys or self).keys(): |
paul@921 | 167 | l.append((key, self.get(key, default, config_default))) |
paul@921 | 168 | return l |
paul@921 | 169 | |
paul@923 | 170 | def choices(self, all_known=False, default=None, config_default=False): |
paul@923 | 171 | |
paul@923 | 172 | """ |
paul@923 | 173 | Return all entries in the preferences or all known entries if |
paul@923 | 174 | 'all_known' is set to a true value, with absent entries providing a |
paul@923 | 175 | default of None or any indicated 'default' or, if 'config_default' is |
paul@923 | 176 | set to a true value, the default value from the config module. |
paul@923 | 177 | |
paul@923 | 178 | Each entry will have the form (key, value, choices). |
paul@923 | 179 | """ |
paul@923 | 180 | |
paul@923 | 181 | l = [] |
paul@923 | 182 | for key, value in self.items(all_known, default, config_default): |
paul@923 | 183 | l.append((key, value, self.known_key_choices.get(key))) |
paul@923 | 184 | return l |
paul@923 | 185 | |
paul@147 | 186 | def __getitem__(self, name): |
paul@693 | 187 | |
paul@693 | 188 | "Return the value for 'name', raising a KeyError if absent." |
paul@693 | 189 | |
paul@147 | 190 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 191 | if not filename or not exists(filename): |
paul@147 | 192 | raise KeyError, name |
paul@147 | 193 | |
paul@147 | 194 | f = open(filename) |
paul@147 | 195 | try: |
paul@147 | 196 | return f.read().strip() |
paul@147 | 197 | finally: |
paul@147 | 198 | f.close() |
paul@147 | 199 | |
paul@147 | 200 | def __setitem__(self, name, value): |
paul@693 | 201 | |
paul@693 | 202 | "Set for 'name' the given 'value'." |
paul@693 | 203 | |
paul@147 | 204 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 205 | if not filename: |
paul@147 | 206 | return False |
paul@147 | 207 | |
paul@147 | 208 | f = open(filename, "w") |
paul@147 | 209 | try: |
paul@147 | 210 | f.write(value) |
paul@147 | 211 | finally: |
paul@147 | 212 | f.close() |
paul@147 | 213 | fix_permissions(filename) |
paul@147 | 214 | |
paul@147 | 215 | return True |
paul@147 | 216 | |
paul@147 | 217 | # vim: tabstop=4 expandtab shiftwidth=4 |