Moved custom param types to param_types

This commit is contained in:
Rhet Turnbull
2023-02-25 14:43:37 -08:00
parent 7eb35b31ac
commit 0694662f78
2 changed files with 35 additions and 33 deletions

View File

@@ -17,39 +17,7 @@ from osxphotos.sqlitekvstore import SQLiteKVStore
from .cli_commands import echo, echo_error, selection_command, verbose from .cli_commands import echo, echo_error, selection_command, verbose
from .kvstore import kvstore from .kvstore import kvstore
from .param_types import TemplateString from .param_types import Latitude, Longitude, TemplateString
class Latitude(click.ParamType):
name = "Latitude"
def convert(self, value, param, ctx):
try:
latitude = float(value)
if latitude < -90 or latitude > 90:
raise ValueError
return latitude
except Exception:
self.fail(
f"Invalid latitude {value}. Must be a floating point number between -90 and 90."
)
class Longitude(click.ParamType):
name = "Longitude"
def convert(self, value, param, ctx):
try:
longitude = float(value)
if longitude < -180 or longitude > 180:
raise ValueError
return longitude
except Exception:
self.fail(
f"Invalid longitude {value}. Must be a floating point number between -180 and 180."
)
@selection_command(name="batch-edit") @selection_command(name="batch-edit")

View File

@@ -23,6 +23,8 @@ __all__ = [
"DeprecatedPath", "DeprecatedPath",
"ExportDBType", "ExportDBType",
"FunctionCall", "FunctionCall",
"Latitude",
"Longitude",
"PathOrStdin", "PathOrStdin",
"StrpDateTimePattern", "StrpDateTimePattern",
"TemplateString", "TemplateString",
@@ -274,3 +276,35 @@ class StrpDateTimePattern(click.ParamType):
self.fail(f"Invalid strpdatetime format string: {value}. {e}") self.fail(f"Invalid strpdatetime format string: {value}. {e}")
else: else:
return value return value
class Latitude(click.ParamType):
name = "Latitude"
def convert(self, value, param, ctx):
try:
latitude = float(value)
if latitude < -90 or latitude > 90:
raise ValueError
return latitude
except Exception:
self.fail(
f"Invalid latitude {value}. Must be a floating point number between -90 and 90."
)
class Longitude(click.ParamType):
name = "Longitude"
def convert(self, value, param, ctx):
try:
longitude = float(value)
if longitude < -180 or longitude > 180:
raise ValueError
return longitude
except Exception:
self.fail(
f"Invalid longitude {value}. Must be a floating point number between -180 and 180."
)