Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Add support for loading yaml strings as unicode.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshualross committed Jun 2, 2014
1 parent a863686 commit faacaef
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 6 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
@@ -1,10 +1,15 @@
Changelog for Charlatan
=======================

0.3.5 (unreleased)
------------------

- Support loading all strings as unicode

0.3.4 (2014-01-21)
------------------

- Fix getting attribute from relationhips
- Fix getting attribute from relationships

0.3.3 (2014-01-18)
------------------
Expand Down
17 changes: 15 additions & 2 deletions charlatan/file_format.py
@@ -1,8 +1,8 @@
from __future__ import absolute_import
from __future__ import unicode_literals
import datetime

import yaml
from yaml.constructor import Constructor

from charlatan.utils import apply_delta, datetime_to_epoch_timestamp

Expand Down Expand Up @@ -57,7 +57,19 @@ def relationship_constructor(loader, node):
yaml.add_constructor(u'!rel', relationship_constructor)


def load_file(filename):
def configure_output(use_unicode=False):
"""Configure output options of the values loaded by pyyaml
:param boolean use_unicode: Use unicode constructor for loading strings
"""
if use_unicode:
yaml.add_constructor(
u'tag:yaml.org,2002:str',
Constructor.construct_python_unicode,
)


def load_file(filename, unicode_output=False):
"""Load fixtures definition from file.
:param str filename:
Expand All @@ -69,6 +81,7 @@ def load_file(filename):
if filename.endswith(".yaml"):
# Load the custom YAML tags
configure_yaml()
configure_output(use_unicode=unicode_output)
content = yaml.load(content)
else:
raise ValueError("Unsupported filetype: '%s'" % filename)
Expand Down
7 changes: 4 additions & 3 deletions charlatan/fixtures_manager.py
Expand Up @@ -47,10 +47,11 @@ class FixturesManager(object):
"""

def __init__(self, db_session=None):
def __init__(self, db_session=None, unicode_output=False):
self.hooks = {}
self.session = db_session
self.installed_keys = []
self.unicode_output = unicode_output

def load(self, filename, models_package=""):
"""Pre-load the fixtures.
Expand Down Expand Up @@ -87,7 +88,7 @@ def _load_fixtures(self, filename):
:param str filename: file that holds the fixture data
"""

content = load_file(filename)
content = load_file(filename, self.unicode_output)

fixtures = {}
for k, v in _compat.iteritems(content):
Expand Down Expand Up @@ -421,7 +422,7 @@ def set_hook(self, hookname, func):
:param function func:
"""

if not hookname in ALLOWED_HOOKS:
if hookname not in ALLOWED_HOOKS:
raise KeyError("'%s' is not an allowed hook." % hookname)

self.hooks[hookname] = func
2 changes: 2 additions & 0 deletions charlatan/tests/data/strings.yaml
@@ -0,0 +1,2 @@
foo: bar
baz: foobar
54 changes: 54 additions & 0 deletions charlatan/tests/test_strings.py
@@ -0,0 +1,54 @@
from __future__ import absolute_import
from sys import version_info

import unittest
from yaml import add_constructor
from yaml.constructor import SafeConstructor

from charlatan import file_format, testing


class TestUnicodeLoad(testing.TestCase):

def setUp(self):
# preserve the original constructor for strings
self.str_constructor = SafeConstructor.yaml_constructors[
u'tag:yaml.org,2002:str'
]
self.yaml = file_format.load_file(
'./charlatan/tests/data/strings.yaml',
unicode_output=True,
)

def tearDown(self):
# reset the constructor
add_constructor(u'tag:yaml.org,2002:str', self.str_constructor)

@unittest.skipIf(version_info[0] == 3, 'Unicode is undefined in Python 3')
def test_strings_are_unicode(self):
"""Assert all strings are loaded as unicode"""
for key, val in self.yaml.items():
self.assertTrue(isinstance(key, unicode)) # noqa
self.assertTrue(isinstance(val, unicode)) # noqa


class TestStringLoad(testing.TestCase):

def setUp(self):
self.yaml = file_format.load_file(
'./charlatan/tests/data/strings.yaml',
)

@unittest.skipIf(version_info[0] == 3, 'Iteration has changed in Python 3')
def test_strings_are_strings(self):
"""Assert all strings are loaded as strings"""
for key, val in self.yaml.items():
self.assertTrue(isinstance(key, str))
self.assertTrue(isinstance(val, str))

@unittest.skipIf(version_info[0] == 2, 'Iteration has changed in Python 3')
def test_strings_are_strings_python3(self):
"""Assert all strings are loaded as strings"""
for key, val in list(self.yaml.items()):
self.assertTrue(isinstance(key, str))
self.assertTrue(isinstance(val, str))
9 changes: 9 additions & 0 deletions docs/file-format.rst
Expand Up @@ -275,3 +275,12 @@ All the same time deltas work.

.. versionadded:: 0.2.9
It is now possible to use times in seconds since the epoch

Unicode Strings
---------------

.. versionadded:: 0.3.5

In python 2 strings are not, by default, loaded as unicode. To load all the
strings from the yaml files as unicode strings, pass the option
`unicode_output` as `True` when you instantiate your fixture manager.

0 comments on commit faacaef

Please sign in to comment.