Skip to content

Commit

Permalink
chore: Merge validate_args and __init__ into main(), as they are call…
Browse files Browse the repository at this point in the history
…ed once (were extracted in #939)
  • Loading branch information
jpmckinney committed Oct 17, 2023
1 parent 1220d80 commit 36c78b9
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions csvkit/utilities/csvjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,30 @@ def add_arguments(self):
'-I', '--no-inference', dest='no_inference', action='store_true',
help='Disable type inference (and --locale, --date-format, --datetime-format) when parsing CSV input.')

def __init__(self, args=None, output_file=None):
super().__init__(args, output_file)
def main(self):
"""
Convert CSV to JSON.
"""
if self.args.lat and not self.args.lon:
self.argparser.error('--lon is required whenever --lat is specified.')
if self.args.lon and not self.args.lat:
self.argparser.error('--lat is required whenever --lon is specified.')

self.validate_args()
if self.args.crs and not self.args.lat:
self.argparser.error('--crs is only allowed when --lat and --lon are also specified.')
if self.args.type and not self.args.lat:
self.argparser.error('--type is only allowed when --lat and --lon are also specified.')
if self.args.geometry and not self.args.lat:
self.argparser.error('--geometry is only allowed when --lat and --lon are also specified.')

if self.args.key and self.args.streamOutput and not (self.args.lat and self.args.lon):
self.argparser.error('--key is only allowed with --stream when --lat and --lon are also specified.')

self.json_kwargs = {
'ensure_ascii': False,
'indent': self.args.indent,
}

def main(self):
"""
Convert CSV to JSON.
"""

if self.can_stream():
if self.additional_input_expected():
sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n')
Expand Down Expand Up @@ -111,22 +120,6 @@ def can_stream(self):
def is_geo(self):
return self.args.lat and self.args.lon

def validate_args(self):
if self.args.lat and not self.args.lon:
self.argparser.error('--lon is required whenever --lat is specified.')
if self.args.lon and not self.args.lat:
self.argparser.error('--lat is required whenever --lon is specified.')

if self.args.crs and not self.args.lat:
self.argparser.error('--crs is only allowed when --lat and --lon are also specified.')
if self.args.type and not self.args.lat:
self.argparser.error('--type is only allowed when --lat and --lon are also specified.')
if self.args.geometry and not self.args.lat:
self.argparser.error('--geometry is only allowed when --lat and --lon are also specified.')

if self.args.key and self.args.streamOutput and not (self.args.lat and self.args.lon):
self.argparser.error('--key is only allowed with --stream when --lat and --lon are also specified.')

def read_csv_to_table(self):
sniff_limit = self.args.sniff_limit if self.args.sniff_limit != -1 else None
return agate.Table.from_csv(
Expand Down

0 comments on commit 36c78b9

Please sign in to comment.