def test_unicode_or_str_decodes(self): """UnicodeOrString should decode plain strings.""" a = u"\N{HIRAGANA LETTER A}" self.assertEquals( UnicodeOrString("utf-8").coerce(a.encode("utf-8"), PATH), a) letter = u"\N{LATIN SMALL LETTER A WITH GRAVE}" self.assertEquals( UnicodeOrString("latin-1").coerce(letter.encode("latin-1"), PATH), letter)
def test_unicode_or_str_bad_encoding(self): """Decoding errors should be converted to a SchemaError.""" error = self.assertRaises(SchemaError, UnicodeOrString("utf-8").coerce, "\xff", PATH) self.assertEquals( str(error), r"<path>: expected unicode or utf-8 string, " r"got '\xff'")
def test_unicode_or_str(self): schema = UnicodeOrString("utf-8") self.assertEquals(schema.coerce(u"foo", PATH), u"foo")
def test_list_multiple_items(self): a = u"\N{HIRAGANA LETTER A}" schema = List(UnicodeOrString("utf-8")) self.assertEquals(schema.coerce([a, a.encode("utf-8")], PATH), [a, a])
def test_unicode_or_str_bad(self): error = self.assertRaises(SchemaError, UnicodeOrString("utf-8").coerce, 32, PATH) self.assertEquals(str(error), "<path>: expected unicode or utf-8 string, got 32")
def test_unicode_or_str_accepts_str(self): self.assertEquals(UnicodeOrString("utf-8").coerce("foo", PATH), u"foo")
import logging import os import yaml from juju.charm.errors import MetaDataError from juju.errors import FileNotFound from juju.lib.schema import (SchemaError, Bool, Constant, Dict, Int, KeyDict, OneOf, UnicodeOrString) log = logging.getLogger("juju.charm") UTF8_SCHEMA = UnicodeOrString("utf-8") INTERFACE_SCHEMA = KeyDict({ "interface": UTF8_SCHEMA, "limit": OneOf(Constant(None), Int()), "optional": Bool() }) class InterfaceExpander(object): """Schema coercer that expands the interface shorthand notation. We need this class because our charm shorthand is difficult to work with (unfortunately). So we coerce shorthand and then store the desired format in ZK. Supports the following variants:: provides: