Skip to content

Commit

Permalink
CI: add flake8 job, fix flake8 violations
Browse files Browse the repository at this point in the history
  • Loading branch information
deltragon committed Jul 25, 2024
1 parent de5e7ea commit cf5552b
Show file tree
Hide file tree
Showing 22 changed files with 278 additions and 111 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
indent_style = space
indent_size = 4

[*.{py}]
trim_trailing_whitespace = true
13 changes: 13 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[flake8]
extend-ignore =
# don't check max complexity
C901,
# pygobject requires calling gi.require_version before import
E402
exclude =
.git, __pycache__,
build, dist,
# don't check setup.py
setup.py
max-line-length = 120
builtins = _
17 changes: 17 additions & 0 deletions .github/workflows/flake8.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: flake8 Lint

on: [push, pull_request]

jobs:
flake8-lint:
runs-on: ubuntu-latest
name: Lint
steps:
- name: Check out source repository
uses: actions/checkout@v3
- name: Set up Python environment
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: flake8 Lint
uses: py-actions/flake8@v2
30 changes: 24 additions & 6 deletions safeeyes/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ def __running():
else:
# In older versions cmdline was a list object
cmd_line = proc.cmdline
if ('python3' in cmd_line[0] or 'python' in cmd_line[0]) and ('safeeyes' in cmd_line[1] or 'safeeyes' in cmd_line):
if (
('python3' in cmd_line[0] or 'python' in cmd_line[0])
and ('safeeyes' in cmd_line[1] or 'safeeyes' in cmd_line)
):
process_count += 1
if process_count > 1:
return True
Expand All @@ -67,25 +70,40 @@ def main():
"""
Start the Safe Eyes.
"""
system_locale = gettext.translation('safeeyes', localedir=utility.LOCALE_PATH, languages=[utility.system_locale(), 'en_US'], fallback=True)
system_locale = gettext.translation(
'safeeyes',
localedir=utility.LOCALE_PATH,
languages=[utility.system_locale(), 'en_US'],
fallback=True
)
system_locale.install()
try:
# locale.bindtextdomain is required for Glade files
locale.bindtextdomain('safeeyes', utility.LOCALE_PATH)
except AttributeError:
logging.warning('installed python\'s gettext module does not support locale.bindtextdomain. locale.bindtextdomain is required for Glade files')

logging.warning(
'installed python\'s gettext module does not support locale.bindtextdomain. locale.bindtextdomain is required for Glade files' # noqa: E501
)

parser = argparse.ArgumentParser(prog='safeeyes', description=_('description'))
group = parser.add_mutually_exclusive_group()
group.add_argument('-a', '--about', help=_('show the about dialog'), action='store_true')
group.add_argument('-d', '--disable', help=_('disable the currently running safeeyes instance'), action='store_true')
group.add_argument(
'-d',
'--disable',
help=_('disable the currently running safeeyes instance'),
action='store_true'
)
group.add_argument('-e', '--enable', help=_('enable the currently running safeeyes instance'), action='store_true')
group.add_argument('-q', '--quit', help=_('quit the running safeeyes instance and exit'), action='store_true')
group.add_argument('-s', '--settings', help=_('show the settings dialog'), action='store_true')
group.add_argument('-t', '--take-break', help=_('Take a break now').lower(), action='store_true')
parser.add_argument('--debug', help=_('start safeeyes in debug mode'), action='store_true')
parser.add_argument('--status', help=_('print the status of running safeeyes instance and exit'), action='store_true')
parser.add_argument(
'--status',
help=_('print the status of running safeeyes instance and exit'),
action='store_true'
)
parser.add_argument('--version', action='version', version='%(prog)s ' + SAFE_EYES_VERSION)
args = parser.parse_args()

Expand Down
15 changes: 8 additions & 7 deletions safeeyes/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import time

from safeeyes import utility
from safeeyes.model import Break
from safeeyes.model import BreakType
from safeeyes.model import BreakQueue
from safeeyes.model import EventHook
Expand Down Expand Up @@ -133,17 +132,18 @@ def postpone(self, duration=-1):
logging.debug("Postpone the break for %d seconds", self.postpone_duration)
self.context['postponed'] = True

def get_break_time(self, break_type = None):
def get_break_time(self, break_type=None):
"""
Returns the next break time
"""
break_obj = self.break_queue.get_break(break_type)
if not break_obj:
return False
time = self.scheduled_next_break_time + datetime.timedelta(minutes=break_obj.time - self.break_queue.get_break().time)
time = self.scheduled_next_break_time + \
datetime.timedelta(minutes=break_obj.time - self.break_queue.get_break().time)
return time

def take_break(self, break_type = None):
def take_break(self, break_type=None):
"""
Calling this method stops the scheduler and show the next break screen
"""
Expand All @@ -153,13 +153,13 @@ def take_break(self, break_type = None):
return
utility.start_thread(self.__take_break, break_type=break_type)

def has_breaks(self, break_type = None):
def has_breaks(self, break_type=None):
"""
Check whether Safe Eyes has breaks or not. Use the break_type to check for either short or long break.
"""
return not self.break_queue.is_empty(break_type)

def __take_break(self, break_type = None):
def __take_break(self, break_type=None):
"""
Show the next break screen
"""
Expand Down Expand Up @@ -269,7 +269,8 @@ def __fire_start_break(self):
# Plugins want to postpone this break
self.context['postponed'] = False
# Update the next break time
self.scheduled_next_break_time = self.scheduled_next_break_time + datetime.timedelta(seconds=self.postpone_duration)
self.scheduled_next_break_time = self.scheduled_next_break_time + \
datetime.timedelta(seconds=self.postpone_duration)
self.__fire_on_update_next_break(self.scheduled_next_break_time)
# Wait in user thread
utility.start_thread(self.__postpone_break)
Expand Down
45 changes: 24 additions & 21 deletions safeeyes/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ def __init__(self, config, context):
self.__build_longs()
self.__build_shorts()


# Interface guarantees that short_interval >= 1
# And that long_interval is a multiple of short_interval
short_interval = config.get('short_break_interval')
long_interval = config.get('long_break_interval')
long_interval = config.get('long_break_interval')
self.__cycle_len = int(long_interval / short_interval)
# To count every long break as a cycle in .next() if there are no short breaks
if self.__short_queue is None:
Expand All @@ -114,7 +113,7 @@ def __init__(self, config, context):
while brk != current_break and brk.name != last_break:
brk = self.next()

def get_break(self, break_type = None):
def get_break(self, break_type=None):
if self.__current_break is None:
self.__current_break = self.next()

Expand All @@ -123,20 +122,20 @@ def get_break(self, break_type = None):

if break_type == BreakType.LONG_BREAK:
if self.__long_queue is None:
return None;
return None
return self.__long_queue[self.__current_long]

if self.__short_queue is None:
return None;
return None
return self.__short_queue[self.__current_short]

def is_long_break(self):
return self.__current_break is not None and self.__current_break.type == BreakType.LONG_BREAK

def next(self, break_type = None):
def next(self, break_type=None):
break_obj = None
shorts = self.__short_queue
longs = self.__long_queue
longs = self.__long_queue

# Reset break that has just ended
if self.is_long_break():
Expand Down Expand Up @@ -175,7 +174,7 @@ def reset(self):
for break_object in self.__long_queue:
break_object.time = self.__long_break_time

def is_empty(self, break_type = None):
def is_empty(self, break_type=None):
"""
Check if the given break type is empty or not. If the break_type is None, check for both short and long breaks.
"""
Expand All @@ -187,7 +186,6 @@ def is_empty(self, break_type = None):
return self.__short_queue is None and self.__long_queue is None

def __next_short(self):
longs = self.__long_queue
shorts = self.__short_queue
break_obj = shorts[self.__current_short]
self.context['break_type'] = 'short'
Expand All @@ -198,7 +196,7 @@ def __next_short(self):
return break_obj

def __next_long(self):
longs = self.__long_queue
longs = self.__long_queue
break_obj = longs[self.__current_long]
self.context['break_type'] = 'long'

Expand Down Expand Up @@ -243,17 +241,20 @@ def __build_queue(self, break_type, break_configs, break_time, break_duration):
return queue

def __build_shorts(self):
self.__short_queue = self.__build_queue(BreakType.SHORT_BREAK,
self.__config.get('short_breaks'),
self.__short_break_time,
self.__config.get('short_break_duration'))
self.__short_queue = self.__build_queue(
BreakType.SHORT_BREAK,
self.__config.get('short_breaks'),
self.__short_break_time,
self.__config.get('short_break_duration')
)

def __build_longs(self):
self.__long_queue = self.__build_queue(BreakType.LONG_BREAK,
self.__config.get('long_breaks'),
self.__long_break_time,
self.__config.get('long_break_duration'))

self.__long_queue = self.__build_queue(
BreakType.LONG_BREAK,
self.__config.get('long_breaks'),
self.__long_break_time,
self.__config.get('long_break_duration')
)


class State(Enum):
Expand Down Expand Up @@ -421,13 +422,15 @@ def build(cls, name, icon_path, icon_id, action):
else:
return TrayAction(name, icon_path, action, False)


@dataclass
class PluginDependency:
message: str
link: str|None = None
link: str | None = None


class RequiredPluginException(Exception):
def __init__(self, plugin_id, plugin_name: str, message: str|PluginDependency):
def __init__(self, plugin_id, plugin_name: str, message: str | PluginDependency):
if isinstance(message, PluginDependency):
msg = message.message
else:
Expand Down
14 changes: 12 additions & 2 deletions safeeyes/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ def __load_plugin(self, plugin):
# The plugin is already enabled or partially loaded due to break_override_allowed

# Validate the dependencies again
if utility.check_plugin_dependencies(plugin['id'], plugin_config, plugin.get('settings', {}), plugin_path):
if utility.check_plugin_dependencies(
plugin['id'],
plugin_config,
plugin.get('settings', {}),
plugin_path
):
plugin_obj['enabled'] = False
utility.remove_if_exists(self.__plugins_on_start, plugin_obj)
utility.remove_if_exists(self.__plugins_on_stop, plugin_obj)
Expand Down Expand Up @@ -301,7 +306,12 @@ def __load_plugin(self, plugin):
else:
# This is the first time to load the plugin
# Check for dependencies
message = utility.check_plugin_dependencies(plugin['id'], plugin_config, plugin.get('settings', {}), plugin_path)
message = utility.check_plugin_dependencies(
plugin['id'],
plugin_config,
plugin.get('settings', {}),
plugin_path
)
if message:
if plugin_config.get('required_plugin', False):
raise RequiredPluginException(
Expand Down
2 changes: 1 addition & 1 deletion safeeyes/plugins/healthstats/dependency_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import datetime
from safeeyes import utility


def validate(plugin_config, plugin_settings):
if not utility.module_exist("croniter"):
return _("Please install the Python module '%s'") % "croniter"
4 changes: 3 additions & 1 deletion safeeyes/plugins/healthstats/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
next_reset_time = None
start_time = None


def init(ctx, safeeyes_config, plugin_config):
"""
Initialize the plugin.
Expand Down Expand Up @@ -169,7 +170,8 @@ def _get_next_reset_time():
next_reset_time = cron.get_next(datetime.datetime)
session['next_reset_time'] = next_reset_time.strftime("%Y-%m-%d %H:%M:%S")
logging.debug("Health stats will be reset at " + session['next_reset_time'])
except:
except: # noqa E722
# TODO: consider catching Exception here instead of bare except
logging.error("Error in statistics reset expression: " + statistics_reset_cron)
next_reset_time = None

Expand Down
2 changes: 1 addition & 1 deletion safeeyes/plugins/limitconsecutiveskipping/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
session = None
enabled = True


def init(ctx, safeeyes_config, plugin_config):
"""
Initialize the plugin.
Expand Down Expand Up @@ -96,4 +97,3 @@ def get_widget_content(break_obj):
return ""

return _('Skipped or postponed %d/%d breaks in a row') % (no_of_skipped_breaks, no_allowed_skips)

1 change: 0 additions & 1 deletion safeeyes/plugins/mediacontrol/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
Media Control plugin lets users to pause currently playing media player from the break screen.
"""

import logging
import os
import re
import gi
Expand Down
Loading

0 comments on commit cf5552b

Please sign in to comment.