Skip to content

Commit

Permalink
make pylint happy
Browse files Browse the repository at this point in the history
adjust for:
 - unspecified-encoding
 - use-list-literal
 - redundant-u-string-prefix
  • Loading branch information
marmarek committed Apr 12, 2022
1 parent ff47df3 commit 2f5afdf
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion qubesadmin/backup/core2.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ def import_core2_vm(self, element):
vm.devices['pci'][('dom0', pcidev.replace(':', '_'))] = {}

def load(self):
with open(self.store) as fh:
with open(self.store, encoding='utf-8') as fh:
try:
# pylint: disable=no-member
tree = lxml.etree.parse(fh)
Expand Down
2 changes: 1 addition & 1 deletion qubesadmin/backup/core3.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def import_core3_vm(self, element):
self.domains[vm.name] = vm

def load(self):
with open(self.store) as fh:
with open(self.store, encoding='utf-8') as fh:
try:
# pylint: disable=no-member
tree = lxml.etree.parse(fh)
Expand Down
6 changes: 3 additions & 3 deletions qubesadmin/backup/restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def validate(self):

def save(self, filename):
'''Save backup header into a file'''
with open(filename, "w") as f_header:
with open(filename, "w", encoding='utf-8') as f_header:
# make sure 'version' is the first key
f_header.write('version={}\n'.format(self.version))
for key, header in self.known_headers.items():
Expand Down Expand Up @@ -1153,11 +1153,11 @@ def _verify_and_decrypt(self, filename, output=None):
else:
fulloutput = os.path.join(self.tmpdir, origname)
if origname == HEADER_FILENAME:
passphrase = u'{filename}!{passphrase}'.format(
passphrase = '{filename}!{passphrase}'.format(
filename=origname,
passphrase=self.passphrase)
else:
passphrase = u'{backup_id}!{filename}!{passphrase}'.format(
passphrase = '{backup_id}!{filename}!{passphrase}'.format(
backup_id=self.header_data.backup_id,
filename=origname,
passphrase=self.passphrase)
Expand Down
9 changes: 6 additions & 3 deletions qubesadmin/tests/tools/qvm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5278,7 +5278,8 @@ def test_240_qubes_release(self, mock_exists):
self.assertEqual(mock_exists.mock_calls, [
mock.call('/usr/share/qubes/marker-vm')
])
mock_open.assert_called_with('/etc/os-release', 'r')
mock_open.assert_called_with('/etc/os-release', 'r',
encoding='ascii')
self.assertAllCalled()

@mock.patch('os.path.exists')
Expand All @@ -5302,7 +5303,8 @@ def test_241_qubes_release_quotes(self, mock_exists):
self.assertEqual(mock_exists.mock_calls, [
mock.call('/usr/share/qubes/marker-vm')
])
mock_open.assert_called_with('/etc/os-release', 'r')
mock_open.assert_called_with('/etc/os-release', 'r',
encoding='ascii')
self.assertAllCalled()

@mock.patch('os.path.exists')
Expand All @@ -5321,7 +5323,8 @@ def test_242_qubes_release_quotes(self, mock_exists):
self.assertEqual(mock_exists.mock_calls, [
mock.call('/usr/share/qubes/marker-vm')
])
mock_open.assert_called_with('/usr/share/qubes/marker-vm', 'r')
mock_open.assert_called_with('/usr/share/qubes/marker-vm', 'r',
encoding='ascii')
self.assertAllCalled()

@skipUnless(which('rpmcanon'), 'rpmcanon not installed')
Expand Down
4 changes: 2 additions & 2 deletions qubesadmin/tools/qvm_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def main(args=None, app=None):
# summary
profile_path = os.path.join(
backup_profile_dir, profile_name + '.conf')
with open(profile_path, 'w') as f_profile:
with open(profile_path, 'w', encoding='utf-8') as f_profile:
write_backup_profile(f_profile, args)
else:
if args.save_profile:
Expand Down Expand Up @@ -190,7 +190,7 @@ def main(args=None, app=None):
if getpass.getpass("Enter again for verification: ") != passphrase:
parser.error_runtime("Passphrase mismatch!")

with open(profile_path, 'w') as f_profile:
with open(profile_path, 'w', encoding='utf-8') as f_profile:
write_backup_profile(f_profile, args, passphrase)

loop = asyncio.get_event_loop()
Expand Down
2 changes: 1 addition & 1 deletion qubesadmin/tools/qvm_backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def main(args=None, app=None):
if args.pass_file == '-':
passphrase = sys.stdin.readline().rstrip()
else:
with open(args.pass_file) as pass_f:
with open(args.pass_file, encoding='utf-8') as pass_f:
passphrase = pass_f.readline().rstrip()
else:
passphrase = getpass.getpass("Please enter the passphrase to verify "
Expand Down
4 changes: 2 additions & 2 deletions qubesadmin/tools/qvm_ls.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ def tree_append_child(self, parent, level):
:param qubes.vm.qubesvm.QubesVM: Parent vm of the children VMs
'''
childs = list()
childs = []
for child in parent.connected_vms:
if child.provides_network and child in self.domains:
childs.append((level, child))
Expand All @@ -433,7 +433,7 @@ def sort_to_tree(self, domains):
:param list() domains: The domains which will be sorted
:return list(tuple()) tree: returns a list of tuple(insertion, vm)
'''
tree = list()
tree = []
# We need a copy of domains, because domains.remove() is not allowed
# while iterating over it. Besides, the domains should be sorted anyway.
sorted_doms = sorted(domains)
Expand Down
12 changes: 7 additions & 5 deletions qubesadmin/tools/qvm_start_daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,11 +360,12 @@ async def send_monitor_layout(self, vm, layout=None, startup=False):
vm.log.info('Sending monitor layout')

if not startup:
with open(self.guid_pidfile(vm.xid)) as pidfile:
with open(self.guid_pidfile(vm.xid), encoding='ascii') as pidfile:
pid = int(pidfile.read())
os.kill(pid, signal.SIGHUP)
try:
with open(self.guid_pidfile(vm.stubdom_xid)) as pidfile:
with open(self.guid_pidfile(vm.stubdom_xid),
encoding='ascii') as pidfile:
pid = int(pidfile.read())
os.kill(pid, signal.SIGHUP)
except FileNotFoundError:
Expand Down Expand Up @@ -453,7 +454,7 @@ def common_guid_args(self, vm):
@staticmethod
def write_guid_config(config_path, config):
"""Write guid configuration to a file"""
with open(config_path, 'w') as config_file:
with open(config_path, 'w', encoding='ascii') as config_file:
config_file.write(config)

@staticmethod
Expand Down Expand Up @@ -500,7 +501,8 @@ async def start_gui_for_vm(self, vm, monitor_layout=None):
stubdom_guid_pidfile = self.guid_pidfile(vm.stubdom_xid)
if not vm.debug and os.path.exists(stubdom_guid_pidfile):
# Terminate stubdom guid once "real" gui agent connects
with open(stubdom_guid_pidfile, 'r') as pidfile:
with open(stubdom_guid_pidfile, 'r',
encoding='ascii') as pidfile:
stubdom_guid_pid = pidfile.read().strip()
guid_cmd += ['-K', stubdom_guid_pid]

Expand Down Expand Up @@ -807,7 +809,7 @@ def main(args=None):
loop.close()
elif args.notify_monitor_layout:
try:
with open(pidfile_path, 'r') as pidfile:
with open(pidfile_path, 'r', encoding='ascii') as pidfile:
pid = int(pidfile.read().strip())
os.kill(pid, signal.SIGHUP)
except (FileNotFoundError, ValueError) as e:
Expand Down
8 changes: 4 additions & 4 deletions qubesadmin/tools/qvm_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class SignatureVerificationError(Exception):
def qubes_release() -> str:
"""Return the Qubes release."""
if os.path.exists('/usr/share/qubes/marker-vm'):
with open('/usr/share/qubes/marker-vm', 'r') as fd:
with open('/usr/share/qubes/marker-vm', 'r', encoding='ascii') as fd:
# Get the first non-comment line
release = [l.strip() for l in fd.readlines()
if l.strip() and not l.startswith('#')]
# sanity check
if release and release[0] and release[0][0].isdigit():
return release[0]
with open('/etc/os-release', 'r') as fd:
with open('/etc/os-release', 'r', encoding='ascii') as fd:
release = None
distro_id = None
for line in fd:
Expand Down Expand Up @@ -496,7 +496,7 @@ def check_newline(string, name):
payload += spec + '\n'
payload += '---\n'
for path in args.repo_files:
with open(path, 'r') as fd:
with open(path, 'r', encoding='utf-8') as fd:
payload += fd.read() + '\n'
return payload

Expand Down Expand Up @@ -918,7 +918,7 @@ def locked(func):
"""Execute given function under a lock in *LOCK_FILE*"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
with open(LOCK_FILE, 'w') as lock:
with open(LOCK_FILE, 'w', encoding='ascii') as lock:
try:
fcntl.flock(lock.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
except OSError:
Expand Down
11 changes: 7 additions & 4 deletions qubesadmin/tools/qvm_template_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,23 @@ def import_appmenus(vm, source_dir, skip_generate=True):
# name according to the FreeDesktop spec
source_dir = pathlib.Path(source_dir)
try:
with open(source_dir / 'vm-whitelisted-appmenus.list', 'r') as fd:
with open(source_dir / 'vm-whitelisted-appmenus.list', 'r',
encoding='ascii') as fd:
vm.features['default-menu-items'] = \
' '.join([x.rstrip() for x in fd])
except FileNotFoundError as e:
vm.log.warning('Cannot set default-menu-items, %s not found',
e.filename)
try:
with open(source_dir / 'whitelisted-appmenus.list', 'r') as fd:
with open(source_dir / 'whitelisted-appmenus.list', 'r',
encoding='ascii') as fd:
vm.features['menu-items'] = ' '.join([x.rstrip() for x in fd])
except FileNotFoundError as e:
vm.log.warning('Cannot set menu-items, %s not found',
e.filename)
try:
with open(source_dir / 'netvm-whitelisted-appmenus.list', 'r') as fd:
with open(source_dir / 'netvm-whitelisted-appmenus.list', 'r',
encoding='ascii') as fd:
vm.features['netvm-menu-items'] = ' '.join([x.rstrip() for x in fd])
except FileNotFoundError as e:
vm.log.warning('Cannot set netvm-menu-items, %s not found',
Expand All @@ -209,7 +212,7 @@ def import_appmenus(vm, source_dir, skip_generate=True):

def parse_template_config(path):
'''Parse template.conf from template package. (KEY=VALUE format)'''
with open(path, 'r') as fd:
with open(path, 'r', encoding='ascii') as fd:
return dict(line.rstrip('\n').split('=', 1) for line in fd)

async def call_postinstall_service(vm):
Expand Down
2 changes: 1 addition & 1 deletion qubesadmin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def __init__(self, path, nonblock=False):
"""Open the file. Call *acquire* or enter the context to lock
the file"""
# pylint: disable=consider-using-with
self.file = open(path, "w")
self.file = open(path, "w", encoding='ascii')
self.nonblock = nonblock

def __enter__(self, *args, **kwargs):
Expand Down

0 comments on commit 2f5afdf

Please sign in to comment.