Skip to content

Commit

Permalink
develop #comment Allow string config entries to be surrounded with qu…
Browse files Browse the repository at this point in the history
…otes (single or double).

 - Rework expectation order in config unit tests.
  • Loading branch information
Luc Sinet committed Jan 11, 2019
1 parent 36a7331 commit 22ef7cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
17 changes: 10 additions & 7 deletions RaspberryCast/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,21 @@ def _load_cache(self):

def _parse_entry(self, parser, category, entry_name):
entry = getattr(category, entry_name)
entry_value = entry
value = entry

if type(entry) is int:
entry_value = parser.getint(entry_name, fallback=entry)
value = parser.getint(entry_name, fallback=entry)
elif type(entry) is float:
entry_value = parser.getfloat(entry_name, fallback=entry)
value = parser.getfloat(entry_name, fallback=entry)
elif type(entry) is bool:
entry_value = parser.getboolean(entry_name, fallback=entry)
value = parser.getboolean(entry_name, fallback=entry)
else:
entry_value = parser.get(entry_name, fallback=entry)

setattr(category, entry_name, entry_value)
value = parser.get(entry_name, fallback=entry)
if (type(entry) is str and
(value.startswith(("'", '"')) and
value[0] is value[-1])):
value = value[1:-1]
setattr(category, entry_name, value)


config = Config()
4 changes: 2 additions & 2 deletions config.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Server]
# The IP from where the service is accessible
host : 0.0.0.0
host : '0.0.0.0'
# The port used by the server
port : 2020

Expand All @@ -12,4 +12,4 @@ history_size : 15

[Downloader]
# The directory used to store downloaded videos.
output_directory : /tmp
output_directory : '/tmp'
32 changes: 28 additions & 4 deletions test/config_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .util import TestCase

from RaspberryCast.config import (
Server,
VideoPlayer,
Downloader,
config,
Expand All @@ -9,16 +10,17 @@

class ConfigTest(TestCase):
def test_categories(self):
self.assertIsInstance(config['Server'], Server)
self.assertIsInstance(config['VideoPlayer'], VideoPlayer)
self.assertIsInstance(config['Downloader'], Downloader)

def test_default_values(self):
vp = config['VideoPlayer']
self.assertTrue(vp.hide_background)
self.assertEqual(15, vp.history_size)
self.assertEqual(vp.history_size, 15)

dl = config['Downloader']
self.assertEqual('/tmp', dl.output_directory)
self.assertEqual(dl.output_directory, '/tmp')

def test_parse_video_player_config(self):
config.load_from_dict({
Expand All @@ -30,7 +32,7 @@ def test_parse_video_player_config(self):

vp = config['VideoPlayer']
self.assertFalse(vp.hide_background)
self.assertEqual(10, vp.history_size)
self.assertEqual(vp.history_size, 10)

def test_parse_downloader_config(self):
config.load_from_dict({
Expand All @@ -40,4 +42,26 @@ def test_parse_downloader_config(self):
})

dl = config['Downloader']
self.assertEqual('/Video', dl.output_directory)
self.assertEqual(dl.output_directory, '/Video')

def test_parse_server_config(self):
config.load_from_dict({
'Server': {
'host': 'host',
'port': 42
}
})

sv = config['Server']
self.assertEqual(sv.host, 'host')
self.assertEqual(sv.port, 42)

def test_parse_quoted_string(self):
config.load_from_dict({
'Server': {
'host': '"host"',
}
})

sv = config['Server']
self.assertEqual(sv.host, 'host')

0 comments on commit 22ef7cf

Please sign in to comment.