Blender-Pipeline/network/http_client.py
2023-12-16 23:17:56 +02:00

389 lines
12 KiB
Python

# (c) J.Y.Amihud 2023
# GPL-3 or any later version
import os
import json
import time
import hashlib
import urllib.request
import urllib.parse
import subprocess
# GTK module ( Graphical interface
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import GLib
from gi.repository import Gdk
import cairo
# Own modules
from settings import settings
from settings import talk
from project_manager import pm_project
#UI modules
from UI import UI_elements
from UI import UI_color
from studio import story
from studio import analytics
# This is a http client. To work on projects remotely.
def csize(x):
x = float(x)
l = ["B","KB", "MB", "GB", "TB"]
for i in range(5):
if x > 1024:
x = x / 1024
else:
return str(round(x, 2))+" "+l[i]
return str(round(x, 2))+" "+l[i]
def go(win, website, path):
url = website+path
print("url", url)
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode("utf-8")
data = json.loads(text)
return data
def down(win, website, filename, fsize=100000):
# If it is a blend file, we are in trouble.
if filename.endswith(".blend"):
dp = go(win, website, "/blend"+filename)
dplen = len(dp.get("files", {}))
for f in dp.get("files", {}):
try:
hashis = hashlib.md5(open(win.project+f,'rb').read()).hexdigest()
except:
hashis = ""
if hashis != dp.get("files", {}).get(f, {}).get("md5"):
win.current["http-server"]["message"] = f[f.rfind("/")+1:]
filesize = dp.get("files", {}).get(f, {}).get("filesize")
print("trying to get:", f)
pp = down(win, website, f, filesize)
# Making folder for the files
try:
os.makedirs(win.project+filename[:filename.rfind("/")+1])
except:
pass
# downloading the file
url = website+"/download"+filename
response = urllib.request.urlopen(url)
savef = open(win.project+filename, "wb")
sofar = 0
chunk = response.read(8192)
while chunk:
savef.write(chunk)
chunk = response.read(8192)
sofar = sofar + 8192
win.current["http-server"]["message"] = filename[filename.rfind("/")+1:]
win.current["http-server"]["fileprog"] = sofar / fsize
savef.close()
def get_folders(win):
# This function will get specified subfolders
win.current["http-server"]["message"] = "Getting Remote Folders"
folder = win.current["http-server"]["args"]
website = win.analytics["remote-server-url"]
call = win.current["http-server"]["call"]
try:
answer = go(win, website, "/list"+folder)
for n, i in enumerate(answer.get("folders", [])):
win.current["http-server"]["progress"] = n/len(answer.get("folders", []))
win.current["http-server"]["message"] = i
try:
os.makedirs(win.project+folder+"/"+i)
except:
pass
except Exception as e:
error = "Error: "+ str(e)
print(error)
win.current["http-server"]["message"] = error
time.sleep(1)
# Ending the process
win.current["calls"][call]["var"] = False
def get_asset_files(win):
# This function will get specified subfolders
win.current["http-server"]["message"] = "Getting / Updating Files"
folder = win.current["http-server"]["args"]
website = win.analytics["remote-server-url"]
call = win.current["http-server"]["call"]
folder = folder.replace("//", "/")
if folder.endswith("/"):
folder = folder[:-1]
try:
answer = go(win, website, "/list"+folder)
for n, i in enumerate(answer.get("files", [])):
win.current["http-server"]["progress"] = n/len(answer.get("files", []))
win.current["http-server"]["message"] = i
filename = folder+"/"+i
try:
hashis = hashlib.md5(open(win.project+filename,'rb').read()).hexdigest()
except:
hashis = ""
if hashis != answer.get("files", {}).get(i, {}).get("md5"):
filesize = answer.get("files", {}).get(i, {}).get("filesize")
down(win, website, filename, filesize)
win.current["http-server"]["message"] = "Getting Thumbnail"
other_folder = folder+"/renders"
print("otherfolder 1", other_folder)
answer = go(win, website, "/list"+other_folder)
for n, i in enumerate(answer.get("files", [])):
win.current["http-server"]["progress"] = n/len(answer.get("files", []))
win.current["http-server"]["message"] = i
if i.startswith("Preview"):
print("getting", i)
filename = other_folder+"/"+i
try:
hashis = hashlib.md5(open(win.project+filename,'rb').read()).hexdigest()
except:
hashis = ""
if hashis != answer.get("files", {}).get(i, {}).get("md5"):
filesize = answer.get("files", {}).get(i, {}).get("filesize")
down(win, website, filename, filesize)
win.current["http-server"]["message"] = "Getting Asset Blend"
other_folder = folder[:folder.rfind("/")+1].replace("/dev/", "/ast/")
print("otherfolder 2", other_folder, folder)
answer = go(win, website, "/list"+other_folder)
for n, i in enumerate(answer.get("files", [])):
win.current["http-server"]["progress"] = n/len(answer.get("files", []))
win.current["http-server"]["message"] = i
if i.startswith(folder[folder.rfind("/")+1:]):
print("getting", i)
filename = other_folder+"/"+i
try:
hashis = hashlib.md5(open(win.project+filename,'rb').read()).hexdigest()
except:
hashis = ""
if hashis != answer.get("files", {}).get(i, {}).get("md5"):
filesize = answer.get("files", {}).get(i, {}).get("filesize")
down(win, website, filename, filesize)
except Exception as e:
error = "Error: "+ str(e)
print(error)
win.current["http-server"]["message"] = error
time.sleep(1)
# Ending the process
win.current["calls"][call]["var"] = False
def get_files(win):
# This function will get specified subfolders
win.current["http-server"]["message"] = "Getting / Updating Files"
folder = win.current["http-server"]["args"]
website = win.analytics["remote-server-url"]
call = win.current["http-server"]["call"]
try:
answer = go(win, website, "/list"+folder)
for n, i in enumerate(answer.get("files", [])):
win.current["http-server"]["progress"] = n/len(answer.get("files", []))
win.current["http-server"]["message"] = i
filename = folder+"/"+i
try:
hashis = hashlib.md5(open(win.project+filename,'rb').read()).hexdigest()
except:
hashis = ""
if hashis != answer.get("files", {}).get(i, {}).get("md5"):
filesize = answer.get("files", {}).get(i, {}).get("filesize")
down(win, website, filename, filesize)
except Exception as e:
error = "Error: "+ str(e)
print(error)
win.current["http-server"]["message"] = error
time.sleep(1)
# Ending the process
win.current["calls"][call]["var"] = False
def get_essentials(win):
# This function will get essential parts of the project.
# Essentials are: Story, Analitycs, Project.progress and Banner Image.
essentials = [
"/pln/story.vcss",
"/set/analytics.json",
"/set/project.progress",
"/set/banner.png"]
website = win.analytics["remote-server-url"]
call = win.current["http-server"]["call"]
for n, i in enumerate(essentials):
win.current["http-server"]["progress"] = n/len(essentials)
win.current["http-server"]["message"] = i
try:
down(win, website, i)
except Exception as e:
error = "Error: "+ str(e)
print(error)
win.current["http-server"]["message"] = error
time.sleep(1)
win.story = story.load(win.project)
win.analytics = analytics.load(win.project)
win.analytics["remote-server-url"] = website
win.analytics["from-remote-server"] = True
analytics.save(win.project, win.analytics)
# Ending the process
win.current["calls"][call]["var"] = False
def layer(win, call):
# Making the layer
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, win.current['w'],
win.current['h'])
layer = cairo.Context(surface)
#text setting
layer.select_font_face("Monospace", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
UI_color.set(layer, win, "dark_overdrop")
layer.rectangle(
0,
0,
win.current["w"],
win.current["h"],
)
layer.fill()
# So it's going to be like a little window in the center of the VCStudio
# with a simple UI. Probably like 2 things. Folder and a projectname.
UI_color.set(layer, win, "node_background")
UI_elements.roundrect(layer, win,
win.current["w"]/2-250,
win.current["h"]/2-60,
500,
150,
10)
# Title of the operation. Incase the user forgot.
UI_elements.text(layer, win, "scan_project_title",
win.current["w"]/2-250,
win.current["h"]/2-35,
500,
30,
10,
fill=False,
centered=True,
editable=False)
win.text["scan_project_title"]["text"] = win.current["http-server"]["message"]
# File Progressbar
UI_color.set(layer, win, "progress_background")
UI_elements.roundrect(layer, win,
win.current["w"]/2-200,
win.current["h"]/2+10,
400,
20,
10,
tip="Hello")
UI_color.set(layer, win, "progress_active")
UI_elements.roundrect(layer, win,
win.current["w"]/2-200,
win.current["h"]/2+10,
400*max(0, min(1, win.current["http-server"]["fileprog"])),
20,
10,)
# Progressbar
UI_color.set(layer, win, "progress_background")
UI_elements.roundrect(layer, win,
win.current["w"]/2-200,
win.current["h"]/2+40,
400,
20,
10,
tip="Hello")
UI_color.set(layer, win, "progress_active")
UI_elements.roundrect(layer, win,
win.current["w"]/2-200,
win.current["h"]/2+40,
400*max(0, min(1, win.current["http-server"]["progress"])),
20,
10,)
return surface