Automated update of Base Application package.

This commit is contained in:
bot 2024-12-03 13:19:34 +00:00
parent 6f1d1262e9
commit da6ba57b48
11 changed files with 142 additions and 128 deletions

Binary file not shown.

BIN
dist/app-1.0.0.tar.gz vendored

Binary file not shown.

View File

@ -2,8 +2,8 @@ import logging
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
format="%(asctime)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
log = logging.getLogger(__name__)

View File

@ -1,13 +1,11 @@
from aiohttp import web
from .app import create_app
from .args import parse_args
from .server import serve
def main():
args = parse_args()
serve(args.host, args.port)
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@ -1,19 +1,32 @@
from aiohttp import web
import time
from . import log
import json
import time
import uuid
import dataset
from aiohttp import web
from . import log
def get_timestamp():
from datetime import datetime
now = datetime.now()
formatted_datetime = now.strftime("%Y-%m-%d %H:%M:%S")
return formatted_datetime
class BaseApplication(web.Application):
def __init__(self, username = None, password=None, cookie_name=None,session=None, *args, **kwargs):
def __init__(
self,
username=None,
password=None,
cookie_name=None,
session=None,
*args,
**kwargs,
):
self.cookie_name = cookie_name or str(uuid.uuid4())
self.username = username
self.password = password
@ -24,7 +37,6 @@ class BaseApplication(web.Application):
middlewares.append(self.session_middleware)
super().__init__(*args, **kwargs)
async def authenticate(self, username, password):
return self.username == username and self.password == password
@ -37,7 +49,7 @@ class BaseApplication(web.Application):
return web.Response(
status=401,
text="Unauthorized",
headers={"WWW-Authenticate": 'Basic realm="Restricted"'}
headers={"WWW-Authenticate": 'Basic realm="Restricted"'},
)
try:
@ -51,21 +63,25 @@ class BaseApplication(web.Application):
return web.Response(
status=401,
text="Invalid Credentials",
headers={"WWW-Authenticate": 'Basic realm="Restricted"'}
headers={"WWW-Authenticate": 'Basic realm="Restricted"'},
)
return await handler(request)
@web.middleware
async def request_middleware(self, request, handler):
time_start = time.time()
created = get_timestamp()
request = await handler(request)
time_end = time.time()
await self.insert("http_access",dict(
created=created,
path=request.path,
duration=time_end - time_start,
))
await self.insert(
"http_access",
{
"created": created,
"path": request.path,
"duration": time_end - time_start,
},
)
return request
@web.middleware
@ -78,17 +94,15 @@ class BaseApplication(web.Application):
if not session_id:
session_id = str(uuid.uuid4())
response.set_cookie(
self.cookie_name,
session_id,
max_age=3600,
httponly=True
)
response.set_cookie(self.cookie_name, session_id, max_age=3600, httponly=True)
return response
class WebDbApplication(BaseApplication):
def __init__(self, db=None, db_web=False,db_path="sqlite:///:memory:",*args, **kwargs):
def __init__(
self, db=None, db_web=False, db_path="sqlite:///:memory:", *args, **kwargs
):
super().__init__(*args, **kwargs)
self.db_web = db_web
self.db_path = db_path
@ -103,43 +117,47 @@ class WebDbApplication(BaseApplication):
self.router.add_post("/delete", self.delete_handler)
async def insert_handler(self, request):
obj = await request.json()
await request.json()
response = await self.insert(request.get("table"), request.get("data"))
return web.json_response(response)
async def update_handler(self, request):
obj = await request.json()
response = await self.update(request.get('table'), request.get('data'), request.get('where',{}))
await request.json()
response = await self.update(
request.get("table"), request.get("data"), request.get("where", {})
)
return web.json_response(response)
async def upsert_handler(self, request):
obj = await request.json()
response = await self.upsert(request.get('table'), request.get('data'), request.get('keys',[]))
await request.json()
response = await self.upsert(
request.get("table"), request.get("data"), request.get("keys", [])
)
return web.json_response(response)
async def find_handler(self, request):
obj = await request.json()
response = await self.find(request.get('table'), requesst.get('where',{}))
await request.json()
response = await self.find(request.get("table"), requesst.get("where", {}))
return web.json_response(response)
async def find_one_handler(self, request):
obj = await request.json()
response = await self.find_one(request.get('table'), requesst.get('where',{}))
await request.json()
response = await self.find_one(request.get("table"), requesst.get("where", {}))
return web.json_response(response)
async def delete_handler(self, request):
obj = await request.json()
response = await self.delete(request.get('table'), requesst.get('where',{}))
await request.json()
response = await self.delete(request.get("table"), requesst.get("where", {}))
return web.json_response(response)
async def set(self, key, value):
value = json.dumps(value, default=str)
self.db['kv'].upsert(dict(key=key,value=value),['key'])
self.db["kv"].upsert({"key": key, "value": value}, ["key"])
async def get(self, key, default=None):
record = self.db['kv'].find_one(key=key)
record = self.db["kv"].find_one(key=key)
if record:
return json.loads(record.get('value','null'))
return json.loads(record.get("value", "null"))
return default
async def insert(self, table_name, data):
@ -168,8 +186,6 @@ class WebDbApplication(BaseApplication):
return self.db[table_name].delete(**where)
class Application(WebDbApplication):
def __init__(self, *args, **kwargs):
@ -196,12 +212,15 @@ class Application(WebDbApplication):
async def index_handler(self, request):
return web.json_response(dict(
request_count=await self.inc_request_count(),
timestamp=get_timestamp(),
uptime=self.uptime,
running_since=self.running_since
),content_type="application/json")
return web.json_response(
{
"request_count": await self.inc_request_count(),
"timestamp": get_timestamp(),
"uptime": self.uptime,
"running_since": self.running_since,
},
content_type="application/json",
)
def create_app(*args, **kwargs):

View File

@ -1,36 +1,31 @@
import argparse
def parse_args():
parser = argparse.ArgumentParser(
description="Async web service"
)
parser = argparse.ArgumentParser(description="Async web service")
parser.add_argument(
'--host',
"--host",
type=str,
required=False,
default="0.0.0.0",
help='Host to serve on. Default: 0.0.0.0.'
help="Host to serve on. Default: 0.0.0.0.",
)
parser.add_argument(
'--port',
"--port",
type=int,
required=False,
default=8888,
help='Port to serve on Default: 8888.'
help="Port to serve on Default: 8888.",
)
parser.add_argument(
'--url',
"--url",
type=str,
default="http://localhost:8888",
required=False,
help='Base URL.'
help="Base URL.",
)
return parser.parse_args()

View File

@ -1,10 +1,10 @@
import asyncio
from aiohttp import ClientSession
from .args import parse_args
import time
from aiohttp import ClientSession
from . import log
from .args import parse_args
async def cli_client(url):
@ -18,6 +18,7 @@ async def cli_client(url):
print(ex)
print(await response.text())
async def bench(url):
index = 0
while True:
@ -30,11 +31,12 @@ async def bench(url):
print(await response.text())
# print(await response.json())
time_end = time.time()
print("Request {}. Duration: {}".format(index,time_end-time_start))
print(f"Request {index}. Duration: {time_end - time_start}")
except Exception as ex:
log.exception(ex)
await asyncio.sleep(1)
def cli_bench():
args = parse_args()
asyncio.run(bench(args.url))
@ -44,9 +46,6 @@ def main():
args = parse_args()
asyncio.run(cli_client(args.url))
if __name__ == '__main__':
if __name__ == "__main__":
main()

View File

@ -1,17 +1,18 @@
#!/usr/bin/python3
import sys
import shutil
import sys
from readchar import readkey
def text_editor(init='', prompt=''):
'''
def text_editor(init="", prompt=""):
"""
Allow user to edit a line of text complete with support for line wraps
and a cursor | you can move back and forth with the arrow keys.
init = initial text supplied to edit
prompt = Decoration presented before the text (not editable and not returned)
'''
"""
term_width = shutil.get_terminal_size()[0]
ptr = len(init)
@ -25,26 +26,24 @@ def text_editor(init='', prompt=''):
copy = prompt + text.copy()
if ptr < len(text):
copy.insert(ptr + len(prompt), '|')
copy.insert(ptr + len(prompt), "|")
# Line wraps support:
if len(copy) > term_width:
cut = len(copy) + 3 - term_width
if ptr > len(copy) / 2:
copy = ['<'] * 3 + copy[cut:]
copy = ["<"] * 3 + copy[cut:]
else:
copy = copy[:-cut] + ['>'] * 3
copy = copy[:-cut] + [">"] * 3
# Display current line
print('\r' * term_width + ''.join(copy), end=' ' * (term_width - len(copy)))
print("\r" * term_width + "".join(copy), end=" " * (term_width - len(copy)))
# Read new character into c
if c in (53, 54):
# Page up/down bug
c = readkey()
if c == '~':
if c == "~":
continue
else:
c = readkey()
@ -77,7 +76,7 @@ def text_editor(init='', prompt=''):
num = ord(c)
if num in (13, 10): # Enter
print()
return ''.join(text)
return "".join(text)
elif num == 127: # Backspace
if text:
text.pop(ptr - 1)
@ -89,5 +88,6 @@ def text_editor(init='', prompt=''):
text.insert(ptr, c)
ptr += 1
if __name__ == "__main__":
print("Result =", text_editor('Edit this text', prompt="Prompt: "))
print("Result =", text_editor("Edit this text", prompt="Prompt: "))

View File

@ -8,6 +8,7 @@ def repl(**kwargs):
variables.update(kwargs)
code.interact(local=variables)
if __name__ == "__main__":
repl()

View File

@ -1,11 +1,14 @@
from aiohttp import web
from .app import create_app
from . import log
import time
from aiohttp import web
from . import log
from .app import create_app
def serve(host="0.0.0.0", port=8888):
app = create_app()
log.info("Serving on {}:{}".format(host,port))
log.info(f"Serving on {host}:{port}")
while True:
try:
web.run_app(app, host=host, port=port)

View File

@ -1,20 +1,19 @@
from .app import WebDbApplication
import unittest
import asyncio
from .app import WebDbApplication
class WebDbApplicationTestCase(unittest.IsolatedAsyncioTestCase):
def setUp(self):
self.db = WebDbApplication()
async def test_insert(self):
self.assertEqual(await self.db.insert("test",dict(test=True)), 1)
self.assertEqual(len(await self.db.find("test",dict(test=True, id=1))), 1)
self.assertEqual(await self.db.insert("test", {"test": True}), 1)
self.assertEqual(len(await self.db.find("test", {"test": True, "id": 1})), 1)
async def test_find(self):
print(await self.db.find("test", None))
# self.assertEqual(len(await self.db.find("test",dict(test=True, id=1))), 1)