Skip to content

Commit

Permalink
MAINT: switch yaml linting button working
Browse files Browse the repository at this point in the history
  • Loading branch information
jotelha committed Oct 30, 2023
1 parent 17e3505 commit c34fc2a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
45 changes: 24 additions & 21 deletions dtool_lookup_gui/views/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

import yamllint
from yamllint.config import YamlLintConfig
from yamllint.linter import run
import yamllint.linter

# As of dtool-lookup-api 0.5.0, the following line still is a necessity to
# disable prompting for credentials on the command line. This behavior
Expand Down Expand Up @@ -282,6 +282,9 @@ def __init__(self, *args, **kwargs):
style_context = self.curr_page_button.get_style_context()
style_context.add_class('suggested-action')

# initialize linting_problems cache
self.linting_problems = None

# utility methods
def refresh(self):
"""Refresh view."""
Expand Down Expand Up @@ -798,34 +801,34 @@ def on_save_metadata_button_clicked(self, widget):
yaml_content = text_buffer.get_text(start_iter, end_iter, True)

# Check the state of the linting switch before linting
if not settings.yaml_linting_enabled:
# If linting is turned off, just save
print("YAML Turned off")
self.dataset_list_box.get_selected_row().dataset.put_readme(yaml_content)
return

# Lint the YAML content if the above condition wasn't met (i.e., linting is enabled)
conf = YamlLintConfig('extends: default') # using the default config
self.problems = list(run(yaml_content, conf)) # Make it an instance variable
_logger.debug(str(self.problems))
if self.problems:
total_errors = len(self.problems)
if total_errors == 1:
error_message = f"YAML Linter Error:\n{str(self.problems[0])}"
if settings.yaml_linting_enabled:
# Lint the YAML content if the above condition wasn't met (i.e., linting is enabled)
conf = YamlLintConfig('extends: default') # using the default config
self.linting_problems = list(yamllint.linter.run(yaml_content, conf)) # Make it an instance variable
_logger.debug(str(self.linting_problems))
total_errors = len(self.linting_problems)
if total_errors > 0:
if total_errors == 1:
error_message = f"YAML Linter Error:\n{str(self.linting_problems[0])}"
else:
other_errors_count = total_errors - 1 # since we're showing the first error
error_message = f"YAML Linter Error:\n{str(self.linting_problems[0])} and {other_errors_count} other YAML linting errors.\nClick here for more details"
self.linting_errors_button.set_label(error_message)
else:
other_errors_count = total_errors - 1 # since we're showing the first error
error_message = f"YAML Linter Error:\n{str(self.problems[0])} and {other_errors_count} other YAML linting errors.\nClick here for more details"
self.linting_errors_button.set_label(error_message)
self.linting_errors_button.set_label("No linting issues found!")
self.dataset_list_box.get_selected_row().dataset.put_readme(yaml_content)
else:
self.linting_errors_button.set_label("No linting issues found!")
# if linting is turned off, just save as is
self.linting_errors_button.set_label("YAML linting turned off.")
_logger.debug("YAML linting turned off.")
self.dataset_list_box.get_selected_row().dataset.put_readme(yaml_content)

@Gtk.Template.Callback()
def on_linting_errors_button_clicked(self, widget):
# Check if the problems attribute exists
if hasattr(self, 'problems') and self.problems:
if hasattr(self, 'linting_problems') and self.linting_problems:
# Join the linting error messages into a single string
error_text = '\n\n'.join(str(problem) for problem in self.problems)
error_text = '\n\n'.join(str(problem) for problem in self.linting_problems)

# Set the linting error text to the dialog
self.error_linting_dialog.set_error_text(error_text)
Expand Down
8 changes: 5 additions & 3 deletions dtool_lookup_gui/views/settings_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ def __init__(self, *args, **kwargs):
settings.settings.bind("open-downloaded-item",
self.open_downloaded_item_checkbox,
'active', Gio.SettingsBindFlags.DEFAULT)
settings.settings.bind("yaml-linting-enabled", self, "yaml_linting_enabled", Gio.SettingsBindFlags.DEFAULT)
settings.settings.bind("yaml-linting-enabled",
self.yaml_linting_switch,
'active', Gio.SettingsBindFlags.DEFAULT)


# register own refresh method as listener for app-central dtool-config-changed signal
Expand Down Expand Up @@ -294,8 +296,8 @@ def on_base_uri_state_changed(self, widget, state):

@Gtk.Template.Callback()
def on_yaml_linting_switch_state_set(self, widget, state):
if state != Gtk.StateType.ACTIVE:
self.yaml_linting_enabled = False
"""Switch yaml linting on and off"""
settings.yaml_linting_enabled = state



Expand Down

0 comments on commit c34fc2a

Please sign in to comment.