|
3 | 3 | import glob |
4 | 4 | import json |
5 | 5 | import subprocess |
| 6 | +from abc import ABC, abstractmethod |
6 | 7 | from typing import Any, Container, Dict, Iterator, List, Optional, Sequence, Union, cast |
7 | 8 |
|
8 | 9 | import numpy as np |
@@ -72,7 +73,7 @@ def _json(self) -> str: |
72 | 73 |
|
73 | 74 |
|
74 | 75 | class Stage: |
75 | | - def __init_subclass__(cls, type_prefix: str) -> None: |
| 76 | + def __init_subclass__(cls, type_prefix: Optional[str] = None) -> None: |
76 | 77 | for driver in _PDAL_DRIVERS: |
77 | 78 | name = driver["name"] |
78 | 79 | prefix, _, suffix = name.partition(".") |
@@ -114,36 +115,38 @@ def _add_constructor(cls, type: str, name: str, description: str) -> None: |
114 | 115 | setattr(cls, name, classmethod(constructor)) |
115 | 116 |
|
116 | 117 |
|
117 | | -class Reader(Stage, type_prefix="readers"): |
118 | | - def __init__(self, filename: str, **options: Any): |
119 | | - super().__init__(filename=filename, **options) |
| 118 | +class InferableTypeStage(ABC, Stage): |
| 119 | + @staticmethod |
| 120 | + @abstractmethod |
| 121 | + def infer_type(filename: str) -> str: |
| 122 | + """Infer the driver type from the filename""" |
120 | 123 |
|
121 | 124 | @property |
122 | 125 | def type(self) -> str: |
123 | 126 | try: |
124 | 127 | return super().type |
125 | 128 | except KeyError: |
126 | | - filename = self._options["filename"] |
127 | | - return cast(str, libpdalpython.infer_reader_driver(filename)) |
| 129 | + return self.infer_type(self._options["filename"]) |
| 130 | + |
| 131 | + |
| 132 | +class Reader(InferableTypeStage, type_prefix="readers"): |
| 133 | + infer_type = staticmethod(libpdalpython.infer_reader_driver) |
| 134 | + |
| 135 | + def __init__(self, filename: str, **options: Any): |
| 136 | + super().__init__(filename=filename, **options) |
128 | 137 |
|
129 | 138 |
|
130 | 139 | class Filter(Stage, type_prefix="filters"): |
131 | 140 | def __init__(self, type: str, **options: Any): |
132 | 141 | super().__init__(type=type, **options) |
133 | 142 |
|
134 | 143 |
|
135 | | -class Writer(Stage, type_prefix="writers"): |
| 144 | +class Writer(InferableTypeStage, type_prefix="writers"): |
| 145 | + infer_type = staticmethod(libpdalpython.infer_writer_driver) |
| 146 | + |
136 | 147 | def __init__(self, filename: Optional[str] = None, **options: Any): |
137 | 148 | super().__init__(filename=filename, **options) |
138 | 149 |
|
139 | | - @property |
140 | | - def type(self) -> str: |
141 | | - try: |
142 | | - return super().type |
143 | | - except KeyError: |
144 | | - filename = self._options["filename"] |
145 | | - return cast(str, libpdalpython.infer_writer_driver(filename)) |
146 | | - |
147 | 150 |
|
148 | 151 | def _parse_stages(text: str) -> Iterator[Stage]: |
149 | 152 | json_stages = json.loads(text) |
|
0 commit comments