Exemple #1
0
    def test_basics(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app, default_locale='de_DE')

        with app.test_request_context():
            assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!'
            assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 3) == u'3 Äpfel'
            assert ngettext(u'%(num)s Apple', u'%(num)s Apples', 1) == u'1 Apfel'
Exemple #2
0
    def test_basics(self):
        app = flask.Flask(__name__)
        b = babel.Babel(app, default_locale='de_DE')

        with app.test_request_context():
            assert gettext(u'Hello %(name)s!', name='Peter') == 'Hallo Peter!'
            assert ngettext(u'%(num)s Apple', u'%(num)s Apples',
                            3) == u'3 Äpfel'
            assert ngettext(u'%(num)s Apple', u'%(num)s Apples',
                            1) == u'1 Apfel'
Exemple #3
0
 def display(self,count=None):
     if len(self.values) == 0: return
     print()
     printheader(self.title) 
     for key,itemlist in sorted(self.values.items()):
         linenums = [str(item.linenum) for item in itemlist]
         if len(linenums) > 10:
             linenums = linenums[0:10] 
         links = [f"<a href='#' class='gedcomlink'>{linenum}</a>" for linenum in linenums]
         txt = ngettext(
             "count=%(num)d, line %(lines)s",
             "count=%(num)d, lines %(lines)s",
              num=len(itemlist), lines=', '.join(links))
         if len(itemlist) > len(linenums): txt += ",..."
         printitem(f"<b>{key:25}</b><td>({txt})")
     if count is not None:
         if count > len(self.values): printitem(f"<b>...")
         printitem("<b>" + _("Count") +f": {count}")
     printtrailer() 
Exemple #4
0
def search():
    """View the results of a search."""
    start_time = time.process_time()

    if current_app.config['KERKO_USE_TRANSLATIONS']:
        babel_domain.as_default()

    criteria = Criteria(request)
    form = SearchForm(csrf_enabled=False)
    if form.validate_on_submit():
        url = criteria.build_add_keywords_url(scope=form.scope.data,
                                              value=form.keywords.data)
        return redirect(url, 302)

    search_results, facet_results, total_count, page_count, last_sync = run_query(
        criteria, get_search_return_fields(criteria))

    breadbox = build_breadbox(criteria, facet_results)
    context = {
        'facet_results':
        facet_results,
        'breadbox':
        breadbox,
        'active_facets':
        breadbox['filters'].keys() if 'filters' in breadbox else [],
        'pager':
        build_pager(criteria, page_count, criteria.page_len),
        'sorter':
        build_sorter(criteria),
        'total_count':
        total_count,
        'total_count_formatted':
        format_number(total_count, locale=get_locale()),
        'page_count':
        page_count,
        'page_count_formatted':
        format_number(page_count, locale=get_locale()),
        'page_len':
        criteria.page_len,
        'is_searching':
        criteria.has_keyword_search() or criteria.has_filter_search(),
        'locale':
        get_locale(),
        'last_sync':
        datetime.fromtimestamp(last_sync,
                               tz=datetime.now().astimezone().tzinfo).strftime(
                                   '%Y-%m-%d %H:%M %Z') if last_sync else '',
    }

    if criteria.page_len == 1 and total_count != 0:
        list_page_num = int((criteria.page_num - 1) /
                            current_app.config['KERKO_PAGE_LEN'] + 1)
        build_creators_display(search_results[0])
        build_item_facet_results(search_results[0])
        return render_template(
            current_app.config['KERKO_TEMPLATE_SEARCH_ITEM'],
            title=search_results[0].get('data', {}).get('title', ''),
            item=search_results[0],
            item_url=url_for('.item_view',
                             item_id=search_results[0]['id'],
                             _external=True) if search_results[0] else '',
            back_url=criteria.build_url(page_num=list_page_num),
            time=time.process_time() - start_time,
            **context)

    if total_count > 0:
        for i, result in enumerate(search_results):
            result['url'] = criteria.build_url(
                page_num=(criteria.page_num - 1) * (criteria.page_len or 0) +
                i + 1,
                page_len=1)
        if context['is_searching']:
            context['title'] = ngettext('Result', 'Results', total_count)
        else:
            context['title'] = gettext('Full bibliography')
    else:
        context['title'] = gettext('Your search did not match any resources')
    return render_template(
        current_app.config['KERKO_TEMPLATE_SEARCH'],
        form=form,
        search_results=search_results,
        print_url=criteria.build_url(page_len='all', print_preview=True),
        print_preview=criteria.print_preview,
        download_urls={
            key: criteria.build_download_url(key)
            for key in
            current_app.config['KERKO_COMPOSER'].citation_formats.keys()
        },
        time=time.process_time() - start_time,
        **context)
Exemple #5
0
def process_gedcom(arglist, transform_module):
    """ Implements a mechanism for Gedcom transforms.
    
    Returns a dictionary:
        - stdout        result texts for the user log page
        - stderr        errors texts for the user log page 
        - oldname       original name of gedcom file
        - logfile       log file name

    The transform_module is assumed to contain the following methods:
    - initialize
    - transform: implements the actual transformation for a single line block ("item")
    - fixlines: preprocesses the Gedcom contents (list of lines/strings)
    - add_args: adds the transform-specific arguments (ArgumentParser style)

    See sukujutut.py as an example
    """

    msg = _("Transform '{}' started at {}").format(transform_module.name,
                                                   util.format_timestamp())
    LOG.info(f"------ {msg} ------")

    parser = ArgumentParser()
    #    parser.add_argument('transform', help="Name of the transform (Python module)")
    parser.add_argument('input_gedcom',
                        help=_("Name of the input GEDCOM file"))
    parser.add_argument('--logfile',
                        help=_("Name of the log file"),
                        default="_LOGFILE")
    #    parser.add_argument('--output_gedcom', help="Name of the output GEDCOM file; this file will be created/overwritten" )
    parser.add_argument('--display-changes',
                        action='store_true',
                        help=_('Display changed rows'))
    parser.add_argument('--dryrun',
                        action='store_true',
                        help=_('Do not produce an output file'))
    parser.add_argument('--nolog',
                        action='store_true',
                        help=_('Do not produce a log in the output file'))
    parser.add_argument('--encoding',
                        type=str,
                        default="UTF-8",
                        choices=["UTF-8", "UTF-8-SIG", "ISO8859-1"],
                        help=_("Encoding of the input GEDCOM"))
    transform_module.add_args(parser)
    args = parser.parse_args(arglist)
    args.output_gedcom = None
    args.nolog = True  # replaced by history file
    gedcom_utils.history_append(args.input_gedcom, "\n" + msg)
    gedcom_utils.history_append_args(args)

    # You may deny stdout redirect by setting GEDCOM_REDIRECT_SYSOUT=False in config.py
    if not 'GEDCOM_REDIRECT_SYSOUT' in globals():
        GEDCOM_REDIRECT_SYSOUT = True
    try:
        gedcom_utils.init_log(args.logfile)
        with Output(args) as out:
            out.original_line = None
            out.transform_name = transform_module.__name__
            if GEDCOM_REDIRECT_SYSOUT:
                saved_stdout = sys.stdout
                saved_stderr = sys.stderr
                sys.stdout = io.StringIO()
                sys.stderr = io.StringIO()
            if args.dryrun:
                old_name = ""
            else:
                old_name = out.new_name

            print(f"<h3>------ {msg} ------</h3>")
            t = transformer.Transformer(
                transform_module=transform_module,
                display_callback=gedcom_utils.display_changes,
                options=args)
            """ Create a Gedcom transformer g from Transformer t and execute
                the transformations.
                
                The resulting Items are in the list g.items 
            """
            g = t.transform_file(args.input_gedcom)
            g.print_items(out)
            print("<div>")
            print("<b>------ " + ngettext(
                "Total {num} change", "Total {num} changes",
                num=t.num_changes).format(num=t.num_changes) + "</b>")
            #print(_("------ Number of changes: {}").format(t.num_changes))
    except:
        traceback.print_exc()
    finally:
        if old_name:
            gedcom_utils.history_append(
                args.input_gedcom,
                _('File saved as {}').format(args.input_gedcom))
            gedcom_utils.history_append(
                args.input_gedcom,
                _("Old file saved as {}").format(old_name))
        else:
            gedcom_utils.history_append(
                args.input_gedcom,
                _("File saved as {}").format(args.input_gedcom + "temp"))
            msg = _("Transform '{}' ended at {}").format(
                transform_module.name, util.format_timestamp())
        gedcom_utils.history_append(args.input_gedcom, msg)
        print(f"<h3>------ {msg} ------</h3>")
        print("</div>")
        output = None
        errors = None
        if GEDCOM_REDIRECT_SYSOUT:
            output = sys.stdout.getvalue()
            errors = sys.stderr.getvalue()
            sys.stdout = saved_stdout
            sys.stderr = saved_stderr
    if old_name:
        old_basename = os.path.basename(old_name)
    else:
        old_basename = ""
    if errors and old_basename:
        os.rename(old_name, args.input_gedcom)
        old_basename = ""
    rsp = dict(stdout=output,
               stderr=errors,
               oldname=old_basename,
               logfile=args.logfile)
    if hasattr(transform_module, "output_format") \
       and transform_module.output_format == "plain_text":
        rsp["plain_text"] = True
    return rsp
Exemple #6
0
def search():
    """View the results of a search."""
    start_time = time.process_time()

    if current_app.config['KERKO_USE_TRANSLATIONS']:
        babel_domain.as_default()

    criteria = Criteria(request)
    form = SearchForm(csrf_enabled=False)
    if form.validate_on_submit():
        url = criteria.build_add_keywords_url(
            scope=form.scope.data,
            value=form.keywords.data)
        return redirect(url, 302)

    search_results, facet_results, total_count, page_count, last_sync = run_query(
        criteria, get_search_return_fields(criteria.page_len)
    )

    if criteria.page_len == 1 and criteria.id and (
        total_count == 0 or criteria.id != search_results[0]['id']
    ):
        # The search result page no longer points to the desired item.
        return redirect(url_for('.item_view', item_id=criteria.id, _external=True), 301)

    criteria.fit_pager(page_count)
    breadbox = build_breadbox(criteria, facet_results)
    pager_sections = get_pager_sections(criteria.page_num, page_count)
    context = {
        'facet_results': facet_results,
        'breadbox': breadbox,
        'active_facets': breadbox['filters'].keys() if 'filters' in breadbox else [],
        'sorter': build_sorter(criteria),
        'total_count': total_count,
        'total_count_formatted': format_number(total_count, locale=get_locale()),
        'page_count': page_count,
        'page_count_formatted': format_number(page_count, locale=get_locale()),
        'page_len': criteria.page_len,
        'show_abstracts': criteria.show_abstracts,
        'abstracts_toggler_url': criteria.build_url(
            show_abstracts=not criteria.show_abstracts, page_num=criteria.page_num
        ),
        'is_searching': criteria.has_keyword_search() or criteria.has_filter_search(),
        'locale': get_locale(),
        'last_sync': datetime.fromtimestamp(
            last_sync, tz=datetime.now().astimezone().tzinfo
        ).strftime('%Y-%m-%d %H:%M %Z') if last_sync else '',
    }

    if criteria.page_len == 1 and total_count != 0:
        # Retrieve item ids corresponding to individual result page numbers.
        page_kwargs = {}
        page_criteria = copy.deepcopy(criteria)
        for page_num in get_page_numbers(pager_sections):
            if page_num == criteria.page_num:
                # We already know the current page's item id. No further query necessary.
                page_kwargs[page_num] = {'id_': search_results[0]['id']}
            else:
                # Run a search query to get the item id corresponding to the page number.
                page_criteria.page_num = page_num
                page_search_results, _, _, _, _ = run_query(
                    page_criteria, return_fields=['id'], query_facets=False
                )
                if page_search_results:
                    page_kwargs[page_num] = {'id_': page_search_results[0]['id']}
        context['pager'] = build_pager(pager_sections, criteria, page_kwargs)

        list_page_num = int((criteria.page_num - 1) / current_app.config['KERKO_PAGE_LEN'] + 1)
        build_creators_display(search_results[0])
        build_item_facet_results(search_results[0])
        build_relations(
            search_results[0],
            get_search_return_fields(page_len=None, exclude=['coins']),
            sort=current_app.config['KERKO_RELATIONS_SORT']
        )
        return render_template(
            current_app.config['KERKO_TEMPLATE_SEARCH_ITEM'],
            title=search_results[0].get('data', {}).get('title', ''),
            item=search_results[0],
            item_url=url_for(
                '.item_view', item_id=search_results[0]['id'], _external=True
            ) if search_results[0] else '',
            back_url=criteria.build_url(page_num=list_page_num),
            time=time.process_time() - start_time,
            **context
        )

    if total_count > 0:
        context['pager'] = build_pager(pager_sections, criteria)
        search_results_urls = [
            criteria.build_url(
                page_num=(criteria.page_num - 1) * (criteria.page_len or 0) + i + 1,
                page_len=1,
                id_=result['id'],
            ) for i, result in enumerate(search_results)
        ]
        search_results = zip(search_results, search_results_urls)
        if context['is_searching']:
            context['title'] = ngettext('Result', 'Results', total_count)
        else:
            context['title'] = gettext('Full bibliography')
    else:
        context['title'] = gettext('Your search did not match any resources')
    return render_template(
        current_app.config['KERKO_TEMPLATE_SEARCH'],
        form=form,
        search_results=search_results,
        print_url=criteria.build_url(page_len='all', print_preview=True),
        print_preview=criteria.print_preview,
        download_urls={
            key: criteria.build_download_url(key)
            for key in current_app.config['KERKO_COMPOSER'].citation_formats.keys()
        },
        time=time.process_time() - start_time,
        **context
    )
Exemple #7
0
def search():
    start_time = time.process_time()

    if current_app.config['KERKO_USE_TRANSLATIONS']:
        babel_domain.as_default()
    criteria = Criteria(request)

    form = SearchForm()
    if form.validate_on_submit():
        url = criteria.build_add_keywords_url(scope=form.scope.data,
                                              value=form.keywords.data)
        return redirect(url, 302)

    if criteria.page_len != 1:  # Note: page_len can be None (for all results).
        return_fields = ['id', 'bib', 'coins']
        if current_app.config['KERKO_RESULTS_ABSTRACT']:
            return_fields.append('data')
    else:
        return_fields = None  # All fields

    search_results, facet_results, total_count, page_count = run_query(
        criteria, return_fields)

    breadbox = build_breadbox(criteria, facet_results)
    context = {
        'facet_results':
        facet_results,
        'breadbox':
        breadbox,
        'active_facets':
        breadbox['filters'].keys() if 'filters' in breadbox else [],
        'pager':
        build_pager(criteria, page_count, criteria.page_len),
        'sorter':
        build_sorter(criteria),
        'total_count':
        total_count,
        'total_count_formatted':
        format_number(total_count, locale=get_locale()),
        'page_count':
        page_count,
        'page_count_formatted':
        format_number(page_count, locale=get_locale()),
        'page_len':
        criteria.page_len,
        'is_searching':
        criteria.has_keyword_search() or criteria.has_filter_search(),
        'locale':
        get_locale(),
    }

    if criteria.page_len == 1 and total_count != 0:
        list_page_num = int((criteria.page_num - 1) /
                            current_app.config['KERKO_PAGE_LEN'] + 1)
        build_creators_display(search_results[0])
        build_fake_facet_results(search_results[0])
        return render_template(
            'kerko/search-item.html.jinja2',
            title=search_results[0].get('data', {}).get('title', ''),
            item=search_results[0],
            item_url=url_for('.item',
                             item_id=search_results[0]['id'],
                             _external=True) if search_results[0] else '',
            back_url=criteria.build_url(page_num=list_page_num),
            time=time.process_time() - start_time,
            **context)

    if total_count > 0:
        for i, result in enumerate(search_results):
            result['url'] = criteria.build_url(
                page_num=(criteria.page_num - 1) * (criteria.page_len or 0) +
                i + 1,
                page_len=1)
        if context['is_searching']:
            context['title'] = ngettext('Result', 'Results', total_count)
        else:
            context['title'] = gettext('Full bibliography')
    else:
        context['title'] = gettext('Your search did not match any resources')
    return render_template('kerko/search.html.jinja2',
                           form=form,
                           search_results=search_results,
                           print_url=criteria.build_url(page_len='all',
                                                        print_preview=True),
                           print_preview=criteria.print_preview,
                           time=time.process_time() - start_time,
                           **context)