Beispiel #1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        label = Text(''.join(
            map(lambda x: '<p>%s</p>' % x, [
                'Version: %s' % about.version(),
                'Copyright 2019-%s <a href="twitter.com/bartFeenstra">Bart Feenstra</a> & contributors. Betty is made available to you under the <a href="https://www.gnu.org/licenses/gpl-3.0.en.html">GNU General Public License, Version 3</a> (GPLv3).'
                % datetime.now().year,
                'Follow Betty on <a href="https://twitter.com/Betty_Project">Twitter</a> and <a href="https://github.com/bartfeenstra/betty">Github</a>.'
            ])))
        label.setAlignment(QtCore.Qt.AlignCenter)
        self.setCentralWidget(label)
Beispiel #2
0
def build_specification(app: App) -> Dict:
    specification = {
        'openapi': '3.0.0',
        'servers': [
            {
                'url': app.static_url_generator.generate('/', absolute=True),
            }
        ],
        'info': {
            'title': 'Betty',
            'version': about.version()
        },
        'paths': {},
        'components': {
            'responses': {
                '401': {
                    'description': _('Unauthorized'),
                    'content': {
                        'application/json': {
                            'schema': {
                                '$ref': '#/components/schemas/betty/error',
                            },
                        },
                    },
                },
                '403': {
                    'description': _('Forbidden'),
                    'content': {
                        'application/json': {
                            'schema': {
                                '$ref': '#/components/schemas/betty/error',
                            },
                        },
                    },
                },
                '404': {
                    'description': _('Not found'),
                    'content': {
                        'application/json': {
                            'schema': {
                                '$ref': '#/components/schemas/betty/error',
                            },
                        },
                    },
                },
            },
            'parameters': {
                'id': {
                    'name': 'id',
                    'in': 'path',
                    'required': True,
                    'description': _('The ID for the resource to retrieve.'),
                    'schema': {
                        'type': 'string',
                    },
                },
            },
            'headers': {
                'Content-Language': {
                    'description': _('An HTTP [Content-Language](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Language) header.'),
                    'schema': {
                        'type': 'string',
                    },
                    'example': app.configuration.locales.default.locale,
                },
            },
            'schemas': {
                'betty': {
                    '$ref': app.static_url_generator.generate('/schema.json#/definitions'),
                },
            },
        },
    }

    # Add resource operations.
    for resource in _get_resources():
        if app.configuration.content_negotiation:
            collection_path = '/%s/' % resource.name
            single_path = '/%s/{id}/' % resource.name
        else:
            collection_path = '/{locale}/%s/index.json' % resource.name
            single_path = '/{locale}/%s/{id}/index.json' % resource.name
        specification['paths'].update({
            collection_path: {
                'get': {
                    'summary': resource.collection_endpoint_summary,
                    'responses': {
                        '200': {
                            'description': resource.collection_response_description,
                            'content': {
                                'application/json': {
                                    'schema': {
                                        '$ref': '#/components/schemas/betty/%sCollection' % resource.name,
                                    },
                                },
                            },
                        },
                    },
                },
            },
            single_path: {
                'get': {
                    'summary': resource.single_endpoint_summary,
                    'responses': {
                        '200': {
                            'description': resource.single_response_description,
                            'content': {
                                'application/json': {
                                    'schema': {
                                        '$ref': '#/components/schemas/betty/%s' % resource.name,
                                    },
                                },
                            },
                        },
                    },
                },
            },
        })

    # Add components for content negotiation.
    if app.configuration.content_negotiation:
        specification['components']['parameters']['Accept'] = {
            'name': 'Accept',
            'in': 'header',
            'description': _('An HTTP [Accept](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept) header.'),
            'schema': {
                'enum': list(EXTENSIONS.keys()),
            },
        }
        specification['components']['parameters']['Accept-Language'] = {
            'name': 'Accept-Language',
            'in': 'header',
            'description': _('An HTTP [Accept-Language](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Language) header.'),
            'schema': {
                'type': 'string',
            },
            'example': app.configuration.locales[app.configuration.locales.default.locale].alias,
        }
        specification['components']['schemas']['html'] = {
            'type': 'string',
            'description': _('An HTML5 document.'),
        }
    else:
        specification['components']['parameters']['locale'] = {
            'name': 'locale',
            'in': 'path',
            'required': True,
            'description': _('A locale name.'),
            'schema': {
                'type': 'string',
                'enum': [locale_configuration.locale for locale_configuration in app.configuration.locales],
            },
            'example': app.configuration.locales[app.configuration.locales.default.locale].alias,
        }

    # Add default behavior to all requests.
    for path in specification['paths']:
        specification['paths'][path]['get']['responses'].update({
            '401': {
                '$ref': '#/components/responses/401',
            },
            '403': {
                '$ref': '#/components/responses/403',
            },
            '404': {
                '$ref': '#/components/responses/404',
            },
        })
        if app.configuration.content_negotiation:
            specification['paths'][path]['parameters'] = [
                {
                    '$ref': '#/components/parameters/Accept',
                },
                {
                    '$ref': '#/components/parameters/Accept-Language',
                },
            ]
        else:
            specification['paths'][path]['parameters'] = [
                {
                    '$ref': '#/components/parameters/locale',
                },
            ]

    # Add default behavior to all responses.
    if app.configuration.content_negotiation:
        responses = list(specification['components']['responses'].values())
        for path in specification['paths']:
            responses.append(
                specification['paths'][path]['get']['responses']['200'])
        for response in responses:
            response['content']['text/html'] = {
                'schema': {
                    '$ref': '#/components/schemas/html'
                }
            }
            if 'headers' not in response:
                response['headers'] = {}
            response['headers']['Content-Language'] = {
                '$ref': '#/components/headers/Content-Language',
            }

    return specification
Beispiel #3
0
 def test(self):
     self.assertIsInstance(about.version(), str)
Beispiel #4
0
        subprocess.run(sys.argv, env=env)

    return _with_utf8


@ensure_utf8
@click.command(cls=_BettyCommands)
@click.option(
    '--configuration',
    '-c',
    'app',
    is_eager=True,
    help=
    'The path to a Betty configuration file. Defaults to betty.json|yaml|yml in the current working directory. This will make additional commands available.',
    callback=_init_ctx)
@click.version_option(about.version(), prog_name='Betty')
def main(app):
    pass


@click.command(help='Clear all caches.')
@global_command
async def _clear_caches():
    with suppress(FileNotFoundError):
        shutil.rmtree(betty._CACHE_DIRECTORY_PATH)
    logging.getLogger().info('All caches cleared.')


@click.command(help='Explore a demonstration site.')
@global_command
async def _demo():