]> kaliko git repositories - mpd-sima.git/blobdiff - sima/utils/utils.py
Init genre attribute in Track object
[mpd-sima.git] / sima / utils / utils.py
index 187d6dad96c25e2f01435da02a10202eac8c087d..83799e0e2555b75f94051b04aec9c912a45e226e 100644 (file)
@@ -30,10 +30,13 @@ from argparse import ArgumentError, Action
 from base64 import b64decode as push
 from codecs import getencoder
 from datetime import datetime
-from os import environ, access, getcwd, W_OK, R_OK
+from os import getenv, access, getcwd, W_OK, R_OK
 from os.path import dirname, isabs, join, normpath, exists, isdir, isfile
 from time import sleep
 
+from musicpd import VERSION as mversion
+from sima.info import __version__ as sversion
+
 
 def getws(dic):
     """
@@ -46,21 +49,33 @@ def getws(dic):
     dic.update({'apikey': aka})
 
 
+def parse_mpd_host(value):
+    passwd = host = None
+    # If password is set: MPD_HOST=pass@host
+    if '@' in value:
+        mpd_host_env = value.split('@', 1)
+        if mpd_host_env[0]:
+            # A password is actually set
+            passwd = mpd_host_env[0]
+            if mpd_host_env[1]:
+                host = mpd_host_env[1]
+        elif mpd_host_env[1]:
+            # No password set but leading @ is an abstract socket
+            host = '@'+mpd_host_env[1]
+    else:
+        # MPD_HOST is a plain host
+        host = value
+    return host, passwd
+
+
 def get_mpd_environ():
     """
     Retrieve MPD env. var.
     """
     passwd = host = None
-    mpd_host_env = environ.get('MPD_HOST')
-    if mpd_host_env:
-        # If password is set:
-        # mpd_host_env = ['pass', 'host'] because MPD_HOST=pass@host
-        mpd_host_env = mpd_host_env.split('@')
-        mpd_host_env.reverse()
-        host = mpd_host_env[0]
-        if len(mpd_host_env) > 1 and mpd_host_env[1]:
-            passwd = mpd_host_env[1]
-    return (host, environ.get('MPD_PORT', None), passwd)
+    if getenv('MPD_HOST'):
+        host, passwd = parse_mpd_host(getenv('MPD_HOST'))
+    return (host, getenv('MPD_PORT', None), passwd)
 
 
 def normalize_path(path):
@@ -76,6 +91,8 @@ def exception_log():
     log = logging.getLogger(__name__)
     log.error('Unhandled Exception!!!')
     log.error(''.join(traceback.format_exc()))
+    log.info('musicpd python module version: %s', mversion)
+    log.info('MPD_sima version: %s', sversion)
     log.info('Please report the previous message'
              ' along with some log entries right before the crash.')
     log.info('thanks for your help :)')
@@ -83,10 +100,6 @@ def exception_log():
     sys.exit(1)
 
 
-class SigHup(Exception):
-    """SIGHUP raises this Exception"""
-
-
 # ArgParse Callbacks
 class Obsolete(Action):
     # pylint: disable=R0903
@@ -110,7 +123,6 @@ class FileAction(Action):
     def checks(self):
         """control method
         """
-        pass
 
 
 class Wfile(FileAction):
@@ -121,7 +133,6 @@ class Wfile(FileAction):
         if isdir(self._file):
             self.parser.error('need a file not a directory: {}'.format(self._file))
         if not exists(self._dir):
-            #raise ArgumentError(self, '"{0}" does not exist'.format(self._dir))
             self.parser.error('directory does not exist: {0}'.format(self._dir))
         if not exists(self._file):
             # Is parent directory writable then
@@ -178,6 +189,10 @@ class MPDSimaException(Exception):
     """Generic MPD_sima Exception"""
 
 
+class SigHup(MPDSimaException):
+    """SIGHUP raises this Exception"""
+
+
 # http client exceptions (for webservices)
 class WSError(MPDSimaException):
     pass