Default repository for execsql.py
Revision | feb27e06a7799cc56a83be77dca8bd02063cc202 (tree) |
---|---|
Time | 2020-06-01 09:10:41 |
Author | Dreas Nielsen <dreas.nielsen@gmai...> |
Commiter | Dreas Nielsen |
Modified to close additional output files after writing.
@@ -27,12 +27,12 @@ | ||
27 | 27 | # |
28 | 28 | # =============================================================================== |
29 | 29 | |
30 | -__version__ = "1.73.4" | |
30 | +__version__ = "1.73.5" | |
31 | 31 | __vdate = "2020-05-31" |
32 | 32 | |
33 | 33 | primary_vno = 1 |
34 | 34 | secondary_vno = 73 |
35 | -tertiary_vno = 4 | |
35 | +tertiary_vno = 5 | |
36 | 36 | |
37 | 37 | import os |
38 | 38 | import os.path |
@@ -5071,6 +5071,8 @@ | ||
5071 | 5071 | ofile = EncodedFile(output_dest, conf.output_encoding).open("w") |
5072 | 5072 | for dd in data_dict_rows: |
5073 | 5073 | ofile.write(self.template.safe_substitute(dd)) |
5074 | + if output_dest != 'stdout': | |
5075 | + ofile.close() | |
5074 | 5076 | |
5075 | 5077 | class JinjaTemplateReport(object): |
5076 | 5078 | # Exporting/reporting using the Jinja2 templating library. |
@@ -5101,6 +5103,8 @@ | ||
5101 | 5103 | raise ErrInfo("error", other_msg="Jinja2 template error (%s)" % e.message) |
5102 | 5104 | except: |
5103 | 5105 | raise |
5106 | + if output_dest != 'stdout': | |
5107 | + ofile.close() | |
5104 | 5108 | |
5105 | 5109 | class AirspeedTemplateReport(object): |
5106 | 5110 | # Exporting/reporting using the Airspeed templating library. |
@@ -5133,6 +5137,8 @@ | ||
5133 | 5137 | raise ErrInfo("error", other_msg=e.msg) |
5134 | 5138 | except: |
5135 | 5139 | raise |
5140 | + if output_dest != 'stdout': | |
5141 | + ofile.close() | |
5136 | 5142 | |
5137 | 5143 | |
5138 | 5144 | # End of template-based exports. |
@@ -9511,7 +9517,9 @@ | ||
9511 | 9517 | global conf |
9512 | 9518 | if outf: |
9513 | 9519 | check_dir(outf) |
9514 | - EncodedFile(outf, conf.output_encoding).open('a').write(msg) | |
9520 | + of = EncodedFile(outf, conf.output_encoding).open('a') | |
9521 | + of.write(msg) | |
9522 | + of.close() | |
9515 | 9523 | if (not outf) or tee: |
9516 | 9524 | try: |
9517 | 9525 | output.write(msg) |
@@ -10351,6 +10359,8 @@ | ||
10351 | 10359 | if comment: |
10352 | 10360 | o.write(u"-- %s\n" % comment) |
10353 | 10361 | o.write(u"%s\n" % sql) |
10362 | + if outfile: | |
10363 | + o.close() | |
10354 | 10364 | |
10355 | 10365 | metacommands.append(MetaCommand( |
10356 | 10366 | ins_table_rxs(r'^\s*WRITE\s+CREATE_TABLE\s+', |
@@ -10386,6 +10396,8 @@ | ||
10386 | 10396 | if comment: |
10387 | 10397 | o.write(u"-- %s\n" % comment) |
10388 | 10398 | o.write(u"%s\n" % sql) |
10399 | + if outfile: | |
10400 | + o.close() | |
10389 | 10401 | |
10390 | 10402 | metacommands.append(MetaCommand( |
10391 | 10403 | ins_table_rxs(r'^\s*WRITE\s+CREATE_TABLE\s+', |
@@ -10428,6 +10440,8 @@ | ||
10428 | 10440 | if comment: |
10429 | 10441 | o.write(u"-- %s\n" % comment) |
10430 | 10442 | o.write(u"%s\n" % sql) |
10443 | + if outfile: | |
10444 | + o.close() | |
10431 | 10445 | |
10432 | 10446 | metacommands.append(MetaCommand( |
10433 | 10447 | ins_table_rxs(r'^\s*WRITE\s+CREATE_TABLE\s+', |
@@ -10478,6 +10492,8 @@ | ||
10478 | 10492 | if comment: |
10479 | 10493 | o.write(u"-- %s\n" % comment) |
10480 | 10494 | o.write(u"%s\n" % sql) |
10495 | + if outfile: | |
10496 | + o.close() | |
10481 | 10497 | |
10482 | 10498 | metacommands.append(MetaCommand( |
10483 | 10499 | ins_table_rxs(r'^\s*WRITE\s+CREATE_TABLE\s+', |
@@ -10768,7 +10784,7 @@ | ||
10768 | 10784 | if output_dest is None or output_dest == 'stdout': |
10769 | 10785 | ofile = output |
10770 | 10786 | else: |
10771 | - check_dir(outfile) | |
10787 | + check_dir(output_dest) | |
10772 | 10788 | if append: |
10773 | 10789 | ofile = EncodedFile(output_dest, conf.output_encoding).open("a") |
10774 | 10790 | else: |
@@ -10782,6 +10798,8 @@ | ||
10782 | 10798 | for line in lines: |
10783 | 10799 | ofile.write(u"%s\n" % line) |
10784 | 10800 | ofile.write(u"END SCRIPT %s\n" % script_id) |
10801 | + if output_dest is not None and output_dest != 'stdout': | |
10802 | + ofile.close() | |
10785 | 10803 | |
10786 | 10804 | metacommands.append(MetaCommand(ins_fn_rxs(r'^\s*(?:DEBUG\s+)?WRITE\s+SCRIPT\s+(?P<script_id>\w+)(?:\s+(?P<append>APPEND\s+)?TO\s+', r')?\s*$'), x_writescript)) |
10787 | 10805 |
@@ -10920,6 +10938,8 @@ | ||
10920 | 10938 | for s in subvars.substitutions: |
10921 | 10939 | if user is None or s[0][0].isalnum() or s[0][0] == '_': |
10922 | 10940 | ofile.write(u"Substitution [%s] = [%s]\n" % s) |
10941 | + if output_dest is not None and output_dest != 'stdout': | |
10942 | + ofile.close() | |
10923 | 10943 | |
10924 | 10944 | metacommands.append(MetaCommand(ins_fn_rxs(r'^\s*DEBUG\s+WRITE(?:\s+(?P<local>LOCAL))?(?:\s+(?P<user>USER))?\s+SUBVARS(?:\s+(?P<append>APPEND\s+)?TO\s+', r')?\s*$'), x_debug_write_subvars)) |
10925 | 10945 |
@@ -10969,6 +10989,8 @@ | ||
10969 | 10989 | ofile.write(u"Config; Email CSS = %s\n" % conf.email_css) |
10970 | 10990 | ofile.write(u"Config; DAO flush delay (seconds) = %s\n" % conf.dao_flush_delay_secs) |
10971 | 10991 | ofile.write(u"Config; Configuration files read = %s\n" % ", ".join(conf.files_read)) |
10992 | + if output_dest is not None and output_dest != 'stdout': | |
10993 | + ofile.close() | |
10972 | 10994 | |
10973 | 10995 | metacommands.append(MetaCommand(ins_fn_rxs(r'^\s*DEBUG\s+WRITE\s+CONFIG(?:\s+(?P<append>APPEND\s+)?TO\s+', r')?\s*$'), x_debug_write_config)) |
10974 | 10996 |
@@ -10990,6 +11012,8 @@ | ||
10990 | 11012 | ofile = EncodedFile(output_dest, conf.output_encoding).open("w") |
10991 | 11013 | for d in pyodbc.drivers(): |
10992 | 11014 | ofile.write(u"%s\n" % d) |
11015 | + if output_dest is not None and output_dest != 'stdout': | |
11016 | + ofile.close() | |
10993 | 11017 | |
10994 | 11018 | metacommands.append(MetaCommand(ins_fn_rxs(r'^\s*DEBUG\s+WRITE\s+ODBC_DRIVERS(?:\s+(?P<append>APPEND\s+)?TO\s+', r')?\s*$'), x_debug_write_odbc_drivers)) |
10995 | 11019 |
@@ -11968,6 +11992,8 @@ | ||
11968 | 11992 | for row in rows: |
11969 | 11993 | ln = u"%s%s\n" % (margin, row) |
11970 | 11994 | ofile.write(ln) |
11995 | + if output_dest != 'stdout': | |
11996 | + ofile.close() | |
11971 | 11997 | return None |
11972 | 11998 | |
11973 | 11999 | def prettyprint_query(select_stmt, db, outfile, append=False, nd_val=u'', desc=None): |
@@ -12166,6 +12192,8 @@ | ||
12166 | 12192 | quoted_row = ["'%s'" % v.replace("'", "''") if isinstance(v, stringtypes) else type(u"")(v) if v is not None else u"NULL" for v in r] |
12167 | 12193 | f.write(u" (%s)" % u", ".join(quoted_row)) |
12168 | 12194 | f.write(u"\n ;\n") |
12195 | + if outfile.lower() != 'stdout': | |
12196 | + f.close() | |
12169 | 12197 | |
12170 | 12198 | def write_query_to_values(select_stmt, db, outfile, append=False, desc=None): |
12171 | 12199 | try: |
@@ -12231,11 +12259,13 @@ | ||
12231 | 12259 | f.write(u'\n</head>\n<body>\n') |
12232 | 12260 | write_table(f) |
12233 | 12261 | f.write(u'</body>\n</html>\n') |
12262 | + f.close() | |
12234 | 12263 | else: |
12235 | 12264 | if not os.path.isfile(outfile): |
12236 | 12265 | ef = EncodedFile(outfile, conf.output_encoding) |
12237 | 12266 | f = ef.open("wt") |
12238 | 12267 | write_table(f) |
12268 | + f.close() | |
12239 | 12269 | else: |
12240 | 12270 | ef = EncodedFile(outfile, conf.output_encoding) |
12241 | 12271 | f = ef.open("rt") |
@@ -12304,6 +12334,8 @@ | ||
12304 | 12334 | f.write(u'\\begin{document}\n') |
12305 | 12335 | write_table(f) |
12306 | 12336 | f.write(u'\\end{document}\n') |
12337 | + if outfile.lower() != 'stdout': | |
12338 | + f.close() | |
12307 | 12339 | else: |
12308 | 12340 | if outfile.lower() == 'stdout' or not os.path.isfile(outfile): |
12309 | 12341 | if outfile.lower() == 'stdout': |
@@ -12312,6 +12344,8 @@ | ||
12312 | 12344 | ef = EncodedFile(outfile, conf.output_encoding) |
12313 | 12345 | f = ef.open("wt") |
12314 | 12346 | write_table(f) |
12347 | + if outfile.lower() != 'stdout': | |
12348 | + f.close() | |
12315 | 12349 | else: |
12316 | 12350 | ef = EncodedFile(outfile, conf.output_encoding) |
12317 | 12351 | f = ef.open("rt") |
@@ -4,7 +4,7 @@ | ||
4 | 4 | long_description = f.read() |
5 | 5 | |
6 | 6 | setuptools.setup(name='execsql', |
7 | - version='1.73.4', | |
7 | + version='1.73.5', | |
8 | 8 | description="Runs a SQL script against a PostgreSQL, MS-Access, SQLite, MS-SQL-Server, MySQL, MariaDB, Firebird, or Oracle database, or an ODBC DSN. Provides metacommands to import and export data, copy data between databases, conditionally execute SQL and metacommands, and dynamically alter SQL and metacommands with substitution variables. Data can be exported in 18 different formats, including CSV, TSV, ODS, HTML, JSON, LaTeX, and Markdown tables, and using custom templates.", |
9 | 9 | author='Dreas Nielsen', |
10 | 10 | author_email='dreas.nielsen@gmail.com', |