4
4
from functools import partial
5
5
import os
6
6
import json
7
+ import requests
8
+ import zipfile
9
+ import shutil
10
+ import asyncio
7
11
8
12
app_icon = "Paintapp_Logo.png"
9
13
software_name = "Paintapp"
10
14
software_dir = "paintapp"
11
15
is_GUI = True
12
16
min_size = (600 , 400 )
13
17
max_size = (900 , 500 )
18
+ default_size = None
14
19
15
20
# Use vars
16
21
color = "#000000"
23
28
24
29
nbr_imported_images = 0
25
30
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
+
26
75
os .chdir (os .path .dirname (os .path .realpath (__file__ )))
27
76
# Translations
28
77
if software_api .REGISTRY ["SYSTEM_LANG" ].lower () == "fr" :
31
80
translation_file = open ("translations_en.json" , "r" )
32
81
TRANSLATIONS = json .load (translation_file )
33
82
translation_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
+
34
93
os .chdir ("../../../" )
35
94
36
95
def on_app_launch (frame :tk .Frame , width :int = 100 , height :int = 100 ):
@@ -252,7 +311,7 @@ def import_image(path):
252
311
global nbr_imported_images
253
312
os .chdir (os .path .dirname (os .path .realpath (__file__ )))
254
313
255
- path_content = path .get ()
314
+ path_content = path .get () if not isinstance ( path , str ) else path
256
315
257
316
# Import the image
258
317
try :
@@ -261,7 +320,12 @@ def import_image(path):
261
320
f"/_images/_{ software_dir } /" + path_content .replace ("../" , "" )
262
321
)
263
322
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 } " )
265
329
print (e )
266
330
return None
267
331
@@ -289,3 +353,6 @@ def apply_canvas_size(width, height):
289
353
width = width ,
290
354
height = height
291
355
)
356
+
357
+ def on_file_open (path ):
358
+ import_image (path .split ("/" )[- 1 ])
0 commit comments