44from functools import partial
55import os
66import json
7+ import requests
8+ import zipfile
9+ import shutil
10+ import asyncio
711
812app_icon = "Paintapp_Logo.png"
913software_name = "Paintapp"
1014software_dir = "paintapp"
1115is_GUI = True
1216min_size = (600 , 400 )
1317max_size = (900 , 500 )
18+ default_size = None
1419
1520# Use vars
1621color = "#000000"
2328
2429nbr_imported_images = 0
2530
31+ async def download_repo (URL :str , folder :str , files_to_delete :(list , tuple )= (".gitattributes" , "README.md" ), log :bool = True , notify_function = print , * args , ** kwargs ):
32+ """
33+ Downloads the specified repository into the specified folder.
34+ :param URL: The URL of the repository main archive.
35+ :param folder: The folder to place the repository in.
36+ :param files_to_delete: The files to delete after downloading the repository. DEFAULT : '.gitattributes', 'README.md'
37+ :param log: Log the informations in the console. DEFAULT : True
38+ """
39+ if log : notify_function ("Downloading... This might take a while." , * args , ** kwargs )
40+ r = requests .get (URL )
41+ assert r .status_code == 200 , "Something happened.\n Status code : " + str (r .status_code )
42+
43+ if log : notify_function ("Writing the zip..." , * args , ** kwargs )
44+ # Writing the zip
45+ with open (f"{ folder } .zip" , "wb" ) as code :
46+ code .write (r .content )
47+ code .close ()
48+
49+ # Creating a folder for the zip content
50+ if not os .path .exists (folder ):
51+ os .mkdir (folder )
52+
53+ if log : notify_function ("Extracting..." , * args , ** kwargs )
54+ # Extracting the zip
55+ with zipfile .ZipFile (f"{ folder } .zip" , "r" ) as zip_ref :
56+ zip_ref .extractall (folder )
57+
58+ if log : notify_function ("Moving the files..." , * args , ** kwargs )
59+ suffix = URL .split ("/" )[- 1 ].replace (".zip" , "" , 1 )
60+ repo_name = URL .split ("/" )[4 ]
61+ # Moving the file to parent
62+ for filename in os .listdir (os .path .join (folder , f'{ repo_name } -{ suffix } ' )):
63+ shutil .move (os .path .join (folder , f'{ repo_name } -{ suffix } ' , filename ), os .path .join (folder , filename ))
64+ # Deleting unnecessary files
65+ shutil .rmtree (f"{ folder } /{ repo_name } -{ suffix } " )
66+ os .remove (f"{ folder } .zip" )
67+ for file in files_to_delete :
68+ try :
69+ os .remove (f"{ folder } /{ file } " )
70+ except FileNotFoundError :
71+ pass
72+
73+ if log : notify_function ("Download complete !" , * args , ** kwargs )
74+
2675os .chdir (os .path .dirname (os .path .realpath (__file__ )))
2776# Translations
2877if software_api .REGISTRY ["SYSTEM_LANG" ].lower () == "fr" :
3180 translation_file = open ("translations_en.json" , "r" )
3281TRANSLATIONS = json .load (translation_file )
3382translation_file .close ()
83+
84+ # Installing GhostScript if not installed
85+ if not os .path .exists ("gs9.53.3/" ):
86+ software_api .notify (software_name , "Downloading GhostScript..." )
87+
88+ def notifier (text ):
89+ software_api .notify (software_name , text )
90+
91+ asyncio .run (download_repo ("https://github.com/megat69/GhostScript/archive/refs/heads/main.zip" , "gs.53.3" , notify_function = notifier ))
92+
3493os .chdir ("../../../" )
3594
3695def on_app_launch (frame :tk .Frame , width :int = 100 , height :int = 100 ):
@@ -252,7 +311,7 @@ def import_image(path):
252311 global nbr_imported_images
253312 os .chdir (os .path .dirname (os .path .realpath (__file__ )))
254313
255- path_content = path .get ()
314+ path_content = path .get () if not isinstance ( path , str ) else path
256315
257316 # Import the image
258317 try :
@@ -261,7 +320,12 @@ def import_image(path):
261320 f"/_images/_{ software_dir } /" + path_content .replace ("../" , "" )
262321 )
263322 except Exception as e :
264- path .set (TRANSLATIONS ["UnableLoadImage" ])
323+ print (path )
324+ try :
325+ path .set (TRANSLATIONS ["UnableLoadImage" ] + " " + path_content )
326+ except :
327+ pass
328+ software_api .notify (software_name , f"Unable to load the image { path_content } " )
265329 print (e )
266330 return None
267331
@@ -289,3 +353,6 @@ def apply_canvas_size(width, height):
289353 width = width ,
290354 height = height
291355 )
356+
357+ def on_file_open (path ):
358+ import_image (path .split ("/" )[- 1 ])
0 commit comments