Skip to content

Commit

Permalink
feat: add support for subdirectories
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Nov 22, 2024
1 parent 8cdeddc commit 933596f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ resources/
├── Questionnaire/
│ ├── questionnaire1.yaml
│ ├── questionnaire2.yaml
│ └── questionnaire3.yaml
│ └── sudbir/
│ └── questionnaire3.yaml
```

## Supported operations
Expand Down
5 changes: 2 additions & 3 deletions fhirsnake/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,8 @@ def server(host: str, port: int) -> None:
server.run()


def watch(url: str, headers: list[str] | None):
headers = headers or []
headers = {v.split(":", 1)[0]: v.split(":", 1)[1] for v in headers}
def watch(url: str, headers_list: list[str] | None):
headers = {v.split(":", 1)[0]: v.split(":", 1)[1] for v in (headers_list or [])}
start_watcher(url, headers)


Expand Down
36 changes: 19 additions & 17 deletions fhirsnake/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,40 @@
import yaml


def load_resources(abs_path):
def load_resources(root_path: str):
resources = {}

for resource_type in os.listdir(abs_path):
if not os.path.isdir(os.path.join(abs_path, resource_type)):
for resource_type in os.listdir(root_path):
if not os.path.isdir(os.path.join(root_path, resource_type)):
continue
resources[resource_type] = load_resources_by_ids(abs_path, resource_type)
resources[resource_type] = load_resources_by_ids(root_path, resource_type)
return resources


def load_resources_by_ids(abs_path, resource_type):
def load_resources_by_ids(root_path: str, resource_type: str):
resources = {}
path = os.path.join(abs_path, resource_type)

for filename in os.listdir(path):
resource = load_resource(os.path.join(path, filename))
if not resource:
continue
resources[resource["id"]] = resource
for abs_files_dir, _, files in os.walk(os.path.join(root_path, resource_type)):
for filename in files:
resource = load_resource(root_path, os.path.join(abs_files_dir, filename))
if not resource:
continue
resources[resource["id"]] = resource
return resources


def load_resource(path: str):
# path: /path/to/resources/Patient/id.json
resource_type = path.split("/")[-2]
file_name = path.split("/")[-1]
def load_resource(root_path: str, abs_path: str):
path = os.path.relpath(abs_path, root_path)
# path: Patient/id.json or Patient/subdir/id.json
resource_type, *_, file_name = path.split("/")
if "." not in file_name:
return None

resource_id, file_ext = file_name.rsplit(".", 1)

if file_ext not in ("yaml", "yml", "json"):
return None

with open(path) as f:
with open(abs_path) as f:
if file_ext in ("yaml", "yml"):
resource = yaml.safe_load(f)
elif file_ext == "json":
Expand Down
7 changes: 4 additions & 3 deletions fhirsnake/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

class FileChangeHandler(FileSystemEventHandler):
def __init__(
self, external_fhir_server_url: str, external_fhir_server_headers: dict[str, str], *args, **kwargs
self, target_dir: str, external_fhir_server_url: str, external_fhir_server_headers: dict[str, str], *args, **kwargs
) -> None:
self.target_dir = target_dir
self.external_fhir_server_url = external_fhir_server_url
self.external_fhir_server_headers = external_fhir_server_headers
super().__init__(*args, **kwargs)
Expand All @@ -28,7 +29,7 @@ def on_modified(self, event):

def process_file(self, file_path):
try:
resource = load_resource(file_path)
resource = load_resource(self.target_dir, file_path)
except Exception:
logging.exception("Unable to load resource %s", file_path)
return
Expand Down Expand Up @@ -56,7 +57,7 @@ def process_file(self, file_path):


def start_watcher(external_fhir_server_url: str, external_fhir_server_headers: dict[str, str]):
event_handler = FileChangeHandler(external_fhir_server_url, external_fhir_server_headers)
event_handler = FileChangeHandler(RESOURCE_DIR, external_fhir_server_url, external_fhir_server_headers)
observer = Observer()
observer.schedule(event_handler, RESOURCE_DIR, recursive=True)
observer.start()
Expand Down

0 comments on commit 933596f

Please sign in to comment.