|
3 | 3 | import sys
|
4 | 4 |
|
5 | 5 | import agate
|
| 6 | +from agate import config |
6 | 7 |
|
7 | 8 | from csvkit.cli import CSVKitUtility
|
8 | 9 |
|
9 | 10 |
|
10 | 11 | class CSVPy(CSVKitUtility):
|
11 | 12 | description = 'Load a CSV file into a CSV reader and then drop into a Python shell.'
|
| 13 | + override_flags = ['l', 'zero'] |
12 | 14 |
|
13 | 15 | def add_arguments(self):
|
14 |
| - self.argparser.add_argument('--dict', dest='as_dict', action='store_true', |
15 |
| - help='Load the CSV file into a DictReader.') |
16 |
| - self.argparser.add_argument('--agate', dest='as_agate', action='store_true', |
17 |
| - help='Load the CSV file into an agate table.') |
| 16 | + self.argparser.add_argument( |
| 17 | + '--dict', dest='as_dict', action='store_true', |
| 18 | + help='Load the CSV file into a DictReader.') |
| 19 | + self.argparser.add_argument( |
| 20 | + '--agate', dest='as_agate', action='store_true', |
| 21 | + help='Load the CSV file into an agate table.') |
| 22 | + self.argparser.add_argument( |
| 23 | + '--no-number-ellipsis', dest='no_number_ellipsis', action='store_true', |
| 24 | + help='Disable the ellipsis if the max precision is exceeded.') |
| 25 | + self.argparser.add_argument( |
| 26 | + '-y', '--snifflimit', dest='sniff_limit', type=int, default=1024, |
| 27 | + help='Limit CSV dialect sniffing to the specified number of bytes. ' |
| 28 | + 'Specify "0" to disable sniffing entirely, or "-1" to sniff the entire file.') |
| 29 | + self.argparser.add_argument( |
| 30 | + '-I', '--no-inference', dest='no_inference', action='store_true', |
| 31 | + help='Disable type inference when parsing the input. This disables the reformatting of values.') |
18 | 32 |
|
19 | 33 | def main(self):
|
20 | 34 | if self.input_file == sys.stdin:
|
21 | 35 | self.argparser.error('csvpy cannot accept input as piped data via STDIN.')
|
22 | 36 |
|
| 37 | + if self.args.no_number_ellipsis: |
| 38 | + config.set_option('number_truncation_chars', '') |
| 39 | + |
23 | 40 | # Attempt reading filename, will cause lazy loader to access file and raise error if it does not exist
|
24 | 41 | filename = self.input_file.name
|
25 | 42 |
|
26 | 43 | if self.args.as_dict:
|
27 | 44 | klass = agate.csv.DictReader
|
28 | 45 | class_name = 'agate.csv.DictReader'
|
29 | 46 | variable_name = 'reader'
|
| 47 | + input_file = self.skip_lines() |
| 48 | + kwargs = {} |
30 | 49 | elif self.args.as_agate:
|
31 | 50 | klass = agate.Table.from_csv
|
32 | 51 | class_name = 'agate.Table'
|
33 | 52 | variable_name = 'table'
|
| 53 | + input_file = self.input_file |
| 54 | + |
| 55 | + sniff_limit = self.args.sniff_limit if self.args.sniff_limit != -1 else None |
| 56 | + kwargs = dict( |
| 57 | + skip_lines=self.args.skip_lines, |
| 58 | + sniff_limit=sniff_limit, |
| 59 | + column_types=self.get_column_types(), |
| 60 | + ) |
34 | 61 | else:
|
35 | 62 | klass = agate.csv.reader
|
36 | 63 | class_name = 'agate.csv.reader'
|
37 | 64 | variable_name = 'reader'
|
| 65 | + input_file = self.skip_lines() |
| 66 | + kwargs = {} |
38 | 67 |
|
39 |
| - variable = klass(self.input_file, **self.reader_kwargs) |
| 68 | + variable = klass(input_file, **kwargs, **self.reader_kwargs) |
40 | 69 |
|
41 | 70 | welcome_message = 'Welcome! "{}" has been loaded in an {} object named "{}".'.format(
|
42 | 71 | filename, class_name, variable_name)
|
|
0 commit comments