| 1 | import os |
|---|
| 2 | import sys |
|---|
| 3 | import tempfile |
|---|
| 4 | |
|---|
| 5 | # Note on path and filename encodings: |
|---|
| 6 | # |
|---|
| 7 | # In general it is very difficult to do this correctly. |
|---|
| 8 | # We may pull information from environment variables, and what encoding that is |
|---|
| 9 | # in is anyone's guess. Any information we request directly from the file |
|---|
| 10 | # system will be in filesystemencoding, and (parts of) paths that we write in |
|---|
| 11 | # this source code will be in whatever encoding the source is in. (I hereby |
|---|
| 12 | # declare this file to be UTF-8 encoded.) |
|---|
| 13 | # |
|---|
| 14 | # To make things more complicated, modern Windows filesystems use UTF-16, but |
|---|
| 15 | # the API tends to hide this from us. |
|---|
| 16 | # |
|---|
| 17 | # I tried to minimize problems by passing Unicode strings to OS functions as |
|---|
| 18 | # much as possible. Hopefully this makes the function return an Unicode string |
|---|
| 19 | # as well. If not, we get an 8-bit string in filesystemencoding, which we can |
|---|
| 20 | # happily pass to functions that operate on files and directories, so we can |
|---|
| 21 | # just leave it as is. Since these paths are meant to be internal to Gajim and |
|---|
| 22 | # not displayed to the user, Unicode is not really necessary here. |
|---|
| 23 | |
|---|
| 24 | def fse(s): |
|---|
| 25 | '''Convert from filesystem encoding if not already Unicode''' |
|---|
| 26 | return unicode(s, sys.getfilesystemencoding()) |
|---|
| 27 | |
|---|
| 28 | class ConfigPaths: |
|---|
| 29 | def __init__(self, root=None): |
|---|
| 30 | self.root = root |
|---|
| 31 | self.paths = {} |
|---|
| 32 | |
|---|
| 33 | if self.root is None: |
|---|
| 34 | if os.name == 'nt': |
|---|
| 35 | try: |
|---|
| 36 | # Documents and Settings\[User Name]\Application Data\Gajim |
|---|
| 37 | |
|---|
| 38 | # How are we supposed to know what encoding the environment |
|---|
| 39 | # variable 'appdata' is in? Assuming it to be in filesystem |
|---|
| 40 | # encoding. |
|---|
| 41 | self.root = os.path.join(fse(os.environ[u'appdata']), u'Gajim') |
|---|
| 42 | except KeyError: |
|---|
| 43 | # win9x, in cwd |
|---|
| 44 | self.root = u'.' |
|---|
| 45 | else: # Unices |
|---|
| 46 | # Pass in an Unicode string, and hopefully get one back. |
|---|
| 47 | self.root = os.path.expanduser(u'~/.gajim') |
|---|
| 48 | |
|---|
| 49 | def add_from_root(self, name, path): |
|---|
| 50 | self.paths[name] = (True, path) |
|---|
| 51 | |
|---|
| 52 | def add(self, name, path): |
|---|
| 53 | self.paths[name] = (False, path) |
|---|
| 54 | |
|---|
| 55 | def __getitem__(self, key): |
|---|
| 56 | relative, path = self.paths[key] |
|---|
| 57 | if not relative: |
|---|
| 58 | return path |
|---|
| 59 | return os.path.join(self.root, path) |
|---|
| 60 | |
|---|
| 61 | def get(self, key, default=None): |
|---|
| 62 | try: |
|---|
| 63 | return self[key] |
|---|
| 64 | except KeyError: |
|---|
| 65 | return default |
|---|
| 66 | |
|---|
| 67 | def iteritems(self): |
|---|
| 68 | for key in self.paths.iterkeys(): |
|---|
| 69 | yield (key, self[key]) |
|---|
| 70 | |
|---|
| 71 | def windowsify(s): |
|---|
| 72 | if os.name == 'nt': |
|---|
| 73 | return s.capitalize() |
|---|
| 74 | return s |
|---|
| 75 | |
|---|
| 76 | def init(): |
|---|
| 77 | paths = ConfigPaths() |
|---|
| 78 | |
|---|
| 79 | # LOG is deprecated |
|---|
| 80 | k = ( 'LOG', 'LOG_DB', 'VCARD', 'AVATAR', 'MY_EMOTS' ) |
|---|
| 81 | v = (u'logs', u'logs.db', u'vcards', u'avatars', u'emoticons') |
|---|
| 82 | |
|---|
| 83 | if os.name == 'nt': |
|---|
| 84 | v = map(lambda x: x.capitalize(), v) |
|---|
| 85 | |
|---|
| 86 | for n, p in zip(k, v): |
|---|
| 87 | paths.add_from_root(n, p) |
|---|
| 88 | |
|---|
| 89 | paths.add('DATA', os.path.join(u'..', windowsify(u'data'))) |
|---|
| 90 | paths.add('HOME', fse(os.path.expanduser('~'))) |
|---|
| 91 | paths.add('TMP', fse(tempfile.gettempdir())) |
|---|
| 92 | |
|---|
| 93 | try: |
|---|
| 94 | import svn_config |
|---|
| 95 | svn_config.configure(paths) |
|---|
| 96 | except (ImportError, AttributeError): |
|---|
| 97 | pass |
|---|
| 98 | |
|---|
| 99 | # for k, v in paths.iteritems(): |
|---|
| 100 | # print "%s: %s" % (repr(k), repr(v)) |
|---|
| 101 | |
|---|
| 102 | return paths |
|---|
| 103 | |
|---|
| 104 | gajimpaths = init() |
|---|
| 105 | |
|---|
| 106 | def init_profile(profile, paths=gajimpaths): |
|---|
| 107 | conffile = windowsify(u'config') |
|---|
| 108 | pidfile = windowsify(u'gajim') |
|---|
| 109 | |
|---|
| 110 | if len(profile) > 0: |
|---|
| 111 | conffile += u'.' + profile |
|---|
| 112 | pidfile += u'.' + profile |
|---|
| 113 | pidfile += u'.pid' |
|---|
| 114 | paths.add_from_root('CONFIG_FILE', conffile) |
|---|
| 115 | paths.add_from_root('PID_FILE', pidfile) |
|---|
| 116 | |
|---|
| 117 | # for k, v in paths.iteritems(): |
|---|
| 118 | # print "%s: %s" % (repr(k), repr(v)) |
|---|