Skip to content

Commit

Permalink
Merge branch 'pre-dev' of https://github.com/SK1Y101/LOaBIS
Browse files Browse the repository at this point in the history
# Conflicts:
#	README.md
  • Loading branch information
SK1Y101 committed Nov 28, 2019
2 parents 35d6984 + 0299f14 commit 9a99122
Show file tree
Hide file tree
Showing 9 changed files with 1,070 additions and 70 deletions.
271 changes: 246 additions & 25 deletions LOaBIS.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,257 @@
import sys, time, os
import sys, time, os, subprocess, csv, importlib, threading
from datetime import datetime
from tkinter import *
from tkinter.ttk import *

def _elog(err="",say=1):
_log("Error: "+str(type(err))+"; "+str(err)+"\n")
if say:
_say("Software encountered an error, consider submitting log to developers","Error")
def execute(func=[],glob=[]):
for x in glob:
if x not in globals():
globals()[x]=glob[x]
for x in func:
try:
globals()[str(x)]()
except Exception as e:
elog(e)

def _say(txt="",cli="System"):
for x in str(cli+": "+txt)+"\n":
sys.stdout.write(x)
time.sleep(0.003)
def log(txt="",sy=0):
with open("log.txt","a") as y:
y.write("["+str(datetime.now())+"] - "+txt[0].upper()+txt[1:]+"\n")
if sy:
say(txt,console=1)

def _log(txt="",say=0):
y=open("log.txt","a")
y.write("["+str(datetime.now())+"] - "+txt[0].upper()+txt[1:]+"\n")
y.close()
if say:
_say(txt)
def elog(err="",sy=1):
log("Error: "+str(type(err))+"; "+str(err))
if sy:
say("Software encountered an error, consider submitting log to developers","Error",1)

def elapsed(start=datetime.now(),text=""):
_time=divmod((datetime.now()-start).total_seconds(),60)
log(text+" complete, took "+str(_time[0]*60+_time[1])+" seconds")

def getcom(text=""):
say(cli="",di="")
x=getdat(module.cwords,text,"null")
y=module.funcs[module.cwords.index(x)]
log("Function "+y+" called")
if y=="closesoftware":
return True
try:
globals()[y](text.replace(y,"").replace(x,""))
except Exception as e:
elog(e,0)
say("'"+text.split(" ")[0]+"' not executable\n")
return False

def listen(txt="",cli="System",console=0,di=": "):
say(txt,cli,console)
try:
if not console:
a=text.get("1.0",END)
b=a
while b==a:
b=text.get("1.0",END)
c=b.replace(a[:-2],"")[1:-2].replace(str(username)+di,"")
else:
int("")
except:
c=input(str(username)+di)
return c

def say(txt="",cli="System",console=0,di=": "):
try:
if not console:
text["state"]="normal"
text.insert(END,str(cli+di+txt+"\n"))
text.yview_pickplace("end")
text["state"]="disabled"
else:
int("")
except:
for x in str(cli+di+txt)+"\n":
sys.stdout.write(x)
time.sleep(0.003)

def gettext(event=""):
m=mtxt.get()
if len(m)>0:
mtxt.set("")
say(str(m),str(username))

def interface():
width,height,x,y=getgeometry()
wwidth=350
ui=genwindow(str(core[0])+" v"+str(core[1])+" - "+str(core[3]),wwidth,height-75,310,200,width-wwidth-10)
module.ui=True

scrollbar = Scrollbar(ui)
globals()["text"]=Text(ui,state=DISABLED,width=36,height=8)
scrollbar.grid(row=0,column=1,sticky=N+S+E)
text.grid(row=0,columnspan=1,sticky=N+S+W+E,padx=5)
scrollbar.config(command=text.yview)
ui.grid_columnconfigure(0,weight=1)
ui.grid_rowconfigure(0,weight=1)

globals()["mtxt"] = StringVar()
textenter = Entry(ui,textvariable=mtxt)
textenter.bind("<Return>",gettext)
textenter.grid(row=1,column=0,sticky=W+E,padx=5)

b = Button(ui,text="Enter",width=5,command=gettext)
b.grid(row=1,column=1,columnspan=2)

ui.after(1000,lambda: checkclose(ui))
mainloop()

def checkclose(self):
if module.ui:
self.after(1000,lambda: checkclose(self))
else:
self.destroy()

if __name__=="__main__":
try:
start_time=datetime.now()
_log("Initialising software",1)
globals()["shutdown"]=False
modules=next(os.walk(os.getcwd()))[1]
modules.sort()
for x in ["_core","__pycache__"]:
if x in modules:
modules.pop(modules.index(x))
_log(str(len(modules))+" Modules located: "+str(modules))

start_time=datetime.now()
log("Initialising software",1)
from _core import *
from _core import module
avoid=["__pycache__",".git"]
modules=getexcept(next(os.walk(os.getcwd()))[1],avoid)
modules.sort()
log(str(len(modules))+" Modules located: "+str(modules))

log("Loading module data",1)
modinfo,_mods=[],[]
for x in modules:
modinfo.append(getinfo(x))
_mods.append(getinfo(x))
core,_mods=getdat(modinfo,"LOaBIS Core"),getexcept(_mods,"LOaBIS Core")
log(str(core[0])+" v"+str(core[1])+" Loaded",1)
subprocess.call("title "+str(core[0])+" v"+str(core[1])+" - "+str(core[3]),shell=True)

stype=[0,0,0,[],[],[],"prevented","unstable","stable"]
log("Fetching pip modules",1)
pipmods=getpip()
log("Fetching dependancies",1)

for x in _mods:
tmp,tma=[],[]
if x[6]:
for y in x[6]:
if getdat(getexcept(modinfo,x[0]),y):
tma.append(modinfo.index(getdat(modinfo,y)))
else:
stype[0]+=1
tmp.append(y)
for y in x[7]:
if not getdat(pipmods,y):
tmp+=module.inst(y)
b=simver(core[1],x[2])
if b:
stype[b]+=1
stype[5-b].append(x[0]+" v"+x[1])
else:
tmp.append(1)
if tmp:
log(str(x[0])+" missing "+str(len(tmp))+" dependancies: "+str(tmp))
stype[5].append(x[0]+" v"+x[1])
modinfo.remove(x)
elif tma:
modinfo.insert(1+max(tma),x)

for x in range(3):
log(str(stype[2-x])+" "+str(stype[8-x])+" modules: "+str(stype[3+x]))
say(str(sum(stype[1:3]))+" modules loaded, "+str(stype[0])+" prevented from loading")

import_start=datetime.now()
fn=[["sta",0,0],["shu",0,0],["per",0,0],["pst",0,0],["rep",0,0]]
log("Importing module functions",1)
for x in modinfo:
a,b,d=0,[],[0,0,0,0,0]
try:
if x != core:
exec("from "+str(x[0])+" import *")
try:
with open(str(x[0])+sep+"commands.txt","r") as f:
for row in csv.reader(f):
c=row[0].split(":")[1]
module.funcs.append(c)
module.cwords.append(str(row[0].split(":")[0]+" ").replace(" "," "))
module.desc.append(row[1])
b.append(c)
log("Imported functions from "+str(x[0])+"; "+str(b))
a=1
except Exception as e:
elog(e)
except Exception as e:
log("unknown error, "+str(x[0])+" not loaded",1)
elog(e,0)
modinfo.pop(modinfo.index(x))
if a:
for y in x[8]:
module.stafunc.append(y)
d[0]+=1
for y in x[9]:
module.endfunc.append(y)
d[1]+=1
for y in x[10]:
module.perfunc.append(y)
d[2]+=1
for y in x[11]:
module.pstfunc.append(y)
d[3]+=1
for y in x[12]:
try:
globals()[y[0]]=globals()[y[1]]
d[4]+=1
except:
pass
for x in range(len(d)):
fn[x][1:3]=fn[x][1]+d[x],fn[x][2]+int(bool(d[x]))
for x in fn:
if x[1]:
log(str(x[1])+" "+str(x[0])+" functions loaded from "+str(x[2])+" modules")
module.funcs.append("null")
module.cwords.append("null")
module.desc.append("null")

for x in modules:
importlib.reload(sys.modules[x])

elapsed(import_start,"Function initialisation")

log("Executing final procedures",1)
if module.stafunc:
log("Running startup functions",1)
execute(module.stafunc,globals())
if (module.perfunc or module.pstfunc) and getelse(module.settings,"persist",False):
log("Running persistence handler")
pers=threading.Thread(name="Persistence",target=lambda:subprocess.call(["python","persistent.py",repr(modinfo)],creationflags=subprocess.CREATE_NEW_CONSOLE))
pers.start()
server,client=startsock()

elapsed(start_time,"Startup")

log("Initialisation complete, loading interface",1)
if getelse(module.settings,"gui",False):
_ui=threading.Thread(target=interface,name="Ui",daemon=True)
_ui.start()
time.sleep(1)

while not globals()["shutdown"]:
globals()["shutdown"]=getcom(listen("What would you like to do?"))
module.ui=False

end_time=datetime.now()
if (module.perfunc or module.pstfunc) and module.settings["persist"]:
msg(client,"shutdown")
server.close()
if module.endfunc:
log("Running shutdown functions",1)
execute(module.endfunc)

elapsed(end_time,"Shutdown")
log("Software shutdown successfully\n",1)

except Exception as e:
_elog(e)
elog(e)
Loading

0 comments on commit 9a99122

Please sign in to comment.