| 1 |
import re |
| 2 |
from zope.interface import implementer |
| 3 |
from twisted.web import template |
| 4 |
from . import iformal |
| 5 |
|
| 6 |
|
| 7 |
_IDENTIFIER_REGEX = re.compile('^[a-zA-Z_][a-zA-Z0-9_]*$') |
| 8 |
|
| 9 |
|
| 10 |
def titleFromName(name): |
| 11 |
|
| 12 |
def _(): |
| 13 |
|
| 14 |
it = iter(name) |
| 15 |
last = None |
| 16 |
|
| 17 |
while 1: |
| 18 |
try: |
| 19 |
ch = next(it) |
| 20 |
except StopIteration: |
| 21 |
return |
| 22 |
if ch == '_': |
| 23 |
if last != '_': |
| 24 |
yield ' ' |
| 25 |
elif last in (None,'_'): |
| 26 |
yield ch.upper() |
| 27 |
elif ch.isupper() and not last.isupper(): |
| 28 |
yield ' ' |
| 29 |
yield ch.upper() |
| 30 |
else: |
| 31 |
yield ch |
| 32 |
last = ch |
| 33 |
|
| 34 |
return ''.join(_()) |
| 35 |
|
| 36 |
def keytocssid(fieldKey, *extras): |
| 37 |
return render_cssid(fieldKey, *extras) |
| 38 |
|
| 39 |
def render_cssid(fieldKey, *extras): |
| 40 |
""" |
| 41 |
Render the CSS id for the form field's key. |
| 42 |
""" |
| 43 |
l = [template.slot('formName'), '-', '-'.join(fieldKey.split('.'))] |
| 44 |
for extra in extras: |
| 45 |
l.append('-') |
| 46 |
l.append(str(extra)) |
| 47 |
return l |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
def validIdentifier(name): |
| 52 |
""" |
| 53 |
Test that name is a valid Python identifier. |
| 54 |
""" |
| 55 |
return _IDENTIFIER_REGEX.match(name) is not None |
| 56 |
|
| 57 |
@implementer( iformal.IKey, iformal.ILabel ) |
| 58 |
class SequenceKeyLabelAdapter(object): |
| 59 |
|
| 60 |
def __init__(self, original): |
| 61 |
self.original = original |
| 62 |
|
| 63 |
def key(self): |
| 64 |
return str(self.original[0]) |
| 65 |
|
| 66 |
def label(self): |
| 67 |
return str(self.original[1]) |
| 68 |
|
| 69 |
|
| 70 |
@implementer(iformal.ILabel) |
| 71 |
class StringableLabelAdapter(object): |
| 72 |
|
| 73 |
def __init__(self, original): |
| 74 |
self.original = str(original) |
| 75 |
|
| 76 |
def label(self): |
| 77 |
return self.original |
| 78 |
|
| 79 |
|
| 80 |
# This is a duplication of gavo.utils.misctricks; I don't want either |
| 81 |
# of the two depend on each other. |
| 82 |
class CaseSemisensitiveDict(dict): |
| 83 |
"""A dictionary allowing case-insensitive access to its content. |
| 84 |
|
| 85 |
This is used for DAL renderers which, unfortunately, are supposed |
| 86 |
to be case insensitive. Since case insensitivity is at least undesirable |
| 87 |
for service-specific keys, we go a semi-insenstitve approach here: |
| 88 |
First, we try literal matches, if that does not work, we try matching |
| 89 |
against an all-uppercase version. |
| 90 |
|
| 91 |
Name clashes resulting from different names being mapped to the |
| 92 |
same normalized version are handled in some random way. Don't do this. |
| 93 |
And don't rely on case normalization if at all possible. |
| 94 |
|
| 95 |
Only strings are allowed as keys here. This class is not concerned |
| 96 |
with the values. |
| 97 |
>>> d = CaseSemisensitiveDict({"a": 1, "A": 2, "b": 3}) |
| 98 |
>>> d["a"], d["A"], d["b"], d["B"] |
| 99 |
(1, 2, 3, 3) |
| 100 |
>>> d["B"] = 9; d["b"], d["B"] |
| 101 |
(3, 9) |
| 102 |
>>> del d["b"]; d["b"], d["B"] |
| 103 |
(9, 9) |
| 104 |
>>> "B" in d, "b" in d, "u" in d |
| 105 |
(True, True, False) |
| 106 |
>>> d.pop("a"), list(d.keys()) |
| 107 |
(1, ['A', 'B']) |
| 108 |
""" |
| 109 |
def __init__(self, *args, **kwargs): |
| 110 |
dict.__init__(self, *args, **kwargs) |
| 111 |
self._normCasedCache = None |
| 112 |
|
| 113 |
def __getitem__(self, key): |
| 114 |
try: |
| 115 |
return dict.__getitem__(self, key) |
| 116 |
except KeyError: |
| 117 |
pass # try again with normalized case. |
| 118 |
return self._normCased[key.upper()] |
| 119 |
|
| 120 |
def __setitem__(self, key, value): |
| 121 |
self._normCasedCache = None |
| 122 |
dict.__setitem__(self, key, value) |
| 123 |
|
| 124 |
def __contains__(self, key): |
| 125 |
return dict.__contains__(self, key) or key.upper() in self._normCased |
| 126 |
|
| 127 |
def get(self, key, default=None): |
| 128 |
try: |
| 129 |
return self[key] |
| 130 |
except KeyError: |
| 131 |
return default |
| 132 |
|
| 133 |
def pop(self, key, default=KeyError): |
| 134 |
try: |
| 135 |
return dict.pop(self, key) |
| 136 |
except KeyError: |
| 137 |
pass # try again with normalized case. |
| 138 |
try: |
| 139 |
return self._normCased.pop(key.upper()) |
| 140 |
except KeyError: |
| 141 |
if default is not KeyError: |
| 142 |
return default |
| 143 |
raise |
| 144 |
|
| 145 |
@property |
| 146 |
def _normCased(self): |
| 147 |
if self._normCasedCache is None: |
| 148 |
self._normCasedCache = dict((k.upper(), v) |
| 149 |
for k, v in self.items()) |
| 150 |
return self._normCasedCache |
| 151 |
|
| 152 |
@classmethod |
| 153 |
def fromDict(cls, aDict): |
| 154 |
if isinstance(aDict, CaseSemisensitiveDict): |
| 155 |
return aDict |
| 156 |
else: |
| 157 |
return cls(aDict) |
| 158 |
|
| 159 |
|