-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli, export): re-write using CLI and add export command
- Loading branch information
Showing
9 changed files
with
118 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters