11import urllib .parse
22from datetime import datetime
3- from random import randint
43from typing import Any
54import importlib .util
65import importlib
76import traceback
87import getpass
98import socket
10- import json
11- import sys
129import os
1310import re
1411from .widgets import *
15- from . import protocol
1612from .login import *
13+ import requests
14+ import TheProtocols
15+
1716
1817https_codes = {
19- 404 : 'Not Found' ,
20- 500 : 'Internal Server Error' ,
2118 200 : 'OK' ,
19+ 201 : 'Created' ,
20+ 202 : 'Accepted' ,
21+ 203 : 'Non-Authoritative Information' ,
22+ 204 : 'No Content' ,
23+ 205 : 'Reset Content' ,
2224 301 : 'Moved Permanently' ,
23- 303 : 'See Other'
25+ 302 : 'Found' ,
26+ 303 : 'See Other' ,
27+ 307 : 'Temporary Redirect' ,
28+ 308 : 'Permanent Redirect' ,
29+ 400 : 'Bad Request' ,
30+ 401 : 'Unauthorized' ,
31+ 402 : 'Payment Required' ,
32+ 403 : 'Forbidden' ,
33+ 405 : 'Method Not Allowed' ,
34+ 404 : 'Not Found' ,
35+ 410 : 'Gone' ,
36+ 415 : 'Unsupported Media Type' ,
37+ 418 : 'I\' m a teapot' ,
38+ 429 : 'Too Many Requests' ,
39+ 451 : 'Unavailable For Legal Reasons' ,
40+ 500 : 'Internal Server Error' ,
41+ 501 : 'Not Implemented' ,
42+ 502 : 'Bad Gateway' ,
43+ 503 : 'Service Unavailable' ,
44+ 504 : 'Gateway Timeout' ,
45+ 506 : 'Variant Also Negotiates' ,
46+ 511 : 'Network Authentication Required'
2447}
2548admins = {}
2649mime_types = {
@@ -64,6 +87,7 @@ def __init__(self, data: dict[str], app) -> None:
6487 try :
6588 self .email = data ['credentials' ]['email' ]
6689 self .password = data ['credentials' ]['password' ]
90+ self .user = TheProtocols .ID (self .email , self .password )
6791 except TypeError :
6892 pass
6993 self .app = app
@@ -82,17 +106,12 @@ class Response:
82106 def __init__ (self , body : (Page , str , dict , list ), ** kwargs ) -> None :
83107 self .body = body
84108 self .credentials = {}
85- kwargs_info = {
86- 'status_code' : [int , 200 ],
87- 'headers' : [dict , {
88- 'Content-Type' : 'text/html; charset=utf-8'
89- }]
90- }
109+ kwargs_info = ['status_code' , 'headers' ]
110+ self .headers = {'Content-Type' : 'text/html; charset=utf-8' }
111+ self .status_code = 200
91112 for kwarg in kwargs_info :
92113 if kwarg in kwargs :
93114 setattr (self , kwarg , kwargs_info [kwarg ][0 ](kwargs [kwarg ]))
94- else :
95- setattr (self , kwarg , kwargs_info [kwarg ][1 ])
96115
97116 def login (self , email , password ) -> None :
98117 self .credentials = {'email' : email , 'password' : password }
@@ -117,30 +136,33 @@ def __init__(self, package, developer, administrator, secret, style, icon='/favi
117136 self .routes = {}
118137 if isinstance (style , dict ):
119138 self .style = style
120- else :
139+ elif isinstance ( style , str ) :
121140 if os .path .isfile (style ):
122141 self .style = json .load (open (style , 'rb' ))
123- elif style .startswith ('https://' ) or style . startswith ( 'http://' ) :
124- r = protocol . requests .get (style )
142+ elif style .startswith ('https://' ):
143+ r = requests .get (style )
125144 if r .status_code == 200 :
126145 self .style = r .json ()
146+ else :
147+ self .style = {}
148+ else :
149+ self .style = {}
150+ else :
151+ self .style = {}
127152 self .icon = icon
128153 self .keywords = keywords
129- if not administrator :
130- admins .update ({self .package : protocol .get_admin (
131- administrator ,
132- getpass .getpass (f'Password for { administrator } : ' ))
133- })
154+ if administrator :
155+ self .admin = TheProtocols .ID (administrator , getpass .getpass (f'Password for { administrator } : ' ))
134156 print ()
135- admin = get_admin (package )
136- print (f"Welcome { admin ['name' ]} { admin ['surname' ]} !" )
157+ print (f"Welcome { self .admin .id } !" )
137158 print ()
138159
139- def route (self , path ) -> Any :
160+ def route (self , path , whitelist : list = None , blacklist : list = None ) -> Any :
140161 def decorator (func ) -> Any :
141162 self .routes .update ({path : func })
142- def wrapper (* args , ** kwargs ) -> Any :
143- return func (* args , ** kwargs )
163+
164+ def wrapper (r : Request , ** kwargs ) -> Any :
165+ return func (r , ** kwargs )
144166 return wrapper
145167 return decorator
146168
@@ -205,7 +227,7 @@ def error_handler(self, request, status_code, exception) -> Response:
205227 title = https_codes [status_code ],
206228 style = self .style ,
207229 childs = e_boxes ,
208- selector = f'body_{ current_user ( request ) .settings .theme_color } '
230+ selector = f'body_{ request . user . id .settings .theme_color } '
209231 ),
210232 status_code = status_code
211233 )
@@ -279,8 +301,6 @@ def run(self, host: str = '127.0.0.1', port: int = 5000, debug: bool = True):
279301 page_script_path = page_script_path .replace ('//' , '/' )
280302 if not os .path .isfile (page_script_path ):
281303 page_script_path += '/__init__.py'
282- if not os .path .isfile (page_script_path ):
283- resp = self .error_handler (Request (recv , self ), 404 , '' )
284304 if os .path .isfile (page_script_path ):
285305 if page_script_path .endswith ('.py' ):
286306 page_script_spec = importlib .util .spec_from_file_location (
@@ -290,7 +310,10 @@ def run(self, host: str = '127.0.0.1', port: int = 5000, debug: bool = True):
290310 page_script = importlib .util .module_from_spec (page_script_spec )
291311 try :
292312 page_script_spec .loader .exec_module (page_script )
293- resp = getattr (page_script , recv ['method' ].lower ())(Request (recv , self ))
313+ if recv ['method' ].lower () in page_script .__dict__ :
314+ resp = getattr (page_script , recv ['method' ].lower ())(Request (recv , self ))
315+ else :
316+ resp = self .error_handler (Request (recv , self ), 405 , '' )
294317 except FileNotFoundError :
295318 resp = self .error_handler (Request (recv , self ), 404 , '' )
296319 else :
@@ -306,7 +329,9 @@ def run(self, host: str = '127.0.0.1', port: int = 5000, debug: bool = True):
306329 'Connection' : 'keep-alive'
307330 }
308331 )
309- except Exception as e :
332+ else :
333+ resp = self .error_handler (Request (recv , self ), 404 , '' )
334+ except Exception :
310335 resp = self .error_handler (Request (recv , self ), 500 , traceback .format_exc ())
311336 if isinstance (resp , Page ):
312337 resp .data ['lang' ] = ''
0 commit comments