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@147 | 22 | from imiptools.config import PREFERENCES_DIR |
paul@147 | 23 | from imiptools.filesys import fix_permissions, FileBase |
paul@147 | 24 | from os.path import exists, isdir |
paul@147 | 25 | from os import makedirs |
paul@147 | 26 | |
paul@147 | 27 | class Preferences(FileBase): |
paul@147 | 28 | |
paul@147 | 29 | "A simple preferences file manager." |
paul@147 | 30 | |
paul@639 | 31 | def __init__(self, user, store_dir=None): |
paul@639 | 32 | FileBase.__init__(self, store_dir or PREFERENCES_DIR) |
paul@147 | 33 | self.user = user |
paul@147 | 34 | |
paul@147 | 35 | def get(self, name, default=None): |
paul@693 | 36 | |
paul@693 | 37 | """ |
paul@693 | 38 | Return the value for 'name', with absent entries providing a default of |
paul@693 | 39 | None or any indicated 'default'. |
paul@693 | 40 | """ |
paul@693 | 41 | |
paul@147 | 42 | try: |
paul@147 | 43 | return self[name] |
paul@147 | 44 | except KeyError: |
paul@147 | 45 | return default |
paul@147 | 46 | |
paul@791 | 47 | def get_all(self, names): |
paul@791 | 48 | |
paul@791 | 49 | """ |
paul@791 | 50 | Return a dictionary containing values for entries having the given |
paul@791 | 51 | 'names'. Absent entries for names are omitted without error. |
paul@791 | 52 | """ |
paul@791 | 53 | |
paul@791 | 54 | d = {} |
paul@791 | 55 | for name in names: |
paul@791 | 56 | value = self.get(name) |
paul@791 | 57 | if value is not None: |
paul@791 | 58 | d[name] = value |
paul@791 | 59 | return d |
paul@791 | 60 | |
paul@791 | 61 | def has_key(self, name): |
paul@791 | 62 | |
paul@791 | 63 | "Return whether an entry exists for 'name'." |
paul@791 | 64 | |
paul@791 | 65 | try: |
paul@791 | 66 | self[name] |
paul@791 | 67 | return True |
paul@791 | 68 | except KeyError: |
paul@791 | 69 | return False |
paul@791 | 70 | |
paul@147 | 71 | def __getitem__(self, name): |
paul@693 | 72 | |
paul@693 | 73 | "Return the value for 'name', raising a KeyError if absent." |
paul@693 | 74 | |
paul@147 | 75 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 76 | if not filename or not exists(filename): |
paul@147 | 77 | raise KeyError, name |
paul@147 | 78 | |
paul@147 | 79 | f = open(filename) |
paul@147 | 80 | try: |
paul@147 | 81 | return f.read().strip() |
paul@147 | 82 | finally: |
paul@147 | 83 | f.close() |
paul@147 | 84 | |
paul@147 | 85 | def __setitem__(self, name, value): |
paul@693 | 86 | |
paul@693 | 87 | "Set for 'name' the given 'value'." |
paul@693 | 88 | |
paul@147 | 89 | filename = self.get_object_in_store(self.user, name) |
paul@147 | 90 | if not filename: |
paul@147 | 91 | return False |
paul@147 | 92 | |
paul@147 | 93 | f = open(filename, "w") |
paul@147 | 94 | try: |
paul@147 | 95 | f.write(value) |
paul@147 | 96 | finally: |
paul@147 | 97 | f.close() |
paul@147 | 98 | fix_permissions(filename) |
paul@147 | 99 | |
paul@147 | 100 | return True |
paul@147 | 101 | |
paul@147 | 102 | # vim: tabstop=4 expandtab shiftwidth=4 |