Skip to content

Commit 83c91a7

Browse files
authored
Merge pull request #83 from Hashmap-Software-Agency/81-static_files_directory_in_apps
81 static files directory in apps
2 parents a45c6da + 5fd43a3 commit 83c91a7

File tree

6 files changed

+80
-14
lines changed

6 files changed

+80
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pip install pyttman
142142
pyttman new app my_first_app
143143
144144
# Create an Ability module with files from a template
145-
pyttman new ability
145+
pyttman new ability ability_name my_first_app
146146
147147
# Run it in dev mode
148148
pyttman dev my_first_app

devtools/build.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
# Set the current working dir to parent directory
88
sys.path.append(Path.cwd().parent.as_posix())
9-
os.chdir("..")
10-
CATALOGS = ("build", "dist")
119

10+
CATALOGS = ("build", "dist")
1211
BUILD_CMD = "python -m setup sdist bdist_wheel".split()
1312

13+
1414
if __name__ == "__main__":
15-
[shutil.rmtree(i) for i in CATALOGS]
15+
subprocess.Popen(BUILD_CMD)
16+
[shutil.rmtree(i) for i in CATALOGS if Path(i).exists()]
1617
subprocess.run(BUILD_CMD)
18+

devtools/create_environment.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
from time import sleep
88

99
sys.path.append(Path.cwd().parent.as_posix())
10-
os.chdir("..")
10+
11+
if not Path("setup.py").exists():
12+
print("You're not in the right directory. Run this script from the "
13+
r"project's root directory, e.g. 'C:\users\your_user\projects\pyttman'.")
14+
exit(-1)
1115

1216
LAB_ENV_PATH = Path.cwd() / Path("dev_env")
1317
BUILD_OUTPUT_PATH = Path.cwd() / "dist"
@@ -17,10 +21,7 @@
1721
shutil.rmtree((LAB_ENV_PATH / "venv").as_posix())
1822

1923
if not Path("dist").exists():
20-
print("\nCannot create local testing environment as there is no "
21-
"build generated for the current local version of Pyttman.",
22-
"Run 'build.py' to create one.")
23-
exit(-1)
24+
subprocess.check_call("python devtools/build.py".split())
2425

2526
LAB_ENV_PATH.mkdir(exist_ok=True)
2627
os.chdir(LAB_ENV_PATH.as_posix())
@@ -39,5 +40,16 @@
3940
subprocess.run(f"{venv_python} -m pip install multidict".split())
4041
subprocess.run(f"{venv_python} -m pip install {package_file}".split())
4142

42-
print("\nFinished! You can now create an app and start testing in "
43-
f"{LAB_ENV_PATH.as_posix()}.")
43+
clear_sc = "clear" if os.name == "posix" else "cls"
44+
os.system(clear_sc)
45+
46+
os.system("cls")
47+
print("\nFinished! Here's how to get started:",
48+
f"1. Activate the virtual environment:\n\tcd dev_env\n\tvenv/scripts/activate",
49+
f"2. Run the command 'pyttman' to see available commands to the Pyttman CLI",
50+
"3. If it's the first time you're running Pyttman, run 'pyttman new app {app_name}' to create a new project."
51+
"4. Run 'pyttman dev {app_name}' to start the development server.",
52+
"5. If you've made changes to the Pyttman framework which you want to test in your testing project, "
53+
"run this script again. Your app will be left untouched, but the Pyttman version is upgraded to "
54+
"your current HEAD in the Pyttman repo.",
55+
sep="\n")

pyttman/core/internals.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
from dataclasses import dataclass, field
66
from datetime import datetime
77
from typing import Any
8+
import json
9+
from collections import UserDict
10+
811

912
import pyttman
1013
from pyttman.core.containers import MessageMixin, Reply
@@ -32,7 +35,6 @@ def depr_graceful(message: str, version: str):
3235
out = f"{message} - This was deprecated in version {version}."
3336
warnings.warn(out, DeprecationWarning)
3437

35-
3638
class Settings:
3739
"""
3840
Dataclass holding settings configured in the settings.py
@@ -48,7 +50,10 @@ class Settings:
4850
aren't valid settings.
4951
"""
5052

51-
def __init__(self, **kwargs):
53+
def __init__(self, dictionary=None, **kwargs):
54+
if dictionary is None:
55+
dictionary = {}
56+
self.__dict__.update(dictionary)
5257
self.APPEND_LOG_FILES: bool = True
5358
self.MIDDLEWARE: dict | None = None
5459
self.ABILITIES: list | None = None
@@ -59,15 +64,30 @@ def __init__(self, **kwargs):
5964
self.APP_NAME: str | None = None
6065
self.LOG_FORMAT: str | None = None
6166
self.LOG_TO_STDOUT: bool = False
67+
self.STATIC_FILES_DIR: Path | None = None
68+
self.TIME_ZONE: pytz.timezone = None
6269

63-
[setattr(self, k, v) for k, v in kwargs.items()
70+
[self._set_attr(k, v) for k, v in kwargs.items()
6471
if not inspect.ismodule(v)
6572
and not inspect.isfunction(v)]
6673

74+
def __getitem__(self, item):
75+
return self.__dict__[item]
76+
77+
def _set_attr(self, k, v):
78+
tmp = v
79+
if isinstance(v, dict):
80+
tmp = Settings._dict_to_object(v)
81+
82+
setattr(self, k, tmp)
83+
6784
def __repr__(self):
6885
_attrs = {name: value for name, value in self.__dict__.items()}
6986
return f"Settings({_attrs})"
7087

88+
@staticmethod
89+
def _dict_to_object(dictionary):
90+
return json.loads(json.dumps(dictionary), object_hook=Settings)
7191

7292
def _generate_name(name):
7393
"""

requirements.txt

-72 Bytes
Binary file not shown.

tests/core/test_settings.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from tests.module_helper import PyttmanInternalBaseTestCase
2+
from pyttman.core.internals import Settings
3+
4+
from importlib import import_module
5+
import pytest
6+
7+
@pytest.fixture
8+
def mockSettings():
9+
10+
mock_settings = {
11+
"d":{
12+
"k1":"v1",
13+
"k2":{
14+
"a":"a",
15+
"b":"b"
16+
}
17+
},
18+
"foo":"bar"
19+
}
20+
21+
return Settings(**mock_settings)
22+
23+
def test_read_settings_with_dictionary(mockSettings):
24+
assert mockSettings.d.k2.a == "a"
25+
assert mockSettings.d["k2"].a == "a"
26+
assert mockSettings.d["k2"]["a"] == "a"
27+
28+
assert mockSettings.d.k1 == "v1"
29+
assert mockSettings.d["k1"] == "v1"
30+
31+
assert mockSettings.foo == "bar"
32+

0 commit comments

Comments
 (0)