BDServer/modules/Set.py

225 lines
6.3 KiB
Python
Raw Normal View History

2024-11-19 13:47:45 +02:00
# AGPL 3 or any later version
# (C) J.Y.Amihud ( Blender Dumbass )
from modules.Common import *
import os
import sys
import json
def Folder():
conf = "BDServer"
try:
data_dir = os.environ["XDG_DATA_HOME"] + "/" + conf
except:
data_dir = os.path.expanduser("~/.local/share/"+ conf)
try:
os.makedirs(data_dir)
except:
pass
# Adding nessesary subcategories
for i in ["pictures", "tabs", "logs", "accounts"]:
try:
os.makedirs(data_dir+"/"+i)
except:
pass
2024-11-19 13:47:45 +02:00
return data_dir
def Load():
try:
with open(Folder()+"/config.json") as o:
data = json.load(o)
except Exception as e:
data = {}
print(clr["bold"]+clr["tdyl"]+"Error:"+clr["norm"]+" Config doesn't exist! Making new config.")
return data
def Save(data):
try:
with open(Folder()+"/config.json", "w") as save:
2024-11-24 23:28:19 +02:00
json.dump(data, save, indent=4)
2024-11-19 13:47:45 +02:00
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot save config!", e)
def Set():
if len(sys.argv) < 3:
from modules import Help
Help.Set()
if "--title" in sys.argv:
try:
title = sys.argv[ sys.argv.index("--title") + 1]
if "--" in title: 1/0 # Failing this for the error message.
Title(title)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Title Wasn't Specified!")
print('Use: $ python3 run.py --set --title "My Website"')
if "--tagline" in sys.argv:
try:
tagline = sys.argv[ sys.argv.index("--tagline") + 1]
if "--" in tagline: 1/0 # Failing this for the error message.
Tagline(tagline)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tagline Wasn't Specified!")
print('Use: $ python3 run.py --set --title "Ultimate Club for Ultimate Hackers"')
2024-11-19 13:47:45 +02:00
if "--port" in sys.argv:
try:
port = int(sys.argv[ sys.argv.index("--port") + 1])
Port(port)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Port Wasn't Specified Correctly!")
print('Use: $ python3 run.py --set --port 8080')
2024-11-19 13:47:45 +02:00
if "--add_tab" in sys.argv:
try:
tab = sys.argv[ sys.argv.index("--add_tab") + 1]
if "--" in tab: 1/0 # Failing this for the error message.
AddTab(tab)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
print('Use: $ python3 run.py --set --add_tab "Articles"')
2024-11-19 13:47:45 +02:00
if "--edit_tab" in sys.argv:
try:
tab = sys.argv[ sys.argv.index("--edit_tab") + 1]
if "--" in tab: 1/0 # Failing this for the error message.
EditTab(tab)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Tab Name Wasn't Specified!")
print('Use: $ python3 run.py --set --edit_tab "Articles"')
if "--css" in sys.argv:
try:
filename = sys.argv[ sys.argv.index("--css") + 1]
if "--" in filename: 1/0 # Failing this for the error message.
SetCSS(filename)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" CSS File Wasn't Specified!", e)
print('Use: $ python3 run.py --set --css ~/mycssfile.css')
if "--favicon" in sys.argv:
try:
filename = sys.argv[ sys.argv.index("--favicon") + 1]
if "--" in filename: 1/0 # Failing this for the error message.
SetFavicon(filename)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" File Wasn't Specified!", e)
print('Use: $ python3 run.py --set --css ~/favicon.png')
2024-11-19 13:47:45 +02:00
if "--css_edit" in sys.argv:
config = Load()
os.system("nano "+config.get("css", "default.css"))
2024-11-19 13:47:45 +02:00
def Title(title):
data = Load()
data["title"] = title
Save(data)
print(clr["bold"]+clr["tbyl"]+title+clr["norm"]+" is set as title.")
def Tagline(tagline):
data = Load()
data["tagline"] = tagline
Save(data)
print(clr["bold"]+clr["tbyl"]+tagline+clr["norm"]+" is set as tagline.")
2024-11-19 13:47:45 +02:00
def Port(port):
data = Load()
data["port"] = port
Save(data)
print(clr["bold"]+clr["tbyl"]+str(port)+clr["norm"]+" is set as the website's port.")
def AddTab(tab):
# Tab's Folder
tabsFolder = Folder()+"/tabs/"+Simplify(tab)
try: os.makedirs(tabsFolder)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Could not add tab!", e)
return
# Tab's Config File
data = {"title":tab,
"icon":"folder"}
try:
with open(tabsFolder+"/config.json", "w") as save:
json.dump(data, save, indent=4, sort_keys=True)
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot save tab's config!", e)
return
print(clr["bold"]+clr["tbyl"]+tab+clr["norm"]+" tab is added as "+clr["bold"]+"/"+Simplify(tab)+clr["norm"])
def EditTab(tab):
tabsFolder = Folder()+"/tabs/"+Simplify(tab)
os.system("nano "+tabsFolder+"/config.json")
def SetCSS(filename):
2024-11-19 13:47:45 +02:00
try:
f = open(filename, "r")
f = f.read()
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read CSS file!", e)
return
css = open(Folder()+"/style.css", "w")
css.write(f)
css.close()
config = Load()
config["css"] = Folder()+"/style.css"
Save(config)
print("New CSS is set at "+clr["bold"]+Folder()+"/style.css"+clr["norm"])
def SetFavicon(filename):
try:
f = open(filename, "rb")
f = f.read()
except Exception as e:
print(clr["bold"]+clr["tdrd"]+"Error:"+clr["norm"]+" Cannot Read Image!", e)
return
css = open(Folder()+"/pictures/favicon.png", "wb")
css.write(f)
css.close()
config = Load()
config["favicon"] = "/pictures/favicon.png"
Save(config)
print("New favicon is set at "+clr["bold"]+Folder()+"/pictures/favicon.png"+clr["norm"])
2024-11-24 23:28:19 +02:00