123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- # 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
|