Skip to content

Commit 28f0055

Browse files
Simplify output formats
1 parent d53e026 commit 28f0055

File tree

3 files changed

+60
-99
lines changed

3 files changed

+60
-99
lines changed

ddev/src/ddev/cli/size/diff.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ def diff_mode(
142142
params["app"].display_error(
143143
f"No size differences were detected between the selected commits for {params['platform']}"
144144
)
145-
formatted_modules = format_modules(integrations + dependencies, params["platform"], params["version"])
145+
return []
146146
else:
147147
formatted_modules = format_modules(integrations + dependencies, params["platform"], params["version"])
148148
formatted_modules.sort(key=lambda x: x["Size_Bytes"], reverse=True)

ddev/src/ddev/cli/size/timeline.py

Lines changed: 40 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -84,33 +84,34 @@ def timeline(
8484
transient=True,
8585
console=console,
8686
) as progress:
87-
module = name # module is the name of the integration or the dependency
88-
if (
89-
initial_commit
90-
and final_commit
91-
and len(initial_commit) < MINIMUM_LENGTH_COMMIT
92-
and len(final_commit) < MINIMUM_LENGTH_COMMIT
93-
):
94-
raise click.BadParameter(f"Commit hashes must be at least {MINIMUM_LENGTH_COMMIT} characters long")
95-
elif initial_commit and len(initial_commit) < MINIMUM_LENGTH_COMMIT:
96-
raise click.BadParameter(
97-
f"Initial commit hash must be at least {MINIMUM_LENGTH_COMMIT} characters long.", param_hint="initial"
98-
)
99-
elif final_commit and len(final_commit) < MINIMUM_LENGTH_COMMIT:
100-
raise click.BadParameter(
101-
f"Final commit hash must be at least {MINIMUM_LENGTH_COMMIT} characters long.", param_hint="final"
102-
)
103-
elif final_commit and initial_commit and final_commit == initial_commit:
104-
raise click.BadParameter("Commit hashes must be different")
105-
if format:
106-
for fmt in format:
107-
if fmt not in ["png", "csv", "markdown", "json"]:
108-
raise ValueError(f"Invalid format: {fmt}. Only png, csv, markdown, and json are supported.")
109-
task = progress.add_task("[cyan]Calculating timeline...", total=None)
110-
url = app.repo.path
111-
112-
with GitRepo(url) as gitRepo:
113-
try:
87+
try:
88+
module = name # module is the name of the integration or the dependency
89+
if (
90+
initial_commit
91+
and final_commit
92+
and len(initial_commit) < MINIMUM_LENGTH_COMMIT
93+
and len(final_commit) < MINIMUM_LENGTH_COMMIT
94+
):
95+
raise click.BadParameter(f"Commit hashes must be at least {MINIMUM_LENGTH_COMMIT} characters long")
96+
elif initial_commit and len(initial_commit) < MINIMUM_LENGTH_COMMIT:
97+
raise click.BadParameter(
98+
f"Initial commit hash must be at least {MINIMUM_LENGTH_COMMIT} characters long.",
99+
param_hint="initial",
100+
)
101+
elif final_commit and len(final_commit) < MINIMUM_LENGTH_COMMIT:
102+
raise click.BadParameter(
103+
f"Final commit hash must be at least {MINIMUM_LENGTH_COMMIT} characters long.", param_hint="final"
104+
)
105+
elif final_commit and initial_commit and final_commit == initial_commit:
106+
raise click.BadParameter("Commit hashes must be different")
107+
if format:
108+
for fmt in format:
109+
if fmt not in ["png", "csv", "markdown", "json"]:
110+
raise ValueError(f"Invalid format: {fmt}. Only png, csv, markdown, and json are supported.")
111+
task = progress.add_task("[cyan]Calculating timeline...", total=None)
112+
url = app.repo.path
113+
114+
with GitRepo(url) as gitRepo:
114115
if final_commit and type == "dependency":
115116
date_str, _, _ = gitRepo.get_commit_metadata(final_commit)
116117
date = datetime.strptime(date_str, "%b %d %Y").date()
@@ -232,9 +233,9 @@ def timeline(
232233
if format:
233234
export_format(app, format, modules, None, module, compressed)
234235

235-
except Exception as e:
236-
progress.stop()
237-
app.abort(str(e))
236+
except Exception as e:
237+
progress.stop()
238+
app.abort(str(e))
238239

239240

240241
@overload
@@ -281,10 +282,14 @@ def timeline_mode(
281282
if not params["format"] or params["format"] == ["png"]: # if no format is provided for the data print the table
282283
print_table(params["app"], "Status", formatted_modules)
283284

284-
treemap_path = f"treemap_{params['platform']}.png" if params["format"] and "png" in params["format"] else None
285+
timeline_path = (
286+
f"timeline_{params['module']}_{params['platform']}.png"
287+
if params["platform"] and params["format"] and "png" in params["format"]
288+
else f"timeline_{params['module']}.png" if params["format"] and "png" in params["format"] else None
289+
)
285290

286-
if params["show_gui"] or treemap_path:
287-
plot_linegraph(formatted_modules, params["module"], params["platform"], params["show_gui"], treemap_path)
291+
if params["show_gui"] or timeline_path:
292+
plot_linegraph(formatted_modules, params["module"], params["platform"], params["show_gui"], timeline_path)
288293

289294
return formatted_modules
290295

@@ -627,44 +632,14 @@ def format_modules(
627632
"""
628633
Formats the modules list, adding platform and Python version information if needed.
629634
630-
If the modules list is empty, returns a default empty entry (with or without platform information).
631-
632635
Args:
633636
modules: List of modules to format.
634637
platform: Platform string to add to each entry if needed.
635-
version: Python version string to add to each entry if needed.
636-
i: Index of the current platform, version) combination being processed.
637-
If None, it means the data is being processed for only one platform.
638638
639639
Returns:
640640
A list of formatted entries.
641641
"""
642-
if modules == [] and platform:
643-
empty_module_platform: CommitEntryPlatformWithDelta = {
644-
"Size_Bytes": 0,
645-
"Version": "",
646-
"Date": datetime.min.date(),
647-
"Author": "",
648-
"Commit_Message": "",
649-
"Commit_SHA": "",
650-
"Delta_Bytes": 0,
651-
"Delta": " ",
652-
"Platform": "",
653-
}
654-
return [empty_module_platform]
655-
elif modules == []:
656-
empty_module: CommitEntryWithDelta = {
657-
"Size_Bytes": 0,
658-
"Version": "",
659-
"Date": datetime.min.date(),
660-
"Author": "",
661-
"Commit_Message": "",
662-
"Commit_SHA": "",
663-
"Delta_Bytes": 0,
664-
"Delta": " ",
665-
}
666-
return [empty_module]
667-
elif platform:
642+
if platform:
668643
new_modules: list[CommitEntryPlatformWithDelta] = [{**entry, "Platform": platform} for entry in modules]
669644
return new_modules
670645
else:
@@ -829,7 +804,7 @@ def plot_linegraph(
829804
show: If True, displays the plot interactively.
830805
path: If provided, saves the plot to this file path.
831806
"""
832-
if not any(str(value).strip() not in ("", "0", "0001-01-01") for value in modules[0].values()): # table is empty
807+
if modules == []:
833808
return
834809

835810
dates = [entry["Date"] for entry in modules]

ddev/src/ddev/cli/size/utils/common_funcs.py

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -320,26 +320,11 @@ def format_modules(
320320
) -> list[FileDataEntryPlatformVersion]:
321321
"""
322322
Formats the modules list, adding platform and Python version information.
323-
324-
If the modules list is empty, returns a default empty entry.
325323
"""
326-
327-
if modules == []:
328-
empty_entry_with_platform: FileDataEntryPlatformVersion = {
329-
"Name": "",
330-
"Version": "",
331-
"Size_Bytes": 0,
332-
"Size": "",
333-
"Type": "",
334-
"Platform": "",
335-
"Python_Version": "",
336-
}
337-
return [empty_entry_with_platform]
338-
else:
339-
new_modules: list[FileDataEntryPlatformVersion] = [
340-
{**entry, "Platform": platform, "Python_Version": py_version} for entry in modules
341-
]
342-
return new_modules
324+
new_modules: list[FileDataEntryPlatformVersion] = [
325+
{**entry, "Platform": platform, "Python_Version": py_version} for entry in modules
326+
]
327+
return new_modules
343328

344329

345330
def save_json(
@@ -352,12 +337,11 @@ def save_json(
352337
| list[CommitEntryPlatformWithDelta]
353338
),
354339
) -> None:
355-
filtered_modules = [
356-
row for row in modules if any(str(value).strip() not in ("", "0", "0001-01-01") for value in row.values())
357-
]
340+
if modules == []:
341+
return
358342

359343
with open(file_path, "w", encoding="utf-8") as f:
360-
json.dump(filtered_modules, f, default=str, indent=2)
344+
json.dump(modules, f, default=str, indent=2)
361345
app.display(f"JSON file saved to {file_path}")
362346

363347

@@ -366,14 +350,16 @@ def save_csv(
366350
modules: list[FileDataEntryPlatformVersion] | list[CommitEntryWithDelta] | list[CommitEntryPlatformWithDelta],
367351
file_path: str,
368352
) -> None:
353+
if modules == []:
354+
return
355+
369356
headers = [k for k in modules[0].keys() if k not in ["Size", "Delta"]]
370357

371358
with open(file_path, "w", encoding="utf-8") as f:
372359
f.write(",".join(headers) + "\n")
373360

374361
for row in modules:
375-
if any(str(value).strip() not in ("", "0", "0001-01-01") for value in row.values()):
376-
f.write(",".join(format(str(row.get(h, ""))) for h in headers) + "\n")
362+
f.write(",".join(format(str(row.get(h, ""))) for h in headers) + "\n")
377363

378364
app.display(f"CSV file saved to {file_path}")
379365

@@ -391,9 +377,8 @@ def save_markdown(
391377
modules: list[FileDataEntryPlatformVersion] | list[CommitEntryWithDelta] | list[CommitEntryPlatformWithDelta],
392378
file_path: str,
393379
) -> None:
394-
395-
if all(str(value).strip() in ("", "0", "0001-01-01") for value in modules[0].values()):
396-
return # skip empty table
380+
if modules == []:
381+
return
397382

398383
headers = [k for k in modules[0].keys() if "Bytes" not in k]
399384

@@ -441,12 +426,14 @@ def print_table(
441426
mode: str,
442427
modules: list[FileDataEntryPlatformVersion] | list[CommitEntryWithDelta] | list[CommitEntryPlatformWithDelta],
443428
) -> None:
429+
if modules == []:
430+
return
431+
444432
columns = [col for col in modules[0].keys() if "Bytes" not in col]
445433
modules_table: dict[str, dict[int, str]] = {col: {} for col in columns}
446434
for i, row in enumerate(modules):
447-
if any(str(value).strip() not in ("", "0", "0001-01-01") for value in row.values()):
448-
for key in columns:
449-
modules_table[key][i] = str(row.get(key, ""))
435+
for key in columns:
436+
modules_table[key][i] = str(row.get(key, ""))
450437

451438
app.display_table(mode, modules_table)
452439

@@ -506,8 +493,7 @@ def plot_treemap(
506493
mode: Literal["status", "diff"],
507494
path: Optional[str] = None,
508495
) -> None:
509-
if not any(str(value).strip() not in ("", "0") for value in modules[0].values()):
510-
# table is empty
496+
if modules == []:
511497
return
512498

513499
# Initialize figure and axis

0 commit comments

Comments
 (0)