Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding script (command line) notifier #336

Merged
merged 34 commits into from
Jun 3, 2023
Merged

Conversation

sjoerdj
Copy link
Contributor

@sjoerdj sjoerdj commented May 7, 2023

Pull Request Checklist:

  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Did you make your Pull Request on the dev branch?
  • Does your submission pass tests? make test
  • Have you lint your code locally prior to submission? make lint
  • Could you build and run the docker image successfully? make image
  • Could you create a running executable? make executable
  • Have you added an explanation of what your changes do and why you'd like to include them?
  • Have you written new tests for your changes, as applicable?
  • Have you successfully ran manual tests with your changes locally?

Copy link
Owner

@Der-Henning Der-Henning left a comment

Choose a reason for hiding this comment

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

Hi @sjoerdj,
Thank you for your contribution. I didn't test your code for now, but I want to give you a first review.

  1. Please use linting and formatting as configured in the pre-commit script. To run it you can type pre-commit run -a.
  2. Please don't change code, including formatting, that is out of the scope of the feature you want to implement. (config.sample.ini and config.py)
  3. To make your PR perfect, it would be nice, if you provide a unit test for your notifier.



def __repr__(self) -> str:
return " "
Copy link
Owner

Choose a reason for hiding this comment

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

The returned string is displayed on startup when displaying the activated notifiers.

Suggested change
return " "
return f"Shell script: {self.command}"

Copy link
Owner

@Der-Henning Der-Henning left a comment

Choose a reason for hiding this comment

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

Hi. What is the problem with

cmd = item.unmask(self.command)
subprocess.Popen(cmd)

?

Comment on lines 29 to 30
commandline = commandline.replace('${{','~${{')
commandline = commandline.replace('}}','}}~')
Copy link
Owner

Choose a reason for hiding this comment

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

What is the reason for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem that I faced while testing is that if the unmasked command line item contains spaces (e.g. display_name), the separation of the command line arguments goes wrong. That's why I added a tilde sign before and after the accolades before unmasking.

commandlist.append(commandline[start:pos])
start = pos+1
commandlist.append(commandline[start:pos])
commandlist = [a.replace('~','') for a in commandlist]
Copy link
Owner

Choose a reason for hiding this comment

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

This could make trouble when using a script placed in the home folder and using a path like ~\script.sh.

Copy link
Contributor Author

@sjoerdj sjoerdj May 21, 2023

Choose a reason for hiding this comment

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

I agree that the tilde is not the best choice, a pipe symbol might have been a better idea. Anyway, I now built simpler and much cleaner code to list the arguments, eliminating the need for quoting and unquoting:

commandlist = self.command.split()
commandlist = [item.unmask(a) for a in commandlist]



def _send(self, item: Item) -> None:
import subprocess
Copy link
Owner

Choose a reason for hiding this comment

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

Why do you place the import here and not on the module level?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good one, thanks! I have now put the import at the top of script.py

self.cron = config.script.get("cron")

if self.enabled:
try:
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe raise a ConfigurationError when the command is missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good suggestion, thanks! I just added:

if self.enabled and (not self.command):
raise ScriptConfigurationError()

@Der-Henning
Copy link
Owner

Hi @sjoerdj.
I think the code only needs minimal adjustments to be merged in the dev branch. Would you mind if I resolve the merge conflicts and merge the PR?

@sjoerdj
Copy link
Contributor Author

sjoerdj commented Jun 3, 2023

Hi Would you mind if I resolve the merge conflicts and merge the PR?

Not at all, please go ahead. Thanks a lot for your review, it has been a nice lesson on clean coding!

@Der-Henning Der-Henning marked this pull request as ready for review June 3, 2023 12:00
@Der-Henning Der-Henning merged commit 0fcf480 into Der-Henning:dev Jun 3, 2023
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