Skip to content

More refactoring of Python code to address Sonar issues#906

Merged
rdmark merged 8 commits intodevelopfrom
rdmark-refactor-python-again
Oct 12, 2022
Merged

More refactoring of Python code to address Sonar issues#906
rdmark merged 8 commits intodevelopfrom
rdmark-refactor-python-again

Conversation

@rdmark
Copy link
Copy Markdown
Member

@rdmark rdmark commented Oct 10, 2022

  • Use Path objects for file operations
  • Use urllib to sanitize URLs
  • Some explicit type conversions
  • Consistent regex syntax
  • Add rudimentary logging when archive extraction caching fails
  • Fixed two cases of the property file creation not being notified in the Flash message
  • (not Python related but) added doctype and html lang attribute to the web server 502 page

@rdmark rdmark requested a review from nucleogenic October 10, 2022 02:38
@rdmark rdmark marked this pull request as ready for review October 10, 2022 04:48
if file.name in prop_files:
process = self.read_drive_properties(f"{CFG_DIR}/{file.name}.{PROPERTIES_SUFFIX}")
process = self.read_drive_properties(
Path(CFG_DIR) / f"{file.name}.{PROPERTIES_SUFFIX}"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, this is super weird at first sight (operator overloading) but cool to see in practice.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's spooky isn't it!

Comment on lines +244 to +254
if (
# Note: This check will fail if the wireless interface doesn't start with wlan
interface.startswith("wlan") and
sys_cmd.introspect_file("/etc/sysctl.conf", r"^net\.ipv4\.ip_forward=1$") and
Path("/etc/iptables/rules.v4").is_file()
or sys_cmd.introspect_file(
"/etc/dhcpcd.conf",
r"^denyinterfaces " + interface + r"$",
):
status = False
return_msg = _("Configure the network bridge before using a wired network device.")
elif not Path("/etc/network/interfaces.d/rascsi_bridge").is_file():
status = False
return_msg = _("Configure the network bridge before using a wired network device.")

return {"status": status, "msg": return_msg + f" ({interface})"}
)
and Path("/etc/network/interfaces.d/rascsi_bridge").is_file()
):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method's structure is worth another look because the change obfuscates the intent to a greater degree than the existing code. There's quite a lot going on and I found it difficult to grok when merged into a single expression.

There's also a loss of verbosity. If we want to simply the messaging in the web UI, then I think moving those messages to the logs should be considered.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, let me revert this change and add a TODO instead. I shouldn't be changing localized strings this close to the next release.

Comment on lines +363 to +366
return response(message=[
(_("Image file created: %(file_name)s", file_name=full_file_name), "success"),
(process["msg"], "success"),
])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Optional) We should try to have one message per operation if possible.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. I can add TODOs here. Once we've tagged the next release we can refactor these status messages. I don't want to mess with localized strings this close to the release.

@rdmark rdmark requested a review from nucleogenic October 12, 2022 19:44
@rdmark rdmark merged commit 5a67950 into develop Oct 12, 2022
@rdmark rdmark deleted the rdmark-refactor-python-again branch October 12, 2022 19:59
@sonarqubecloud
Copy link
Copy Markdown

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants