-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.py
70 lines (52 loc) · 2.47 KB
/
cli.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# make_drawio_erd/cli.py
import argparse
import sys
import logging
from make_drawio_erd.parsers.metadata_csv_parser import MetaDataCSVParser
from make_drawio_erd.erd_drawio import ERDGenerator
def main():
parser = argparse.ArgumentParser(description='Generate draw.io ERD diagrams from a metadata CSV file.')
parser.add_argument('input_csv', help='Path to the input metadata CSV file')
parser.add_argument('output_drawio', help='Path to the output draw.io diagram file')
parser.add_argument('-v', '--verbose', action='store_true', help='Increase output verbosity')
parser.add_argument('--matching', help='Unix-style glob pattern to match table names')
args = parser.parse_args()
# Set up logging
logging.basicConfig(level=logging.INFO if args.verbose else logging.WARNING)
logger = logging.getLogger(__name__)
try:
logger.info('Parsing the CSV file...')
# Parse the CSV file
csv_parser = MetaDataCSVParser(args.input_csv)
df = csv_parser.parse()
# Filter tables based on the pattern if provided
if args.matching:
logger.info(f"Filtering tables using pattern: {args.matching}")
df = filter_tables_by_pattern(df, args.matching)
logger.info('Generating the ERD diagram...')
# Generate the ERD diagram
erd_generator = ERDGenerator(df)
drawio_xml = erd_generator.generate_drawio_xml()
logger.info('Saving the ERD diagram to the output file...')
# Save the XML to a file
with open(args.output_drawio, 'w', encoding='utf-8') as f:
f.write(drawio_xml)
print(f"ERD diagram has been generated and saved to {args.output_drawio}")
except Exception as e:
logger.error(f"An error occurred: {e}")
sys.exit(1)
def filter_tables_by_pattern(df, pattern):
import fnmatch
# Create a new column with the full table name
df['full_table_name'] = df['Catalog'] + '.' + df['Database'] + '.' + df['Table']
# Use fnmatch to filter the full_table_name based on the pattern
matched_tables = df['full_table_name'].apply(lambda x: fnmatch.fnmatch(x, pattern))
# Filter the DataFrame
filtered_df = df[matched_tables].copy()
if filtered_df.empty:
raise ValueError(f"No tables match the pattern '{pattern}'.")
# Remove the 'full_table_name' column before returning
filtered_df.drop(columns=['full_table_name'], inplace=True)
return filtered_df
if __name__ == "__main__":
main()