Skip to content

Commit

Permalink
chore: Enable PyLint rule R0801 and refactoring
Browse files Browse the repository at this point in the history
Thank you to Ihor Pryyma (@Ihor-Pryyma) for this contribution.

Related to #1755 

---------

Co-authored-by: buhtz <c.buhtz@posteo.jp>
  • Loading branch information
Ihor-Pryyma and buhtz committed Aug 25, 2024
1 parent 1e0079b commit e90ce00
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 92 deletions.
8 changes: 2 additions & 6 deletions common/encfstools.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,8 @@ class EncFS_SSH(EncFS_mount):
rsync will then sync the encrypted view on / to the remote path
"""
def __init__(self, cfg = None, profile_id = None, mode = None, parent = None,*args, **kwargs):
self.config = cfg
if self.config is None:
self.config = config.Config()
self.profile_id = profile_id
if self.profile_id is None:
self.profile_id = self.config.currentProfile()
self.config = cfg or config.Config()
self.profile_id = profile_id or self.config.currentProfile()
self.mode = mode
if self.mode is None:
self.mode = self.config.snapshotsMode(self.profile_id)
Expand Down
28 changes: 9 additions & 19 deletions common/mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,18 @@ def postUmountCheck(self):
return True
"""
import getpass
import json
import os
import subprocess
import json
import getpass
from zlib import crc32
from time import sleep
from zlib import crc32

import config
import logger
import tools
import password
from exceptions import MountException, HashCollision
import tools
from exceptions import HashCollision, MountException


class Mount(object):
Expand Down Expand Up @@ -143,14 +143,8 @@ def __init__(self,
profile_id = None,
tmp_mount = False,
parent = None):
self.config = cfg
if self.config is None:
self.config = config.Config()

self.profile_id = profile_id
if self.profile_id is None:
self.profile_id = self.config.currentProfile()

self.config = cfg or config.Config()
self.profile_id = profile_id or self.config.currentProfile()
self.tmp_mount = tmp_mount
self.parent = parent

Expand Down Expand Up @@ -437,13 +431,9 @@ def __init__(self,
self.mountproc = None
self.symlink_subfolder = None

self.config = cfg
if self.config is None:
self.config = config.Config()
self.config = cfg or config.Config()

self.profile_id = profile_id
if self.profile_id is None:
self.profile_id = self.config.currentProfile()
self.profile_id = profile_id or self.config.currentProfile()

self.tmp_mount = tmp_mount
self.hash_id = hash_id
Expand Down
24 changes: 24 additions & 0 deletions common/test/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# SPDX-FileCopyrightText: © 2008-2022 Oprea Dan
# SPDX-FileCopyrightText: © 2008-2022 Bart de Koning
# SPDX-FileCopyrightText: © 2008-2022 Richard Bailey
# SPDX-FileCopyrightText: © 2008-2022 Germar Reitze
# SPDX-FileCopyrightText: © 2024 Ihor Pryyma
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This file is part of the program "Back In Time" which is released under GNU
# General Public License v2 (GPLv2).
# See file LICENSE or go to <https://www.gnu.org/licenses/#GPL>.
"""
Constants that are used in the test files.
"""
import grp
import os
import pwd

CURRENTUID = os.geteuid()
CURRENTUSER = pwd.getpwuid(CURRENTUID).pw_name
CURRENTGID = os.getegid()
CURRENTGROUP = grp.getgrgid(CURRENTGID).gr_name
CURRENTUID = os.geteuid()
CURRENTGID = os.getegid()
10 changes: 6 additions & 4 deletions common/test/test_applicationinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
# with this program; if not, write to the Free Software Foundation,Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import subprocess
import os
import sys
import subprocess
from unittest.mock import patch
from threading import Thread

from test import generic

sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
Expand Down Expand Up @@ -70,9 +71,10 @@ def _createProcess(self):

def _killProcess(self):
if self.subproc:
self.subproc.kill()
self.subproc.wait()
self.subproc = None
subproc = self.subproc
subproc.kill()
subproc.wait()
self.subproc = None

def test_create_and_remove_pid_file(self):
# create pid file
Expand Down
32 changes: 14 additions & 18 deletions common/test/test_config_crontab.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
# Back In Time
# Copyright (C) 2024 Kosta Vukicevic, Christian Buhtz
# SPDX-FileCopyrightText: © 2024 Christian Buhtz <c.buhtz@posteo.jp>
# SPDX-FileCopyrightText: © 2024 Kosta Vukicevic
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# SPDX-License-Identifier: GPL-2.0-or-later
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation,Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# This file is part of the program "Back In Time" which is released under GNU
# General Public License v2 (GPLv2).
# See file LICENSE or go to <https://www.gnu.org/licenses/#GPL>.
"""Tests about Cron-related behavior of the config module.
See also test_schedule.py for low-level-Cron-behavior implemented in schedule
module."""
import unittest
import pyfakefs.fake_filesystem_unittest as pyfakefs_ut
import sys
import inspect
import os
import sys
import tempfile
import inspect
import unittest
from pathlib import Path
from unittest import mock

import pyfakefs.fake_filesystem_unittest as pyfakefs_ut

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

import config


Expand Down Expand Up @@ -80,6 +75,7 @@ def setUp(self):

def _create_config_file(self, parent_path):
"""Minimal config file"""
# pylint: disable-next=R0801
cfg_content = inspect.cleandoc('''
config.version=6
profile1.snapshots.include.1.type=0
Expand Down
3 changes: 2 additions & 1 deletion common/test/test_lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,12 @@ def test_with_pylint(self):
'R0201', # no-self-use
'R0202', # no-classmethod-decorator
'R0203', # no-staticmethod-decorator
'R0801', # duplicate-code

# Enable asap. This list is a selection of existing (not all!)
# problems currently existing in the BIT code base. Quite easy to fix
# because their count is low.
# 'R0801', # duplicate-code
# 'W0237', # arguments-renamed
# 'W0221', # arguments-differ
# 'W0603', # global-statement
]
Expand Down
9 changes: 6 additions & 3 deletions common/test/test_plugin_usercallback.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import sys
import inspect
import tempfile
from pathlib import Path
import stat
import io
import unittest
import unittest.mock as mock
import tempfile
import stat
from contextlib import redirect_stdout, redirect_stderr
from ast import literal_eval
from pathlib import Path

# This workaround will become obsolet when migrating to src-layout
sys.path.append(str(Path(__file__).parent))
sys.path.append(str(Path(__file__).parent / 'plugins'))

import pluginmanager
from config import Config
from snapshots import Snapshots
Expand Down Expand Up @@ -206,6 +207,8 @@ def _create_source_and_destination_folders(cls, parent_path):

@classmethod
def _create_config_file(cls, parent_path):
"""Minimal config file"""
# pylint: disable-next=R0801
cfg_content = inspect.cleandoc('''
config.version=6
profile1.snapshots.include.1.type=0
Expand Down
9 changes: 2 additions & 7 deletions common/test/test_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,15 @@
import os
import sys
import unittest
import pwd
import grp
import stat
from tempfile import TemporaryDirectory

from test import generic
from test.constants import CURRENTUSER, CURRENTGROUP

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import mount

CURRENTUID = os.geteuid()
CURRENTUSER = pwd.getpwuid(CURRENTUID).pw_name

CURRENTGID = os.getegid()
CURRENTGROUP = grp.getgrgid(CURRENTGID).gr_name

class RestoreTestCase(generic.SnapshotsWithSidTestCase):
def setUp(self):
Expand Down
8 changes: 1 addition & 7 deletions common/test/test_snapshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation,Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import os
import sys
import pathlib
import shutil
import stat
import pwd
import grp
import re
import random
Expand All @@ -30,6 +28,7 @@
from datetime import date, datetime
from tempfile import TemporaryDirectory
from test import generic
from test.constants import CURRENTUSER, CURRENTGROUP, CURRENTGID, CURRENTUID

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import logger
Expand All @@ -38,11 +37,6 @@
import tools
import mount

CURRENTUID = os.geteuid()
CURRENTUSER = pwd.getpwuid(CURRENTUID).pw_name

CURRENTGID = os.getegid()
CURRENTGROUP = grp.getgrgid(CURRENTGID).gr_name

# all groups the current user is member in
GROUPS = [i.gr_name for i in grp.getgrall() if CURRENTUSER in i.gr_mem]
Expand Down
8 changes: 4 additions & 4 deletions common/test/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
import gzip
import stat
import signal
from datetime import datetime
from time import sleep
from unittest.mock import patch
from copy import deepcopy
from tempfile import NamedTemporaryFile, TemporaryDirectory
from datetime import datetime
from test import generic
from time import sleep
import pyfakefs.fake_filesystem_unittest as pyfakefs_ut
from test import generic

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
import tools
Expand Down Expand Up @@ -98,7 +98,7 @@ def _kill_process(self):
if self.subproc:
self.subproc.kill()
self.subproc.wait()
self.subproc = None
self.subproc = None

def test_sharePath(self):
share = tools.sharePath()
Expand Down
7 changes: 2 additions & 5 deletions qt/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
import languagedialog
import messagebox
from aboutdlg import AboutDlg
import qttools


class MainWindow(QMainWindow):
Expand Down Expand Up @@ -878,13 +879,9 @@ def updateProfiles(self):

self.comboProfiles.clear()

qttools.update_combo_profiles(self.config, self.comboProfiles, self.config.currentProfile())
profiles = self.config.profilesSortedByName()

for profile_id in profiles:
self.comboProfiles.addProfileID(profile_id)
if profile_id == self.config.currentProfile():
self.comboProfiles.setCurrentProfileID(profile_id)

self.comboProfilesAction.setVisible(len(profiles) > 1)

self.updateProfile()
Expand Down
9 changes: 3 additions & 6 deletions qt/logviewdialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import encfstools
import snapshotlog
import tools
import qttools


class LogViewDialog(QDialog):
Expand Down Expand Up @@ -187,16 +188,12 @@ def updateProfiles(self):

self.comboProfiles.clear()

profiles = self.config.profilesSortedByName()
for profile_id in profiles:
self.comboProfiles.addProfileID(profile_id)
if profile_id == current_profile_id:
self.comboProfiles.setCurrentProfileID(profile_id)
qttools.update_combo_profiles(self.config, self.comboProfiles, current_profile_id)

self.enableUpdate = True
self.updateLog()

if len(profiles) <= 1:
if len(self.config.profilesSortedByName()) <= 1:
self.lblProfile.setVisible(False)
self.comboProfiles.setVisible(False)

Expand Down
4 changes: 2 additions & 2 deletions qt/qtsystrayicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,13 +201,13 @@ def updateInfo(self):
self.menuProgress.setVisible(False)

def getMenuProgress(self, pg):
d = (
data = (
('sent', _('Sent:')),
('speed', _('Speed:')),
('eta', _('ETA:'))
)

for key, txt in d:
for key, txt in data:
value = pg.strValue(key, '')

if not value:
Expand Down
15 changes: 15 additions & 0 deletions qt/qttools.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,21 @@ def set_wrapped_tooltip(widget: QWidget,
widget.setToolTip('\n'.join(result))



def update_combo_profiles(config, combo_profiles, current_profile_id):
"""
Updates the combo box with profiles.
:param config: Configuration object with access to profile data.
:param combo_profiles: The combo box widget to be updated.
:param current_profile_id: The ID of the current profile to be selected.
"""
profiles = config.profilesSortedByName()
for profile_id in profiles:
combo_profiles.addProfileID(profile_id)
if profile_id == current_profile_id:
combo_profiles.setCurrentProfileID(profile_id)

# |---------------------|
# | Misc / Uncatgorized |
# |---------------------|
Expand Down
Loading

0 comments on commit e90ce00

Please sign in to comment.