-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from dalager/general-improvements
General improvements
- Loading branch information
Showing
14 changed files
with
473 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -163,4 +163,6 @@ cython_debug/ | |
venv/Lib/site-packages | ||
venv | ||
*.xlsx | ||
*.xlsx.png | ||
*.png | ||
combined_project.py | ||
plotsettings.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
def combine_python_files(file_list, output_file): | ||
""" | ||
Combines multiple Python files into a single file with comments indicating | ||
the start and end of each original file. | ||
Parameters: | ||
- file_list: List of Python file names to combine. | ||
- output_file: Name of the output file. | ||
""" | ||
with open(output_file, "w") as outfile: | ||
for fname in file_list: | ||
# Add a comment indicating the start of a file | ||
outfile.write(f"# --- Start of {fname} ---\n\n") | ||
with open(fname, "r") as infile: | ||
outfile.write(infile.read()) | ||
outfile.write("\n") | ||
# Add a comment indicating the end of a file | ||
outfile.write(f"# --- End of {fname} ---\n\n") | ||
print(f"All files have been combined into {output_file}") | ||
|
||
|
||
if __name__ == "__main__": | ||
# Replace these with your actual file names | ||
python_files = [ | ||
"src/graphedexcel/__main__.py", | ||
"src/graphedexcel/graphbuilder.py", | ||
"src/graphedexcel/graph_visualizer.py", | ||
"src/graphedexcel/graph_summarizer.py", | ||
"src/graphedexcel/excel_parser.py", | ||
] | ||
output_filename = "combined_project.py" | ||
combine_python_files(python_files, output_filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
import sys | ||
from .graphbuilder import extract_formulas_and_build_dependencies | ||
|
||
# package graphedexcel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,146 @@ | ||
import os | ||
import sys | ||
from .graphbuilder import extract_formulas_and_build_dependencies | ||
import argparse | ||
import logging | ||
from .graphbuilder import build_graph_and_stats | ||
from .graph_summarizer import print_summary | ||
from .graph_visualizer import visualize_dependency_graph | ||
import src.graphedexcel.logger_config # noqa | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) > 1: | ||
path_to_excel = sys.argv[1] | ||
else: | ||
print("Please provide the path to the Excel file as an argument.") | ||
sys.exit(1) | ||
logger = logging.getLogger("graphedexcel.main") | ||
|
||
|
||
def parse_arguments(): | ||
parser = argparse.ArgumentParser( | ||
prog="graphedexcel", | ||
description="Process an Excel file to build and visualize dependency graphs.", | ||
) | ||
|
||
# Positional argument for the path to the Excel file | ||
parser.add_argument( | ||
"path_to_excel", type=str, help="Path to the Excel file to process." | ||
) | ||
|
||
# Optional flags with shorthand aliases | ||
parser.add_argument( | ||
"--remove-unconnected", | ||
"-r", | ||
action="store_true", | ||
help="Remove unconnected nodes from the dependency graph.", | ||
) | ||
|
||
parser.add_argument( | ||
"--as-directed-graph", | ||
"-d", | ||
action="store_true", | ||
help="Treat the dependency graph as directed.", | ||
) | ||
|
||
parser.add_argument( | ||
"--no-visualize", | ||
"-n", | ||
action="store_true", | ||
help="Skip the visualization of the dependency graph.", | ||
) | ||
|
||
parser.add_argument( | ||
"--layout", | ||
"-l", | ||
type=str, | ||
default="spring", | ||
choices=["spring", "circular", "kamada_kawai", "shell", "spectral"], | ||
help="Layout algorithm for graph visualization (default: spring).", | ||
) | ||
|
||
parser.add_argument( | ||
"--config", | ||
"-c", | ||
type=str, | ||
help="Path to the configuration file for visualization. See README for details.", | ||
) | ||
|
||
parser.add_argument( | ||
"--output-path", | ||
"-o", | ||
type=str, | ||
default=None, | ||
help="Specify the output path for the generated graph image.", | ||
) | ||
|
||
parser.add_argument( | ||
"--open-image", | ||
action="store_true", | ||
help="Open the generated image after visualization.", | ||
) | ||
|
||
return parser.parse_args() | ||
|
||
# does the file exist? | ||
|
||
def main(): | ||
args = parse_arguments() | ||
|
||
path_to_excel = args.path_to_excel | ||
|
||
# Check if the file exists | ||
if not os.path.exists(path_to_excel): | ||
print(f"File not found: {path_to_excel}") | ||
logger.error(f"File not found: {path_to_excel}") | ||
sys.exit(1) | ||
|
||
# Extract formulas and build the dependency graph | ||
dependency_graph, functions = extract_formulas_and_build_dependencies(path_to_excel) | ||
# Build the dependency graph and gather statistics | ||
dependency_graph, function_stats = build_graph_and_stats( | ||
path_to_excel, | ||
remove_unconnected=args.remove_unconnected, | ||
as_directed=args.as_directed_graph, | ||
) | ||
|
||
# Print summary of the dependency graph | ||
print_summary(dependency_graph, function_stats) | ||
|
||
if args.no_visualize: | ||
logger.info("Skipping visualization as per the '--no-visualize' flag.") | ||
sys.exit(0) | ||
|
||
logger.info("Visualizing the graph of dependencies. (This might take a while...)") | ||
|
||
# Determine layout | ||
layout = args.layout | ||
|
||
print_summary(dependency_graph, functions) | ||
# Configuration path | ||
config_path = args.config | ||
|
||
if "--no-visualize" not in sys.argv: | ||
print( | ||
"\033[1;30;40m\nVisualizing the graph of dependencies.\nThis might take a while...\033[0;37;40m\n" # noqa | ||
) | ||
# Determine output filename | ||
if args.output_path: | ||
filename = args.output_path | ||
else: | ||
# Create a default filename based on the Excel file name | ||
base_name = os.path.splitext(os.path.basename(path_to_excel))[0] | ||
filename = f"{base_name}_dependency_graph.png" | ||
|
||
# Visualize the dependency graph | ||
visualize_dependency_graph(dependency_graph, filename, config_path, layout) | ||
|
||
logger.info(f"Dependency graph image saved to {filename}.") | ||
|
||
# Open the image file if requested | ||
if args.open_image: | ||
try: | ||
os.startfile(filename) # Note: os.startfile is Windows-specific | ||
except AttributeError: | ||
# For macOS and Linux, use 'open' and 'xdg-open' respectively | ||
import subprocess | ||
import platform | ||
|
||
# if commandline argument --config is provided with a path to a JSON file, pass that path to the visualizer | ||
if platform.system() == "Darwin": # macOS | ||
subprocess.call(["open", filename]) | ||
elif platform.system() == "Linux": | ||
subprocess.call(["xdg-open", filename]) | ||
else: | ||
logger.warning("Unable to open the image automatically on this OS.") | ||
|
||
if "--config" in sys.argv: | ||
config_index = sys.argv.index("--config") | ||
config_path = sys.argv[config_index + 1] | ||
visualize_dependency_graph(dependency_graph, path_to_excel, config_path) | ||
else: | ||
visualize_dependency_graph(dependency_graph, path_to_excel) | ||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except Exception as e: | ||
logger.exception("An unexpected error occurred:", e) | ||
sys.exit(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.