sightplace-rep/sightfactory.py

121 lines
4.4 KiB
Python

import base
from base import log
import json
import random
from sight import Sight
class SightFactory():
def __init__(self):
self.list = {}
self.data = {} #Global data(s) for the sight(s)
def create(self, id, page):
log.byCode("SF1000E","The page '" + str(page) + "' with '"+str(id)+"' id.")
if str(id) == "main" and self.checkKey(id):
log.warning("SF1000W", "The '"+str(id)+"' window already exist! You can't create more than one 'main' page sight!")
return None
if base.data["sight_mode"] == "unique":
if len(id) == 0:
print("No ID was given! In single mode need declare an ID to the Sight in advance!")
return None
if self.checkKey(id):
print("This ['"+page+"'-'"+str(id)+"'] already exist!")
return None
if base.data["sight_mode"] == "auto":
if len(id) == 0:
id = self.createUniqueId()
if len(id) > 0 and self.checkKey(id) and str(id) != "main":
id = self.createUniqueId()
if base.data["sight_mode"] == "normal":
if self.checkKey(id) and len(id) != 0:
print("This ['"+page+"'-'"+str(id)+"'] already exist!")
return None
if len(id) == 0:
id = self.createUniqueId()
self.list[id] = Sight(id, page)
return id
def show(self, id):
if self.checkKey(id):
self.list[id].show()
log.byCode("SF1000E","The '"+str(id)+"' sight is showed!")
return True
log.byCode("SF1001W", "This '"+str(id)+"' id isn't exist to show!")
return False
def getSights(self):
return self.list
def get(self, id):
if self.checkKey(id):
return self.list[id]
"""print("This '"+str(id)+"' id isn't exist to get!")"""
def closeChildren(self, id, removeFromParent = True):
""" CLOSE ITS CHILDREN. It doesnt close the children's children just the current parent children
will be closed. """
for cid in self.get(id).getChildren():
self.close(cid)
print("CLOSED - " + cid)
if removeFromParent:
""" If the parent doesn't exist anymore! """
if self.get(self.get(id).getParent()) is not None:
children = self.get(self.get(id).getParent()).getChildren()
if len(children) > 0:
self.get(self.get(id).getParent()).removeChild(id)
print("AFTER REMOVED CHILDREN " + str(self.get(self.get(id).getParent()).getChildren()) )
def close(self, id, closeChildrenToo=True):
if closeChildrenToo:
self.closeChildren(id)
if self.checkKey(id):
if id == "main":
print("Program Exit!")
base.app.exit(0)
self.list[id].close()
if id in self.list:
self.list[id].deleteLater()
del self.list[id]
"""print("The '"+str(id)+"' is closed now!")"""
return True
else:
print("The '"+str(id)+"' isn't exist'")
return False
def sendMessage(self, label, sight_list, data):
data = json.dumps(data)
if len(sight_list) == 0:
for sight in self.list.values():
sight.browser.page().runJavaScript("message('" + label + "', '"+str(data)+"')", self.ready)
else:
for sight_id in sight_list:
if base.sights.checkKey(sight_id):
base.sights.get(sight_id).browser.page().runJavaScript("message('" + label + "', '" + str(data) + "')", self.ready)
def ready(self, returnValue):
if returnValue is not None:
print("RETURN "+returnValue)
def processes(self):
if len(self.list) > 0:
print(self.list)
def checkKey(self, id):
if id in self.list:
return True
return False
def addGlobalData(self, key, value):
self.data[key] = value
def getGlobauniquelData(self, key):
if key in self.data:
return self.data[key]
return None
def createUniqueId(self):
new_id = str(random.randint(1000, 9999))
if self.checkKey(new_id):
self.createUniqueId()
return new_id