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@1001 | 27 | import codecs |
paul@923 | 28 | import pytz |
paul@923 | 29 | |
paul@923 | 30 | def identity_dict(l): |
paul@923 | 31 | return dict([(i, i) for i in l]) |
paul@147 | 32 | |
paul@147 | 33 | class Preferences(FileBase): |
paul@147 | 34 | |
paul@147 | 35 | "A simple preferences file manager." |
paul@147 | 36 | |
paul@921 | 37 | # See: docs/preferences.txt |
paul@921 | 38 | |
paul@921 | 39 | known_keys = { |
paul@921 | 40 | "CN" : "", |
paul@921 | 41 | "LANG" : config.LANG, |
paul@921 | 42 | "TZID" : get_default_timezone(), |
paul@921 | 43 | "add_method_response" : config.ADD_RESPONSE_DEFAULT, |
paul@921 | 44 | "event_refreshing" : config.REFRESHING_DEFAULT, |
paul@921 | 45 | "freebusy_bundling" : config.BUNDLING_DEFAULT, |
paul@921 | 46 | "freebusy_messages" : config.NOTIFYING_DEFAULT, |
paul@921 | 47 | "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT, |
paul@921 | 48 | "freebusy_publishing" : config.PUBLISHING_DEFAULT, |
paul@921 | 49 | "freebusy_sharing" : config.SHARING_DEFAULT, |
paul@921 | 50 | "incoming" : config.INCOMING_DEFAULT, |
paul@921 | 51 | "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT, |
paul@921 | 52 | "participating" : config.PARTICIPATING_DEFAULT, |
paul@921 | 53 | "permitted_times" : None, |
paul@921 | 54 | } |
paul@921 | 55 | |
paul@923 | 56 | known_key_choices = { |
paul@923 | 57 | "TZID" : identity_dict(pytz.all_timezones), |
paul@923 | 58 | "add_method_response" : { |
paul@923 | 59 | "add" : "Add events", |
paul@923 | 60 | "ignore" : "Ignore requests", |
paul@923 | 61 | "refresh" : "Ask for refreshed event details" |
paul@923 | 62 | }, |
paul@923 | 63 | "event_refreshing" : { |
paul@923 | 64 | "never" : "Do not respond", |
paul@923 | 65 | "always" : "Always respond" |
paul@923 | 66 | }, |
paul@923 | 67 | "freebusy_bundling" : { |
paul@923 | 68 | "never" : "Never", |
paul@923 | 69 | "always" : "Always" |
paul@923 | 70 | }, |
paul@923 | 71 | "freebusy_messages" : { |
paul@923 | 72 | "none" : "Do not notify", |
paul@923 | 73 | "notify" : "Notify" |
paul@923 | 74 | }, |
paul@923 | 75 | "freebusy_publishing" : { |
paul@923 | 76 | "publish" : "Publish", |
paul@923 | 77 | "no" : "Do not publish" |
paul@923 | 78 | }, |
paul@923 | 79 | "freebusy_sharing" : { |
paul@923 | 80 | "share" : "Share", |
paul@923 | 81 | "no" : "Do not share" |
paul@923 | 82 | }, |
paul@923 | 83 | "incoming" : { |
paul@923 | 84 | "message-only" : "Original message only", |
paul@923 | 85 | "message-then-summary" : "Original message followed by a separate summary message", |
paul@923 | 86 | "summary-then-message" : "Summary message followed by the original message", |
paul@923 | 87 | "summary-only" : "Summary message only", |
paul@923 | 88 | "summary-wraps-message" : "Summary message wrapping the original message" |
paul@923 | 89 | }, |
paul@923 | 90 | "organiser_replacement" : { |
paul@923 | 91 | "any" : "Anyone", |
paul@923 | 92 | "attendee" : "Existing attendees only", |
paul@923 | 93 | "never" : "Never allow organiser replacement" |
paul@923 | 94 | }, |
paul@923 | 95 | "participating" : { |
paul@923 | 96 | "participate" : "Participate", |
paul@923 | 97 | "no" : "Do not participate" |
paul@923 | 98 | } |
paul@923 | 99 | } |
paul@923 | 100 | |
paul@639 | 101 | def __init__(self, user, store_dir=None): |
paul@921 | 102 | FileBase.__init__(self, store_dir or config.PREFERENCES_DIR) |
paul@147 | 103 | self.user = user |
paul@147 | 104 | |
paul@921 | 105 | def get(self, name, default=None, config_default=False): |
paul@693 | 106 | |
paul@693 | 107 | """ |
paul@693 | 108 | Return the value for 'name', with absent entries providing a default of |
paul@921 | 109 | None or any indicated 'default' or, if 'config_default' is set to a true |
paul@921 | 110 | value, the default value from the config module. |
paul@693 | 111 | """ |
paul@693 | 112 | |
paul@147 | 113 | try: |
paul@147 | 114 | return self[name] |
paul@147 | 115 | except KeyError: |
paul@921 | 116 | if config_default: |
paul@921 | 117 | return self.known_keys.get(name, default) |
paul@921 | 118 | else: |
paul@921 | 119 | return default |
paul@147 | 120 | |
paul@791 | 121 | def get_all(self, names): |
paul@791 | 122 | |
paul@791 | 123 | """ |
paul@791 | 124 | Return a dictionary containing values for entries having the given |
paul@791 | 125 | 'names'. Absent entries for names are omitted without error. |
paul@791 | 126 | """ |
paul@791 | 127 | |
paul@791 | 128 | d = {} |
paul@791 | 129 | for name in names: |
paul@791 | 130 | value = self.get(name) |
paul@791 | 131 | if value is not None: |
paul@791 | 132 | d[name] = value |
paul@791 | 133 | return d |
paul@791 | 134 | |
paul@791 | 135 | def has_key(self, name): |
paul@791 | 136 | |
paul@791 | 137 | "Return whether an entry exists for 'name'." |
paul@791 | 138 | |
paul@791 | 139 | try: |
paul@791 | 140 | self[name] |
paul@791 | 141 | return True |
paul@791 | 142 | except KeyError: |
paul@791 | 143 | return False |
paul@791 | 144 | |
paul@921 | 145 | def keys(self): |
paul@921 | 146 | |
paul@921 | 147 | "Return all entry names in the preferences." |
paul@921 | 148 | |
paul@921 | 149 | filename = self.get_object_in_store(self.user) |
paul@921 | 150 | if not filename or not isdir(filename): |
paul@921 | 151 | return [] |
paul@921 | 152 | |
paul@921 | 153 | return listdir(filename) |
paul@921 | 154 | |
paul@921 | 155 | def items(self, all_known=False, default=None, config_default=False): |
paul@921 | 156 | |
paul@921 | 157 | """ |
paul@921 | 158 | Return all entries in the preferences or all known entries if |
paul@921 | 159 | 'all_known' is set to a true value, with absent entries providing a |
paul@921 | 160 | default of None or any indicated 'default' or, if 'config_default' is |
paul@921 | 161 | set to a true value, the default value from the config module. |
paul@923 | 162 | |
paul@923 | 163 | Each entry will have the form (key, value). |
paul@921 | 164 | """ |
paul@921 | 165 | |
paul@921 | 166 | l = [] |
paul@921 | 167 | for key in (all_known and self.known_keys or self).keys(): |
paul@921 | 168 | l.append((key, self.get(key, default, config_default))) |
paul@921 | 169 | return l |
paul@921 | 170 | |
paul@923 | 171 | def choices(self, all_known=False, default=None, config_default=False): |
paul@923 | 172 | |
paul@923 | 173 | """ |
paul@923 | 174 | Return all entries in the preferences or all known entries if |
paul@923 | 175 | 'all_known' is set to a true value, with absent entries providing a |
paul@923 | 176 | default of None or any indicated 'default' or, if 'config_default' is |
paul@923 | 177 | set to a true value, the default value from the config module. |
paul@923 | 178 | |
paul@923 | 179 | Each entry will have the form (key, value, choices). |
paul@923 | 180 | """ |
paul@923 | 181 | |
paul@923 | 182 | l = [] |
paul@923 | 183 | for key, value in self.items(all_known, default, config_default): |
paul@923 | 184 | l.append((key, value, self.known_key_choices.get(key))) |
paul@923 | 185 | return l |
paul@923 | 186 | |
paul@147 | 187 | def __getitem__(self, name): |
paul@693 | 188 | |
paul@693 | 189 | "Return the value for 'name', raising a KeyError if absent." |
paul@693 | 190 | |
paul@147 | 191 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 192 | if not filename or not exists(filename): |
paul@147 | 193 | raise KeyError, name |
paul@147 | 194 | |
paul@1001 | 195 | f = codecs.open(filename, encoding="utf-8") |
paul@147 | 196 | try: |
paul@147 | 197 | return f.read().strip() |
paul@147 | 198 | finally: |
paul@147 | 199 | f.close() |
paul@147 | 200 | |
paul@147 | 201 | def __setitem__(self, name, value): |
paul@693 | 202 | |
paul@693 | 203 | "Set for 'name' the given 'value'." |
paul@693 | 204 | |
paul@147 | 205 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 206 | if not filename: |
paul@147 | 207 | return False |
paul@147 | 208 | |
paul@1001 | 209 | f = codecs.open(filename, "w", encoding="utf-8") |
paul@147 | 210 | try: |
paul@147 | 211 | f.write(value) |
paul@147 | 212 | finally: |
paul@147 | 213 | f.close() |
paul@147 | 214 | fix_permissions(filename) |
paul@147 | 215 | |
paul@147 | 216 | return True |
paul@147 | 217 | |
paul@147 | 218 | # vim: tabstop=4 expandtab shiftwidth=4 |