Skip to content

Commit 84b404b

Browse files
committed
Output to stdout by default, log to stderr; remove separate file output mode
1 parent aba9f9a commit 84b404b

File tree

1 file changed

+23
-35
lines changed

1 file changed

+23
-35
lines changed

code/Python/dbd_to_sql.py

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,39 @@
55
from argparse import ArgumentParser
66
from collections import defaultdict
77
from glob import glob
8-
9-
script_dir:str = os.path.dirname(os.path.abspath(__file__));
8+
from io import TextIOWrapper
9+
from sys import stdout, stderr
1010

1111
parser = ArgumentParser();
1212
group = parser.add_mutually_exclusive_group();
1313
group.add_argument('--layout', type=str, help="target layout, e.g. '90747013'");
1414
group.add_argument('--build', type=str, help="target build, e.g. '10.0.0.43342'");
1515
parser.add_argument('dbds', type=str, nargs='*', help='directory with / list of for dbd files to process');
16-
parser.add_argument('--output', type=str, default=os.path.join(script_dir, 'dbds.sql'), help='file or directory to dump sql to');
16+
parser.add_argument('--output', type=str, default=stdout, help='file to dump sql to');
1717
args = parser.parse_args();
1818

1919
dbds:list[str] = args.dbds or os.path.join(
2020
os.path.dirname( # WoWDBDefs/
2121
os.path.dirname( # code/
22-
script_dir # Python/
23-
)),
22+
os.path.dirname( # Python/
23+
os.path.abspath(__file__) # ./dbd_to_sql.py
24+
))),
2425
'definitions'
2526
);
2627
if not dbds[0].endswith(dbd.file_suffix):
2728
dbds = glob(os.path.join(dbds[0], '*.dbd'));
2829

29-
print(f"Found {len(dbds)} definitions to process");
30+
outfile:TextIOWrapper = args.output;
31+
if type(outfile) != TextIOWrapper:
32+
outfile = open(outfile, 'a');
33+
34+
def log(*args, **kwargs)->None:
35+
print(*args, file=stderr if outfile == stdout else stdout, **kwargs);
3036

31-
outfile:str = args.output;
32-
outdir:str = '';
33-
if outfile.endswith('.sql'):
34-
with open(outfile, 'w') as file:
35-
file.write("SET SESSION FOREIGN_KEY_CHECKS=0;\n");
36-
else:
37-
if not os.path.isdir(outfile):
38-
os.makedirs(outfile);
39-
outdir = outfile;
40-
outfile = None;
37+
log(f"Found {len(dbds)} definitions to process");
4138

42-
print(f"Outputting to {outdir or outfile}");
39+
log(f"Outputting to {outfile}");
40+
outfile.write("SET SESSION FOREIGN_KEY_CHECKS=0;\n");
4341

4442
def get_sql_type(type:str, int_width:int=0, is_unsigned:bool=False)->str:
4543
type = {
@@ -72,7 +70,7 @@ def get_sql_type(type:str, int_width:int=0, is_unsigned:bool=False)->str:
7270
def process_dbd(file:str)->bool:
7371
parsed:dbd.dbd_file = dbd.parse_dbd_file(file);
7472
if not len(parsed.definitions):
75-
print(f"No definitions found in {file}! Skipping");
73+
log(f"No definitions found in {file}! Skipping");
7674
return False;
7775

7876
dirname:str = os.path.dirname(file);
@@ -92,7 +90,7 @@ def process_dbd(file:str)->bool:
9290
if args.layout:
9391
definition = next(defn for defn in parsed.definitions if args.layout in defn.layouts);
9492
if not definition:
95-
print(f"No definition found for layout {args.layout}! Skipping");
93+
log(f"No definition found for layout {args.layout}! Skipping");
9694
return False;
9795
elif args.build:
9896
definition = next(defn for defn in parsed.definitions if args.build in defn.builds);
@@ -119,10 +117,10 @@ def process_dbd(file:str)->bool:
119117
foreign_dbd:str = next((f for f in dbds if os.path.basename(f) == f"{foreign.table}.dbd"), None);
120118
if foreign_dbd:
121119
if not process_dbd(foreign_dbd):
122-
print(f"Could not process table {foreign.table} referenced by {name}.{entry.column}");
120+
log(f"Could not process table {foreign.table} referenced by {name}.{entry.column}");
123121
return False;
124122
if not foreign_dbd:
125-
print(f"FK {name}.{entry.column} references {foreign.column} in {foreign.table} which was not supplied");
123+
log(f"FK {name}.{entry.column} references {foreign.column} in {foreign.table} which was not supplied");
126124

127125
sql_type = keys[foreign.table.string].get(foreign.column.string, None) or sql_type;
128126
fkeys.append(
@@ -144,22 +142,12 @@ def process_dbd(file:str)->bool:
144142
if len(fkeys):
145143
fields.append(', '.join(fkeys));
146144

147-
stmt:str = f"CREATE OR REPLACE TABLE `{name}` ({', '.join(fields)})";
148-
149-
if outfile:
150-
with open(outfile, 'a') as file:
151-
file.write(f"{stmt};\n");
152-
elif outdir:
153-
with open(os.path.join(outdir, f"{name}.sql"), 'w') as file:
154-
file.write(stmt);
155-
145+
outfile.write(f"CREATE OR REPLACE TABLE `{name}` ({', '.join(fields)});\n");
156146
return True;
157147

158148
for file in dbds:
159149
process_dbd(file);
160150

161-
if outfile:
162-
with open(outfile, 'a') as file:
163-
file.write("SET SESSION FOREIGN_KEY_CHECKS=1;\n");
164-
165-
print('Done.');
151+
outfile.write("SET SESSION FOREIGN_KEY_CHECKS=1;\n");
152+
outfile.close();
153+
log('Done.');

0 commit comments

Comments
 (0)