Skip to content

Commit d042f3a

Browse files
committed
fix init and refactor
1 parent 8381ebf commit d042f3a

File tree

2 files changed

+38
-44
lines changed

2 files changed

+38
-44
lines changed

src/warnet/main.py

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,40 +77,37 @@ def quickstart():
7777
return False
7878

7979

80-
@cli.command()
81-
@click.argument("directory", type=click.Path(file_okay=False, dir_okay=True, resolve_path=True))
82-
def create(directory: Path):
83-
"""Create a new warnet project in the specified directory"""
84-
_create(directory)
85-
86-
87-
def _create(directory: Path):
88-
full_path = Path(directory)
89-
if full_path.exists():
90-
richprint(f"[red]Error: Directory {full_path} already exists[/red]")
91-
return
80+
def create_warnet_project(directory: Path, check_empty: bool = False):
81+
"""Common function to create a warnet project"""
82+
if check_empty and any(directory.iterdir()):
83+
richprint("[yellow]Warning: Directory is not empty[/yellow]")
84+
if not click.confirm("Do you want to continue?", default=True):
85+
return
9286

9387
try:
94-
copy_network_defaults(full_path)
95-
copy_scenario_defaults(full_path)
96-
richprint(f"[green]Copied network example files to {full_path / 'networks'}[/green]")
97-
richprint(f"[green]Created warnet project structure in {full_path}[/green]")
88+
copy_network_defaults(directory)
89+
copy_scenario_defaults(directory)
90+
richprint(f"[green]Copied network example files to {directory / 'networks'}[/green]")
91+
richprint(f"[green]Created warnet project structure in {directory}[/green]")
9892
except Exception as e:
9993
richprint(f"[red]Error creating project: {e}[/red]")
10094

95+
@cli.command()
96+
@click.argument(
97+
"directory", type=click.Path(file_okay=False, dir_okay=True, resolve_path=True, path_type=Path)
98+
)
99+
def create(directory: Path):
100+
"""Create a new warnet project in the specified directory"""
101+
if directory.exists():
102+
richprint(f"[red]Error: Directory {directory} already exists[/red]")
103+
return
104+
create_warnet_project(directory)
101105

102106
@cli.command()
103107
def init():
104108
"""Initialize a warnet project in the current directory"""
105-
current_dir = os.getcwd()
106-
if os.listdir(current_dir):
107-
richprint("[yellow]Warning: Current directory is not empty[/yellow]")
108-
if not click.confirm("Do you want to continue?", default=True):
109-
return
110-
111-
copy_network_defaults(current_dir)
112-
richprint(f"[green]Copied network example files to {Path(current_dir) / 'networks'}[/green]")
113-
richprint(f"[green]Created warnet project structure in {current_dir}[/green]")
109+
current_dir = Path.cwd()
110+
create_warnet_project(current_dir, check_empty=True)
114111

115112

116113
@cli.command()

src/warnet/network.py

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,16 @@ def setup_logging_helm() -> bool:
6868
return True
6969

7070

71-
def copy_network_defaults(directory: Path):
72-
"""Create the project structure for a warnet project"""
73-
(directory / WAR_NETWORK_DIR / DEFAULT_NETWORK).mkdir(parents=True, exist_ok=True)
74-
target_network_defaults = directory / WAR_NETWORK_DIR / DEFAULT_NETWORK / DEFAULTS_FILE
75-
target_network_example = directory / WAR_NETWORK_DIR / DEFAULT_NETWORK / NETWORK_FILE
76-
shutil.copy2(WAR_NETWORK_FILES / DEFAULT_NETWORK / DEFAULTS_FILE, target_network_defaults)
77-
shutil.copy2(WAR_NETWORK_FILES / DEFAULT_NETWORK / NETWORK_FILE, target_network_example)
78-
79-
80-
def copy_scenario_defaults(directory: Path):
81-
"""Create the project structure for a warnet project"""
82-
target_dir = directory / WAR_SCENARIOS_DIR
71+
def copy_defaults(directory: Path, target_subdir: str, source_path: Path, exclude_list: list[str]):
72+
"""Generic function to copy default files and directories"""
73+
target_dir = directory / target_subdir
8374
target_dir.mkdir(parents=True, exist_ok=True)
84-
print(f"Creating scenarios directory: {target_dir}")
85-
86-
scenarios_path = WAR_SCENARIOS_FILES.joinpath()
75+
print(f"Creating directory: {target_dir}")
8776

8877
def should_copy(item: Path) -> bool:
89-
return item.name not in ["__init__.py", "__pycache__", "commander.py"]
78+
return item.name not in exclude_list
9079

91-
for item in scenarios_path.iterdir():
80+
for item in source_path.iterdir():
9281
if should_copy(item):
9382
if item.is_file():
9483
shutil.copy2(item, target_dir)
@@ -97,7 +86,15 @@ def should_copy(item: Path) -> bool:
9786
shutil.copytree(item, target_dir / item.name, dirs_exist_ok=True)
9887
print(f"Copied directory: {item.name}")
9988

100-
print(f"Finished copying scenario files to {target_dir}")
89+
print(f"Finished copying files to {target_dir}")
90+
91+
def copy_network_defaults(directory: Path):
92+
"""Create the project structure for a warnet project's network"""
93+
copy_defaults(directory, WAR_NETWORK_DIR, WAR_NETWORK_FILES.joinpath(), [])
94+
95+
def copy_scenario_defaults(directory: Path):
96+
"""Create the project structure for a warnet project's scenarios"""
97+
copy_defaults(directory, WAR_SCENARIOS_DIR, WAR_SCENARIOS_FILES.joinpath(), ["__init__.py", "__pycache__", "commander.py"])
10198

10299

103100
@network.command()
@@ -179,7 +176,7 @@ def _connected():
179176
for peer in peerinfo:
180177
if peer["connection_type"] == "manual":
181178
manuals += 1
182-
# Even if more edges are specifed, bitcoind only allows
179+
# Even if more edges are specified, bitcoind only allows
183180
# 8 manual outbound connections
184181

185182
print("manual " + str(manuals))

0 commit comments

Comments
 (0)