-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2142 from devos50/get_settings_endpoint
READY: Implemented endpoint to fetch Tribler settings
- Loading branch information
Showing
6 changed files
with
151 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import json | ||
import os | ||
|
||
from twisted.web import resource | ||
|
||
from Tribler.Core.Utilities.configparser import CallbackConfigParser | ||
from Tribler.Core.defaults import tribler_defaults | ||
from Tribler.Core.simpledefs import STATEDIR_GUICONFIG | ||
|
||
|
||
class SettingsEndpoint(resource.Resource): | ||
""" | ||
This endpoint is reponsible for handing all requests regarding settings and configuration. | ||
A GET request to this endpoint returns all the session settings that can be found in Tribler. | ||
Please note that a port with a value of -1 means that the port is randomly assigned at startup. | ||
Example GET response: | ||
{ | ||
"settings": { | ||
"barter_community": { | ||
"enabled": false | ||
}, | ||
"libtorrent": { | ||
"anon_listen_port": -1, | ||
... | ||
}, | ||
... | ||
} | ||
} | ||
""" | ||
|
||
def __init__(self, session): | ||
resource.Resource.__init__(self) | ||
self.session = session | ||
|
||
def render_GET(self, request): | ||
""" | ||
Return the settings present in Tribler in a JSON dictionary. | ||
""" | ||
libtribler_settings = self.session.sessconfig.get_config_as_json() | ||
|
||
# Load the Tribler GUI configuration file. | ||
configfilepath = os.path.join(self.session.get_state_dir(), STATEDIR_GUICONFIG) | ||
tribler_config = CallbackConfigParser() | ||
tribler_config.read_file(configfilepath, 'utf-8-sig') | ||
|
||
def set_default_value(section, option): | ||
if not tribler_config.has_option(section, option): | ||
tribler_config.set(section, option, tribler_defaults.get('Tribler', {}).get(option, None)) | ||
|
||
# Make sure some default values are present in the JSON being returned | ||
set_default_value('Tribler', 'showsaveas') | ||
set_default_value('Tribler', 'default_number_hops') | ||
set_default_value('Tribler', 'default_anonymity_enabled') | ||
set_default_value('Tribler', 'default_safeseeding_enabled') | ||
set_default_value('Tribler', 'maxuploadrate') | ||
set_default_value('Tribler', 'maxdownloadrate') | ||
|
||
tribler_settings = tribler_config.get_config_as_json() | ||
|
||
# Merge the configuration of libtribler and the Tribler configuration | ||
settings_dict = libtribler_settings.copy() | ||
settings_dict.update(tribler_settings) | ||
|
||
return json.dumps({"settings": settings_dict}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
Tribler/Test/Core/Modules/RestApi/test_settings_endpoint.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
import os | ||
|
||
from Tribler.Core.Utilities.configparser import CallbackConfigParser | ||
from Tribler.Core.Utilities.twisted_thread import deferred | ||
from Tribler.Test.Core.Modules.RestApi.base_api_test import AbstractApiTest | ||
|
||
|
||
class TestSettingsEndpoint(AbstractApiTest): | ||
|
||
def verify_settings(self, settings): | ||
""" | ||
Verify that the expected sections are present. | ||
""" | ||
check_section = ['barter_community', 'libtorrent', 'mainline_dht', 'torrent_store', 'general', 'Tribler', | ||
'video', 'upgrader', 'torrent_checking', 'allchannel_community', 'tunnel_community', | ||
'http_api', 'torrent_collecting', 'dispersy', 'multichain', 'watch_folder', 'search_community', | ||
'metadata'] | ||
settings_json = json.loads(settings) | ||
self.assertTrue(settings_json['settings']) | ||
for section in check_section: | ||
self.assertTrue(settings_json['settings'][section]) | ||
|
||
@deferred(timeout=10) | ||
def test_get_settings(self): | ||
""" | ||
Testing whether the API returns a correct settings dictionary when the settings are requested | ||
""" | ||
self.should_check_equality = False | ||
tribler_config = CallbackConfigParser() | ||
tribler_config.add_section('Tribler') | ||
tribler_config.write_file(os.path.join(self.session.get_state_dir(), 'tribler.conf')) | ||
|
||
return self.do_request('settings', expected_code=200).addCallback(self.verify_settings) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters