fixed plural rules with pkgutil

This commit is contained in:
mdipierro
2012-08-30 23:55:14 -05:00
parent c6037b0355
commit 156c2c294e
27 changed files with 17 additions and 433 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.0.3 (2012-08-30 23:07:46) stable Version 2.0.3 (2012-08-30 23:55:09) stable
+2 -1
View File
@@ -53,6 +53,7 @@
'Exception instance attributes': 'Exception instance attributes', 'Exception instance attributes': 'Exception instance attributes',
'exposes': 'exposes', 'exposes': 'exposes',
'extends': 'extends', 'extends': 'extends',
'file does not exist': 'file does not exist',
'filter': 'filter', 'filter': 'filter',
'Frames': 'Frames', 'Frames': 'Frames',
'Get from URL:': 'Get from URL:', 'Get from URL:': 'Get from URL:',
@@ -72,8 +73,8 @@
'Logout': 'Logout', 'Logout': 'Logout',
'Models': 'Models', 'Models': 'Models',
'models': 'models', 'models': 'models',
'modules': 'modules',
'Modules': 'Modules', 'Modules': 'Modules',
'modules': 'modules',
'New application wizard': 'New application wizard', 'New application wizard': 'New application wizard',
'New simple application': 'New simple application', 'New simple application': 'New simple application',
'Overwrite installed app': 'Overwrite installed app', 'Overwrite installed app': 'Overwrite installed app',
+2 -6
View File
@@ -223,16 +223,12 @@ for c in controllers: controller_functions+=[c[:-3]+'/%s.html'%x for x in functi
{{=editpluralsfile('languages',pfile,dict(nplurals=p[0]))}} {{=editpluralsfile('languages',pfile,dict(nplurals=p[0]))}}
</span> </span>
<span class="file"> <span class="file">
{{=peekfile('languages',pfile,dict(id=id))}}, {{=peekfile('languages',pfile,dict(id=id))}}
</span> </span>
{{else:}} {{else:}}
<b>{{=T("are not used yet")}}</b>, <b>{{=T("are not used yet")}}</b>
{{pass}} {{pass}}
{{pass}} {{pass}}
{{=T("rules:")}}
<span class="file{{=' error' if p[3]!='ok' else ''}}">
{{=peekfile('gluon/contrib/rules', p[2], dict(app=app, id=id), p[3] if p[3]!='ok' else None)}}
</span>
{{pass}} {{pass}}
) )
</td> </td>
-1
View File
@@ -1 +0,0 @@
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for af (Afrikaans (South Africa))
nplurals=2 # Afrikaans language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for bg (Bulgarian)
nplurals=2 # Bulgarian language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-19
View File
@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for cs (Czech)
nplurals=3 # Czech language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: ( 0 if n==1 else
1 if 2<=n<=4 else
2 )
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for de (Deutsch)
nplurals=2 # German language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-20
View File
@@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for en (English)
nplurals=2 # English language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
construct_plural_form = lambda word, plural_id: (word +
('es' if word[-1:] in ('s','x','o') or
word[-2:] in ('sh','ch')
else 's'))
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for es (Spanish)
nplurals=2 # Spanish language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for fr (French))
nplurals=2 # French language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for he (Hebrew)
nplurals=2 # Hebrew language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for he (Hindi)
nplurals=2 # Hindi has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for hu (Hungarian)
nplurals=2 # Hungarian language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for it (Italian)
nplurals=2 # Italian language has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-14
View File
@@ -1,14 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for ja (Japanese)
nplurals=1 # Japanese language has ONE form!
# Always returns 0:
get_plural_id = lambda n: 0
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: word
-19
View File
@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for lt (Lithuanian)
nplurals=3 # Lithuanian language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n % 10 == 1 and n % 100 != 11 else
1 if n % 10 >= 2 and (n % 100 < 10 or n % 100 >= 20) else
2)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-19
View File
@@ -1,19 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for pl (Polish)
nplurals=3 # Polish language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n==1 else
1 if 2<=n<=4 else
2)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for pt (Portuguese)
nplurals=2 # Portuguese has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-17
View File
@@ -1,17 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for ro (Romanian)
nplurals=2 # Romanian has 2 forms:
# 1 singular and 1 plural
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: int(n != 1)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-20
View File
@@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for ru (Russian)
nplurals=3 # Russian language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n % 10 == 1 and n % 100 != 11 else
1 if n % 10 >= 2 and n % 10 <= 4 and
(n % 100 < 10 or n % 100 >= 20) else
2)
# construct_plural_form() is not used now because of complex
# rules of Russian language. Default version of
# this function is used to simple insert new words into
# plural_dict dictionary)
# construct_plural_form = lambda word, plural_id: word
-20
View File
@@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for sk (Slovak (Slovakia))
nplurals=3 # Slovak language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n % 10 == 1 and n % 100 != 11 else
1 if n % 10 >= 2 and n % 10 <= 4 and
(n % 100 < 10 or n % 100 >= 20) else
2)
# construct_plural_form() is not used now because of complex
# rules of Slovak language. Default version of this function
# is used to simple insert new words into plural_dict dictionary)
# construct_plural_form = lambda word, plural_id: word
-20
View File
@@ -1,20 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for sl (Slovenian)
nplurals=4 # Slovenian language has 4 forms:
# 1 singular and 3 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n % 100 == 1 else
1 if n % 100 == 2 else
2 if n % 100 in (3,4) else
3)
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: (word + 'suffix')
-14
View File
@@ -1,14 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for tr (Turkish)
nplurals=1 # Turkish language has ONE form!
# Always returns 0:
get_plural_id = lambda n: 0
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: word
-21
View File
@@ -1,21 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for uk (Ukrainian)
nplurals=3 # Ukrainian language has 3 forms:
# 1 singular and 2 plurals
# Determine plural_id for number *n* as sequence of positive
# integers: 0,1,...
# NOTE! For singular form ALWAYS return plural_id = 0
get_plural_id = lambda n: (0 if n % 10 == 1 and n % 100 != 11 else
1 if n % 10 >= 2 and n % 10 <= 4 and
(n % 100 < 10 or n % 100 >= 20) else
2)
# construct_plural_form() is not used now because of complex
# rules of Ukrainian language. Default version of
# this function is used to simple insert new words into
# plural_dict dictionary)
# construct_plural_form = lambda word, plural_id: word
-14
View File
@@ -1,14 +0,0 @@
#!/usr/bin/env python
# -*- coding: utf8 -*-
# Plural-Forms for zh (Chinese)
nplurals=1 # Chinese language has ONE form!
# Always returns 0:
get_plural_id = lambda n: 0
# Construct and return plural form of *word* using
# *plural_id* (which ALWAYS>0). This function will be executed
# for words (or phrases) not found in plural_dict dictionary
# construct_plural_form = lambda word, plural_id: word
+12 -37
View File
@@ -12,6 +12,7 @@ Plural subsystem is created by Vladyslav Kozlovskyy (Ukraine)
import os import os
import re import re
import pkgutil
from utf8 import Utf8 from utf8 import Utf8
from cgi import escape from cgi import escape
import portalocker import portalocker
@@ -82,7 +83,6 @@ regex_backslash = re.compile(r"\\([\\{}%])")
regex_plural = re.compile('%({.+?})') regex_plural = re.compile('%({.+?})')
regex_plural_dict = re.compile('^{(?P<w>[^()[\]][^()[\]]*?)\((?P<n>[^()\[\]]+)\)}$') # %%{word(varname or number)} regex_plural_dict = re.compile('^{(?P<w>[^()[\]][^()[\]]*?)\((?P<n>[^()\[\]]+)\)}$') # %%{word(varname or number)}
regex_plural_tuple = re.compile('^{(?P<w>[^[\]()]+)(?:\[(?P<i>\d+)\])?}$') # %%{word[index]} or %%{word} regex_plural_tuple = re.compile('^{(?P<w>[^[\]()]+)(?:\[(?P<i>\d+)\])?}$') # %%{word[index]} or %%{word}
regex_plural_rules = re.compile('^plural_rules-[a-zA-Z]{2}(-[a-zA-Z]{2})?\.py$')
# UTF8 helper functions # UTF8 helper functions
def upper_fun(s): def upper_fun(s):
@@ -215,48 +215,23 @@ def read_possible_languages(appdir):
langs['en'] = ('en', 'English', 0) langs['en'] = ('en', 'English', 0)
return langs return langs
def read_global_plural_rules(filename):
"""
retrieve plural rules from rules/*plural_rules-lang*.py file.
args:
filename (str): plural_rules filename
returns:
(nplurals, get_plural_id, construct_plural_form, status)
e.g.: (3, <function>, <function>, ok)
"""
env = {}
data = portalocker.read_locked(filename)
try:
exec(data) in env
status='ok'
except Exception, e:
status='Syntax error in %s (%s)' % (filename, e)
logging.error(status)
nplurals = env.get('nplurals', DEFAULT_NPLURALS)
get_plural_id = env.get('get_plural_id', DEFAULT_GET_PLURAL_ID)
construct_plural_form = env.get('construct_plural_form',
DEFAULT_CONSTRUCTOR_PLURAL_FORM)
return (nplurals, get_plural_id, construct_plural_form, status)
def read_possible_plurals(): def read_possible_plurals():
""" """
create list of all possible plural rules files create list of all possible plural rules files
result is cached to increase speed result is cached to increase speed
""" """
pdir = pjoin(pdirname(__file__),'contrib','rules') import gluon.contrib.plural_rules as package
plurals = {} plurals = {}
# scan rules directory for plural_rules-*.py files: for importer, modname, ispkg in pkgutil.iter_modules(package.__path__):
if os.path.exists(pdir): if len(modname)==2:
for pname in os.listdir(pdir): module = __import__(package.__name__+'.'+modname)
if not isdir(pname) and regex_plural_rules.match(pname): lang = modname
lang = pname[13:-3] pname = modname+'.py'
fname = ospath.join(pdir, pname) nplurals = getattr(module,'nplurals', DEFAULT_NPLURALS)
n, f1, f2, status = read_global_plural_rules(fname) get_plural_id = getattr(module,'get_plural_id', DEFAULT_GET_PLURAL_ID)
if status == 'ok': construct_plural_form = getattr(module,'construct_plural_form',
plurals[lang] = (lang, n, f1, f2, pname) DEFAULT_CONSTRUCTOR_PLURAL_FORM)
plurals[lang] = (lang, nplurals, get_plural_id, construct_plural_form, pname)
plurals['default'] = ('default', plurals['default'] = ('default',
DEFAULT_NPLURALS, DEFAULT_NPLURALS,
DEFAULT_GET_PLURAL_ID, DEFAULT_GET_PLURAL_ID,