From eb07384c23f497878ac974df239ef815f9cf9493 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sat, 23 Mar 2019 21:42:06 -0700 Subject: [PATCH] moved pluralize logic into sqlhtml for speed reasons, thanks Paolo P. --- gluon/packages/dal | 2 +- gluon/sqlhtml.py | 68 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 52 insertions(+), 18 deletions(-) diff --git a/gluon/packages/dal b/gluon/packages/dal index c0c8d29b..49ddb26b 160000 --- a/gluon/packages/dal +++ b/gluon/packages/dal @@ -1 +1 @@ -Subproject commit c0c8d29ba3fc5b5cfd5635acbeabe62475040442 +Subproject commit 49ddb26bf00b8a10eb20413d359155dc5d54d4af diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 774430e1..9423c2bd 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -47,9 +47,8 @@ try: except ImportError: settings = {} -widget_class = re.compile('^\w*') -REGEX_ALIAS_MATCH = re.compile('^(.*) AS (.*)$') +REGEX_WIDGET_CLASS = re.compile(r'^\w*') def add_class(a, b): @@ -143,6 +142,34 @@ def show_if(cond): raise RuntimeError("Not Implemented Error") +PLURALIZE_RULES = None + +def pluralize(singular, rules=None): + if rules is None: + global PLURALIZE_RULES + if PLURALIZE_RULES is None: + PLURALIZE_RULES = [ + (re.compile('child$'), re.compile('child$'), 'children'), + (re.compile('oot$'), re.compile('oot$'), 'eet'), + (re.compile('ooth$'), re.compile('ooth$'), 'eeth'), + (re.compile('l[eo]af$'), re.compile('l([eo])af$'), 'l\\1aves'), + (re.compile('sis$'), re.compile('sis$'), 'ses'), + (re.compile('man$'), re.compile('man$'), 'men'), + (re.compile('ife$'), re.compile('ife$'), 'ives'), + (re.compile('eau$'), re.compile('eau$'), 'eaux'), + (re.compile('lf$'), re.compile('lf$'), 'lves'), + (re.compile('[sxz]$'), re.compile('$'), 'es'), + (re.compile('[^aeioudgkprt]h$'), re.compile('$'), 'es'), + (re.compile('(qu|[^aeiou])y$'), re.compile('y$'), 'ies'), + (re.compile('$'), re.compile('$'), 's'), + ] + rules = PLURALIZE_RULES + for line in rules: + re_search, re_sub, replace = line + plural = re_search.search(singular) and re_sub.sub(replace, singular) + if plural: return plural + + class FormWidget(object): """ Helper for SQLFORM to generate form input fields (widget), related to the @@ -165,7 +192,7 @@ class FormWidget(object): attr = dict( _id='%s_%s' % (field.tablename, field.name), _class=cls._class or - widget_class.match(str(field.type)).group(), + REGEX_WIDGET_CLASS.match(str(field.type)).group(), _name=field.name, requires=field.requires, ) @@ -1541,7 +1568,7 @@ class SQLFORM(FORM): # SQLCustomType has a widget, use it inp = field.type.widget(field, default) else: - field_type = widget_class.match(str(field.type)).group() + field_type = REGEX_WIDGET_CLASS.match(str(field.type)).group() field_type = field_type in self.widgets and field_type or 'string' inp = self.widgets[field_type].widget(field, default) @@ -3180,7 +3207,8 @@ class SQLFORM(FORM): return dict(form=form) """ - request, T = current.request, current.T + request = current.request + T = current.T if args is None: args = [] @@ -3198,8 +3226,7 @@ class SQLFORM(FORM): links = {} if constraints is None: constraints = {} - field = None - name = None + field = name = None def format(table, row): if not row: @@ -3210,10 +3237,14 @@ class SQLFORM(FORM): return table._format(row) else: return '#' + str(row.id) + + def plural(table): + return table._plural or pluralize(table._singular.lower()).capitalize() + try: nargs = len(args) + 1 - previous_tablename, previous_fieldname, previous_id = \ - table._tablename, None, None + previous_tablename = table._tablename + previous_fieldname = previous_id = None while len(request.args) > nargs: key = request.args(nargs) if '.' in key: @@ -3234,11 +3265,12 @@ class SQLFORM(FORM): if previous_id: if record[previous_fieldname] != int(previous_id): raise HTTP(400) - previous_tablename, previous_fieldname, previous_id = \ - tablename, fieldname, id + previous_tablename = tablename + previous_fieldname = fieldname + previous_id = id name = format(db[referee], record) breadcrumbs.append( - LI(A(T(db[referee]._plural), + LI(A(T(plural(db[referee])), cid=request.cid, _href=url()), SPAN(divider, _class='divider'), @@ -3319,8 +3351,8 @@ class SQLFORM(FORM): if tb: multiple_links = len(linked_fieldnames) > 1 for fieldname in linked_fieldnames: - t = T(tb._plural) if not multiple_links else \ - T(tb._plural + '(' + fieldname + ')') + t = T(plural(tb)) if not multiple_links else \ + T("%s(%s)" % (plural(tb), fieldname)) args0 = tablename + '.' + fieldname linked.append( lambda row, t=t, nargs=nargs, args0=args0: @@ -3332,7 +3364,7 @@ class SQLFORM(FORM): user_signature=user_signature, **kwargs) if isinstance(grid, DIV): - header = table._plural + header = plural(table) next = grid.create_form or grid.update_form or grid.view_form breadcrumbs.append(LI( A(T(header), cid=request.cid, _href=url()), @@ -3397,6 +3429,8 @@ class SQLTABLE(TABLE): """ + REGEX_ALIAS_MATCH = '^(.*) AS (.*)$' + def __init__(self, sqlrows, linkto=None, @@ -3450,7 +3484,7 @@ class SQLTABLE(TABLE): if isinstance(f, field_types): headers[c] = make_name(f) else: - headers[c] = REGEX_ALIAS_MATCH.sub(r'\2', c) + headers[c] = re.sub(self.REGEX_ALIAS_MATCH, r'\2', c) if colgroup: cols = [COL(_id=c.replace('.', '-'), data={'column': i + 1}) for i, c in enumerate(columns)] @@ -3476,7 +3510,7 @@ class SQLTABLE(TABLE): row.append(TH(A(headers.get(c, c), _href=th_link + '?orderby=' + c, cid=cid))) else: - row.append(TH(headers.get(c, REGEX_ALIAS_MATCH.sub(r'\2', c)))) + row.append(TH(headers.get(c, re.sub(self.REGEX_ALIAS_MATCH, r'\2', c)))) if extracolumns: # new implement dict for c in extracolumns: