showlib.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. #!/usr/bin/python3
  2. import os
  3. import time
  4. import sys
  5. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  6. import json
  7. from collections import OrderedDict
  8. from lib.cprint import *
  9. import lib.fixlib as fixlib
  10. import string
  11. import tkinter
  12. tk = tkinter
  13. HOME = os.getenv('HOME')
  14. BASE_PATH = HOME+"/LibreLight/"
  15. SHOW_DIR = BASE_PATH+"/show/"
  16. def _clean_path(fpath):
  17. _path=[]
  18. for i in fpath:
  19. fpath = fpath.replace(" ","_")
  20. if i in string.ascii_letters+string.digits+"äöüßÖÄÜ_-":
  21. _path.append(i)
  22. path = "".join(_path)
  23. return path
  24. def current_show_name(base_path=""):
  25. if not base_path:
  26. base_path = BASE_PATH
  27. fname = base_path+"/init.txt"
  28. show_name = "null"
  29. msg = ""
  30. if not os.path.isfile( fname ):
  31. msg = "current_show_path Error: " +fname +"\n NOT FOUND !"
  32. cprint(msg,color="red")
  33. return show_name #,msg]
  34. try:
  35. f = open(fname,"r")
  36. lines = f.readlines()
  37. f.close()
  38. for line in lines:
  39. line = line.strip()
  40. #print(" init.txt:",[line])
  41. if line.startswith("#"):
  42. continue
  43. if not line:
  44. continue
  45. show_name = line
  46. show_name = show_name.replace(".","")
  47. show_name = show_name.replace("\\","")
  48. show_name = show_name.replace("/","")
  49. except Exception as e:
  50. msg="current_show_path \nError:{}".format(e)
  51. cprint(msg,color="red")
  52. #return [show_name,msg]
  53. return show_name
  54. def current_show_path():
  55. SHOW_PATH = SHOW_DIR + current_show_name()
  56. while "//" in SHOW_PATH:
  57. SHOW_PATH = SHOW_PATH.replace("//","/")
  58. return SHOW_PATH
  59. def generate_show_path(show_name):
  60. show_name = _clean_path(show_name)
  61. SHOW_PATH = SHOW_DIR + show_name
  62. while "//" in SHOW_PATH:
  63. SHOW_PATH = SHOW_PATH.replace("//","/")
  64. return SHOW_PATH
  65. def build_path(fn):
  66. fn = _clean_path(fn)
  67. path = current_show_path().split("/")
  68. path = "/".join(path[:-2])
  69. fpath = path+"/show/"+fn
  70. cprint(fpath,fn,color="red")
  71. return fpath,fn
  72. def create_new_show_path(fpath):
  73. if os.path.isdir(fpath):
  74. cprint(" CREATE DIR FAIL (exist)",[fpath],color="red")
  75. return 0
  76. os.mkdir(fpath)
  77. cprint(" CREATE DIR OK",[fpath],color="green")
  78. return 1
  79. def _read_sav_file(xfname):
  80. cprint("load",xfname)
  81. lines = []
  82. data = OrderedDict()
  83. labels = OrderedDict()
  84. if not os.path.isfile(xfname):
  85. return [] #data,labels
  86. f = open(xfname,"r")
  87. lines = f.readlines()
  88. f.close()
  89. i=0
  90. for line in lines:
  91. r = fixlib._fixture_decode_sav_line(line)
  92. if r:
  93. key,label,jdata = r
  94. fixlib._fixture_repair_nr0(jdata)
  95. data[key] = jdata
  96. labels[key] = label
  97. return data,labels
  98. def list_shows(path=None):
  99. if not path:
  100. path = SHOW_DIR
  101. show_list = list(os.listdir( path ))
  102. out = []
  103. for fname in show_list:
  104. fpath = path+fname
  105. if fname == "EASY": #hidde EASY show in list !
  106. continue
  107. ctime = os.path.getmtime(fpath)
  108. ctime = time.strftime("%Y-%m-%d %X", time.localtime(ctime))
  109. try:
  110. mtime = os.path.getmtime(fpath+"/patch.sav")
  111. mtime = time.strftime("%Y-%m-%d %X", time.localtime(mtime))
  112. except:
  113. mtime = 0
  114. if mtime:
  115. out.append([fname,mtime])#,ctime])
  116. from operator import itemgetter
  117. out=sorted(out, key=itemgetter(1))
  118. out.reverse()
  119. return out
  120. def set_current_show_name(fname):
  121. ok= os.path.isdir(SHOW_DIR+"/"+fname)
  122. ini = BASE_PATH+"init.txt"
  123. print()
  124. print()
  125. cprint("SET SHOW NAME",fname,ok,ini,color="green")
  126. print()
  127. try:
  128. f = open( ini ,"r")
  129. lines = f.readlines()
  130. f.close()
  131. if len(lines) >= 10: # cut show history
  132. cprint("_set",ini,len(lines))
  133. lines = lines[-10:]
  134. f = open( ini ,"w")
  135. f.writelines(lines)
  136. f.close()
  137. exit()
  138. except:pass
  139. if ok:
  140. #self.show_name = fname
  141. f = open( ini ,"a")
  142. f.write(fname+"\n")
  143. f.close()
  144. return 1
  145. class Base():
  146. def __init__(self):
  147. self.show_path = current_show_path()
  148. self.show_name = current_show_name()
  149. cprint("Base.init()",self.show_path,self.show_name,color="yellow")
  150. msg = "<msg>"
  151. if not self.show_name:
  152. r=tkinter.messagebox.showwarning(message=msg,title="444 Error",parent=None)
  153. sys.exit()
  154. if not os.path.isdir(self.show_path):
  155. msg += "Show does not exist\n\n"
  156. msg += "please check\n"
  157. msg += "-{} init.txt\n".format(BASE_PATH)
  158. msg += "-{}".format(self.show_path)
  159. cprint(msg,color="red")
  160. r=tkinter.messagebox.showwarning(message=msg,title="Show Error",parent=None)
  161. exit()
  162. self._check()
  163. def _set(self,fname):
  164. set_current_show_name(fname)
  165. def _check(self):
  166. if not os.path.isdir(self.show_path):
  167. os.mkdir(self.show_path)
  168. def _load(self,filename):
  169. xpath = self.show_path+"/"+str(filename)+".sav"
  170. if not os.path.isfile(xpath):
  171. msg = ""
  172. msg += "\n"*2
  173. msg += "check init.txt"
  174. msg += "\n"*2
  175. msg += xpath
  176. cprint(msg,color="red")
  177. r=tkinter.messagebox.showwarning(message=msg,title="123 Error",parent=None)
  178. return
  179. return _read_sav_file(xpath)
  180. def _create_path(self,fpath):
  181. if not create_new_show_path(fpath):
  182. msg="STOP SHOW EXIST !"
  183. cprint(msg,color="red")
  184. r=tkinter.messagebox.showwarning(message=msg,title="333 Error",parent=None)
  185. return 0
  186. return fpath
  187. def _backup(self,filename,data,labels,save_as):
  188. try:
  189. fpath = self.show_path
  190. if save_as:
  191. fpath = save_as
  192. fpath += "/"+str(filename)+".sav"
  193. f = open(fpath,"w")
  194. for key in data:
  195. line = data[key]
  196. label = "label"
  197. if key in labels:
  198. label = labels[key]
  199. if label == "Name-"+str(key):
  200. label = ""
  201. nline = "{}\t{}\t{}\n".format( key,label,json.dumps(line) )
  202. f.write( nline )
  203. f.close()
  204. cprint(" Base._backup",[fpath],len(data),"OK",color="green")
  205. except Exception as e:
  206. cprint(" Base._backup",[fpath],len(data),"FAIL !",color="red")
  207. raise e
  208. return 1
  209. def test():
  210. print()
  211. print("-- "*40)
  212. print("HOME ",HOME)
  213. print("BASE_PATH ",BASE_PATH)
  214. print("SHOW_DIR ",SHOW_DIR)
  215. print()
  216. print("-- "*20)
  217. print("TEST ")
  218. print("current_show_path",current_show_path())
  219. print()
  220. print("-- "*20)
  221. dl = list_shows()
  222. for i in dl:
  223. print(" - ",i)
  224. print("-- "*20)
  225. xpath = SHOW_DIR + "/" + dl[0][1] +"/presets.sav" # exec.sav
  226. print(xpath)
  227. x= _read_sav_file(xpath)
  228. print("len.x",len(x))
  229. base = Base()
  230. print("::")
  231. #x=base.build_path("TOST")
  232. #print(x)
  233. x=build_path("TEST")
  234. print(x)
  235. show_name = "tEsT"
  236. x= generate_show_path(show_name)
  237. print("generate_show_path",x)
  238. #x=base.build_path(show_name)
  239. #print("build_path",[show_name,x])
  240. if __name__ == "__main__":
  241. test()