Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 0 additions & 25 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,3 @@
],
"genindex": ["links.html", "sourcelink.html", "searchbox.html"],
}

# Avoid issues like below when running under python 3.x:
#
# Expected:
# [u'hello', u'world']
#
# Got:
# ['hello', 'world']

import re
import sys
import doctest

OrigOutputChecker = doctest.OutputChecker


class Py23OutputChecker(OrigOutputChecker):
def check_output(self, want, got, optionflags):
if sys.version_info[0] > 2:
want = re.sub("u'(.*?)'", "'\\1'", want)
want = re.sub('u"(.*?)"', '"\\1"', want)
return OrigOutputChecker.check_output(self, want, got, optionflags)


doctest.OutputChecker = Py23OutputChecker
4 changes: 2 additions & 2 deletions docs/source/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ blank, using default values, or with values taken from your objects.

form = SignInForm.from_flat(request.POST)
if form.validate():
logging.info(u"sign-in: %s" % form['username'].value)
logging.info("sign-in: %s" % form['username'].value)
redirect('/app/')
else:
render('login.html', form=form)
Expand All @@ -81,6 +81,6 @@ used as-is in output templates for form layout, redisplay and error reporting.
>>> isinstance(as_regular_python_data, dict)
True
>>> as_regular_python_data['username']
u'jek'
'jek'
>>> form2 = SignInForm(as_regular_python_data)
>>> assert form['username'].value == form2['username'].value
2 changes: 1 addition & 1 deletion docs/source/markup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ participate in ``tabindex=`` generation.
>>> print(html.textarea(auto_tabindex=True))
<textarea tabindex="100"></textarea>
>>> html.set(auto_tabindex=True)
u''
''
>>> print(html.textarea())
<textarea tabindex="101"></textarea>

Expand Down
10 changes: 5 additions & 5 deletions docs/source/schema/lists.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ Example:

>>> from flatland import List, String
>>> Names = List.named('names').of(String.named('name'))
>>> names = Names([u'a', u'b'])
>>> names = Names(['a', 'b'])
>>> names.value
[u'a', u'b']
['a', 'b']
>>> names.flatten()
[(u'names_0_name', u'a'), (u'names_1_name', u'b')]
[('names_0_name', 'a'), ('names_1_name', 'b')]
>>> names[1].value
u'b'
'b'
>>> names.find_one('1').value
u'b'
'b'

Validation
----------
Expand Down
10 changes: 5 additions & 5 deletions docs/source/schema/schema.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ types:
.. testcode:: fso

from flatland import Dict, String
SearchSchema = Dict.named('search').of(String.named(u'keywords'))
SearchSchema = Dict.named('search').of(String.named('keywords'))

.. TODO:: FIXME UPDATE:

Expand All @@ -29,9 +29,9 @@ types:
.. doctest:: fso
:options: +ELLIPSIS

>>> form = SearchSchema({u'keywords': u'foo bar baz'})
>>> form = SearchSchema({'keywords': 'foo bar baz'})
>>> form.value
{u'keywords': u'foo bar baz'}
{'keywords': 'foo bar baz'}

.. TODO:: FIXME UPDATE:

Expand All @@ -42,10 +42,10 @@ types:

>>> from flatland import List
>>> ComposedSchema = Dict.of(SearchSchema,
... List.named(u'many_searches').of(SearchSchema))
... List.named('many_searches').of(SearchSchema))
>>> form = ComposedSchema()
>>> sorted(form.value.keys())
[u'many_searches', u'search']
['many_searches', 'search']

.. TODO:: FIXME UPDATE:

Expand Down
24 changes: 12 additions & 12 deletions docs/source/schema/traversal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ has a bit of variety in its structure.
'location': {'x': 10, 'y': 20},
}

ann1 = Annotation(sample_data, name=u'ann1')
ann1 = Annotation(sample_data, name='ann1')


Going Raw
Expand All @@ -38,7 +38,7 @@ application. An element's :attr:`~base.Element.value` is a full & recursive
.. doctest::

>>> ann1['title'] # ann1 is a flatland structure
<String u'title'; value=u'Interesting Spot'>
<String 'title'; value='Interesting Spot'>
>>> isinstance(ann1.value, dict) # but its .value is not
True
>>> ann1.value == sample_data
Expand All @@ -59,12 +59,12 @@ For example, ``Form`` and ``Dict`` can be indexed and used like ``dict``:
.. doctest::

>>> ann1['title'].value
u'Interesting Spot'
'Interesting Spot'
>>> ann1['location']['x'].value
10
>>> sorted(ann1['location'].items())
[(u'x', <Integer u'x'; value=10>), (u'y', <Integer u'y'; value=20>)]
>>> u'title' in ann1
[('x', <Integer 'x'; value=10>), ('y', <Integer 'y'; value=20>)]
>>> 'title' in ann1
True

And ``List`` and similar types can be used like lists:
Expand Down Expand Up @@ -108,9 +108,9 @@ to related elements: :attr:`~base.Element.root`,
>>> list(ann1['title'].children) # title is a String and has no children
[]
>>> sorted(el.name for el in ann1.all_children if el.name)
[u'flags', u'location', u'title', u'x', u'y']
['flags', 'location', 'title', 'x', 'y']
>>> [el.name for el in ann1['location']['x'].parents]
[u'location', u'ann1']
['location', 'ann1']

Each of these properties (excepting ``root``) returns an iterator of
elements.
Expand All @@ -128,28 +128,28 @@ use when authoring flexible & reusable validators.
.. doctest::

>>> ann1.find('title') # find 'ann1's child named 'title'
[<String u'title'; value=u'Interesting Spot'>]
[<String 'title'; value='Interesting Spot'>]

Paths are evaluated relative to the element:

.. doctest::

>>> ann1['location'].find('x')
[<Integer u'x'; value=10>]
[<Integer 'x'; value=10>]

Referencing parents is possible with ``..``:

.. doctest::

>>> ann1['location']['x'].find('../../title')
[<String u'title'; value=u'Interesting Spot'>]
[<String 'title'; value='Interesting Spot'>]

Absolute paths begin with a ``/``.

.. doctest::

>>> ann1['location']['x'].find('/title')
[<String u'title'; value=u'Interesting Spot'>]
[<String 'title'; value='Interesting Spot'>]

Members of sequences can be selected like any other child (their index number
is their name), or you can use Python-like slicing:
Expand Down Expand Up @@ -181,7 +181,7 @@ needed to illustrate this:
>>> p = Points([[dict(x=1, y=1), dict(x=2, y=2)],
... [dict(x=3, y=3)]])
>>> p.find('[:][:]/x')
[<Integer u'x'; value=1>, <Integer u'x'; value=2>, <Integer u'x'; value=3>]
[<Integer 'x'; value=1>, <Integer 'x'; value=2>, <Integer 'x'; value=3>]

The equivalent straight Python to select the same set of elements is quite a
bit more wordy.
Expand Down
98 changes: 0 additions & 98 deletions src/flatland/_compat.py

This file was deleted.

19 changes: 7 additions & 12 deletions src/flatland/out/generic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re

from flatland._compat import PY2, bytestring_type, iteritems, text_type
from flatland.out.util import parse_trool
from flatland.schema import Array, Boolean
from flatland.util import Maybe, to_pairs
Expand Down Expand Up @@ -48,9 +47,7 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
if key not in self:
raise KeyError(
f"{key!r} not permitted in this {self.__class__.__name__}"
)
raise KeyError(f"{key!r} not permitted in this {self.__class__.__name__}")
self._frames[-1][key] = value

def __contains__(self, key):
Expand All @@ -65,16 +62,14 @@ def update(self, *iterable, **kwargs):
source = to_pairs(iterable[0])
for key, value in source:
self[key] = value
for key, value in iteritems(kwargs):
if PY2:
key = key.decode("ascii")
for key, value in kwargs.items():
self[key] = value

def __repr__(self):
return f"{self.__class__.__name__}({self._frames[-1]!r})"


class Markup(text_type):
class Markup(str):
"""A string of HTML markup that should not be escaped in output."""

__slots__ = ()
Expand Down Expand Up @@ -162,7 +157,7 @@ def transform_value(tagname, attributes, contents, context, bind):
current = attributes.get("value")
if current is not None:
value = current
elif isinstance(contents, text_type):
elif isinstance(contents, str):
value = contents.strip()
elif contents is None:
value = ""
Expand Down Expand Up @@ -231,7 +226,7 @@ def transform_tabindex(tagname, attributes, contents, context, bind):

current = attributes.get("tabindex")
if forced or current is None and tagname in _auto_tags["tabindex"]:
attributes["tabindex"] = text_type(str(tabindex))
attributes["tabindex"] = str(tabindex)
if tabindex > 0:
context["tabindex"] = tabindex + 1
return contents
Expand Down Expand Up @@ -310,9 +305,9 @@ def _sanitize_domid_suffix(string):
def _unpack(html_string):
"""Extract HTML from a __html__() interface."""
unpacked = html_string.__html__()
if unpacked.__class__ is text_type:
if unpacked.__class__ is str:
return unpacked
return text_type(unpacked)
return str(unpacked)


def _markup_escape(string):
Expand Down
Loading