import locale as _locale
import logging as _logging
import os.path as _os_path
+import os as _os
import pickle as _pickle
import sys as _sys
import time as _time
import ldap as _ldap
import ldap.sasl as _ldap_sasl
+_xdg_import_error = None
+try:
+ import xdg.BaseDirectory as _xdg_basedirectory
+except ImportError as _xdg_import_error:
+ _xdg_basedirectory = None
+
__version__ = '0.1'
class Config (_configparser.SafeConfigParser):
def load(self):
- read_configfiles = self.read(_os_path.expanduser('~/.mutt-ldap.rc'))
+ config_paths = self._get_config_paths()
+ LOG.info(u'load configuration from {0}'.format(config_paths))
+ read_config_paths = self.read(config_paths)
self._setup_defaults()
- LOG.info(u'loaded configuration from {0}'.format(read_configfiles))
+ LOG.info(u'loaded configuration from {0}'.format(read_config_paths))
def get_connection_class(self):
if self.getboolean('cache', 'enable'):
_sys.argv = [unicode(arg, argv_encoding) for arg in _sys.argv]
def _setup_cache_defaults(self):
+ if not self.get('cache', 'path'):
+ self.set('cache', 'path', self._get_cache_path())
if not self.get('cache', 'fields'):
# setup a reasonable default
fields = ['mail', 'cn', 'displayName'] # used by format_entry()
fields.append(optional_column)
self.set('cache', 'fields', ' '.join(fields))
+ def _get_config_paths(self):
+ "Get configuration file paths"
+ if _xdg_basedirectory:
+ paths = list(reversed(list(
+ _xdg_basedirectory.load_config_paths(''))))
+ if not paths: # setup something for a useful log message
+ paths.append(_xdg_basedirectory.save_config_path(''))
+ else:
+ self._log_xdg_import_error()
+ paths = [_os_path.expanduser(_os_path.join('~', '.config'))]
+ return [_os_path.join(path, 'mutt-ldap.cfg') for path in paths]
+
+ def _get_cache_path(self):
+ "Get the cache file path"
+
+ # Some versions of pyxdg don't have save_cache_path (0.20 and older)
+ # See: https://bugs.freedesktop.org/show_bug.cgi?id=26458
+ if _xdg_basedirectory and 'save_cache_path' in dir(_xdg_basedirectory):
+ path = _xdg_basedirectory.save_cache_path('')
+ else:
+ self._log_xdg_import_error()
+ path = _os_path.expanduser(_os_path.join('~', '.cache'))
+ if not _os_path.isdir(path):
+ _os.makedirs(path)
+ return _os_path.join(path, 'mutt-ldap.json')
+
+ def _log_xdg_import_error(self):
+ global _xdg_import_error
+ if _xdg_import_error:
+ LOG.warning(u'could not import xdg.BaseDirectory '
+ u'or lacking necessary support')
+ LOG.warning(_xdg_import_error)
+ _xdg_import_error = None
+
CONFIG = Config()
CONFIG.add_section('connection')
CONFIG.set('results', 'optional-column', '') # mutt can display one optional column
CONFIG.add_section('cache')
CONFIG.set('cache', 'enable', 'yes') # enable caching by default
-CONFIG.set('cache', 'path', '~/.mutt-ldap.cache') # cache results here
+CONFIG.set('cache', 'path', '') # cache results here, defaults to XDG
CONFIG.set('cache', 'fields', '') # fields to cache (if empty, setup in the main block)
-CONFIG.set('cache', 'longevity-days', '14') # TODO: cache results for 14 days by default
+CONFIG.set('cache', 'longevity-days', '14') # Days before cache entries are invalidated
CONFIG.add_section('system')
# HACK: Python 2.x support, see http://bugs.python.org/issue13329#msg147475
CONFIG.set('system', 'output-encoding', '') # match .muttrc's $charset