Skip to content

Commit

Permalink
Merge 6694854 into b47b33b
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanoborini committed Aug 28, 2015
2 parents b47b33b + 6694854 commit 6ea3632
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
5 changes: 3 additions & 2 deletions traits/api.py
Expand Up @@ -41,8 +41,9 @@
Method, Module, Python, ReadOnly, Disallow, Constant,
Delegate, DelegatesTo, PrototypedFrom, Expression, PythonValue, File,
Directory, Range, Enum, Tuple, List, CList, Set, CSet, Dict, Instance,
AdaptedTo, AdaptsTo, Event, Button, ToolbarButton, Either, Type,
Symbol, WeakRef, Date, Time, false, true, undefined, Supports)
IdentifierStr, AdaptedTo, AdaptsTo, Event, Button, ToolbarButton,
Either, Type, Symbol, WeakRef, Date, Time, false, true, undefined,
Supports)

from .trait_types import (ListInt, ListFloat, ListStr, ListUnicode,
ListComplex, ListBool, ListFunction, ListMethod,
Expand Down
22 changes: 19 additions & 3 deletions traits/tests/test_traits.py
Expand Up @@ -20,9 +20,10 @@
from traits.testing.unittest_tools import unittest

from ..api import (Any, CFloat, CInt, CLong, Delegate, Float, HasTraits,
Instance, Int, List, Long, Str, Trait, TraitError,
TraitList, TraitPrefixList, TraitPrefixMap, TraitRange,
Tuple, pop_exception_handler, push_exception_handler)
IdentifierStr, Instance, Int, List, Long, Str, Trait,
TraitError, TraitList, TraitPrefixList, TraitPrefixMap,
TraitRange, Tuple, Undefined, pop_exception_handler,
push_exception_handler)

# Base unit test classes:

Expand Down Expand Up @@ -307,6 +308,21 @@ def coerce(self, value):
return str(value)


class IdentifierStrTrait(HasTraits):
value = IdentifierStr


class IdentifierStrTest(AnyTraitTest):
obj = IdentifierStrTrait()

_default_value = Undefined
_good_values = ['i', '_i', 'foo_bar']
_bad_values = [None, '1', 1, u'unicode', 'for', "0chupacabra", '']

def coerce(self, value):
return str(value)


class UnicodeTrait(HasTraits):
value = Trait(u'unicode')

Expand Down
27 changes: 27 additions & 0 deletions traits/trait_types.py
Expand Up @@ -26,6 +26,7 @@

import datetime
import operator
import keyword
import re
import sys
from os.path import isfile, isdir
Expand Down Expand Up @@ -365,6 +366,30 @@ class Str ( BaseStr ):
fast_validate = ( 11, basestring )


class IdentifierStr(BaseStr):
#: A description of the type of value this trait accepts:
info_text = 'an ASCII identifier string'

#: The default value when first created
default_value = Undefined

# Regexp for identifier recognition
_identifier_regex = re.compile("[_A-Za-z][_a-zA-Z0-9]*$")

def validate ( self, object, name, value ):
""" Validates that a specified value is valid for this trait.
"""

if (isinstance( value, str ) and
len(value) > 0 and
not keyword.iskeyword(value) and
self._identifier_regex.match(value)):

return value

self.error( object, name, value )


class Title ( Str ):
""" Defines a string type which by default uses the traits ui TitleEditor
when used in a View.
Expand Down Expand Up @@ -826,6 +851,8 @@ def __setstate__ ( self, state ):
self.__dict__.update( state )
self._init()



#-------------------------------------------------------------------------------
# 'Regex' trait:
#-------------------------------------------------------------------------------
Expand Down

0 comments on commit 6ea3632

Please sign in to comment.