Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cyrillkuettel committed May 14, 2024
2 parents 6780146 + 3ea5f3b commit dd99d37
Show file tree
Hide file tree
Showing 25 changed files with 396 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# use a more expressive value range while coverage is still relatively low
coverage:
precision: 2
round: down
range: "40...100"
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM python:3.12.2-alpine3.19

# Set the working directory
WORKDIR /app

# Install PostgreSQL development packages required for psycopg
RUN apk add --no-cache postgresql-dev gcc python3-dev musl-dev

# Copy the current directory contents into the container at /app
COPY . /app

# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Make the entrypoint script executable
RUN chmod +x /app/entrypoint.sh

# Set the entrypoint to the script
ENTRYPOINT ["/app/entrypoint.sh"]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ This will remove packages that have been manually installed locally

uv pip sync requirements.txt test_requirements.txt

## Testing the Dockerfile works

docker run --rm -p 8080:6543 -v $PWD/config:/app/config privatim-1 config/development.ini

then open http://127.0.0.1:8080/


## Miscellaneous
### Javascript dependencies
Expand All @@ -90,3 +96,5 @@ These files are included in the project. They have been downloaded from these CD
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/tom-select.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/tom-select.complete.min.js"></script>
```


2 changes: 1 addition & 1 deletion development.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ file_template = %%(year)d%%(month).2d%%(day).2d_%%(rev)s

[server:main]
use = egg:waitress#main
listen = localhost:9090
listen = localhost:8080

###
# logging configuration
Expand Down
18 changes: 18 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

if [ -z "$1" ]; then
echo "No configuration file (input argument) provided. Exiting."
exit 1
fi

echo "test"
echo "Running the entrypoint.sh script."

CONFIG_FILE=$1

# Run the upgrade script with the provided config file
python src/privatim/cli/upgrade.py "$CONFIG_FILE"

python src/privatim/cli/initialize_db.py

pserve "$CONFIG_FILE"
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ chameleon==4.5.4
# via pyramid-chameleon
charset-normalizer==3.3.2
# via requests
click==8.1.7
# via privatim
dnspython==2.6.1
# via email-validator
email-validator==2.1.1
Expand Down
5 changes: 4 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ install_requires =
alembic
bcrypt
Babel
click
email_validator
fanstatic
Markdown
Expand Down Expand Up @@ -68,7 +69,9 @@ fanstatic.libraries =
privatim:css = privatim.static:css_library

console_scripts =
initialize_db = privatim.scripts.initialize_db:main
privatim = privatim.cli:cli
initialize_db = privatim.cli.initialize_db:main
upgrade = privatim.cli.upgrade:upgrade

[options.extras_require]
dev =
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions src/privatim/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import click
from privatim.cli.add_user import add_user
from privatim.utils import first


from typing import TYPE_CHECKING
if TYPE_CHECKING:
from typing import Iterator


@click.group()
def cli() -> None:
pass


cli.add_command(add_user)


def find_ini_files() -> Iterator[str]:
current_path = os.path.dirname(os.path.abspath(__file__))
while current_path != os.path.abspath(os.path.join(current_path, '..')):
for filename in os.listdir(current_path):
if filename.endswith('.ini'):
yield os.path.join(current_path, filename)
current_path = os.path.abspath(os.path.join(current_path, '..'))


def find_ini_file_or_abort() -> str:
""" Search the file system from the current location for the
development.ini or production.ini file
Returns the absolute path to the .ini file """
ini_file = first(find_ini_files())
if click.confirm(f'Found {ini_file} file: continue? y/n'):
click.echo('Continuing...')
return ini_file
else:
click.echo('Stopping.')
click.get_current_context().abort()
25 changes: 25 additions & 0 deletions src/privatim/cli/add_user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import click
from pyramid.paster import bootstrap
from pyramid.paster import get_appsettings

from privatim.models import User
from privatim.orm import get_engine, Base


@click.command()
@click.argument('config_uri')
@click.option('--email', prompt=True)
@click.option('--password', prompt=True, hide_input=True)
def add_user(config_uri: str, email: str, password: str) -> None:

env = bootstrap(config_uri)
settings = get_appsettings(config_uri)
engine = get_engine(settings)
Base.metadata.create_all(engine)

with env['request'].tm:
dbsession = env['request'].dbsession

user = User(email=email)
user.set_password(password)
dbsession.add(user)
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def main(argv: list[str] = sys.argv) -> None:

with env['request'].tm:
db = env['request'].dbsession
add_placeholder_content(db)
add_example_content(db)


def add_placeholder_content(db: 'Session') -> None:
def add_example_content(db: 'Session') -> None:

users = [
User(
email='[email protected]',
Expand All @@ -63,6 +64,7 @@ def add_placeholder_content(db: 'Session') -> None:
last_name='Huber',
),
]
print(f'Adding users: {users}')
for user in users:
user.set_password('test')
db.add(user)
Expand Down Expand Up @@ -97,3 +99,7 @@ def add_placeholder_content(db: 'Session') -> None:
db.add(consultation)
db.add(status)
db.flush()


if __name__ == '__main__':
main()
64 changes: 64 additions & 0 deletions src/privatim/cli/shell.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
from code import InteractiveConsole
import readline
import rlcompleter

import click
from pyramid.paster import bootstrap
from transaction import commit

from typing import Any

from privatim.cli import find_ini_file_or_abort


class EnhancedInteractiveConsole(InteractiveConsole):
"""Wraps the InteractiveConsole with some basic shell features:
- horizontal movement (e.g. arrow keys)
- history (e.g. up and down keys)
- very basic tab completion
"""

def __init__(self, locals: dict[str, Any] | None = None):
super().__init__(locals)
self.init_completer()

def init_completer(self) -> None:
readline.set_completer(
rlcompleter.Completer(
dict(self.locals) if self.locals else {}
).complete
)
readline.set_history_length(100)
readline.parse_and_bind("tab: complete")

@click.command()
def shell() -> None:
"""Enters an interactive shell."""

config_uri = find_ini_file_or_abort()
assert 'development.ini' in config_uri

env = bootstrap(config_uri)
with env['request'].tm:
session = env['request'].dbsession
app = env['app']
shell = EnhancedInteractiveConsole(
{

'app': app,
'session': session,
'commit': commit,
}
)
shell.interact(
banner="""
privatim Shell
==================
Exit the console using exit() or quit().
Available variables: session
Available functions: commit
"""
)
Loading

0 comments on commit dd99d37

Please sign in to comment.