Skip to content

Commit 2cc3e2f

Browse files
authored
Merge pull request #27 from Deric-W/caching-subsystem
Caching subsystem
2 parents 2d097f8 + ea0ab5e commit 2cc3e2f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+3950
-455
lines changed

.gitattributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# disable automatic eol conversions
2-
* text=false
2+
* -text

.github/workflows/Tests.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
run: python setup.py bdist_wheel
4141

4242
- name: Install wheel
43-
run: python -m pip install ./dist/*.whl
43+
run: python -m pip install "dist/pyhp_core-$(python setup.py --version)-py3-none-any.whl[CONFIG]"
4444
shell: bash
4545

4646
- name: run tests

README.md

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PyHP-Interpreter ![Tests](https://github.com/Deric-W/PyHP/workflows/Tests/badge.svg) [![codecov](https://codecov.io/gh/Deric-W/PyHP/branch/master/graph/badge.svg?token=SA72E6KGXT)](https://codecov.io/gh/Deric-W/PyHP) [![License: GPL v3](https://img.shields.io/badge/License-GPLv3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
22

3-
The PyHP Interpreter is a package that allows you to embed Python code like PHP code into HTML and other text files.
4-
The script is called either by the configuration of the web server or a shebang and communicates with the web server via CGI.
3+
PyHP is a package that allows you to embed Python code like PHP code into HTML and other text files.
4+
A script is called either by the configuration of the web server or a shebang and communicates with the web server via CGI.
55

66
## Features:
77

@@ -14,10 +14,8 @@ The script is called either by the configuration of the web server or a shebang
1414
## How it works:
1515

1616
- Python code is contained within the `<?pyhp` and `?>` tags (like PHP)
17-
- the program is called like a interpreter, with the filepath as cli parameter
18-
- if no filepath is given, the program is reading from stdin
19-
- if the `-c` or `--caching` is given, the cache will be enabled and the file will additionally be preprocessed if needed
20-
and cached in cache_path/absolute/path/of/filename.cache
17+
- the program is called like a interpreter, with a name as cli parameter
18+
- if no name is given, the program is reading from stdin, else it is using the name to load code from the backend configured in pyhp.toml
2119
- python code is allowed to have a starting indentation for better readability inside (for example) HTML files
2220
- the following PHP features are available as methods of the `PyHP` class (available from the outside in pyhp.libpyhp):
2321
- `$_SERVER` as `SERVER`
@@ -34,21 +32,23 @@ The script is called either by the configuration of the web server or a shebang
3432
- `setcookie` with an additional `samesite` keyword argument
3533
- `setrawcookie` also with an additional `samesite` keyword argument
3634
- `register_shutdown_function`
37-
38-
39-
## Cache Handlers
35+
36+
## Config file
37+
38+
- is valid toml
39+
- is looked for in these locations (no merging takes place, the first file wins):
40+
- the path given by the `-c` or `--config` cli argument
41+
- the path pointed to by the `PYHPCONFIG` environment variable
42+
- `~/.config/pyhp.toml`
43+
- `/etc/pyhp.toml`
44+
- raises a `RuntimeError` if not found
4045

41-
- are responsible for saving/loading/renewing caches
42-
- are python scripts with the following contents:
43-
- the `Handler` class, wich takes the cache path, absolute file path and `caching` section of the config file as
44-
initialization parameters and provides the following methods:
45-
- `is_available`, wich returns a boolean indicating if the handler can be used
46-
- `is_outdated`, wich returns a boolean indicating if the cache needs to be renewed
47-
- `save`, wich takes an code object as an argument and saves it in the cache
48-
- `load`, wich loads an code object from the cache
49-
- `close`, wich does cleanup tasks and gets called when used as a context manager
50-
- note that code objects have to support the pickle protocol
51-
- examples are available in the *cache_handlers* directory
46+
## Backends
47+
48+
- implement code retrieval or decorate other backends to add i.a. caching
49+
- act as containers for CodeSources
50+
- form a hierarchy configured in pyhp.toml
51+
- are contained inside `pyhp.backends`
5252

5353
## Installation
5454

@@ -58,22 +58,15 @@ The script is called either by the configuration of the web server or a shebang
5858
### Just as python package
5959
1. build the *pyhp-core* python package with `python3 setup.py bdist_wheel`
6060
2. Done! You can now install the wheel contained in the *dist* directory with pip
61-
62-
### As application
63-
If you just installed the python package, then you have to provide `--config` with every call of `python3 -m pyhp`
64-
and can't use the caching feature.
65-
To stop this, you can build a debian package or install PyHP manually.
66-
67-
#### Debian package
61+
62+
### Debian package
6863
1. build the *pyhp-core* python package with `python3 setup.py bdist_wheel`
6964
2. go to the *debian* directory and execute `./build_deb.sh`
7065
3. enter a package version, the path of the *pyhp-core* wheel and the pip command you wish to use
7166
4. Done! You can now install the debian package with `sudo dpkg -i pyhp_<package version>_all.deb`
72-
73-
#### Manually
67+
68+
### Manually
7469
1. install the *pyhp-core* python package
75-
2. copy *pyhp.conf* to */etc*
76-
3. copy *cache_handlers* to */lib/pyhp/*
77-
4. copy *debian/pyhp* to a directoy in your PATH
78-
5. Done! You can now use the `pyhp` command
79-
70+
2. copy *pyhp.toml* to one of the config file locations
71+
3. Done! You can now use the `pyhp` command
72+

cache_handlers/files_mtime.py

Lines changed: 0 additions & 58 deletions
This file was deleted.

debian/build_deb.sh

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,33 @@ then read -p "pip executeable: " pip
1818
else pip=$3
1919
fi
2020

21-
package="pyhp_"$version"_all"
21+
package="pyhp_${version}_all"
2222

2323
mkdir "$package"
2424

25-
# place config file, cache handlers and "executable"
26-
mkdir -p "$package/lib/pyhp/cache_handlers"
27-
cp ../cache_handlers/* "$package/lib/pyhp/cache_handlers"
28-
29-
mkdir "$package/etc"
30-
cp ../pyhp.conf "$package/etc"
25+
# place pyhp-core files
26+
mkdir -p "${package}/usr/lib/python3/dist-packages"
27+
$pip install --target "${package}/usr/lib/python3/dist-packages" --ignore-installed --no-compile "$wheel"
3128

32-
mkdir -p "$package/usr/bin"
33-
cp pyhp "$package/usr/bin"
34-
chmod +x "$package/usr/bin/pyhp"
29+
# place config file and "executable"
30+
mkdir "${package}/etc"
31+
cp ../pyhp.toml "${package}/etc"
3532

36-
# place pyhp-core files
37-
mkdir -p "$package/usr/lib/python3/dist-packages"
38-
$pip install --target "$package/usr/lib/python3/dist-packages" --ignore-installed $wheel
33+
mkdir -p "${package}/usr/bin"
34+
mv "${package}/usr/lib/python3/dist-packages/bin/pyhp" "${package}/usr/bin"
35+
rmdir "${package}/usr/lib/python3/dist-packages/bin"
36+
chmod +x "${package}/usr/bin/pyhp"
3937

4038
# place metadata files
4139
mkdir "$package/DEBIAN"
4240
# calculate installed size
43-
cat control | python3 format.py "$version" $(du -sk --apparent-size --exclude "DEBIAN" "$package" 2>/dev/null | cut -f1) > "$package/DEBIAN/control"
41+
cat control | python3 format.py "${version}" $(du -sk --apparent-size --exclude "DEBIAN" "${package}" 2>/dev/null | cut -f1) > "${package}/DEBIAN/control"
4442
cp conffiles "$package/DEBIAN"
4543

46-
mkdir -p "$package/usr/share/doc/pyhp"
47-
cp copyright "$package/usr/share/doc/pyhp"
48-
cp changelog "$package/usr/share/doc/pyhp/changelog.Debian"
49-
gzip -n --best "$package/usr/share/doc/pyhp/changelog.Debian"
44+
mkdir -p "${package}/usr/share/doc/pyhp"
45+
cp copyright "${package}/usr/share/doc/pyhp"
46+
cp changelog "${package}/usr/share/doc/pyhp/changelog.Debian"
47+
gzip -n --best "${package}/usr/share/doc/pyhp/changelog.Debian"
5048

5149
# generate md5sums file
5250
chdir "$package"
@@ -67,3 +65,5 @@ dpkg-deb --build "$package"
6765
rm -r "$package"
6866

6967
echo "Done"
68+
69+
exit 0

debian/changelog

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
pyhp (2.2-1) stable; urgency=low
2+
3+
* sixth release
4+
* replace cache handlers with backends submodule
5+
* add unbounded version of memory cache
6+
* add files, zipfiles and memory backend
7+
* fix race condition in file cache
8+
* change config format to TOML
9+
10+
-- Eric Wolf <[email protected]> Sun, 14 Mar 2021 19:00:00 +0100
11+
112
pyhp (2.1-1) stable; urgency=low
213

314
* fifth release

debian/conffiles

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
/etc/pyhp.conf
1+
/etc/pyhp.toml

debian/pyhp

Lines changed: 0 additions & 28 deletions
This file was deleted.

pyhp.conf

Lines changed: 0 additions & 51 deletions
This file was deleted.

pyhp.toml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Configuration for PyHP (https://github.com/Deric-W/PyHP)
2+
# This file uses the TOML syntax
3+
4+
[parser]
5+
# regular expression matching the start of a code section
6+
start = '<\?pyhp\s'
7+
8+
# regular expression matching the end of a code section
9+
end = '\s\?>'
10+
11+
12+
[compiler]
13+
# if python code should be stripped of a starting indentation
14+
dedent = true
15+
16+
# optimization level to be used with the builtin compile() function
17+
# all levels: https://docs.python.org/3/library/functions.html#compile
18+
optimization_level = -1
19+
20+
21+
[backend]
22+
# how container names should be resolved to objects
23+
# module: the name is a module with a dot and the container name appended to it
24+
# module.submodule.name
25+
# path: the name is a file path with a double colon and the container name appended to it
26+
# /path/to/file.py:name
27+
resolve = "module"
28+
29+
# array of containers to build a hierarchy
30+
[[backend.containers]]
31+
# the name of the container
32+
name = "pyhp.backends.files.Directory"
33+
34+
# configuration options for the container
35+
config.path = "."
36+
37+
38+
[request]
39+
# order to fill up REQUEST, values already present in REQUEST get overwritten
40+
# unknown methods are skipped
41+
request_order = [
42+
"GET",
43+
"POST",
44+
"COOKIE"
45+
]
46+
47+
# if arameters with no or blank values should be kept
48+
keep_blank_values = true
49+
50+
# fallback value if a parameter is not in REQUEST / GET / POST / COOKIE
51+
# comment out if a KeyError should be raised instead
52+
fallback_value = ""
53+
54+
# if stdin should not be consumed
55+
# this results in POST not being populated
56+
enable_post_data_reading = false
57+
58+
# fallback value of the Content-Type header
59+
default_mimetype = "text/html"

0 commit comments

Comments
 (0)