Commit 01e44afc authored by Hartmut Goebel's avatar Hartmut Goebel
Browse files

Cleanup: pep8ifying sources.

This changes have been applied to all python files using
https://github.com/htgoebel/pep8ify and the result has been checked.
parent 195571ec
__version_info__ = (0,0,5)
__version__ = '%d.%d.%d' % (__version_info__[0],__version_info__[1],__version_info__[2],)
__version_info__ = (0, 0, 5)
__version__ = '%d.%d.%d' % (__version_info__[0], __version_info__[1], __version_info__[2], )
__pgk_name__ = 'compere'
\ No newline at end of file
__pgk_name__ = 'compere'
......@@ -5,7 +5,8 @@
# Copyright 2007, Frank Scholz <coherence@beebits.net>
import os, random
import os
import random
import time
# Twisted
......@@ -24,17 +25,18 @@ import louie
from menus import Menu
class Cover(object):
width=260
height=260
width = 260
height = 260
def __init__(self, uri):
self.uri = uri
self.finished = False
self.image = pygame.Surface((self.width,self.height))
self.image.fill((0, 0, 0)) # black
self.image = pygame.Surface((self.width, self.height))
self.image.fill((0, 0, 0)) # black
self.rect = self.image.get_rect()
def got_error(f):
......@@ -51,16 +53,16 @@ class Cover(object):
import Image
import StringIO
im = Image.open(StringIO.StringIO(result))
self.image = pygame.image.frombuffer( im.tostring("raw","RGB"), im.size, "RGB")
self.image = pygame.image.frombuffer(im.tostring("raw", "RGB"), im.size, "RGB")
except ImportError:
import tempfile
tmp_fp, tmp_path = tempfile.mkstemp()
os.write(tmp_fp,result)
os.write(tmp_fp, result)
os.close(tmp_fp)
self.image = pygame.image.load(tmp_path).convert()
os.unlink(tmp_path)
self.image = pygame.transform.scale(self.image, (self.width,self.height))
self.image = pygame.transform.scale(self.image, (self.width, self.height))
self.rect = self.image.get_rect()
self.finished = True
louie.send('MediaRenderer.Cover.finished', None, None)
......@@ -75,14 +77,14 @@ class TrackView(object):
self.title = title
d = self.client.content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_album_browse, client)
self.image = pygame.Surface((width,height))
self.image.fill((0, 0, 0)) # black
d.addCallback(self.process_album_browse, client)
self.image = pygame.Surface((width, height))
self.image.fill((0, 0, 0)) # black
self.rect = self.image.get_rect()
self.image.blit(cover.image,(100,70))
self.image.blit(cover.image, (100, 70))
def process_album_browse( self, results, client):
for k,v in results.iteritems():
def process_album_browse(self, results, client):
for k, v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
......@@ -95,6 +97,7 @@ class TrackView(object):
print "track", id, title, artist, cover
louie.send('MediaRenderer.TrackView.ready', None, self)
class Album(object):
width = 100
......@@ -106,60 +109,62 @@ class Album(object):
self.title = title
self.artist = artist
self.cover = Cover(cover_uri)
self.image = pygame.Surface((self.width,self.height))
self.image.fill((0, 0, 0)) # black
self.image = pygame.Surface((self.width, self.height))
self.image.fill((0, 0, 0)) # black
self.rect = self.image.get_rect()
self.finished = False
self.touched = 0
print "album created", id, title, artist, cover_uri
louie.connect(self.ready,'MediaRenderer.Cover.finished',louie.Any)
louie.connect(self.ready, 'MediaRenderer.Cover.finished', louie.Any)
def ready(self, result):
self.finished = True
self.image = pygame.transform.scale(self.cover.image, (self.width,self.height))
self.image = pygame.transform.scale(self.cover.image, (self.width, self.height))
self.rect = self.image.get_rect()
louie.send('MediaRenderer.Album.cover', None, None)
class Audio(object):
album_x_offset = 100
album_y_offset = 70
def __init__(self, controller, main_rect):
self.controller = controller
self.screen = pygame.Surface((main_rect.width,main_rect.height))
self.screen = pygame.Surface((main_rect.width, main_rect.height))
self.container_watch = []
self.client = {}
self.local_store = None
self.content = []
self.view = 0 # 0 = albums
# 1 = track list
# 1 = track list
self.track_view = None
self.toggle_to = None
buttons = (('save',(44,50),None,None),
('backward',(31,140),self.proceed_to_previous_song,None),
('pause',(31,216),self.pause,None),
('play',(31,300),self.play,'stop.png'),
('forward',(44,380),self.proceed_to_next_song,None))
buttons = (('save', (44, 50), None, None),
('backward', (31, 140), self.proceed_to_previous_song, None),
('pause', (31, 216), self.pause, None),
('play', (31, 300), self.play, 'stop.png'),
('forward', (44, 380), self.proceed_to_next_song, None))
self.right_menu = Menu(controller, 'right', buttons)
louie.connect(self.media_server_found, 'Coherence.UPnP.ControlPoint.MediaServer.detected', louie.Any)
louie.connect(self.media_server_removed, 'Coherence.UPnP.ControlPoint.MediaServer.removed', louie.Any)
louie.connect(self.album_view_ready,'MediaRenderer.Album.cover',louie.Any)
louie.connect(self.track_view_ready,'MediaRenderer.TrackView.ready',louie.Any)
louie.connect(self.album_view_ready, 'MediaRenderer.Album.cover', louie.Any)
louie.connect(self.track_view_ready, 'MediaRenderer.TrackView.ready', louie.Any)
def check(self, button, pos):
if self.view == 0:
for album in self.content[:18]:
if album.rect.collidepoint(pos[0],pos[1]):
if album.rect.collidepoint(pos[0], pos[1]):
print "touched album", album.id, album.title
t = time.time()
if album.touched > 0 and t - album.touched < 0.5:
print "double click"
album.touched = 0
TrackView(album.client,album.id,album.title,album.cover,
self.screen.get_width(),self.screen.get_height())
TrackView(album.client, album.id, album.title, album.cover,
self.screen.get_width(), self.screen.get_height())
else:
album.touched = time.time()
......@@ -167,21 +172,21 @@ class Audio(object):
return
def clear_screen(self):
self.screen.fill((0,0,0))
self.screen.fill((0, 0, 0))
def track_view_ready(self,track_view):
def track_view_ready(self, track_view):
self.track_view = track_view
self.draw_track_view()
def draw_track_view(self):
#self.clear_screen()
self.screen.blit( self.track_view.image, (0,0))
self.screen.blit(self.track_view.image, (0, 0))
self.view = 1
self.toggle_to = 0
louie.send('MediaRenderer.Screen.redraw', None, self.screen)
pygame.display.set_caption('Compère - %s' % self.track_view.title)
def album_view_ready(self,r):
def album_view_ready(self, r):
for album in self.content[:18]:
if album.finished == False:
return
......@@ -195,7 +200,7 @@ class Audio(object):
album.rect[0] = self.album_x_offset + (column * album.rect[2])
album.rect[1] = self.album_y_offset + (row * album.rect[3])
self.screen.blit(album.image,album.rect)
self.screen.blit(album.image, album.rect)
column += 1
if column == 6:
row += 1
......@@ -223,7 +228,7 @@ class Audio(object):
#d = client.content_directory.browse(0, browse_flag='BrowseMetadata')
d = self.client[usn].content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, client)
d.addCallback(self.process_media_server_browse, client)
client.content_directory.subscribe_for_variable('ContainerUpdateIDs', self.state_variable_change)
client.content_directory.subscribe_for_variable('SystemUpdateID', self.state_variable_change)
......@@ -232,14 +237,14 @@ class Audio(object):
if(isinstance(self.local_store, tuple) is True and
len(self.local_store) == 2 and
usn == self.local_store[0].device.get_usn()):
self.local_store = None
self.right_menu.set_button_action('save',None)
self.local_store = None
self.right_menu.set_button_action('save', None)
if self.client.has_key(usn) == True:
del self.client[usn]
self.content = []
def process_media_server_browse( self, results, client, albums=False):
for k,v in results.iteritems():
def process_media_server_browse(self, results, client, albums=False):
for k, v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
......@@ -248,18 +253,18 @@ class Audio(object):
values['title'] == 'audio'):
d = client.content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, client, albums=True)
d.addCallback(self.process_media_server_browse, client, albums=True)
if albums == True and values['upnp_class'].startswith('object.container'):
id = values['id']
title = values.get('title', 'untitled')
artist = values.get('artist', 'unknown')
cover = values.get('album_art_uri', None)
self.content.append(Album( client, id, title, artist, cover))
self.content.append(Album(client, id, title, artist, cover))
if values['parent_id'] not in self.container_watch:
self.container_watch.append(values['parent_id'])
self.container_watch.append(values['parent_id'])
def process_local_media_server_browse( self, results, client):
for k,v in results.iteritems():
def process_local_media_server_browse(self, results, client):
for k, v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
......@@ -268,15 +273,15 @@ class Audio(object):
values['title'] == 'content'):
d = client.content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_local_media_server_browse, client)
d.addCallback(self.process_local_media_server_browse, client)
if(values['upnp_class'].startswith('object.container') and
values['title'] == 'audio'):
print "found object id", values['id'], "as storage point"
if hasattr(self,'right_menu'):
self.right_menu.set_button_action('save',self.store_locally)
self.local_store = (self.local_store,values['id'])
if hasattr(self, 'right_menu'):
self.right_menu.set_button_action('save', self.store_locally)
self.local_store = (self.local_store, values['id'])
def state_variable_change( self, variable, usn):
def state_variable_change(self, variable, usn):
print variable.name, 'changed from', variable.old_value, 'to', variable.value
if variable.old_value == '':
return
......@@ -285,7 +290,7 @@ class Audio(object):
if len(self.content) == 0:
d = self.client[usn].content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, self.client[usn])
d.addCallback(self.process_media_server_browse, self.client[usn])
if variable.name == 'ContainerUpdateIDs':
changes = variable.value.split(',')
......@@ -297,7 +302,7 @@ class Audio(object):
self.content = []
d = self.client[usn].content_directory.browse(container, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, self.client[usn], albums=True)
d.addCallback(self.process_media_server_browse, self.client[usn], albums=True)
def pause(self):
""" pause audio play """
......
......@@ -5,7 +5,8 @@
# Copyright 2007, Frank Scholz <coherence@beebits.net>
import os, random
import os
import random
# Twisted
from twisted.internet import reactor
......@@ -23,11 +24,12 @@ import louie
from menus import Menu
class Images(object):
def __init__(self, controller, main_rect):
self.controller = controller
self.screen = pygame.Surface((main_rect.width,main_rect.height))
self.screen = pygame.Surface((main_rect.width, main_rect.height))
self.images = []
self.container_watch = []
self.image_index = -1
......@@ -37,17 +39,16 @@ class Images(object):
self.client = {}
self.local_store = None
buttons = (('save',(44,50),None,None),
('backward',(31,140),self.proceed_to_previous_image,None),
('pause',(31,216),self.pause,'play.png'),
('forward',(31,300),self.proceed_to_next_image,None),
('mark-junk',(44,380),self.mark_junk,None))
buttons = (('save', (44, 50), None, None),
('backward', (31, 140), self.proceed_to_previous_image, None),
('pause', (31, 216), self.pause, 'play.png'),
('forward', (31, 300), self.proceed_to_next_image, None),
('mark-junk', (44, 380), self.mark_junk, None))
self.right_menu = Menu(controller, 'right', buttons)
self.display_loop = task.LoopingCall(self.proceed_to_next_image,skip_junk=True)
self.display_loop = task.LoopingCall(self.proceed_to_next_image, skip_junk=True)
louie.connect(self.media_server_found, 'Coherence.UPnP.ControlPoint.MediaServer.detected', louie.Any)
louie.connect(self.media_server_removed, 'Coherence.UPnP.ControlPoint.MediaServer.removed', louie.Any)
#self.log_loop = task.LoopingCall(self.print_state)
#self.log_loop.start(10)
......@@ -57,12 +58,12 @@ class Images(object):
def check(self, button, pos):
pass
def toggle_fullscreen(self,size,fullscreen,bestdepth):
def toggle_fullscreen(self, size, fullscreen, bestdepth):
print "images toggle_fullscreen", size
pygame.display.update(self.displaying)
self.screen = pygame.display.set_mode(size, fullscreen, bestdepth)
self.displaying = self.current_image.get_rect(center=((self.screen.get_width()/2)-1,(self.screen.get_height()/2)-1))
self.screen.blit(self.current_image,self.displaying, (0,0,self.displaying.width,self.displaying.height))
self.displaying = self.current_image.get_rect(center=((self.screen.get_width() / 2) - 1, (self.screen.get_height() / 2) - 1))
self.screen.blit(self.current_image, self.displaying, (0, 0, self.displaying.width, self.displaying.height))
pygame.display.update(self.displaying)
def media_server_found(self, client, usn):
......@@ -72,7 +73,7 @@ class Images(object):
#d = client.content_directory.browse(0, browse_flag='BrowseMetadata')
d = self.client[usn].content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, client)
d.addCallback(self.process_media_server_browse, client)
client.content_directory.subscribe_for_variable('ContainerUpdateIDs', self.state_variable_change)
client.content_directory.subscribe_for_variable('SystemUpdateID', self.state_variable_change)
......@@ -81,31 +82,31 @@ class Images(object):
self.local_store = client
d = client.content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_local_media_server_browse,client)
d.addCallback(self.process_local_media_server_browse, client)
def media_server_removed(self, usn):
print "media_server_removed", usn
if(isinstance(self.local_store, tuple) is True and
len(self.local_store) == 2 and
usn == self.local_store[0].device.get_usn()):
self.local_store = None
self.right_menu.set_button_action('save',None)
self.local_store = None
self.right_menu.set_button_action('save', None)
if self.client.has_key(usn) == True:
if self.display_loop.running:
self.display_loop.stop()
del self.client[usn]
self.images = []
def process_media_server_browse( self, results, client):
def process_media_server_browse(self, results, client):
print "process_media_server_browse", client
for k,v in results.iteritems():
for k, v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
if values['upnp_class'].startswith('object.container'):
d = client.content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, client)
d.addCallback(self.process_media_server_browse, client)
if values['upnp_class'].startswith('object.item.imageItem'):
#print values
if values.has_key('resources'):
......@@ -120,8 +121,8 @@ class Images(object):
if not self.display_loop.running:
self.display_loop.start(20, now=True)
def process_local_media_server_browse( self, results, client):
for k,v in results.iteritems():
def process_local_media_server_browse(self, results, client):
for k, v in results.iteritems():
#print k, v
if k == 'items':
for id, values in v.iteritems():
......@@ -130,15 +131,15 @@ class Images(object):
values['title'] == 'content'):
d = client.content_directory.browse(id, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_local_media_server_browse, client)
d.addCallback(self.process_local_media_server_browse, client)
if(values['upnp_class'].startswith('object.container') and
values['title'] == 'images'):
print "found object id", values['id'], "as storage point"
if hasattr(self,'right_menu'):
self.right_menu.set_button_action('save',self.store_locally)
self.local_store = (self.local_store,values['id'])
if hasattr(self, 'right_menu'):
self.right_menu.set_button_action('save', self.store_locally)
self.local_store = (self.local_store, values['id'])
def state_variable_change( self, variable, usn):
def state_variable_change(self, variable, usn):
print variable.name, 'changed from', variable.old_value, 'to', variable.value
if variable.old_value == '':
return
......@@ -147,7 +148,7 @@ class Images(object):
if len(self.images) == 0:
d = self.client[usn].content_directory.browse(0, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, self.client[usn])
d.addCallback(self.process_media_server_browse, self.client[usn])
if variable.name == 'ContainerUpdateIDs':
changes = variable.value.split(',')
......@@ -161,7 +162,7 @@ class Images(object):
self.images = []
d = self.client[usn].content_directory.browse(container, browse_flag='BrowseDirectChildren',
backward_compatibility=False)
d.addCallback( self.process_media_server_browse, self.client[usn])
d.addCallback(self.process_media_server_browse, self.client[usn])
def loop_start(self, x=None, now=False):
print "try loop_start", x, now, len(self.images), self.display_loop.running
......@@ -186,15 +187,16 @@ class Images(object):
len(self.local_store) == 2):
print "at", self.local_store[0].device.get_friendly_name(), "in container id", self.local_store[1]
d = self.local_store[0].content_directory.create_object(self.local_store[1],
{'title':self.images[self.image_index].get('title'),
'upnp_class':'object.item.imageItem.photo',
'parentID':self.local_store[1]})
def got_import_result(r,s,d):
{'title': self.images[self.image_index].get('title'),
'upnp_class': 'object.item.imageItem.photo',
'parentID': self.local_store[1]})
def got_import_result(r, s, d):
print "import from", s, "to", d, "was successful"
def got_result(r, source_uri):
print "store_locally got_result", r
elt = DIDLLite.DIDLElement.fromString(r.get('Result',''))
elt = DIDLLite.DIDLElement.fromString(r.get('Result', ''))
for item in elt:
for res in item.findall('res'):
destination_uri = res.get('importUri', None)
......@@ -226,7 +228,7 @@ class Images(object):
reactor.callLater(60, self.loop_start, now=False)
if len(self.images)>0:
if len(self.images) > 0:
self.loop_stop()
while True:
try:
......@@ -236,7 +238,7 @@ class Images(object):
self.image_index = 0
image = self.images[self.image_index]
print image
if skip_junk==True and image.has_key('junk'):
if skip_junk == True and image.has_key('junk'):
continue
image_url = image.get('url')
image_title = image.get('title')
......@@ -251,20 +253,20 @@ class Images(object):
def got_error(error, url):
print "proceed_to_previous_image got_error", error, url
self.loop_start(None,now=False)
self.loop_start(None, now=False)
return error
if len(self.images)>0:
if len(self.images) > 0:
self.loop_stop()
while True:
try:
self.image_index -= 1
image = self.images[self.image_index]
except IndexError:
self.image_index = len(self.images)-1
self.image_index = len(self.images) - 1
image = self.images[self.image_index]
print image
if skip_junk==True and image.has_key('junk'):
if skip_junk == True and image.has_key('junk'):
continue
image_url = image.get('url')
image_title = image.get('title')
......@@ -274,17 +276,17 @@ class Images(object):
d.addErrback(got_error, image_url)
break
def got_image(self, result, title = ''):
def got_image(self, result, title=''):
try:
import Image
import StringIO
im = Image.open(StringIO.StringIO(result))
size = im.size
self.current_image = pygame.image.frombuffer( im.tostring("raw","RGB"), im.size, "RGB")
self.current_image = pygame.image.frombuffer(im.tostring("raw", "RGB"), im.size, "RGB")
except ImportError:
import tempfile
tmp_fp, tmp_path = tempfile.mkstemp()
os.write(tmp_fp,result)
os.write(tmp_fp, result)
os.close(tmp_fp)
self.current_image = pygame.image.load(tmp_path).convert()
size = self.current_image.get_size()
......@@ -295,25 +297,25 @@ class Images(object):
screen_height = self.screen.get_height() - self.controller.height_redux
if size[0] > screen_width:
new_width = screen_width
relation = float(screen_width())/float(size[0])
relation = float(screen_width()) / float(size[0])
new_heigth = float(size[1]) * relation
new_height = int(new_height)
if new_height > screen_height:
relation = float(screen_height)/float(new_height)
relation = float(screen_height) / float(new_height)
new_width = float(new_width) * relation
new_height = self.screen.get_height()
transform =(new_width,new_height)
transform = (new_width, new_height)
elif size[1] > screen_height:
relation = float(screen_height)/float(size[1])
relation = float(screen_height) / float(size[1])
new_height = screen_height
new_width = int(float(size[0]) * relation)
transform =(new_width,new_height)
transform = (new_width, new_height)
if transform is not None:
self.current_image = pygame.transform.scale(self.current_image, transform)
if self.displaying is not None:
#print "clear old image", self.displaying
self.controller.clear_part(self.displaying)
self.displaying = self.current_image.get_rect(center=((screen_width/2),(screen_height/2)))
self.controller.redraw_part(self.current_image,self.displaying, screen=self.screen)
self.displaying = self.current_image.get_rect(center=((screen_width / 2), (screen_height / 2)))
self.controller.redraw_part(self.current_image, self.displaying, screen=self.screen)
self.current_title = title
pygame.display.set_caption('Compère - %s' % title)
......@@ -19,12 +19,13 @@ from pygame.locals import *
from resource import resource
class Menu(object):
def __init__(self, controller, location, buttons):
self.image = pygame.image.load(resource(('data','menu-frame-%s.png' % location))).convert()
self.image = pygame.image.load(resource(('data', 'menu-frame-%s.png' % location))).convert()
#sibpath(__file__, os.path.join('data','menu-frame-%s.png' % location)))
self.image.set_colorkey((0,0,0), pygame.RLEACCEL)
self.image.set_colorkey((0, 0, 0), pygame.RLEACCEL)
self.rect = self.image.get_rect()
self.x_offset = 0
if location == 'right':
......@@ -35,14 +36,14 @@ class Menu(object):
self.y_offset = 410
self.rect[1] = self.y_offset
self.select_frame = pygame.image.load(resource(('data','button-selected.png'))).convert()
self.select_frame.set_colorkey((255,255,255), pygame.RLEACCEL)
self.select_frame = pygame.image.load(resource(('data', 'button-selected.png'))).convert()
self.select_frame.set_colorkey((255, 255, 255), pygame.RLEACCEL)
self.unselect_frame = pygame.image.load(resource(('data','button-unselected.png'))).convert()
self.unselect_frame.set_colorkey((255,255,255), pygame.RLEACCEL)
self.unselect_frame = pygame.image.load(resource(('data', 'button-unselected.png'))).convert()
self.unselect_frame.set_colorkey((255, 255, 255), pygame.RLEACCEL)