sdlib.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # stdlibs
  2. import os
  3. import queue
  4. import threading
  5. from threading import BoundedSemaphore
  6. from threading import Semaphore
  7. from time import sleep
  8. # SMB
  9. from urllib.parse import urlparse
  10. from pathlib import PurePath
  11. import platform
  12. from smb.SMBConnection import SMBConnection
  13. from nmb.NetBIOS import NetBIOS
  14. # simpleSMB
  15. class simpleSMB():
  16. __hostname = ''
  17. __remoteip = ''
  18. __sharename = ''
  19. __path = '/'
  20. __enable = True
  21. def __init__(self, uri):
  22. p = urlparse(uri)
  23. if p.scheme != 'smb':
  24. self.__enable = False
  25. return
  26. self.__hostname = p.hostname
  27. path = PurePath(p.path)
  28. pl = list(path.parts)
  29. self.__sharename = pl.pop(1)
  30. self.__path = PurePath(pl.pop(0))
  31. for r in pl:
  32. self.__path /= PurePath(r)
  33. try:
  34. nmb = NetBIOS()
  35. self.__remoteip = nmb.queryName(self.__hostname)[0]
  36. if self.__remoteip is None:
  37. self.__enable = False
  38. return
  39. self.__con = SMBConnection('','',platform.uname().node, self.__hostname)
  40. self.__con.connect(self.__remoteip)
  41. except Exception as e:
  42. self.__enable = False
  43. return
  44. try:
  45. items = self.__con.listPath(self.__sharename, str(self.__path))
  46. self.__files = [item.filename for item in items]
  47. return
  48. except Exception as e:
  49. self.__con.close()
  50. self.__enable = False
  51. return
  52. def isEnable(self):
  53. return self.__enable
  54. def exists(self, file):
  55. return (file in self.__files)
  56. def copyTo(self, file, dest):
  57. d = self.__path / PurePath(file)
  58. try:
  59. with open( dest, 'wb') as f:
  60. self.__con.retrieveFile(self.__sharename, str(d), f)
  61. except Exception as e:
  62. return False
  63. return True
  64. def __del__(self):
  65. if self.__enable:
  66. self.__con.close()
  67. # Timer
  68. class SDTimer(threading.Thread):
  69. __active = False
  70. def __init__(self, sec, callback):
  71. super(SDTimer, self).__init__()
  72. self.daemon = True
  73. self.__sec = sec
  74. self.__callback = callback
  75. def getRemaining(self):
  76. return self.__sec
  77. def setRemaining(self, sec):
  78. self.__sec = sec
  79. def getActive(self):
  80. return self.__active
  81. def cancel(self):
  82. self.__active = False
  83. def run(self):
  84. self.__active = True
  85. while self.__sec != 0:
  86. sleep(1)
  87. self.__sec -= 1
  88. if self.__active == False:
  89. break
  90. if self.__active:
  91. self.__callback()
  92. self.__active = False
  93. return
  94. # Queue
  95. class castQueue(queue.Queue):
  96. __depth = 5
  97. def __init__(self, depth):
  98. super(castQueue, self).__init__()
  99. self.__depth = depth
  100. self.__semaphore = BoundedSemaphore()
  101. def put(self, obj):
  102. self.__semaphore.acquire()
  103. while self.qsize() > self.__depth:
  104. try:
  105. t = super().get_nowait()
  106. except queue.Empty:
  107. pass
  108. super().put(obj)
  109. self.__semaphore.release()
  110. return
  111. def get_nowait(self):
  112. self.__semaphore.acquire()
  113. r = None
  114. try:
  115. r = super().get_nowait()
  116. except Exception as e:
  117. self.__semaphore.release()
  118. raise e
  119. self.__semaphore.release()
  120. return r