• R/O
  • SSH

un-xtab: 提交

Source, documentation, and distribution code for un-xtab.py


Commit MetaInfo

修訂cb48afc3993f2dbcc0d1008c6bb0f3e25ffb2083 (tree)
時間2022-02-02 00:45:37
作者rdnielsen
Commiterrdnielsen

Log Message

Changed to use argparse.

Change Summary

差異

diff -r 12e72acef901 -r cb48afc3993f CHANGELOG.rst
--- a/CHANGELOG.rst Tue Aug 17 07:03:46 2021 -0700
+++ b/CHANGELOG.rst Tue Feb 01 07:45:37 2022 -0800
@@ -1,6 +1,7 @@
11 ========== ========== =================================================================================
22 Date Version Features
33 ========== ========== =================================================================================
4+2022-02-01 1.1.0 Changed argument parser.
45 2019-09-11 1.0.0 Accepts spreadsheet-style column identifiers for the *data_columns* configuration setting.
56 2019-07-20 0.8.0 Completed documentation for ReadTheDocs.
67 2016-01-23 0.3.0 Added the '-n' command-line option.
diff -r 12e72acef901 -r cb48afc3993f setup.py
--- a/setup.py Tue Aug 17 07:03:46 2021 -0700
+++ b/setup.py Tue Feb 01 07:45:37 2022 -0800
@@ -1,7 +1,7 @@
11 from distutils.core import setup
22
33 setup(name='un-xtab',
4- version='1.0.1',
4+ version='1.1.0',
55 description="Un-crosstab data in a text file.",
66 author='Dreas Nielsen',
77 author_email='dreas.nielsen@gmail.com',
diff -r 12e72acef901 -r cb48afc3993f un-xtab/un-xtab.py
--- a/un-xtab/un-xtab.py Tue Aug 17 07:03:46 2021 -0700
+++ b/un-xtab/un-xtab.py Tue Feb 01 07:45:37 2022 -0800
@@ -58,13 +58,14 @@
5858 # 2019-09-11 Modified to allow column IDs for the "data_columns" setting to be
5959 # letters as well as numbers. RDN.
6060 # 2021-08-16 Modified to seek(0) in the output buffer before truncating it. RDN.
61+# 2022-02-01 Modified to use argparse instead of optparse. RDN.
6162 #=====================================================================================
6263
6364
6465 # Standard libraries
6566 import sys
6667 import os.path
67-from optparse import OptionParser
68+from argparse import ArgumentParser
6869 try:
6970 from configparser import ConfigParser
7071 except:
@@ -80,8 +81,8 @@
8081 import traceback
8182 import pprint
8283
83-_version = "1.0.1"
84-_vdate = "2021-08-16"
84+_version = "1.1.0"
85+_vdate = "2022-02-01"
8586
8687
8788 # ******************** Globals ********************
@@ -335,7 +336,7 @@
335336 self.config_key = "header_as_column_" + str(header_as_column_num)
336337 # The column header text is the third item in the configuration specification.
337338 self.header_text = config_vals[self.config_key][2]
338- data_col_offset = min(config_vals[DATA_COLUMNS])
339+ #data_col_offset = min(config_vals[DATA_COLUMNS])
339340 self.row_index = config_vals[self.config_key][0] - 1
340341 def colhdr(self, config_vals, hdr_data ):
341342 return [ self.header_text ]
@@ -384,39 +385,34 @@
384385 def clparser():
385386 """Create a parser object that will scan the command line for options and arguments,
386387 and store them into variables for easy access by other program code."""
387- usage_msg = """Usage: %prog [options] <Input file name> <Output file name>
388-Arguments:
389- Input file name The name of a text (CSV) file with crosstabbed data.
390- Output file name The name of a text (CSV) to create with normalized data."""
391- vers_msg = "%prog " + "%s %s" % (_version, _vdate)
392- desc_msg = "Convert a crosstabbed data set to a normalized data table."
393- parser = OptionParser(usage=usage_msg, version=vers_msg, description=desc_msg)
394- parser.add_option("-c", "--configfile", action="store", type="string", dest="configfile",
388+ vers_msg = "%s, %s" % (_version, _vdate)
389+ desc_msg = "Convert a crosstabbed CSV data table to a normalized data table (%s)." % vers_msg
390+ parser = ArgumentParser(description=desc_msg)
391+ parser.add_argument("-c", "--configfile", action="store", dest="configfile",
395392 default=None,
396393 help="The name of the config file, with path if necessary. The default is to look for a configuration file with the same name as the input file, but with an extension of cfg, in the same directory as the input file.")
397- parser.add_option("-d", "--displayspecs", action="store_true", dest="showspecs",
394+ parser.add_argument("-d", "--displayspecs", action="store_true", dest="showspecs",
398395 default=False,
399396 help="Print the format specifications allowed in the configuration file, then exit.")
400- parser.add_option("-e", "--encoding", action="store", type="string", dest="encoding",
397+ parser.add_argument("-e", "--encoding", action="store", dest="encoding",
401398 default="latin-1",
402399 help="Character encoding of the CSV file. It should be one of the strings listed at http://docs.python.org/library/codecs.html#standard-encodings.")
403-# parser.add_option("-l", "--shortlines", action="store_true", dest="shortlines",
404-# default=False,
405-# help="Allow rows of the CSV file to have fewer columns than in the column headers. The default is to report an error for short data rows. If short data rows are allowed, any row without enough columns to match the format specification will still be reported as an error.")
406- parser.add_option("-n", "--number_rows", action="store", dest="rowseq", type="string",
400+ parser.add_argument("-n", "--number_rows", action="store", dest="rowseq",
407401 default=None,
408402 help="The column header for a column of sequential numbers for the output data rows.")
409- parser.add_option("-o", "--outputheaders",
403+ parser.add_argument("-o", "--outputheaders",
410404 action="store_true", dest="printheaders",
411405 default=False,
412406 help="Print the output column headers, then exit.")
413- parser.add_option("-p", "--printconfig",
407+ parser.add_argument("-p", "--printconfig",
414408 action="store_true", dest="printconfig",
415409 default=False,
416410 help="Pretty-print the configuration data after reading the configuration file, then exit.")
417- parser.add_option("-s", "--specname", action="store", dest="specname",
418- type="string",
411+ parser.add_argument("-s", "--specname", action="store", dest="specname",
419412 help="The name of the section to use in the configuration file. The default is to use the name of the input data file, without its extension.")
413+ parser.add_argument("-v", "--version", action="version", version=vers_msg)
414+ parser.add_argument("inputfile")
415+ parser.add_argument("outputfile")
420416 return parser
421417
422418
@@ -771,22 +767,22 @@
771767
772768 def main():
773769 parser = clparser()
774- (opts, args) = parser.parse_args()
770+ opts = parser.parse_args()
775771 # Process the command-line options.
776772 # If directed, just show the help message for configuration options, and exit.
777773 if opts.showspecs:
778774 print(CONFIG_KEYWORDS)
779775 return 0
780- if len(args)==0:
776+ if len(opts) == 0:
781777 parser.print_help()
782778 return 0
783779 common["printconfig"] = opts.printconfig
784780 common["printheaders"] = opts.printheaders
785781 # Check for two filename arguments, the first of which is an existing file.
786- if len(args) != 2:
787- bad_argument_error("Two command-line arguments, the names of the input nd output CSV files, must be provided.")
788- input_csv_file = args[0]
789- output_csv_file = args[1]
782+ if not ("inputfile" in opts and "outputfile" in opts):
783+ bad_argument_error("Two command-line arguments, the names of the input and output CSV files, must be provided.")
784+ input_csv_file = opts.inputfile
785+ output_csv_file = opts.outputfile
790786 if not os.path.exists(os.path.abspath(input_csv_file)):
791787 bad_argument_error("The input CSV file %s does not exist." % os.path.abspath(input_csv_file))
792788 if not os.path.exists(os.path.split(os.path.abspath(output_csv_file))[0]): # Check the *directory* name
@@ -802,11 +798,13 @@
802798 process_errors
803799
804800
805-if __name__=='__main__':
806- try:
807- main()
808- except SystemExit:
809- pass
810- except Exception as oops:
811- register_error(_FATAL, True, "Unexpected error")
801+main()
812802
803+#if __name__=='__main__':
804+# try:
805+# main()
806+# except SystemExit:
807+# pass
808+# except Exception as oops:
809+# register_error(_FATAL, True, "Unexpected error")
810+
Show on old repository browser