Click common validators for CLI module

pull/562/head
Kieran Prasch 2018-11-22 10:19:03 -08:00
parent 5fe7de5ef6
commit 6a79833165
No known key found for this signature in database
GPG Key ID: 199AB839D4125A62
1 changed files with 48 additions and 0 deletions

48
nucypher/cli/types.py Normal file
View File

@ -0,0 +1,48 @@
"""
This file is part of nucypher.
nucypher is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
nucypher is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with nucypher. If not, see <https://www.gnu.org/licenses/>.
"""
from ipaddress import ip_address
import click
from eth_utils import is_checksum_address
class ChecksumAddress(click.ParamType):
name = 'checksum_address'
def convert(self, value, param, ctx):
if is_checksum_address(value):
return value
self.fail('{} is not a valid EIP-55 checksum address'.format(value, param, ctx))
class IPv4Address(click.ParamType):
name = 'ipv4_address'
def convert(self, value, param, ctx):
try:
_address = ip_address(value)
except ValueError as e:
self.fail(str(e))
else:
return value
IPV4_ADDRESS = IPv4Address()
CHECKSUM_ADDRESS = ChecksumAddress()