baselib.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #!/usr/bin/python3
  2. import os
  3. import time
  4. import json
  5. from collections import OrderedDict
  6. from lib.cprint import *
  7. import lib.fixlib as fixlib
  8. import string
  9. import tkinter
  10. tk = tkinter
  11. HOME = os.getenv('HOME')
  12. def _clean_path(fpath):
  13. _path=[]
  14. for i in fpath:
  15. fpath = fpath.replace(" ","_")
  16. if i in string.ascii_letters+string.digits+"äöüßÖÄÜ_-":
  17. _path.append(i)
  18. path = "".join(_path)
  19. return path
  20. def _read_init_txt(show_path):
  21. fname = show_path+"init.txt"
  22. show_name = None
  23. msg = ""
  24. if not os.path.isfile( fname ):
  25. msg = "_read_init_txt Errror: " +fname +"\n NOT FOUND !"
  26. return [None,msg]
  27. try:
  28. f = open(fname,"r")
  29. for line in f.readlines():
  30. line = line.strip()
  31. print(" init.txt:",[line])
  32. if line.startswith("#"):
  33. continue
  34. if not line:
  35. continue
  36. show_name = line
  37. show_name = show_name.replace(".","")
  38. show_name = show_name.replace("\\","")
  39. show_name = show_name.replace("/","")
  40. except Exception as e:
  41. cprint("show name exception",color="red")
  42. msg="read_init_txt Error:{}".format(e)
  43. finally:
  44. f.close()
  45. return [show_name,msg]
  46. def _read_sav_file(xfname):
  47. cprint("load",xfname)
  48. lines = []
  49. if not os.path.isfile(xfname):
  50. return []
  51. f = open(xfname,"r")
  52. lines = f.readlines()
  53. f.close()
  54. data = OrderedDict()
  55. labels = OrderedDict()
  56. i=0
  57. for line in lines:
  58. r = fixlib._fixture_decode_sav_line(line)
  59. if r:
  60. key,label,jdata = r
  61. fixlib._fixture_repair_nr0(jdata)
  62. data[key] = jdata
  63. labels[key] = label
  64. return data,labels
  65. def _listdir(show_path):
  66. #self._check()
  67. show_list = list(os.listdir( show_path ))
  68. out = []
  69. for fname in show_list:
  70. if fname == "EASY": #hidde EASY show in list !
  71. continue
  72. #print(fname)
  73. ctime = os.path.getmtime(show_path+fname)
  74. ctime = time.strftime("%Y-%m-%d %X", time.localtime(ctime)) #1650748726.6604707))
  75. try:
  76. mtime = os.path.getmtime(show_path+fname+"/patch.sav")
  77. mtime = time.strftime("%Y-%m-%d %X", time.localtime(mtime)) #1650748726.6604707))
  78. except:
  79. mtime = 0
  80. if mtime:
  81. out.append([fname,mtime])#,ctime])
  82. from operator import itemgetter
  83. out=sorted(out, key=itemgetter(1))
  84. out.reverse()
  85. return out
  86. class Base():
  87. def __init__(self):
  88. cprint("Base.init()",color="red")
  89. self._init()
  90. def _init(self):
  91. show_name = "" #DemoShow #"ErrorRead-init.txt"
  92. self.show_path0 = HOME +"/LibreLight/"
  93. self.show_path = self.show_path0
  94. self.show_path1 = self.show_path0 + "show/"
  95. msg = " X "
  96. self.show_name,msg = _read_init_txt(self.show_path)
  97. if not self.show_name:
  98. #r=tkinter.messagebox.showwarning(message=msg,parent=None)
  99. r=tkinter.messagebox.showwarning(message=msg,title="Error",parent=None)
  100. sys.exit()
  101. fpath = self.show_path1 +show_name
  102. if not os.path.isdir(fpath):
  103. cprint(fpath)
  104. cprint( os.path.isdir(fpath))
  105. msg="'{}'\n Show Does Not Exist\n\n".format(show_name)
  106. msg += "please check\n"
  107. msg += "-{}init.txt\n".format(self.show_path0)
  108. msg += "-{}".format(self.show_path1)
  109. #showwarning(msg=msg,title="Show Error")
  110. r=tkinter.messagebox.showwarning(message=msg,title="Show Error",parent=None)
  111. exit()
  112. self._check()
  113. def _set(self,fname):
  114. ok= os.path.isdir(self.show_path1+"/"+fname)
  115. ini = self.show_path0+"init.txt"
  116. cprint("SET SHOW NAME",fname,ok,ini)
  117. try:
  118. f = open( ini ,"r")
  119. lines = f.readlines()
  120. f.close()
  121. if len(lines) >= 10: # cut show history
  122. cprint("_set",ini,len(lines))
  123. lines = lines[-10:]
  124. f = open( ini ,"w")
  125. f.writelines(lines)
  126. f.close()
  127. exit()
  128. except:pass
  129. if ok:
  130. #self.show_name = fname
  131. f = open( ini ,"a")
  132. f.write(fname+"\n")
  133. f.close()
  134. return 1
  135. def _check(self):
  136. if not os.path.isdir(self.show_path):
  137. os.mkdir(self.show_path)
  138. self.show_path += "/show/"
  139. if not os.path.isdir(self.show_path):
  140. os.mkdir(self.show_path)
  141. self.show_path += "/" +self.show_name +"/"
  142. if not os.path.isdir(self.show_path):
  143. os.mkdir(self.show_path)
  144. pass
  145. def _list(self):
  146. cprint("BASE._list()")
  147. out = _listdir(self.show_path1)
  148. return out
  149. def _load(self,filename):
  150. xpath = self.show_path+"/"+str(filename)+".sav"
  151. if not os.path.isfile(xpath):
  152. msg = ""#"Exception: {}".format(e)
  153. msg += "\n\ncheck\n-init.txt"
  154. cprint(msg,color="red")
  155. #showwarning(msg=msg,title="load Error")
  156. r=tkinter.messagebox.showwarning(message=msg,title="Error",parent=None)
  157. return
  158. return _read_sav_file(xpath)
  159. def build_path(self,save_as):
  160. save_as = _clean_path(save_as)
  161. path = self.show_path.split("/")
  162. path = "/".join(path[:-2])
  163. fpath = path+"/"+save_as
  164. return fpath,save_as
  165. def _create_path(self,fpath):
  166. if os.path.isdir(fpath):
  167. msg="STOP SHOW EXIST !"
  168. cprint(msg,color="red")
  169. #showwarning(msg=msg,title="Error")
  170. r=tkinter.messagebox.showwarning(message=msg,title="Error",parent=None)
  171. #r=tkinter.messagebox.showwarning(message=msg,parent=None)
  172. return 0
  173. else:
  174. cprint("CREATE DIR ",fpath,color="green")
  175. os.mkdir(fpath)
  176. #self._set(save_as)
  177. return fpath
  178. def _backup(self,filename,data,labels,save_as):
  179. if save_as:
  180. xfname = save_as +"/"+str(filename)+".sav"
  181. else:
  182. xfname = self.show_path+"/"+str(filename)+".sav"
  183. cprint("backup",xfname)
  184. f = open(xfname,"w")
  185. for key in data:
  186. line = data[key]
  187. #print(line)
  188. label = "label"
  189. if key in labels:
  190. label = labels[key]
  191. if label == "Name-"+str(key):
  192. label = ""
  193. #print(xfname,"load",key,label,len(line))
  194. f.write( "{}\t{}\t{}\n".format( key,label,json.dumps(line) ) )
  195. f.close()
  196. return 1