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@147 | 27 | |
paul@147 | 28 | class Preferences(FileBase): |
paul@147 | 29 | |
paul@147 | 30 | "A simple preferences file manager." |
paul@147 | 31 | |
paul@921 | 32 | # See: docs/preferences.txt |
paul@921 | 33 | |
paul@921 | 34 | known_keys = { |
paul@921 | 35 | "CN" : "", |
paul@921 | 36 | "LANG" : config.LANG, |
paul@921 | 37 | "TZID" : get_default_timezone(), |
paul@921 | 38 | "add_method_response" : config.ADD_RESPONSE_DEFAULT, |
paul@921 | 39 | "event_refreshing" : config.REFRESHING_DEFAULT, |
paul@921 | 40 | "freebusy_bundling" : config.BUNDLING_DEFAULT, |
paul@921 | 41 | "freebusy_messages" : config.NOTIFYING_DEFAULT, |
paul@921 | 42 | "freebusy_offers" : config.FREEBUSY_OFFER_DEFAULT, |
paul@921 | 43 | "freebusy_publishing" : config.PUBLISHING_DEFAULT, |
paul@921 | 44 | "freebusy_sharing" : config.SHARING_DEFAULT, |
paul@921 | 45 | "incoming" : config.INCOMING_DEFAULT, |
paul@921 | 46 | "organiser_replacement" : config.ORGANISER_REPLACEMENT_DEFAULT, |
paul@921 | 47 | "participating" : config.PARTICIPATING_DEFAULT, |
paul@921 | 48 | "permitted_times" : None, |
paul@921 | 49 | } |
paul@921 | 50 | |
paul@639 | 51 | def __init__(self, user, store_dir=None): |
paul@921 | 52 | FileBase.__init__(self, store_dir or config.PREFERENCES_DIR) |
paul@147 | 53 | self.user = user |
paul@147 | 54 | |
paul@921 | 55 | def get(self, name, default=None, config_default=False): |
paul@693 | 56 | |
paul@693 | 57 | """ |
paul@693 | 58 | Return the value for 'name', with absent entries providing a default of |
paul@921 | 59 | None or any indicated 'default' or, if 'config_default' is set to a true |
paul@921 | 60 | value, the default value from the config module. |
paul@693 | 61 | """ |
paul@693 | 62 | |
paul@147 | 63 | try: |
paul@147 | 64 | return self[name] |
paul@147 | 65 | except KeyError: |
paul@921 | 66 | if config_default: |
paul@921 | 67 | return self.known_keys.get(name, default) |
paul@921 | 68 | else: |
paul@921 | 69 | return default |
paul@147 | 70 | |
paul@791 | 71 | def get_all(self, names): |
paul@791 | 72 | |
paul@791 | 73 | """ |
paul@791 | 74 | Return a dictionary containing values for entries having the given |
paul@791 | 75 | 'names'. Absent entries for names are omitted without error. |
paul@791 | 76 | """ |
paul@791 | 77 | |
paul@791 | 78 | d = {} |
paul@791 | 79 | for name in names: |
paul@791 | 80 | value = self.get(name) |
paul@791 | 81 | if value is not None: |
paul@791 | 82 | d[name] = value |
paul@791 | 83 | return d |
paul@791 | 84 | |
paul@791 | 85 | def has_key(self, name): |
paul@791 | 86 | |
paul@791 | 87 | "Return whether an entry exists for 'name'." |
paul@791 | 88 | |
paul@791 | 89 | try: |
paul@791 | 90 | self[name] |
paul@791 | 91 | return True |
paul@791 | 92 | except KeyError: |
paul@791 | 93 | return False |
paul@791 | 94 | |
paul@921 | 95 | def keys(self): |
paul@921 | 96 | |
paul@921 | 97 | "Return all entry names in the preferences." |
paul@921 | 98 | |
paul@921 | 99 | filename = self.get_object_in_store(self.user) |
paul@921 | 100 | if not filename or not isdir(filename): |
paul@921 | 101 | return [] |
paul@921 | 102 | |
paul@921 | 103 | return listdir(filename) |
paul@921 | 104 | |
paul@921 | 105 | def items(self, all_known=False, default=None, config_default=False): |
paul@921 | 106 | |
paul@921 | 107 | """ |
paul@921 | 108 | Return all entries in the preferences or all known entries if |
paul@921 | 109 | 'all_known' is set to a true value, with absent entries providing a |
paul@921 | 110 | default of None or any indicated 'default' or, if 'config_default' is |
paul@921 | 111 | set to a true value, the default value from the config module. |
paul@921 | 112 | """ |
paul@921 | 113 | |
paul@921 | 114 | l = [] |
paul@921 | 115 | for key in (all_known and self.known_keys or self).keys(): |
paul@921 | 116 | l.append((key, self.get(key, default, config_default))) |
paul@921 | 117 | return l |
paul@921 | 118 | |
paul@147 | 119 | def __getitem__(self, name): |
paul@693 | 120 | |
paul@693 | 121 | "Return the value for 'name', raising a KeyError if absent." |
paul@693 | 122 | |
paul@147 | 123 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 124 | if not filename or not exists(filename): |
paul@147 | 125 | raise KeyError, name |
paul@147 | 126 | |
paul@147 | 127 | f = open(filename) |
paul@147 | 128 | try: |
paul@147 | 129 | return f.read().strip() |
paul@147 | 130 | finally: |
paul@147 | 131 | f.close() |
paul@147 | 132 | |
paul@147 | 133 | def __setitem__(self, name, value): |
paul@693 | 134 | |
paul@693 | 135 | "Set for 'name' the given 'value'." |
paul@693 | 136 | |
paul@147 | 137 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 138 | if not filename: |
paul@147 | 139 | return False |
paul@147 | 140 | |
paul@147 | 141 | f = open(filename, "w") |
paul@147 | 142 | try: |
paul@147 | 143 | f.write(value) |
paul@147 | 144 | finally: |
paul@147 | 145 | f.close() |
paul@147 | 146 | fix_permissions(filename) |
paul@147 | 147 | |
paul@147 | 148 | return True |
paul@147 | 149 | |
paul@147 | 150 | # vim: tabstop=4 expandtab shiftwidth=4 |