diff --git a/README.md b/README.md index 9a36032..650c88c 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,8 @@ resources/ ├── Questionnaire/ │ ├── questionnaire1.yaml │ ├── questionnaire2.yaml -│ └── questionnaire3.yaml +│ └── sudbir/ +│ └── questionnaire3.yaml ``` ## Supported operations diff --git a/fhirsnake/cli.py b/fhirsnake/cli.py index 282a89d..7db4d87 100644 --- a/fhirsnake/cli.py +++ b/fhirsnake/cli.py @@ -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) diff --git a/fhirsnake/files.py b/fhirsnake/files.py index f6d7fae..9b9beaa 100644 --- a/fhirsnake/files.py +++ b/fhirsnake/files.py @@ -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": diff --git a/fhirsnake/watch.py b/fhirsnake/watch.py index 7b32a8b..63fcad1 100644 --- a/fhirsnake/watch.py +++ b/fhirsnake/watch.py @@ -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) @@ -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 @@ -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()