summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLaurent Bachelier <laurent@bachelier.name>2012-11-23 20:47:23 +0100
committerLaurent Bachelier <laurent@bachelier.name>2012-11-23 20:47:23 +0100
commitfb152cafb447679253bfa025a8b341d021da1952 (patch)
tree5adf0dd64492401351f67bfa3905c3dd5fc17711
parentNew name (diff)
downloadmpdat-fb152cafb447679253bfa025a8b341d021da1952.tar.xz
PEP8 compliance
-rw-r--r--mpdat/__init__.py2
-rw-r--r--mpdat/cache.py12
-rw-r--r--mpdat/client.py17
-rw-r--r--mpdat/process.py13
-rwxr-xr-xpruning_example.py1
5 files changed, 18 insertions, 27 deletions
diff --git a/mpdat/__init__.py b/mpdat/__init__.py
index 35c8cb1..d948da1 100644
--- a/mpdat/__init__.py
+++ b/mpdat/__init__.py
@@ -1 +1,3 @@
from .client import MPDatClient
+
+__all__ = ["MPDatClient"]
diff --git a/mpdat/cache.py b/mpdat/cache.py
index 19e83c0..ddbcd7b 100644
--- a/mpdat/cache.py
+++ b/mpdat/cache.py
@@ -7,13 +7,14 @@ from hashlib import sha1
USER_DIR = join(expanduser('~'), '.mpdat')
+
def cache(name):
def decorator(fn):
def wrapper(*args, **kwargs):
_updated = kwargs["_updated"]
del kwargs["_updated"]
- if kwargs.has_key("_hash"):
+ if "_hash" in kwargs:
_hash = kwargs["_hash"]
del kwargs["_hash"]
else:
@@ -24,8 +25,8 @@ def cache(name):
makedirs(cache_dir)
cache_file = join(cache_dir, _hash)
- if not exists(cache_file) or \
- getmtime(cache_file) < _updated:
+ if not exists(cache_file) \
+ or getmtime(cache_file) < _updated:
data = fn(*args, **kwargs)
with open(cache_file, 'w') as handle:
pickle.dump(data, handle)
@@ -39,9 +40,11 @@ def cache(name):
return decorator
+
def uhash(*args):
return sha1(pickle.dumps(args)).hexdigest()
+
def test():
from tempfile import mkdtemp
from time import time
@@ -70,7 +73,7 @@ def test():
testval = "plop2"
assert "plop" == f1(_updated=1)
we_should_be_here = True
- assert "plop2" == f1(_updated=time()+42)
+ assert "plop2" == f1(_updated=time() + 42)
# Different cache for different parameters
we_should_be_here = True
@@ -79,4 +82,3 @@ def test():
assert "plap" == f2(_updated=1, _hash=uhash("plap"), testparam="plap")
we_should_be_here = True
assert "plip" == f2(_updated=1, _hash=uhash("plip"), testparam="plip")
-
diff --git a/mpdat/client.py b/mpdat/client.py
index 5f9aecf..ef27871 100644
--- a/mpdat/client.py
+++ b/mpdat/client.py
@@ -6,11 +6,11 @@ import random
import mpdat.process as process
from mpdat.cache import cache, uhash
+
class MPDatClient(mpd.MPDClient):
def __init__(self):
mpd.MPDClient.__init__(self)
-
def connect_from_env(self, host, port):
"""
Connect from MPD_HOST (host parameter) and MPD_PORT (port parameter)
@@ -33,7 +33,6 @@ class MPDatClient(mpd.MPDClient):
if password:
self.password(password)
-
def get_final_dirs(self, root=""):
"""
Get a list of directory containing only files.
@@ -50,10 +49,10 @@ class MPDatClient(mpd.MPDClient):
_hash = uhash(root)
return self._get_final_dirs(_updated=_updated, _hash=_hash, root=root)
-
@cache("final_dirs")
def _get_final_dirs(self, root):
final_dirs = []
+
def update_final_dirs(path=""):
items = self.lsinfo(path)
@@ -67,32 +66,26 @@ class MPDatClient(mpd.MPDClient):
update_final_dirs(root)
return final_dirs
-
def wait_for_update(self):
"""
If mpd is updating the database, block until it has finished.
"""
- while self.status().has_key("updating_db"):
+ while "updating_db" in self.status():
time.sleep(1)
-
def get_random_dir(self):
return random.choice(self.get_final_dirs())
-
def is_playlist_hungry(self, hungriness=100):
return int(self.status()["playlistlength"]) < hungriness
-
def is_playlist_empty(self):
return int(self.status()["playlistlength"]) == 0
-
def get_playlist_time(self):
pl = (process.process_song(item) for item in self.playlistinfo())
return sum([song["time"] for song in pl])
-
def get_playlist_albums(self):
pl = (process.process_song(item) for item in self.playlistinfo())
d = None
@@ -105,12 +98,10 @@ class MPDatClient(mpd.MPDClient):
songs = []
songs.append(song)
-
def get_playlist_albums_time(self):
for album in self.get_playlist_albums():
yield (sum([song["time"] for song in album]), album)
-
def _find_changing_pos(self, number, type):
pl = (process.process_song(item) for item in self.playlistinfo())
value = None
@@ -122,7 +113,6 @@ class MPDatClient(mpd.MPDClient):
if count == number:
return song["pos"]
-
def clean_but(self, number=1, type="album"):
pos = self._find_changing_pos(number, type)
@@ -132,4 +122,3 @@ class MPDatClient(mpd.MPDClient):
self.delete(pos)
except mpd.CommandError:
pass
-
diff --git a/mpdat/process.py b/mpdat/process.py
index 3bc26cb..d5b1ccc 100644
--- a/mpdat/process.py
+++ b/mpdat/process.py
@@ -4,6 +4,7 @@ from os.path import dirname
Process MPDatClient responses
"""
+
def get_files_and_dirs_from_db(items):
"""
Returns (files, directories) from a source with files and directories mixed.
@@ -11,9 +12,9 @@ def get_files_and_dirs_from_db(items):
files = []
dirs = []
for item in items:
- if item.has_key("directory"):
+ if "directory" in item:
dirs.append(item["directory"])
- elif item.has_key("file"):
+ elif "file" in item:
files.append(item["file"])
return (files, dirs)
@@ -23,13 +24,11 @@ def process_song(item):
"""
Adds a "dir" attribute to songs, change "pos" to int
"""
- if item.has_key("file"):
+ if "file" in item:
item["dir"] = dirname(item["file"])
-
- if item.has_key("pos"):
+ if "pos" in item:
item["pos"] = int(item["pos"])
-
- if item.has_key("time"):
+ if "time" in item:
item["time"] = int(item["time"])
return item
diff --git a/pruning_example.py b/pruning_example.py
index 3d61541..eb2c8c3 100755
--- a/pruning_example.py
+++ b/pruning_example.py
@@ -11,4 +11,3 @@ m.connect_from_env(mpd_host, mpd_port)
# Keep only one album in the playlist
m.clean_but(1, "album")
-