Skip to content

Commit

Permalink
Merge pull request #12 from osantana/add-tuple-cast-refactored
Browse files Browse the repository at this point in the history
Add tuple cast refactored
  • Loading branch information
bertonha committed Sep 2, 2015
2 parents 5261a49 + 326a4ef commit 3675b37
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
1.2.0
=====

- New cast type: config.tuple (converts a comma-separated string in tuple)

1.1.2
=====

Expand Down
1 change: 1 addition & 0 deletions docs/source/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Buitin Casts
#. ``config.boolean`` - converts values like ``On|Off``, ``1|0``, ``yes|no``,
``true|false`` into booleans.
#. ``config.list`` - converts comma separated strings into lists.
#. ``config.tuple`` - converts comma separated strings into tuples.
#. ``config.option`` - get a return value based on specific options:

.. code-block:: python
Expand Down
10 changes: 9 additions & 1 deletion prettyconf/casts.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,20 @@ def _parse(self, string):
if element:
elements.append("".join(element))

return [e.strip() for e in elements]
return self.cast(e.strip() for e in elements)

def cast(self, sequence):
return list(sequence)

def __call__(self, value):
return self._parse(value)


class Tuple(List):
def cast(self, sequence):
return tuple(sequence)


class Option(AbstractCast):
"""
Example::
Expand Down
3 changes: 2 additions & 1 deletion prettyconf/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .loaders import EnvFileConfigurationLoader, IniFileConfigurationLoader, EnvVarConfigurationLoader
from .exceptions import InvalidConfigurationFile, InvalidPath, InvalidConfigurationCast, UnknownConfiguration
from .casts import Boolean, List, Option
from .casts import Boolean, Option, List, Tuple


MAGIC_FRAME_DEPTH = 3
Expand Down Expand Up @@ -74,6 +74,7 @@ class Configuration(object):
# Shortcut for standard casts
boolean = Boolean()
list = List()
tuple = Tuple()
option = Option

def __init__(self, configs=None, starting_path=None, root_path="/"):
Expand Down
14 changes: 12 additions & 2 deletions tests/test_casts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from unittest import TestCase

from prettyconf.casts import Boolean, List, Option, InvalidConfiguration
from prettyconf.casts import Boolean, Option, InvalidConfiguration, List, Tuple


class BooleanCastTestCase(TestCase):
Expand Down Expand Up @@ -39,7 +39,7 @@ def test_fail_invalid_boolean_cast(self):
boolean(42)


class ListCastTestCase(TestCase):
class SequenceCastTestCase(TestCase):
def test_basic_list_cast(self):
l = List()

Expand All @@ -50,6 +50,16 @@ def test_basic_list_cast(self):
self.assertEqual(l("foo, 'bar, baz', qux # doo "), ["foo", "'bar, baz'", "qux # doo"])
self.assertEqual(l("foo, '\"bar\", baz ', qux # doo "), ["foo", "'\"bar\", baz '", "qux # doo"])

def test_basic_tuple_cast(self):
t = Tuple()

self.assertEqual(t("foo,bar"), ("foo", "bar"))
self.assertEqual(t("foo, bar"), ("foo", "bar"))
self.assertEqual(t(" foo , bar "), ("foo", "bar"))
self.assertEqual(t(" foo ,, bar "), ("foo", "", "bar"))
self.assertEqual(t("foo, 'bar, baz', qux # doo "), ("foo", "'bar, baz'", "qux # doo"))
self.assertEqual(t("foo, '\"bar\", baz ', qux # doo "), ("foo", "'\"bar\", baz '", "qux # doo"))


class OptionCastTestCase(TestCase):
def test_options(self):
Expand Down

0 comments on commit 3675b37

Please sign in to comment.