Beispiel #1
0
    def plurr_format(self, str1, str2, **kwargs):
        """For plurr-formatted strings, checks the syntax is correct."""
        # Ignore check for empty target strings or non Plurr-formatted
        # source strings
        if str2 == u'' or not plurr_format_regex.search(str1):
            return True

        # Ignore check if library is missing
        try:
            from plurr import Plurr
        except ImportError:
            return True

        plurr = Plurr()

        try:
            plurr.format(str2, {}, {
                'locale': kwargs['language_code'],
                'strict': False,
                'callback': lambda x: '',
            })
        except SyntaxError as e:
            raise checks.FilterFailure(e.message)

        return True
Beispiel #2
0
    def plurr_format(self, str1, str2, **kwargs):
        """For plurr-formatted strings, checks the syntax is correct."""
        # Ignore check for empty target strings or non Plurr-formatted
        # source strings
        if str2 == u"" or not plurr_format_regex.search(str1):
            return True

        # Ignore check if library is missing
        try:
            from plurr import Plurr
        except ImportError:
            return True

        plurr = Plurr()

        try:
            plurr.format(
                str2,
                {},
                {
                    "locale": kwargs["language_code"],
                    "strict": False,
                    "callback": lambda x: "",
                },
            )
        except SyntaxError as e:
            raise checks.FilterFailure(str(e))

        return True
Beispiel #3
0
    def plurr_format(self, str1, str2, **kwargs):
        """For plurr-formatted strings, checks the syntax is correct."""
        # Ignore check for empty target strings or non Plurr-formatted
        # source strings
        if str2 == u'' or not plurr_format_regex.search(str1):
            return True

        # Ignore check if library is missing
        try:
            from plurr import Plurr
        except ImportError:
            return True

        plurr = Plurr()

        try:
            plurr.format(str2, {}, {
                'locale': kwargs['language_code'],
                'strict': False,
                'callback': lambda x: '',
            })
        except SyntaxError as e:
            raise checks.FilterFailure(e.message)

        return True
Beispiel #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append("..")

import time

from plurr import Plurr

p = Plurr()
s = 'Do you want to delete {N_PLURAL:this {N} file|these {N} files} permanently?'
params = {
    'N': 5
}
x = 100000

start = time.clock()

for i in xrange(x):
    dummy = p.format(s, params)

end = time.clock()
time = end - start
print('Execution time ({0} calls): {1} sec ({2} ms per call)'.format(
    x, time, time * 1000 / x))
Beispiel #5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append("..")

import time

from plurr import Plurr


p = Plurr()
x = 1000000

start = time.clock()

for i in xrange(x):
    dummy = p.set_locale('ru')

end = time.clock()
time = end - start
print('Execution time ({0} calls): {1} sec ({2} ms per call)'.format(x, time, time * 1000 / x))
Beispiel #6
0

def t(n, p, s, params, options, message, exception=None):
    result = None
    try:
        result = p.format(s, params, options)
        if exception is not None:
            print("n: fail: should produce exception [{0}]".format(exception))
            return
        print(n + ": "  + ('pass' if result == message else "fail: [{0}] vs [{1}]".format(result, message)))
    except Exception as e:
        s = '{0}: {1}'.format(e.__class__.__name__, str(e))
        print(n + ": "  + ('pass' if s == exception else "fail: [{0}] vs [{1}]".format(s, exception)))


p = Plurr()

t('e.1', p, '', None, None, '', "TypeError: 'params' is not a dict")
t('e.2', p, 'err {', {}, None, '', "SyntaxError: Unmatched { found")
t('e.3', p, 'err }', {}, None, '', "SyntaxError: Unmatched } found")
t('e.4', p, '{foo}', {}, None, '', "LookupError: 'foo' not defined")
t('e.5', p, '{N_PLURAL}', {'N': 'NaN'}, None, '', "ValueError: Value of 'N' is not a zero or positive integer number")
t('e.6', p, '{N_PLURAL}', {'N': 1.5}, None, '', "ValueError: Value of 'N' is not a zero or positive integer number")

s = 'Do you want to delete {N_PLURAL:this {N} file|these {N} files} permanently?'

t('1.1', p, s, {'N': 1}, None, 'Do you want to delete this 1 file permanently?')
t('1.2', p, s, {'N': 5}, None, 'Do you want to delete these 5 files permanently?')

s = 'Do you want to drink {CHOICE:coffee|tea|juice}?'
Beispiel #7
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append("..")

import time

from plurr import Plurr


p = Plurr()
s = 'Do you want to delete {N_PLURAL:this {N} file|these {N} files} permanently?';
params = {'N': 5};
x = 100000

start = time.clock()

for i in xrange(x):
    dummy = p.format(s, params)

end = time.clock()
time = end - start
print('Execution time ({0} calls): {1} sec ({2} ms per call)'.format(x, time, time * 1000 / x))
Beispiel #8
0
    try:
        result = p.format(s, params, options)
        if exception is not None:
            print("n: fail: should produce exception [{0}]".format(exception))
            return
        print(n + ": " +
              ('pass' if result ==
               message else "fail: [{0}] vs [{1}]".format(result, message)))
    except Exception as e:
        s = '{0}: {1}'.format(e.__class__.__name__, str(e))
        print(n + ": " +
              ('pass' if s ==
               exception else "fail: [{0}] vs [{1}]".format(s, exception)))


p = Plurr()

t('e.1', p, '', None, None, '', "TypeError: 'params' is not a dict")
t('e.2', p, 'err {', {}, None, '', "SyntaxError: Unmatched { found")
t('e.3', p, 'err }', {}, None, '', "SyntaxError: Unmatched } found")
t('e.4', p, '{foo}', {}, None, '', "LookupError: 'foo' not defined")
t('e.5', p, '{N_PLURAL}', {'N': 'NaN'}, None, '',
  "ValueError: Value of 'N' is not a zero or positive integer number")
t('e.6', p, '{N_PLURAL}', {'N': 1.5}, None, '',
  "ValueError: Value of 'N' is not a zero or positive integer number")

s = 'Do you want to delete {N_PLURAL:this {N} file|these {N} files} permanently?'

t('1.1', p, s, {'N': 1}, None,
  'Do you want to delete this 1 file permanently?')
t('1.2', p, s, {'N': 5}, None,
Beispiel #9
0

def t(n, p, s, params, options, message, exception=None):
    result = None
    try:
        result = p.format(s, params, options)
        if exception is not None:
            print("n: fail: should produce exception [{0}]".format(exception))
            return
        print(n + ": "  + ('pass' if result == message else "fail: [{0}] vs [{1}]".format(result, message)))
    except Exception as e:
        s = '{0}: {1}'.format(e.__class__.__name__, str(e))
        print(n + ": "  + ('pass' if s == exception else "fail: [{0}] vs [{1}]".format(s, exception)))


p = Plurr()

t('e.1', p, '', None, None, '', "TypeError: 'params' is not a dict")
t('e.2', p, 'err {', {}, None, '', "SyntaxError: Unmatched { found")
t('e.3', p, 'err }', {}, None, '', "SyntaxError: Unmatched } found")
t('e.4', p, '{foo}', {}, None, '', "LookupError: 'foo' not defined")
t('e.5', p, '{N_PLURAL}', {'N': 'NaN'}, None, '', "ValueError: Value of 'N' is not a zero or positive integer number")
t('e.6', p, '{N_PLURAL}', {'N': 1.5}, None, '', "ValueError: Value of 'N' is not a zero or positive integer number")

s = 'Do you want to delete {N_PLURAL:this {N} file|these {N} files} permanently?'

t('1.1', p, s, {'N': 1}, None, 'Do you want to delete this 1 file permanently?')
t('1.2', p, s, {'N': 5}, None, 'Do you want to delete these 5 files permanently?')

s = 'Do you want to drink {CHOICE:coffee|tea|juice}?'
Beispiel #10
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
sys.path.append("..")

import time

from plurr import Plurr


p = Plurr()
x = 100000

start = time.clock()

for i in xrange(x):
    dummy = p.locale('ru')

end = time.clock()
time = end - start
print('Execution time ({0} calls): {1} sec ({2} ms per call)'.format(x, time, time * 1000 / x))