Example #1
0
    def obtainFilter(self, processing, uri):
        '''
        Checks the filter URI.

        @param processing: Processing
            The processing used for delivering the request.
        @param uri: string
            The URI to call, parameters are allowed.
        @return: tuple(boolean|None, integer, string)
            A tuple containing as the first True if the filter URI provided a True value, None if the filter cannot be fetched,
            on the second position the response status and on the last position the response text.
        '''
        assert isinstance(processing,
                          Processing), 'Invalid processing %s' % processing
        assert isinstance(uri, str), 'Invalid URI %s' % uri

        request = processing.ctx.request()
        assert isinstance(request,
                          RequestFilter), 'Invalid request %s' % request

        url = urlparse(uri)
        request.scheme, request.method = self.scheme, HTTP_GET
        request.headers = {}
        request.uri = url.path.lstrip('/')
        request.parameters = parse_qsl(url.query, True, False)
        request.accTypes = [self.mimeTypeJson]
        request.accCharSets = [self.encodingJson]

        chain = Chain(processing)
        chain.process(request=request,
                      requestCnt=processing.ctx.requestCnt(),
                      response=processing.ctx.response(),
                      responseCnt=processing.ctx.responseCnt()).doAll()

        response, responseCnt = chain.arg.response, chain.arg.responseCnt
        assert isinstance(response,
                          ResponseHTTP), 'Invalid response %s' % response
        assert isinstance(
            responseCnt,
            ResponseContentHTTP), 'Invalid response content %s' % responseCnt

        if ResponseHTTP.text in response and response.text:
            text = response.text
        elif ResponseHTTP.code in response and response.code:
            text = response.code
        else:
            text = None
        if ResponseContentHTTP.source not in responseCnt or responseCnt.source is None or not isSuccess(
                response.status):
            return None, response.status, text

        if isinstance(responseCnt.source, IInputStream):
            source = responseCnt.source
        else:
            source = BytesIO()
            for bytes in responseCnt.source:
                source.write(bytes)
            source.seek(0)
        allowed = json.load(codecs.getreader(self.encodingJson)(source))
        return allowed['HasAccess'] == 'True', response.status, text
Example #2
0
 def setUp(self):
     # Use a locale which won't fail to run the tests
     os.environ['LANG'] = 'en_US.UTF-8'
     messages1 = [
         ('foo', {'string': 'Voh'}),
         ('foo', {'string': 'VohCTX', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('Voh1', 'Vohs1')}),
         (('foo1', 'foos1'), {'string': ('VohCTX1', 'VohsCTX1'), 'context': 'foo'}),
     ]
     messages2 = [
         ('foo', {'string': 'VohD'}),
         ('foo', {'string': 'VohCTXD', 'context': 'foo'}),
         (('foo1', 'foos1'), {'string': ('VohD1', 'VohsD1')}),
         (('foo1', 'foos1'), {'string': ('VohCTXD1', 'VohsCTXD1'), 'context': 'foo'}),
     ]
     catalog1 = Catalog(locale='en_GB', domain='messages')
     catalog2 = Catalog(locale='en_GB', domain='messages1')
     for ids, kwargs in messages1:
         catalog1.add(ids, **kwargs)
     for ids, kwargs in messages2:
         catalog2.add(ids, **kwargs)
     catalog1_fp = BytesIO()
     catalog2_fp = BytesIO()
     write_mo(catalog1_fp, catalog1)
     catalog1_fp.seek(0)
     write_mo(catalog2_fp, catalog2)
     catalog2_fp.seek(0)
     translations1 = support.Translations(catalog1_fp)
     translations2 = support.Translations(catalog2_fp, domain='messages1')
     self.translations = translations1.add(translations2, merge=False)
Example #3
0
    def test_sorting(self):
        # Ensure the header is sorted to the first entry so that its charset
        # can be applied to all subsequent messages by GNUTranslations
        # (ensuring all messages are safely converted to unicode)
        catalog = Catalog(locale='en_US')
        catalog.add('', '''\
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n''')
        catalog.add('foo', 'Voh')
        catalog.add(('There is', 'There are'), ('Es gibt', 'Es gibt'))
        catalog.add('Fizz', '')
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
        buf = BytesIO()
        mofile.write_mo(buf, catalog)
        buf.seek(0)
        translations = gettext.GNUTranslations(fp=buf)
        self.assertEqual('Voh', translations.ugettext('foo'))
        assert isinstance(translations.ugettext('foo'), text_type)
        self.assertEqual('Es gibt', translations.ungettext('There is', 'There are', 1))
        assert isinstance(translations.ungettext('There is', 'There are', 1), text_type)
        self.assertEqual('Fizz', translations.ugettext('Fizz'))
        assert isinstance(translations.ugettext('Fizz'), text_type)
        self.assertEqual('Fuzz', translations.ugettext('Fuzz'))
        assert isinstance(translations.ugettext('Fuzz'), text_type)
        self.assertEqual('Fuzzes', translations.ugettext('Fuzzes'))
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
Example #4
0
    def obtainFilter(self, processing, uri):
        """
        Checks the filter URI.
        
        @param processing: Processing
            The processing used for delivering the request.
        @param uri: string
            The URI to call, parameters are allowed.
        @return: tuple(boolean|None, integer, string)
            A tuple containing as the first True if the filter URI provided a True value, None if the filter cannot be fetched,
            on the second position the response status and on the last position the response text.
        """
        assert isinstance(processing, Processing), "Invalid processing %s" % processing
        assert isinstance(uri, str), "Invalid URI %s" % uri

        request = processing.ctx.request()
        assert isinstance(request, RequestFilter), "Invalid request %s" % request

        url = urlparse(uri)
        request.scheme, request.method = self.scheme, HTTP_GET
        request.headers = {}
        request.uri = url.path.lstrip("/")
        request.parameters = parse_qsl(url.query, True, False)
        request.accTypes = [self.mimeTypeJson]
        request.accCharSets = [self.encodingJson]

        chain = Chain(processing)
        chain.process(
            request=request,
            requestCnt=processing.ctx.requestCnt(),
            response=processing.ctx.response(),
            responseCnt=processing.ctx.responseCnt(),
        ).doAll()

        response, responseCnt = chain.arg.response, chain.arg.responseCnt
        assert isinstance(response, ResponseHTTP), "Invalid response %s" % response
        assert isinstance(responseCnt, ResponseContentHTTP), "Invalid response content %s" % responseCnt

        if ResponseHTTP.text in response and response.text:
            text = response.text
        elif ResponseHTTP.code in response and response.code:
            text = response.code
        else:
            text = None
        if (
            ResponseContentHTTP.source not in responseCnt
            or responseCnt.source is None
            or not isSuccess(response.status)
        ):
            return None, response.status, text

        if isinstance(responseCnt.source, IInputStream):
            source = responseCnt.source
        else:
            source = BytesIO()
            for bytes in responseCnt.source:
                source.write(bytes)
            source.seek(0)
        allowed = json.load(codecs.getreader(self.encodingJson)(source))
        return allowed["HasAccess"] == "True", response.status, text
Example #5
0
    def test_sorting(self):
        # Ensure the header is sorted to the first entry so that its charset
        # can be applied to all subsequent messages by GNUTranslations
        # (ensuring all messages are safely converted to unicode)
        catalog = Catalog(locale='en_US')
        catalog.add(
            u(''), '''\
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n''')
        catalog.add(u('foo'), 'Voh')
        catalog.add((u('There is'), u('There are')),
                    (u('Es gibt'), u('Es gibt')))
        catalog.add(u('Fizz'), '')
        catalog.add(('Fuzz', 'Fuzzes'), ('', ''))
        buf = BytesIO()
        mofile.write_mo(buf, catalog)
        buf.seek(0)
        translations = gettext.GNUTranslations(fp=buf)
        self.assertEqual(u('Voh'), translations.ugettext('foo'))
        assert isinstance(translations.ugettext('foo'), text_type)
        self.assertEqual(u('Es gibt'),
                         translations.ungettext('There is', 'There are', 1))
        assert isinstance(translations.ungettext('There is', 'There are', 1),
                          text_type)
        self.assertEqual(u('Fizz'), translations.ugettext('Fizz'))
        assert isinstance(translations.ugettext('Fizz'), text_type)
        self.assertEqual(u('Fuzz'), translations.ugettext('Fuzz'))
        assert isinstance(translations.ugettext('Fuzz'), text_type)
        self.assertEqual(u('Fuzzes'), translations.ugettext('Fuzzes'))
        assert isinstance(translations.ugettext('Fuzzes'), text_type)
Example #6
0
 def setUp(self):
     # Use a locale which won't fail to run the tests
     os.environ['LANG'] = 'en_US.UTF-8'
     messages1 = [
         ('foo', {
             'string': 'Voh'
         }),
         ('foo', {
             'string': 'VohCTX',
             'context': 'foo'
         }),
         (('foo1', 'foos1'), {
             'string': ('Voh1', 'Vohs1')
         }),
         (('foo1', 'foos1'), {
             'string': ('VohCTX1', 'VohsCTX1'),
             'context': 'foo'
         }),
     ]
     messages2 = [
         ('foo', {
             'string': 'VohD'
         }),
         ('foo', {
             'string': 'VohCTXD',
             'context': 'foo'
         }),
         (('foo1', 'foos1'), {
             'string': ('VohD1', 'VohsD1')
         }),
         (('foo1', 'foos1'), {
             'string': ('VohCTXD1', 'VohsCTXD1'),
             'context': 'foo'
         }),
     ]
     catalog1 = Catalog(locale='en_GB', domain='messages')
     catalog2 = Catalog(locale='en_GB', domain='messages1')
     for ids, kwargs in messages1:
         catalog1.add(ids, **kwargs)
     for ids, kwargs in messages2:
         catalog2.add(ids, **kwargs)
     catalog1_fp = BytesIO()
     catalog2_fp = BytesIO()
     write_mo(catalog1_fp, catalog1)
     catalog1_fp.seek(0)
     write_mo(catalog2_fp, catalog2)
     catalog2_fp.seek(0)
     translations1 = support.Translations(catalog1_fp)
     translations2 = support.Translations(catalog2_fp, domain='messages1')
     self.translations = translations1.add(translations2, merge=False)