baselib.py 5.9 KB

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