Skip to content

Commit e568b46

Browse files
committed
Factor out Reader.type and Writer.type to a base InferableTypeStage class
1 parent df7f161 commit e568b46

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

pdal/pipeline.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import glob
44
import json
55
import subprocess
6+
from abc import ABC, abstractmethod
67
from typing import Any, Container, Dict, Iterator, List, Optional, Sequence, Union, cast
78

89
import numpy as np
@@ -72,7 +73,7 @@ def _json(self) -> str:
7273

7374

7475
class Stage:
75-
def __init_subclass__(cls, type_prefix: str) -> None:
76+
def __init_subclass__(cls, type_prefix: Optional[str] = None) -> None:
7677
for driver in _PDAL_DRIVERS:
7778
name = driver["name"]
7879
prefix, _, suffix = name.partition(".")
@@ -114,36 +115,38 @@ def _add_constructor(cls, type: str, name: str, description: str) -> None:
114115
setattr(cls, name, classmethod(constructor))
115116

116117

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"""
120123

121124
@property
122125
def type(self) -> str:
123126
try:
124127
return super().type
125128
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)
128137

129138

130139
class Filter(Stage, type_prefix="filters"):
131140
def __init__(self, type: str, **options: Any):
132141
super().__init__(type=type, **options)
133142

134143

135-
class Writer(Stage, type_prefix="writers"):
144+
class Writer(InferableTypeStage, type_prefix="writers"):
145+
infer_type = staticmethod(libpdalpython.infer_writer_driver)
146+
136147
def __init__(self, filename: Optional[str] = None, **options: Any):
137148
super().__init__(filename=filename, **options)
138149

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-
147150

148151
def _parse_stages(text: str) -> Iterator[Stage]:
149152
json_stages = json.loads(text)

0 commit comments

Comments
 (0)