Browse Source

Move some classes to sdlib

satoshi yoneda 6 years ago
parent
commit
320b12a22b
2 changed files with 159 additions and 78 deletions
  1. 150 0
      tree/usr/local/bin/sdlib.py
  2. 9 78
      tree/usr/local/bin/superdac.py

+ 150 - 0
tree/usr/local/bin/sdlib.py

@@ -0,0 +1,150 @@
+# stdlibs
+import os
+import queue
+import threading
+from threading import BoundedSemaphore
+from threading import Semaphore
+from time import sleep
+
+# SMB
+from urllib.parse import urlparse
+from pathlib import PurePath
+import platform
+from smb.SMBConnection import SMBConnection
+from nmb.NetBIOS import NetBIOS
+
+
+# simpleSMB
+class simpleSMB():
+	
+	__hostname = ''
+	__remoteip = ''
+	__sharename = ''
+	__path = '/'
+	__enable = True
+	
+	def __init__(self, uri):
+		p = urlparse(uri)
+		if p.scheme != 'smb':
+			self.__enable = False
+			return
+		
+		self.__hostname = p.hostname
+		path = PurePath(p.path)
+		pl = list(path.parts)
+		self.__sharename = pl.pop(1)
+		self.__path = PurePath(pl.pop(0))
+		for r in pl:
+			self.__path /= PurePath(r)
+		try:
+			nmb = NetBIOS()
+			self.__remoteip = nmb.queryName(self.__hostname)[0]
+			if self.__remoteip is None:
+				self.__enable = False
+				return
+			self.__con = SMBConnection('','',platform.uname().node, self.__hostname)
+			self.__con.connect(self.__remoteip)
+		except Exception as e:
+			self.__enable = False
+			return
+		
+		try:
+			items = self.__con.listPath(self.__sharename, str(self.__path))
+			self.__files = [item.filename for item in items]
+			return
+		except Exception as e:
+			self.__con.close()
+			self.__enable = False
+			return
+		
+	def isEnable(self):
+		return self.__enable
+	
+	def exists(self, file):
+		return  (file in self.__files)
+	
+	def copyTo(self, file, dest):
+		d = self.__path / PurePath(file)
+		try:
+			with open( dest, 'wb') as f:
+				self.__con.retrieveFile(self.__sharename, str(d), f)
+		except Exception as e:
+			return False
+		return True
+		
+	def __del__(self):
+		if self.__enable:
+			self.__con.close()
+
+
+# Timer
+class SDTimer(threading.Thread):
+
+	__active = False
+	
+	def __init__(self, sec, callback):
+		super(SDTimer, self).__init__()
+		self.daemon = True
+		self.__sec = sec
+		self.__callback = callback
+	
+	def getRemaining(self):
+		return self.__sec
+	
+	def setRemaining(self, sec):
+		self.__sec = sec
+	
+	def getActive(self):
+		return self.__active
+	
+	def cancel(self):
+		self.__active = False
+		
+	def run(self):
+		self.__active = True
+		
+		while self.__sec != 0:
+			sleep(1)
+			self.__sec -= 1
+			if self.__active == False:
+				break
+		
+		if self.__active:
+			self.__callback()
+		self.__active = False
+		
+		return
+
+
+# Queue
+class castQueue(queue.Queue):
+
+	__depth = 5
+
+	def __init__(self, depth):
+		super(castQueue, self).__init__()
+		self.__depth = depth
+		self.__semaphore = BoundedSemaphore()
+	
+	def put(self, obj):
+		self.__semaphore.acquire()
+		while self.qsize() > self.__depth:
+			try:
+				t = super().get_nowait()
+			except queue.Empty:
+				pass
+		super().put(obj)
+		self.__semaphore.release()
+		return
+		
+	def get_nowait(self):
+		self.__semaphore.acquire()
+		r = None
+		try:
+			r = super().get_nowait()
+		except Exception as e:
+			self.__semaphore.release()
+			raise e
+		self.__semaphore.release()
+		return r
+

+ 9 - 78
tree/usr/local/bin/superdac.py

@@ -28,6 +28,7 @@ import queue
 import platform
 from select import select
 from time import sleep
+from urllib.parse import urlparse
 
 # ABCMeta
 from abc import ABCMeta, abstractmethod
@@ -59,6 +60,11 @@ from alsaaudio import Mixer
 from smb.SMBConnection import SMBConnection
 from nmb.NetBIOS import NetBIOS
 
+# sdlib
+from sdlib import simpleSMB
+from sdlib import castQueue
+from sdlib import SDTimer
+
 # LCD spec.
 LCD_WIDTH = 128
 LCD_HEIGHT = 160
@@ -93,78 +99,6 @@ VOLUME_FILE='/var/tmp/.superdac.volume'
 # Openning Logo Image
 LOGO_FILE = '/home/mpd/scripts/logo.jpg'
 
-class SDTimer(threading.Thread):
-
-	__active = False
-	
-	def __init__(self, sec, callback):
-		super(SDTimer, self).__init__()
-		self.daemon = True
-		self.__sec = sec
-		self.__callback = callback
-	
-	def getRemaining(self):
-		return self.__sec
-	
-	def setRemaining(self, sec):
-		self.__sec = sec
-	
-	def getActive(self):
-		return self.__active
-	
-	def cancel(self):
-		self.__active = False
-		
-	def run(self):
-		self.__active = True
-		
-		while self.__sec != 0:
-			sleep(1)
-			self.__sec -= 1
-			if self.__active == False:
-				break
-		
-		if self.__active:
-			self.__callback()
-		self.__active = False
-		
-		return
-
-		
-class castQueue(queue.Queue):
-
-	__depth = 5
-
-	def __init__(self, depth):
-		super(castQueue, self).__init__()
-		self.__depth = depth
-		self.__semaphore = BoundedSemaphore()
-	
-	def put(self, obj):
-		self.__semaphore.acquire()
-		while self.qsize() > self.__depth:
-			try:
-				t = super().get_nowait()
-			except queue.Empty:
-				pass
-		super().put(obj)
-		self.__semaphore.release()
-		return
-		
-	def get_nowait(self):
-		self.__semaphore.acquire()
-		r = None
-		try:
-			r = super().get_nowait()
-		except Exception as e:
-			self.__semaphore.release()
-			raise e
-		self.__semaphore.release()
-		return r
-
-		
-
-
 # Message class
 class Message():
 
@@ -194,7 +128,6 @@ class Message():
 	def getParam(self):
 		return self.__param
 
-
 # LCD Display Item
 class LCDItem():
 	
@@ -278,7 +211,6 @@ class Switches(Worker):
 		
 		return True
 
-
 # Playlist manage worker
 class Playlist(Worker):
 	
@@ -865,8 +797,6 @@ class AlbumArt(Worker, threading.Thread):
 			param = LCDItem(image, pos=LCDItem.TOP)
 			msg = Message(self, Message.DISPLAY, param)
 			self.sendMessage(msg)
-			# インジケータ再描画
-#			self.sendMessage(Message(self, Message.INDICATOR, None))
 		
 		return
 	
@@ -887,8 +817,9 @@ class AlbumArt(Worker, threading.Thread):
 			except queue.Empty:
 				sleep(0.05)
 		return
-		
-		
+
+
+# SongTitle
 class SongTitle(Worker, threading.Thread):
 	
 	term = False