mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
*: pylint: Explicitly specify encoding when open a file
This is recommended by PEP-0597: https://peps.python.org/pep-0597/ Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org> Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
1 parent
05815bc992
commit
3c7bc4a192
49 files changed
+165
-138
No files matched your search
@@ -95,7 +95,7 @@ def _backup_handler(packet, encryption_passphrase=None):
|
||||
'backup': component.manifest
|
||||
} for component in packet.components]
|
||||
}
|
||||
with open(manifest_path, 'w') as manifest_file:
|
||||
with open(manifest_path, 'w', encoding='utf-8') as manifest_file:
|
||||
json.dump(manifests, manifest_file)
|
||||
|
||||
paths = packet.directories + packet.files
|
||||
|
||||
@@ -110,7 +110,8 @@ def test_add_repository_when_directory_exists_and_not_empty(
|
||||
temp_user, temp_home, password):
|
||||
remote_path = os.path.join(temp_home, 'non_empty_dir')
|
||||
os.makedirs(remote_path)
|
||||
open(os.path.join(remote_path, 'somefile.txt'), 'w').close()
|
||||
open(os.path.join(remote_path, 'somefile.txt'), 'w',
|
||||
encoding='utf-8').close()
|
||||
data = {
|
||||
'repository': f'{temp_user}@localhost:{remote_path}',
|
||||
'ssh_password': password,
|
||||
|
||||
@@ -381,7 +381,7 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
||||
known_hosts_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
known_hosts_path.touch()
|
||||
|
||||
with known_hosts_path.open('a') as known_hosts_file:
|
||||
with known_hosts_path.open('a', encoding='utf-8') as known_hosts_file:
|
||||
known_hosts_file.write(ssh_public_key + '\n')
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
|
||||
@@ -117,7 +117,7 @@ def force_upgrade(helper, _packages):
|
||||
|
||||
def get_config():
|
||||
"""Get current configuration"""
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r')]
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r', encoding='utf-8')]
|
||||
|
||||
forwarders = ''
|
||||
dnssec_enabled = False
|
||||
@@ -141,8 +141,8 @@ def get_config():
|
||||
def set_forwarders(forwarders):
|
||||
"""Set DNS forwarders."""
|
||||
flag = 0
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r')]
|
||||
conf_file = open(CONFIG_FILE, 'w')
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r', encoding='utf-8')]
|
||||
conf_file = open(CONFIG_FILE, 'w', encoding='utf-8')
|
||||
for line in data:
|
||||
if re.match(r'^\s*forwarders\s+{', line):
|
||||
conf_file.write(line + '\n')
|
||||
@@ -160,10 +160,10 @@ def set_forwarders(forwarders):
|
||||
|
||||
def set_dnssec(choice):
|
||||
"""Enable or disable DNSSEC."""
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r')]
|
||||
data = [line.strip() for line in open(CONFIG_FILE, 'r', encoding='utf-8')]
|
||||
|
||||
if choice == 'enable':
|
||||
conf_file = open(CONFIG_FILE, 'w')
|
||||
conf_file = open(CONFIG_FILE, 'w', encoding='utf-8')
|
||||
for line in data:
|
||||
if re.match(r'//\s*dnssec-enable\s+yes;', line):
|
||||
line = line.lstrip('/')
|
||||
@@ -171,7 +171,7 @@ def set_dnssec(choice):
|
||||
conf_file.close()
|
||||
|
||||
if choice == 'disable':
|
||||
conf_file = open(CONFIG_FILE, 'w')
|
||||
conf_file = open(CONFIG_FILE, 'w', encoding='utf-8')
|
||||
for line in data:
|
||||
if re.match(r'^\s*dnssec-enable\s+yes;', line):
|
||||
line = '//' + line
|
||||
|
||||
@@ -4,6 +4,7 @@ FreedomBox app for configuring date and time.
|
||||
"""
|
||||
|
||||
import logging
|
||||
import pathlib
|
||||
|
||||
from django.contrib import messages
|
||||
from django.utils.translation import gettext as _
|
||||
@@ -17,6 +18,7 @@ logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class DateTimeAppView(AppView):
|
||||
"""Serve configuration page."""
|
||||
form_class = DateTimeForm
|
||||
app_id = 'datetime'
|
||||
|
||||
@@ -25,9 +27,11 @@ class DateTimeAppView(AppView):
|
||||
status['time_zone'] = self.get_current_time_zone()
|
||||
return status
|
||||
|
||||
def get_current_time_zone(self):
|
||||
@staticmethod
|
||||
def get_current_time_zone():
|
||||
"""Get current time zone."""
|
||||
time_zone = open('/etc/timezone').read().rstrip()
|
||||
path = pathlib.Path('/etc/timezone')
|
||||
time_zone = path.read_text(encoding='utf-8').rstip()
|
||||
return time_zone or 'none'
|
||||
|
||||
def form_valid(self, form):
|
||||
|
||||
@@ -16,6 +16,7 @@ _JSON_FORMAT = {'indent': 4, 'sort_keys': True, 'ensure_ascii': False}
|
||||
|
||||
class Config:
|
||||
"""Read or edit a Deluge configuration file."""
|
||||
|
||||
def __init__(self, file_name):
|
||||
"""Initialize the configuration object."""
|
||||
self.file_name = file_name
|
||||
@@ -26,7 +27,7 @@ class Config:
|
||||
|
||||
def load(self):
|
||||
"""Parse the configuration file into memory."""
|
||||
text = self.file.read_text()
|
||||
text = self.file.read_text(encoding='utf-8')
|
||||
matches = re.match(r'^({[^}]*})(.*)$', text, re.DOTALL)
|
||||
if not matches:
|
||||
raise Exception('Unexpected file format.')
|
||||
|
||||
@@ -61,7 +61,8 @@ def action_set_domains(primary_domain, all_domains):
|
||||
'mydestination': my_destination
|
||||
}
|
||||
postfix.set_config(conf)
|
||||
pathlib.Path('/etc/mailname').write_text(primary_domain + '\n')
|
||||
pathlib.Path('/etc/mailname').write_text(primary_domain + '\n',
|
||||
encoding='utf-8')
|
||||
tls.set_postfix_config(primary_domain, all_domains)
|
||||
tls.set_dovecot_config(primary_domain, all_domains)
|
||||
|
||||
|
||||
@@ -80,4 +80,4 @@ local_name {domain} {{
|
||||
}}
|
||||
'''
|
||||
cert_config = pathlib.Path('/etc/dovecot/conf.d/91-freedombox-tls.conf')
|
||||
cert_config.write_text(content)
|
||||
cert_config.write_text(content, encoding='utf-8')
|
||||
@@ -23,7 +23,7 @@ class FirstbootWizardSecretForm(forms.Form):
|
||||
generated during installation.
|
||||
"""
|
||||
secret_file_path = first_boot.get_secret_file_path()
|
||||
with open(secret_file_path) as secret_file:
|
||||
with open(secret_file_path, encoding='utf-8') as secret_file:
|
||||
if secret != secret_file.read().strip():
|
||||
self.add_error('secret', 'Invalid secret')
|
||||
|
||||
|
||||
@@ -215,7 +215,7 @@ def get_repo_list():
|
||||
|
||||
progress_file = os.path.join(GIT_REPO_PATH, repo, 'clone_progress')
|
||||
if os.path.exists(progress_file):
|
||||
with open(progress_file) as file_handle:
|
||||
with open(progress_file, encoding='utf-8') as file_handle:
|
||||
clone_progress = file_handle.read()
|
||||
repo_info['clone_progress'] = clone_progress
|
||||
|
||||
|
||||
@@ -185,7 +185,7 @@ def get_configured_domain_name():
|
||||
if not is_setup():
|
||||
return None
|
||||
|
||||
with open(SERVER_NAME_PATH) as config_file:
|
||||
with open(SERVER_NAME_PATH, encoding='utf-8') as config_file:
|
||||
config, _, _ = load_yaml_guess_indent(config_file)
|
||||
|
||||
return config['server_name']
|
||||
@@ -196,7 +196,7 @@ def get_turn_configuration() -> (List[str], str, bool):
|
||||
for file_path, managed in ((OVERRIDDEN_TURN_CONF_PATH, False),
|
||||
(TURN_CONF_PATH, True)):
|
||||
if is_non_empty_file(file_path):
|
||||
with open(file_path) as config_file:
|
||||
with open(file_path, encoding='utf-8') as config_file:
|
||||
config, _, _ = load_yaml_guess_indent(config_file)
|
||||
return (TurnConfiguration(None, config['turn_uris'],
|
||||
config['turn_shared_secret']),
|
||||
|
||||
@@ -128,7 +128,7 @@ def is_private_mode_enabled():
|
||||
|
||||
def _get_config_value_in_file(setting_name, config_file):
|
||||
"""Return the value of a setting from a config file."""
|
||||
with open(config_file, 'r') as config:
|
||||
with open(config_file, 'r', encoding='utf-8') as config:
|
||||
for line in config:
|
||||
if line.startswith(setting_name):
|
||||
return re.findall(r'["\'][^"\']*["\']', line)[0].strip('"\'')
|
||||
|
||||
@@ -122,7 +122,8 @@ def is_setup():
|
||||
def is_using_ecc():
|
||||
"""Return whether the service is using ECC."""
|
||||
if os.path.exists(SERVER_CONFIGURATION_FILE):
|
||||
with open(SERVER_CONFIGURATION_FILE, 'r') as file_handle:
|
||||
with open(SERVER_CONFIGURATION_FILE, 'r',
|
||||
encoding='utf-8') as file_handle:
|
||||
for line in file_handle:
|
||||
if line.strip() == 'dh none':
|
||||
return True
|
||||
|
||||
@@ -34,7 +34,7 @@ def fixture_conf_file(tmp_path):
|
||||
def test_identify_rsa_configuration(conf_file):
|
||||
"""Identify RSA configuration based on configuration file."""
|
||||
with patch('plinth.modules.openvpn.SERVER_CONFIGURATION_FILE', conf_file):
|
||||
with open(conf_file, 'w') as file_handle:
|
||||
with open(conf_file, 'w', encoding='utf-8') as file_handle:
|
||||
file_handle.write('dh /etc/openvpn/freedombox-keys/pki/dh.pem')
|
||||
assert not openvpn.is_using_ecc()
|
||||
|
||||
@@ -42,7 +42,7 @@ def test_identify_rsa_configuration(conf_file):
|
||||
def test_identify_ecc_configuration(conf_file):
|
||||
"""Identify ECC configuration based on configuration file."""
|
||||
with patch('plinth.modules.openvpn.SERVER_CONFIGURATION_FILE', conf_file):
|
||||
with open(conf_file, 'w') as file_handle:
|
||||
with open(conf_file, 'w', encoding='utf-8') as file_handle:
|
||||
file_handle.write('dh none')
|
||||
assert openvpn.is_using_ecc()
|
||||
|
||||
|
||||
@@ -118,7 +118,8 @@ def get_domain():
|
||||
"""Read TLS domain from config file select first available if none."""
|
||||
domain = None
|
||||
try:
|
||||
with open('/var/lib/quassel/domain-freedombox') as file_handle:
|
||||
with open('/var/lib/quassel/domain-freedombox',
|
||||
encoding='utf-8') as file_handle:
|
||||
domain = file_handle.read().strip()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
@@ -80,13 +80,13 @@ def enable_fail2ban():
|
||||
|
||||
def get_restricted_access_enabled():
|
||||
"""Return whether restricted access is enabled"""
|
||||
with open(ACCESS_CONF_FILE_OLD, 'r') as conffile:
|
||||
with open(ACCESS_CONF_FILE_OLD, 'r', encoding='utf-8') as conffile:
|
||||
if any(line.strip() in ACCESS_CONF_SNIPPETS
|
||||
for line in conffile.readlines()):
|
||||
return True
|
||||
|
||||
try:
|
||||
with open(ACCESS_CONF_FILE, 'r') as conffile:
|
||||
with open(ACCESS_CONF_FILE, 'r', encoding='utf-8') as conffile:
|
||||
return any(line.strip() in ACCESS_CONF_SNIPPETS
|
||||
for line in conffile.readlines())
|
||||
except FileNotFoundError:
|
||||
|
||||
@@ -97,7 +97,7 @@ def is_newer_version_available():
|
||||
def get_os_release():
|
||||
"""Returns the Debian release number and name."""
|
||||
output = 'Error: Cannot read PRETTY_NAME in /etc/os-release.'
|
||||
with open('/etc/os-release', 'r') as release_file:
|
||||
with open('/etc/os-release', 'r', encoding='utf-8') as release_file:
|
||||
for line in release_file:
|
||||
if 'PRETTY_NAME=' in line:
|
||||
line = line.replace('"', '').strip()
|
||||
|
||||
Reference in new issue
Block a user