Skip to content

Commit

Permalink
feat(cli, export): re-write using CLI and add export command
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Nov 15, 2024
1 parent c677c2c commit 91d1bb5
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# IDE
.DS_Store
.idea

# Build
dist

# Versioning
node_modules
node_modules
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ COPY fhirsnake /app

EXPOSE 8000

CMD ["poetry", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
ENTRYPOINT ["poetry", "run", "python3", "cli.py"]

CMD ["server"]
2 changes: 1 addition & 1 deletion Dockerfile.resources
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM bedasoftware/fhirsnake:latest

COPY resources /app/resources
# The destination should be exactly the same!
# The destination should be exactly the same!
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ resources/
2. Adjust source destination in `Dockerfile.resources` if required
3. Option A: Run a container
```bash
docker run -p 8002:8000 -v ./resources:/app/resources bedasoftware/fhirsnake
docker run -p 8002:8000 -v ./resources:/app/resources bedasoftware/fhirsnake
```
3. Option B: Build an image using the base image
```bash
docker build -t fhirsnake-resources:latest -f Dockerfile.resources .
docker run -p 8000:8000 fhirsnake-resources
```
4. Option C: Export resources as .ndjson or ndjson.gz
```bash
docker run -v ./resources:/app/resources -v ./output:/output bedasoftware/fhirsnake export --output /output/seeds.ndjson.gz
```

## Contribution and feedback
Please, use [Issues](https://github.com/beda-software/fhirsnake/issues)
Expand Down
72 changes: 72 additions & 0 deletions fhirsnake/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import argparse
import gzip
import logging

import ndjson
import uvicorn

from initial_resources import initial_resources

logging.basicConfig(level=logging.INFO)


def main() -> None:
parser = argparse.ArgumentParser(description="CLI for fhirsnake")

subparsers = parser.add_subparsers(dest="command", required=True, help="Sub-command to run")

export_parser = subparsers.add_parser("export", help="Export resources as .ndjson or .ndjson.gz")
export_parser.add_argument(
"--output",
required=True,
help="Specify the output filename",
)

server_parser = subparsers.add_parser("server", help="Run fhirsnake FHIR server")
server_parser.add_argument(
"--host",
default="0.0.0.0",
help="Host",
)

server_parser.add_argument(
"--port",
type=int,
default=8000,
help="Port",
)

args = parser.parse_args()

if args.command == "export":
export_resources(args.output)

if args.command == "server":
server(args.host, args.port)


def server(host: str, port: int) -> None:
config = uvicorn.Config("server:app", host=host, port=port)
server = uvicorn.Server(config)
server.run()


def export_resources(output: str) -> None:
gzipped = output.endswith(".gz")
resources_list = flatten_resources(initial_resources)
dumped_resources = ndjson.dumps(resources_list)

if gzipped:
with gzip.open(output, "w+") as f:
f.write(dumped_resources.encode())
else:
with open(output, "w+") as f:
f.write(dumped_resources)


def flatten_resources(resources: dict[str, dict[str, dict]]) -> list[dict]:
return [resource for by_resource_type in resources.values() for resource in by_resource_type.values()]


if __name__ == "__main__":
main()
20 changes: 20 additions & 0 deletions fhirsnake/initial_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import logging
import os
import sys

from files import load_resources

REPOSITORY_URL = "https://github.com/beda-software/fhirsnake/"
RESOURCE_DIR = "resources"

root_dir = os.path.dirname(os.path.abspath(__name__))
resources_abs_path = os.path.join(root_dir, RESOURCE_DIR)

if not os.path.isdir(resources_abs_path):
logging.error(
f"Required directory '{resources_abs_path}' does not exist. \
Check {REPOSITORY_URL} for details. Stopping application."
)
sys.exit(1)

initial_resources = load_resources(resources_abs_path)
19 changes: 2 additions & 17 deletions fhirsnake/main.py → fhirsnake/server.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
import logging
import os
import sys
import uuid

from fastapi import FastAPI, HTTPException
from files import load_resources
from initial_resources import initial_resources

logging.basicConfig(level=logging.INFO)

REPOSITORY_URL = "https://github.com/beda-software/fhirsnake/"

app = FastAPI()


@app.on_event("startup")
async def load_app_data():
root_dir = os.path.dirname(os.path.abspath(__name__))
logging.warning(root_dir)
resource_dir = "resources"
resources_abs_path = os.path.join(root_dir, resource_dir)

if not os.path.isdir(resources_abs_path):
logging.error(
f"Required directory '{resources_abs_path}' does not exist. \
Check {REPOSITORY_URL} for details. Stopping application."
)
sys.exit(1)

app.state.resources = load_resources(resources_abs_path)
app.state.resources = initial_resources


@app.get("/")
Expand Down
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ python = "^3.11"
fastapi = "^0.112.1"
uvicorn = "^0.30.6"
pyyaml = "^6.0.2"
ndjson = "^0.3.1"

[tool.poetry.dev-dependencies]
ruff = "^0.6.2"
Expand Down

0 comments on commit 91d1bb5

Please sign in to comment.