99from hashlib import sha256
1010from platform import system
1111from warnings import warn
12+ from requests import get as r_get
1213
1314__all__ = [
1415 'PackedProgram' , 'convert_file_to_executable' , 'deconvert' , 'create_archive'
1516]
1617__version__ = '0.0.1'
1718
18- shebang = b'#!/usr/bin/env -S python3 -m programpack run\n '
19- shebang_v = b'#!/usr/bin/env -S python3 -m programpack run --virtual\n '
20- _empty = ''
21- _emptyb = b''
22- _os = system ().strip ().lower ()
19+ shebang = b'#!/usr/bin/env -S python3 -m programpack run\n '
20+ shebang_v = b'#!/usr/bin/env -S python3 -m programpack run --virtual\n '
21+ _empty = ''
22+ _emptyb = b''
23+ _os = system ().strip ().lower ()
24+ _server = 'https://github.com/ProgramPack/hub'
25+ _request_base = '/raw/main/apps/'
26+ _server_p_rbase = '{}{}' .format (_server , _request_base )
2327
28+ def _get_text (url ): return str (r_get (url ).text )
29+ def _get_json (url ): return loads (_get_text (url ).strip ())
2430def _decode (b : bytes or bytearray ) -> str : return b .decode ('utf-8' )
2531def _PropertyBlocked (): raise RuntimeError ('Property is privated; blocked' )
2632def _get_file_sha256 (filename : str = '' ):
@@ -67,6 +73,10 @@ def read_main_file(self):
6773 return self .archive .read (self .main_file )
6874 def read_resources (self ) -> dict :
6975 '''Read "Resources" folder and return dictionary'''
76+ warn (
77+ 'Function "read_resources" may be deprecated in future' ,
78+ DeprecationWarning
79+ )
7080 resources_dict = {}
7181 for resource in self .archive .namelist ():
7282 if resource .startswith (self .resfold ):
@@ -89,15 +99,17 @@ def run(self, w_resources: bool = True, autocall: bool = True, delete: bool = Tr
8999 args = [tempf_name ]
90100 args .extend (self .call_args )
91101 if w_resources :
92- res = self .read_resources ()
93102 tmpresfold_n = join (self .tmpresfold , self .generate_unique_id ())
94- if res :
95- mkdir (tmpresfold_n , exist_ok = True )
96- for key in res :
97- value = res [key ]
98- if key and value :
99- with open (join (tmpresfold_n , key ), 'wb+' ) as f :
100- f .write (value ['content' ])
103+ mkdir (tmpresfold_n , exist_ok = True )
104+ for current_file in self .archive .namelist ():
105+ if current_file .startswith (self .resfold ):
106+ dest = join (tmpresfold_n , current_file .removeprefix (self .resfold ))
107+ if current_file .endswith (sep ): mkdir (dest , exist_ok = True )
108+ else :
109+ self .archive .extract (current_file , tmpresfold_n )
110+ move_file (join (tmpresfold_n , current_file ), join (tmpresfold_n , dest ))
111+ rmtree (join (tmpresfold_n , 'Resources' ), ignore_errors = True )
112+ del current_file
101113 args .append (tmpresfold_n )
102114 if autocall :
103115 if virtual :
@@ -145,6 +157,7 @@ def update_icon(self, _clean: bool = False) -> bool:
145157 )
146158 remove (tempf .name )
147159 return True
160+ def generate_unique_id_local (name : str = 'unnamed' , domain : str = 'com' , author : str = 'unknown' ): return '{}.{}.{}' .format (domain , author , name )
148161def convert_file_to_executable (file_name : str = '' , virtual : bool = False ):
149162 '''Make the file executable by other programs'''
150163 chmod (file_name , 0o777 )
@@ -167,3 +180,11 @@ def get_manifest(file_name: str = '') -> dict or bool:
167180 program = PackedProgram (str (file_name ).strip ())
168181 program .read ()
169182 return program .manifest
183+ def hub_get_id_by (name : str , domain : str , author : str , * args , ** kwargs ): return _server_p_rbase + generate_unique_id_local (name , domain , author , * args , ** kwargs ) + '.json'
184+ def hub_get_meta (name : str , domain : str , author : str ): return _get_json (hub_get_id_by (name , domain , author ))
185+ def hub_download_s (name : str , domain : str , author : str ): return _get_text (hub_get_meta (name , domain , author )['link' ])
186+ def hub_download (name : str , domain : str , author : str , output : str = 'download.propack' ):
187+ data = hub_download_s (name , domain , author )
188+ with open (str (output ).strip (), 'w+' ) as f :
189+ f .write (data )
190+ chmod (output , 0o777 )
0 commit comments