aserver 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- aserver/__init__.py +4 -0
- aserver/renderer.py +90 -0
- aserver/server.py +47 -0
- aserver-0.1.0.dist-info/METADATA +117 -0
- aserver-0.1.0.dist-info/RECORD +9 -0
- aserver-0.1.0.dist-info/WHEEL +5 -0
- aserver-0.1.0.dist-info/entry_points.txt +2 -0
- aserver-0.1.0.dist-info/licenses/LICENSE +21 -0
- aserver-0.1.0.dist-info/top_level.txt +1 -0
aserver/__init__.py
ADDED
aserver/renderer.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
T_TEXT = "TEXT"
|
|
4
|
+
T_VAR = "VAR"
|
|
5
|
+
T_BLOCK = "BLOCK"
|
|
6
|
+
|
|
7
|
+
def tokenize(template_str):
|
|
8
|
+
pattern = re.compile(r'(\{\{.*?\}\}|\{%.*?%\})', re.DOTALL)
|
|
9
|
+
tokens = []
|
|
10
|
+
for chunk in pattern.split(template_str):
|
|
11
|
+
if not chunk:
|
|
12
|
+
continue
|
|
13
|
+
if chunk.startswith('{{'):
|
|
14
|
+
tokens.append((T_VAR, chunk[2:-2].strip()))
|
|
15
|
+
elif chunk.startswith('{%'):
|
|
16
|
+
tokens.append((T_BLOCK, chunk[2:-2].strip()))
|
|
17
|
+
else:
|
|
18
|
+
tokens.append((T_TEXT, chunk))
|
|
19
|
+
return tokens
|
|
20
|
+
|
|
21
|
+
class TextNode:
|
|
22
|
+
def __init__(self, text):
|
|
23
|
+
self.text = text
|
|
24
|
+
|
|
25
|
+
class VarNode:
|
|
26
|
+
def __init__(self, var_name):
|
|
27
|
+
self.var_name = var_name
|
|
28
|
+
|
|
29
|
+
class IfNode:
|
|
30
|
+
def __init__(self, condition, true_nodes):
|
|
31
|
+
self.condition = condition
|
|
32
|
+
self.true_nodes = true_nodes
|
|
33
|
+
|
|
34
|
+
class ForNode:
|
|
35
|
+
def __init__(self, loop_var, iterator_var, body_nodes):
|
|
36
|
+
self.loop_var = loop_var
|
|
37
|
+
self.iterator_var = iterator_var
|
|
38
|
+
self.body_nodes = body_nodes
|
|
39
|
+
|
|
40
|
+
def parse(tokens, until=None):
|
|
41
|
+
nodes = []
|
|
42
|
+
while tokens:
|
|
43
|
+
token_type, value = tokens[0]
|
|
44
|
+
if token_type == T_BLOCK:
|
|
45
|
+
parts = value.split()
|
|
46
|
+
command = parts[0]
|
|
47
|
+
if until and command == until:
|
|
48
|
+
tokens.pop(0)
|
|
49
|
+
return nodes
|
|
50
|
+
tokens.pop(0)
|
|
51
|
+
if command == 'if':
|
|
52
|
+
condition = parts[1]
|
|
53
|
+
true_branch = parse(tokens, until='endif')
|
|
54
|
+
nodes.append(IfNode(condition, true_branch))
|
|
55
|
+
elif command == 'for':
|
|
56
|
+
loop_var = parts[1]
|
|
57
|
+
iterator_var = parts[3]
|
|
58
|
+
body_branch = parse(tokens, until='endfor')
|
|
59
|
+
nodes.append(ForNode(loop_var, iterator_var, body_branch))
|
|
60
|
+
else:
|
|
61
|
+
tokens.pop(0)
|
|
62
|
+
if token_type == T_TEXT:
|
|
63
|
+
nodes.append(TextNode(value))
|
|
64
|
+
elif token_type == T_VAR:
|
|
65
|
+
nodes.append(VarNode(value))
|
|
66
|
+
return nodes
|
|
67
|
+
|
|
68
|
+
def render_nodes(nodes, context):
|
|
69
|
+
output = []
|
|
70
|
+
for node in nodes:
|
|
71
|
+
if isinstance(node, TextNode):
|
|
72
|
+
output.append(node.text)
|
|
73
|
+
elif isinstance(node, VarNode):
|
|
74
|
+
val = context.get(node.var_name, '')
|
|
75
|
+
output.append(str(val))
|
|
76
|
+
elif isinstance(node, IfNode):
|
|
77
|
+
if context.get(node.condition):
|
|
78
|
+
output.append(render_nodes(node.true_nodes, context))
|
|
79
|
+
elif isinstance(node, ForNode):
|
|
80
|
+
iterable = context.get(node.iterator_var, [])
|
|
81
|
+
for item in iterable:
|
|
82
|
+
loop_context = context.copy()
|
|
83
|
+
loop_context[node.loop_var] = item
|
|
84
|
+
output.append(render_nodes(node.body_nodes, loop_context))
|
|
85
|
+
return "".join(output)
|
|
86
|
+
|
|
87
|
+
def render(template_str, context):
|
|
88
|
+
tokens = tokenize(template_str)
|
|
89
|
+
ast = parse(tokens)
|
|
90
|
+
return render_nodes(ast, context)
|
aserver/server.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import http.server
|
|
2
|
+
from .renderer import *
|
|
3
|
+
|
|
4
|
+
class HandleHttp(http.server.BaseHTTPRequestHandler):
|
|
5
|
+
|
|
6
|
+
def do_GET(self):
|
|
7
|
+
reqpath = self.path
|
|
8
|
+
|
|
9
|
+
if reqpath == '/':
|
|
10
|
+
|
|
11
|
+
try:
|
|
12
|
+
index_path = self.server.index_path
|
|
13
|
+
data = self.server.data
|
|
14
|
+
with open(index_path, 'r', encoding='utf-8') as f:
|
|
15
|
+
index = render(open(index_path).read(), data)
|
|
16
|
+
except:
|
|
17
|
+
index = open('index.html').read()
|
|
18
|
+
self.send_response(200)
|
|
19
|
+
self.send_header('Content-type', 'text/html; charset=utf-8')
|
|
20
|
+
self.end_headers()
|
|
21
|
+
self.wfile.write(index.encode('utf-8'))
|
|
22
|
+
|
|
23
|
+
else:
|
|
24
|
+
self.send_response(404)
|
|
25
|
+
self.send_header('Content-type', 'text/plain')
|
|
26
|
+
self.end_headers()
|
|
27
|
+
self.wfile.write(b'404 not found')
|
|
28
|
+
|
|
29
|
+
def do_POST(self):
|
|
30
|
+
self.do_GET()
|
|
31
|
+
|
|
32
|
+
class ServerHTTP:
|
|
33
|
+
def __init__(self, address, index_path, data={}):
|
|
34
|
+
self.addre = address
|
|
35
|
+
self.reqq = HandleHttp
|
|
36
|
+
self.index_path = index_path
|
|
37
|
+
self.data = data
|
|
38
|
+
def serve(self):
|
|
39
|
+
server = http.server.HTTPServer(self.addre, self.reqq)
|
|
40
|
+
server.index_path = self.index_path
|
|
41
|
+
server.data = self.data
|
|
42
|
+
try:
|
|
43
|
+
print(f'server starting http://{self.addre[0]}:{self.addre[1]}...')
|
|
44
|
+
server.serve_forever()
|
|
45
|
+
except KeyboardInterrupt:
|
|
46
|
+
print('shutting down...')
|
|
47
|
+
server.server_close()
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: aserver
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: a custom-flask like basic web server module.
|
|
5
|
+
Author-email: aadisankar1 <aadisankar1001@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
8
|
+
Requires-Python: >=3.13
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Dynamic: license-file
|
|
12
|
+
|
|
13
|
+
# AServer
|
|
14
|
+
|
|
15
|
+
A fast, lightweight Python HTTP server featuring a built-in micro-Jinja template evaluation engine with zero third-party dependencies.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
* **Custom Template Engine**: Native syntax supporting loops (`{% for %}`), conditionals (`{% if %}`), and dynamic variable interpolation (`{{ var }}`).
|
|
22
|
+
* **Resource Safe**: Handles files securely with context managers to prevent memory leaks.
|
|
23
|
+
* **Modern Packaging**: PyPA compliant layout ready for PyPI distribution.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Project Layout
|
|
28
|
+
|
|
29
|
+
```text
|
|
30
|
+
AServer/
|
|
31
|
+
├── LICENSE
|
|
32
|
+
├── README.md
|
|
33
|
+
├── pyproject.toml
|
|
34
|
+
└── src/
|
|
35
|
+
└── aserver/
|
|
36
|
+
├── __init__.py
|
|
37
|
+
├── renderer.py
|
|
38
|
+
└── server.py
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## Installation
|
|
44
|
+
|
|
45
|
+
Install the package directly from your local repository root:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
pip install .
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
To install in editable/development mode:
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
pip install -e .
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
---
|
|
58
|
+
|
|
59
|
+
## Quick Start
|
|
60
|
+
|
|
61
|
+
### 1. Create your Template File (`index.html`)
|
|
62
|
+
|
|
63
|
+
```html
|
|
64
|
+
<div class="container">
|
|
65
|
+
<h1>Welcome, {{ username }}!</h1>
|
|
66
|
+
|
|
67
|
+
{% if show_list %}
|
|
68
|
+
<ul>
|
|
69
|
+
{% for item in items %}
|
|
70
|
+
<li><strong>Item:</strong> {{ item }}</li>
|
|
71
|
+
{% endfor %}
|
|
72
|
+
</ul>
|
|
73
|
+
{% endif %}
|
|
74
|
+
</div>
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### 2. Run the Server
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
from aserver import ServerHTTP
|
|
81
|
+
|
|
82
|
+
# Define address, template path, and initial context variables
|
|
83
|
+
address = ('127.0.0.1', 8080)
|
|
84
|
+
template = 'index.html'
|
|
85
|
+
context_data = {
|
|
86
|
+
"username": "Alice",
|
|
87
|
+
"show_list": True,
|
|
88
|
+
"items": ["Apples", "Guavas", "Cherries"]
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
# Initialize and start serving
|
|
92
|
+
server = ServerHTTP(address, template, data=context_data)
|
|
93
|
+
server.serve()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Configuration
|
|
99
|
+
|
|
100
|
+
You can also control application settings natively through the `pyproject.toml` file under the custom tool namespace:
|
|
101
|
+
|
|
102
|
+
```toml
|
|
103
|
+
[tool.aserver.settings]
|
|
104
|
+
host = "127.0.0.1"
|
|
105
|
+
port = 8080
|
|
106
|
+
default_template = "index.html"
|
|
107
|
+
|
|
108
|
+
[tool.aserver.initial_data]
|
|
109
|
+
username = "Developer"
|
|
110
|
+
site_title = "AServer Web App"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## License
|
|
116
|
+
|
|
117
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
aserver/__init__.py,sha256=eMMOfXyEJ6JKZwx7OyEZFe9XFw44hoA0P4_jgsOSCcY,126
|
|
2
|
+
aserver/renderer.py,sha256=cOBR0ptmxXRUhHhu27W4-N2rI38R3hIidFGcVz0cNSo,2966
|
|
3
|
+
aserver/server.py,sha256=54dkth_LfO0lLLnZJrpZAPHy7N7LA_RPg58PnJIvE-k,1564
|
|
4
|
+
aserver-0.1.0.dist-info/licenses/LICENSE,sha256=yTKRFHmCqHAJgNo6L7m0nAQde8URr9iZPfb6N-uE8n0,1091
|
|
5
|
+
aserver-0.1.0.dist-info/METADATA,sha256=c1GbW9UA8QwDYqFtkNy52Gihp4M0I75j-C6MPJm_31c,2501
|
|
6
|
+
aserver-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
7
|
+
aserver-0.1.0.dist-info/entry_points.txt,sha256=ipmHVaUwDnE_SWOEJVchBR7UuGflXbAdE8iQoCestyo,41
|
|
8
|
+
aserver-0.1.0.dist-info/top_level.txt,sha256=gwMBQ_9af5RXgp2tf8aGUCnWQKZSAyCnIgyWaYmUqWM,8
|
|
9
|
+
aserver-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Aadisankar NS
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
aserver
|