Skip to content

Commit 5f7c7c3

Browse files
authored
Merge pull request #58 from Deric-W/Issueä57
Issue#57
2 parents 44575bf + dfaadf7 commit 5f7c7c3

File tree

8 files changed

+91
-55
lines changed

8 files changed

+91
-55
lines changed

debian/postinst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# postinst script which compiles python scripts
55

66
if which py3compile >/dev/null 2>&1; then
7-
py3compile -p python3-pyhp-core
7+
py3compile -p python3-pyhp-core
88
elif which python3 >/dev/null 2>&1; then
99
python3 -m compileall -q /usr/lib/python3/dist-packages/pyhp
1010
fi

debian/prerm

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55

66
case "$1" in
77
remove|upgrade|deconfigure)
8-
# clear cache
9-
set +e
10-
pyhp-backend clear >/dev/null 2>&1
11-
ret="$?"
12-
set -e
13-
case "$ret" in
14-
0) echo "Info: PyHP backend is a cache and has been cleared";;
15-
1) echo "Warning: Exception while clearing PyHP backend";;
16-
3) echo "Info: PyHP backend is not a cache and has not been cleared";;
17-
*) echo "Warning: Error while clearing PyHP backend";;
18-
esac
8+
# clear cache if toml module is installed
9+
if which python3 >/dev/null 2>&1 && python3 -c "import toml" >/dev/null 2>&1; then
10+
set +e
11+
pyhp-backend clear >/dev/null 2>&1
12+
ret="$?"
13+
set -e
14+
case "$ret" in
15+
0) echo "Info: PyHP backend is a cache and has been cleared";;
16+
1) echo "Warning: Exception while clearing PyHP backend";;
17+
3) echo "Info: PyHP backend is not a cache and has not been cleared";;
18+
*) echo "Warning: Error while clearing PyHP backend";;
19+
esac
20+
else
21+
echo "Info: not clearing PyHP backend because python3-toml is not installed"
22+
fi
1923

2024
# remove __pycache__
2125
if which py3clean >/dev/null 2>&1; then

pyhp/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"compiler",
2525
"backends",
2626
"wsgi",
27-
"main"
27+
"main",
28+
"config"
2829
)

pyhp/backends/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from typing import Callable, Union, Optional, Tuple, List, Iterable, TextIO
2020
import toml
2121
from .. import __version__
22-
from ..main import load_config
22+
from ..config import load_config
2323
from ..compiler import parsers, util
2424
from . import CodeSourceContainer, CodeSource, TimestampedCodeSource
2525
from .caches import CacheSource, CacheSourceContainer

pyhp/config.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/python3
2+
3+
"""Module containing configuration utilities"""
4+
# The config module is part of PyHP (https://github.com/Deric-W/PyHP)
5+
# Copyright (C) 2021 Eric Wolf
6+
7+
# This program is free software: you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation, version 3.
10+
11+
# You should have received a copy of the GNU General Public License
12+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
13+
# SPDX-License-Identifier: GPL-3.0-only
14+
15+
import os
16+
import os.path
17+
from typing import Any, Iterable, MutableMapping
18+
import toml
19+
20+
__all__ = (
21+
"CONFIG_LOCATIONS",
22+
"load_config"
23+
)
24+
25+
CONFIG_LOCATIONS = (
26+
os.path.expanduser("~/.config/pyhp.toml"),
27+
"/etc/pyhp.toml"
28+
)
29+
30+
31+
def load_config(search_paths: Iterable[str] = CONFIG_LOCATIONS) -> MutableMapping[str, Any]:
32+
"""locate and parse the config file"""
33+
try:
34+
path = os.environ["PYHPCONFIG"]
35+
except KeyError:
36+
pass
37+
else:
38+
return toml.load(path)
39+
for path in search_paths:
40+
try:
41+
return toml.load(path)
42+
except FileNotFoundError:
43+
pass
44+
raise RuntimeError("failed to locate the config file")

pyhp/main.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,20 @@
1313
# SPDX-License-Identifier: GPL-3.0-only
1414

1515
import sys
16-
import os
1716
import argparse
1817
from wsgiref.handlers import CGIHandler
19-
from typing import Any, Iterable, MutableMapping
2018
import toml
2119
from . import __version__
20+
from .config import load_config
2221
from .wsgi.apps import SimpleWSGIApp
2322
from .wsgi.util import SimpleWSGIAppFactory
2423
from .backends.memory import MemorySource
2524

2625

2726
__all__ = (
28-
"CONFIG_LOCATIONS",
2927
"argparser",
3028
"cli_main",
31-
"cgi_main",
32-
"load_config"
33-
)
34-
35-
CONFIG_LOCATIONS = (
36-
os.path.expanduser("~/.config/pyhp.toml"),
37-
"/etc/pyhp.toml"
29+
"cgi_main"
3830
)
3931

4032
argparser = argparse.ArgumentParser(
@@ -88,22 +80,6 @@ def handle_error(self) -> None:
8880
self.log_exception(sys.exc_info())
8981

9082

91-
def load_config(search_paths: Iterable[str] = CONFIG_LOCATIONS) -> MutableMapping[str, Any]:
92-
"""locate and parse the config file"""
93-
try:
94-
path = os.environ["PYHPCONFIG"]
95-
except KeyError:
96-
pass
97-
else:
98-
return toml.load(path)
99-
for path in search_paths:
100-
try:
101-
return toml.load(path)
102-
except FileNotFoundError:
103-
pass
104-
raise RuntimeError("failed to locate the config file")
105-
106-
10783
def cli_main() -> int:
10884
"""cli entry point"""
10985
args = argparser.parse_args()

tests/test_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/python3
2+
3+
"""Unit tests for pyhp.config"""
4+
5+
import os
6+
import unittest
7+
import toml
8+
from pyhp import config
9+
10+
11+
class TestFunctions(unittest.TestCase):
12+
"""test free functions"""
13+
14+
def test_load_config(self) -> None:
15+
"""test load_config"""
16+
with self.assertRaises(RuntimeError): # no config
17+
config.load_config()
18+
os.environ["PYHPCONFIG"] = "pyhp.toml" # env var
19+
try:
20+
self.assertEqual(config.load_config(), toml.load("pyhp.toml"))
21+
finally:
22+
del os.environ["PYHPCONFIG"]
23+
self.assertEqual( # search path
24+
config.load_config(("test1", "test2", "pyhp.toml")),
25+
toml.load("pyhp.toml")
26+
)

tests/test_main.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import unittest
99
import unittest.mock
1010
import subprocess
11-
import toml
1211
from pyhp import main
1312
from pyhp.compiler import util, generic, parsers
1413
from pyhp.backends import CodeSourceContainer
@@ -50,20 +49,6 @@ def test_invalid_path(self) -> None:
5049
stderr=subprocess.DEVNULL
5150
)
5251

53-
def test_config_location(self) -> None:
54-
"""test config file location"""
55-
with self.assertRaises(RuntimeError): # no config
56-
main.load_config()
57-
os.environ["PYHPCONFIG"] = "pyhp.toml" # env var
58-
try:
59-
self.assertEqual(main.load_config(), toml.load("pyhp.toml"))
60-
finally:
61-
del os.environ["PYHPCONFIG"]
62-
self.assertEqual( # search path
63-
main.load_config(("test1", "test2", "pyhp.toml")),
64-
toml.load("pyhp.toml")
65-
)
66-
6752
def test_cli_handler(self) -> None:
6853
"""test CLIHandler"""
6954
args = main.argparser.parse_args(["test.pyhp", "-a", "b"])

0 commit comments

Comments
 (0)