[Pkg-privacy-commits] [onionshare] 48/256: Refactor Settings object so it does not load from file by default. Make it so you can pass a Settings into Onion, to test settings
Ulrike Uhlig
ulrike at moszumanska.debian.org
Fri May 26 12:53:10 UTC 2017
This is an automated email from the git hooks/post-receive script.
ulrike pushed a commit to branch master
in repository onionshare.
commit 5bfa4da6489ae3b9473029fe514652f2721cb5fc
Author: Micah Lee <micah at micahflee.com>
Date: Thu Dec 29 08:02:32 2016 -0800
Refactor Settings object so it does not load from file by default. Make it so you can pass a Settings into Onion, to test settings
---
onionshare/onion.py | 9 +++--
onionshare/settings.py | 33 ++++++++----------
onionshare_gui/settings_dialog.py | 70 +++++++++++++++++++++++----------------
3 files changed, 63 insertions(+), 49 deletions(-)
diff --git a/onionshare/onion.py b/onionshare/onion.py
index 9088151..22a95c1 100644
--- a/onionshare/onion.py
+++ b/onionshare/onion.py
@@ -57,11 +57,16 @@ class Onion(object):
onion services are supported. If not, it falls back to modifying the
Tor configuration.
"""
- def __init__(self, transparent_torification=False, stealth=False):
+ def __init__(self, transparent_torification=False, stealth=False, settings=False):
self.transparent_torification = transparent_torification
self.stealth = stealth
- self.settings = Settings()
+ # Either use settings that are passed in, or load them from disk
+ if settings:
+ self.settings = settings
+ else:
+ self.settings = Settings()
+ self.settings.load()
# files and dirs to delete on shutdown
self.cleanup_filenames = []
diff --git a/onionshare/settings.py b/onionshare/settings.py
index 6ba2779..1fa743b 100644
--- a/onionshare/settings.py
+++ b/onionshare/settings.py
@@ -31,7 +31,18 @@ class Settings(object):
"""
def __init__(self):
self.filename = self.build_filename()
- self.load()
+
+ # These are the default settings. They will get overwritten when loading from disk
+ self._settings = {
+ 'version': helpers.get_version(),
+ 'connection_type': 'automatic',
+ 'control_port_address': '127.0.0.1',
+ 'control_port_port': '9051',
+ 'socket_file_path': '/var/run/tor/control',
+ 'auth_type': 'no_auth',
+ 'auth_password': '',
+ 'auth_cookie_path': '/var/run/tor/control.authcookie'
+ }
def build_filename(self):
"""
@@ -50,28 +61,12 @@ class Settings(object):
"""
Load the settings from file.
"""
- default_settings = {
- 'version': helpers.get_version(),
- 'connection_type': 'automatic',
- 'control_port_address': '127.0.0.1',
- 'control_port_port': '9051',
- 'socket_file_path': '/var/run/tor/control',
- 'auth_type': 'no_auth',
- 'auth_password': '',
- 'auth_cookie_path': '/var/run/tor/control.authcookie'
- }
-
+ # If the settings file exists, load it
if os.path.exists(self.filename):
- # If the settings file exists, load it
try:
self._settings = json.loads(open(self.filename, 'r').read())
except:
- # If the settings don't work, use default ones instead
- self._settings = default_settings
-
- else:
- # Otherwise, use default settings
- self._settings = default_settings
+ pass
def save(self):
"""
diff --git a/onionshare_gui/settings_dialog.py b/onionshare_gui/settings_dialog.py
index 1f1120e..13db727 100644
--- a/onionshare_gui/settings_dialog.py
+++ b/onionshare_gui/settings_dialog.py
@@ -21,6 +21,7 @@ from PyQt5 import QtCore, QtWidgets, QtGui
from onionshare import strings
from onionshare.settings import Settings
+from onionshare.onion import Onion
class SettingsDialog(QtWidgets.QDialog):
"""
@@ -146,26 +147,28 @@ class SettingsDialog(QtWidgets.QDialog):
# Load settings, and fill them in
- self.settings = Settings()
- connection_type = self.settings.get('connection_type')
+ settings = Settings()
+ settings.load()
+
+ connection_type = settings.get('connection_type')
if connection_type == 'automatic':
self.connection_type_automatic_radio.setChecked(True)
elif connection_type == 'control_port':
self.connection_type_control_port_radio.setChecked(True)
elif connection_type == 'socket_file':
self.connection_type_socket_file_radio.setChecked(True)
- self.connection_type_control_port_extras_address.setText(self.settings.get('control_port_address'))
- self.connection_type_control_port_extras_port.setText(self.settings.get('control_port_port'))
- self.connection_type_socket_file_extras_path.setText(self.settings.get('socket_file_path'))
- auth_type = self.settings.get('auth_type')
+ self.connection_type_control_port_extras_address.setText(settings.get('control_port_address'))
+ self.connection_type_control_port_extras_port.setText(settings.get('control_port_port'))
+ self.connection_type_socket_file_extras_path.setText(settings.get('socket_file_path'))
+ auth_type = settings.get('auth_type')
if auth_type == 'no_auth':
self.authenticate_no_auth_radio.setChecked(True)
elif auth_type == 'password':
self.authenticate_password_radio.setChecked(True)
elif auth_type == 'cookie':
self.authenticate_cookie_radio.setChecked(True)
- self.authenticate_password_extras_password.setText(self.settings.get('auth_password'))
- self.authenticate_cookie_extras_cookie_path.setText(self.settings.get('auth_cookie_path'))
+ self.authenticate_password_extras_password.setText(settings.get('auth_password'))
+ self.authenticate_cookie_extras_cookie_path.setText(settings.get('auth_cookie_path'))
# Show the dialog
self.exec_()
@@ -232,38 +235,49 @@ class SettingsDialog(QtWidgets.QDialog):
Test Settings button clicked. With the given settings, see if we can
successfully connect and authenticate to Tor.
"""
- pass
+ print("Testing settings")
+ settings = self.settings_from_fields()
+ onion = Onion(settings=settings)
def save_clicked(self):
"""
Save button clicked. Save current settings to disk.
"""
+ settings = self.settings_from_fields()
+ settings.save()
+ self.close()
+
+ def cancel_clicked(self):
+ """
+ Cancel button clicked.
+ """
+ self.close()
+
+ def settings_from_fields(self):
+ """
+ Return a Settings object that's full of values from the settings dialog.
+ """
+ settings = Settings()
+
if self.connection_type_automatic_radio.isChecked():
- self.settings.set('connection_type', 'automatic')
+ settings.set('connection_type', 'automatic')
if self.connection_type_control_port_radio.isChecked():
- self.settings.set('connection_type', 'control_port')
+ settings.set('connection_type', 'control_port')
if self.connection_type_socket_file_radio.isChecked():
- self.settings.set('connection_type', 'socket_file')
+ settings.set('connection_type', 'socket_file')
- self.settings.set('control_port_address', self.connection_type_control_port_extras_address.text())
- self.settings.set('control_port_port', self.connection_type_control_port_extras_port.text())
- self.settings.set('socket_file_path', self.connection_type_socket_file_extras_path.text())
+ settings.set('control_port_address', self.connection_type_control_port_extras_address.text())
+ settings.set('control_port_port', self.connection_type_control_port_extras_port.text())
+ settings.set('socket_file_path', self.connection_type_socket_file_extras_path.text())
if self.authenticate_no_auth_radio.isChecked():
- self.settings.set('auth_type', 'no_auth')
+ settings.set('auth_type', 'no_auth')
if self.authenticate_password_radio.isChecked():
- self.settings.set('auth_type', 'password')
+ settings.set('auth_type', 'password')
if self.authenticate_cookie_radio.isChecked():
- self.settings.set('auth_type', 'cookie')
+ settings.set('auth_type', 'cookie')
- self.settings.set('auth_password', self.authenticate_password_extras_password.text())
- self.settings.set('auth_cookie_path', self.authenticate_cookie_extras_cookie_path.text())
-
- self.settings.save()
- self.close()
+ settings.set('auth_password', self.authenticate_password_extras_password.text())
+ settings.set('auth_cookie_path', self.authenticate_cookie_extras_cookie_path.text())
- def cancel_clicked(self):
- """
- Cancel button clicked.
- """
- self.close()
+ return settings
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/onionshare.git
More information about the Pkg-privacy-commits
mailing list