MCPcopy Index your code
hub / github.com/wireservice/csvkit / main

Method main

csvkit/utilities/csvstack.py:43–145  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

41 help='Use the filename of each input file as its grouping value. When specified, -g will be ignored.')
42
43 def main(self):
44 if isatty(sys.stdin) and self.args.input_paths == ['-']:
45 sys.stderr.write('No input file or piped data provided. Waiting for standard input:\n')
46
47 has_groups = self.args.groups is not None or self.args.group_by_filenames
48
49 if self.args.groups is not None and not self.args.group_by_filenames:
50 groups = self.args.groups.split(',')
51
52 if len(groups) != len(self.args.input_paths):
53 self.argparser.error(
54 'The number of grouping values must be equal to the number of CSV files being stacked.')
55 else:
56 groups = None
57
58 group_name = self.args.group_name if self.args.group_name else 'group'
59 use_fieldnames = not self.args.no_header_row
60
61 if use_fieldnames:
62 Reader = agate.csv.DictReader
63 else:
64 Reader = agate.csv.reader
65
66 headers = []
67 stdin_fieldnames = []
68 stdin_first_row = []
69
70 for path in self.args.input_paths:
71 f = self._open_input_file(path)
72 file_is_stdin = path == '-'
73
74 _skip_lines(f, self.args)
75 rows = Reader(f, **self.reader_kwargs)
76
77 if use_fieldnames:
78 if rows.fieldnames:
79 for field in rows.fieldnames:
80 if field not in headers:
81 headers.append(field)
82
83 # If the file is standard input, store the fieldnames so that the rows can be read correctly later.
84 if file_is_stdin:
85 stdin_fieldnames = rows.fieldnames
86 else:
87 f.close()
88 else:
89 row = next(rows, [])
90 headers = list(make_default_headers(len(row)))
91
92 # If the file is standard input, store the row that was used to calculate the number of columns.
93 if file_is_stdin:
94 stdin_first_row = row
95 else:
96 f.close()
97
98 # If we aren't using header rows, we only look at the first file and stack columns in the same order.
99 break
100

Callers

nothing calls this directly

Calls 5

isattyFunction · 0.90
make_default_headersFunction · 0.90
_skip_linesFunction · 0.85
_open_input_fileMethod · 0.80
closeMethod · 0.80

Tested by

no test coverage detected