コード例 #1
0
class NewQuery:

    new_query: Callable[[OutData], MenuItemInternal] = field(
        default_factory=lambda: lambda args: MenuItemInternal(
            MainMenu.NEW_QUERY('new-query'), te('Concordance'), 'query').
        add_args(('corpname', args['corpname']), ('usesubcorp', args[
            'usesubcorp']), *[('align', v) for v in args['align']]
                 if len(args['align']) else [('align', None)]).mark_indirect())

    pquery: MenuItemInternal = field(
        default_factory=lambda: lambda args: MenuItemInternal(
            MainMenu.NEW_QUERY('paradigmatic-query'), te('Paradigmatic query'),
            'pquery/index').add_args(('corpname', args['corpname']), (
                'usesubcorp', args['usesubcorp'])).mark_indirect())

    word_list: HideOnCustomCondItem = field(
        default_factory=lambda: lambda args: HideOnCustomCondItem(
            MainMenu.NEW_QUERY('wordlist'), te('Word List'), 'wordlist/form'
        ).add_args(('corpname', args['corpname']),
                   ('include_nonwords', 1)).mark_indirect())

    recent_queries: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.NEW_QUERY('history'),
            te('Recent queries'),
            'MAIN_MENU_SHOW_QUERY_HISTORY',
            key_code=72,
            key_mod='shift'  # key = 'h'
        ).mark_indirect())
コード例 #2
0
class Filter:

    filter_pos: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.FILTER('positive'),
            te('Positive'),
            'MAIN_MENU_SHOW_FILTER',
            key_code=70  # key = 'f'
        ).add_args(('pnfilter', 'p')).mark_indirect())

    filter_neg: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.FILTER('negative'), te('Negative'),
            'MAIN_MENU_SHOW_FILTER').add_args(
                ('pnfilter', 'n')).mark_indirect())

    filter_subhits: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.FILTER('subhits'), te('Remove nested matches'),
            'MAIN_MENU_FILTER_APPLY_SUBHITS_REMOVE'))

    filter_each_first: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.FILTER('each-first'), te('First hits in documents'),
            'MAIN_MENU_FILTER_APPLY_FIRST_OCCURRENCES').enable_if(
                lambda d: len(d.get('aligned_corpora', [])) == 0))
コード例 #3
0
class View:
    view_mode_switch: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.VIEW('kwic-sent-switch'),
            te('KWIC/Sentence'),
            'CONCORDANCE_SWITCH_KWIC_SENT_MODE',
            key_code=86)  # key = 'v'
    )

    view_structs_attrs: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.VIEW('structs-attrs'),
            te('Corpus-specific settings'),
            'MAIN_MENU_SHOW_ATTRS_VIEW_OPTIONS',
            key_code=79  # key = 'o'
        ).mark_corpus_dependent().mark_indirect())

    view_global: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.VIEW('global-options'),
            te('General view options'),
            'MAIN_MENU_SHOW_GENERAL_VIEW_OPTIONS',
            key_code=79,
            key_mod='shift'  # key = 'o'
        ).mark_indirect())
コード例 #4
0
class Frequency:

    freq_lemmas: Callable[[OutData], ConcMenuItem] = field(
        default_factory=lambda: lambda args: ConcMenuItem(
            MainMenu.FREQUENCY('lemmas'), te('Lemmas'), 'freqs').add_args(
                ('fcrit', 'lemma/e 0~0>0'), ('freq_type', 'tokens')).enable_if(
                    lambda d: 'lemma' in
                    [x['n'] for x in args.get('AttrList', ())]))

    freq_node_forms_i: ConcMenuItem = field(
        default_factory=lambda: ConcMenuItem(MainMenu.FREQUENCY('node-forms'),
                                             te('Node forms') + ' [A=a]',
                                             'freqs',
                                             hint=te('case insensitive')).
        add_args(('fcrit', 'word/ie 0~0>0'), ('freq_type', 'tokens')))

    freq_doc_ids: Callable[[OutData], ConcMenuItem] = field(
        default_factory=lambda: lambda args: ConcMenuItem(
            MainMenu.FREQUENCY('doc-ids'), te('Doc IDs'), 'freqs').add_args(
                ('fcrit', args['fcrit_shortref']), ('freq_type', 'tokens')).
        enable_if(lambda d: 'fcrit_shortref' in args and '.' in args[
            'fcrit_shortref'].split('/')[0]))

    @staticmethod
    def _create_text_types(args):
        fcrit_tmp = args.get('ttcrit', [])
        if len(fcrit_tmp) > 0:
            fcrit_args = [('fcrit', fcrit_tmp[0])]
            fcrit_async_args = [('fcrit_async', f) for f in fcrit_tmp[1:]]
        else:
            fcrit_args = []
            fcrit_async_args = []
        return ConcMenuItem(
            MainMenu.FREQUENCY('text-types'), te('Text Types'),
            'freqs').add_args(*fcrit_args).add_args(
                *fcrit_async_args).add_args(
                    ('freq_type',
                     'text-types')).enable_if(lambda d: bool(d['ttcrit']))

    freq_text_types: Callable[[OutData], ConcMenuItem] = field(
        default_factory=lambda: Frequency._create_text_types)

    freq_custom: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.FREQUENCY('custom'),
            te('Custom'),
            'MAIN_MENU_SHOW_FREQ_FORM',
            key_code=70,
            key_mod='shift'  # key = 'f'
        ).mark_indirect())
コード例 #5
0
def _create_archive_conc_item(args):
    if args['explicit_conc_persistence_ui']:
        return EventTriggeringItem(
            MainMenu.CONCORDANCE('archive-conc'), te('Permanent link'),
            'MAIN_MENU_MAKE_CONC_LINK_PERSISTENT').mark_indirect().enable_if(
                lambda d: d.get('user_owns_conc', False))
    else:
        return None
コード例 #6
0
class Collocations:
    colloc_custom: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.COLLOCATIONS('custom'),
            te('Custom'),
            'MAIN_MENU_SHOW_COLL_FORM',
            key_code=67,
            key_mod='shift'  # key = 'c'
        ).mark_indirect())
コード例 #7
0
class Corpora:

    avail_corpora: MenuItemInternal = field(
        default_factory=lambda: MenuItemInternal(
            MainMenu.CORPORA('avail-corpora'), te('Available corpora'),
            'corpora/corplist').mark_indirect())

    my_subcorpora: MenuItemInternal = field(
        default_factory=lambda: MenuItemInternal(
            MainMenu.CORPORA('my-subcorpora'), te('My subcorpora'),
            'subcorpus/list').mark_indirect())

    public_subcorpora: MenuItemInternal = field(
        default_factory=lambda: MenuItemInternal(
            MainMenu.CORPORA('public-subcorpora'), te('Public subcorpora'),
            'subcorpus/list_published'))

    create_subcorpus: Callable[[OutData], MenuItemInternal] = field(
        default_factory=lambda: lambda args: MenuItemInternal(
            MainMenu.CORPORA('create-subcorpus'), te('Create new subcorpus'),
            'subcorpus/new').add_args(
                ('corpname', args['corpname'])).mark_indirect())
コード例 #8
0
 def _create_text_types(args):
     fcrit_tmp = args.get('ttcrit', [])
     if len(fcrit_tmp) > 0:
         fcrit_args = [('fcrit', fcrit_tmp[0])]
         fcrit_async_args = [('fcrit_async', f) for f in fcrit_tmp[1:]]
     else:
         fcrit_args = []
         fcrit_async_args = []
     return ConcMenuItem(
         MainMenu.FREQUENCY('text-types'), te('Text Types'),
         'freqs').add_args(*fcrit_args).add_args(
             *fcrit_async_args).add_args(
                 ('freq_type',
                  'text-types')).enable_if(lambda d: bool(d['ttcrit']))
コード例 #9
0
class ConcordanceDefault:

    curr_conc: ConcMenuItem = field(default_factory=lambda: ConcMenuItem(
        MainMenu.CONCORDANCE('current-concordance'), te('Current concordance'),
        'view'))

    sorting: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('sorting'),
            te('Sorting'),
            'MAIN_MENU_SHOW_SORT',
            key_code=83).mark_indirect())

    shuffle: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('shuffle'), te('Shuffle'),
            'MAIN_MENU_APPLY_SHUFFLE'))

    sample: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('sample'),
            te('Sample'),
            'MAIN_MENU_SHOW_SAMPLE',
            key_code=77).mark_indirect())

    query_overview: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('query-overview'), te('Query overview'),
            'MAIN_MENU_OVERVIEW_SHOW_QUERY_INFO'))

    query_save_as: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('query-save-as'), te('Archive query'),
            'MAIN_MENU_SHOW_SAVE_QUERY_AS_FORM').mark_indirect().enable_if(
                lambda d: d.get('user_owns_conc', False)))

    # we need lazy evaluation for archive_conc (all the result args must be ready)
    archive_conc: Callable[[OutData], Optional[EventTriggeringItem]] = field(
        default_factory=lambda: _create_archive_conc_item)

    query_undo: EventTriggeringItem = field(
        default_factory=lambda: EventTriggeringItem(
            MainMenu.CONCORDANCE('undo'), te('Undo'),
            'MAIN_MENU_UNDO_LAST_QUERY_OP').enable_if(lambda d: len(
                d.get('undo_q', [])) > 0))
コード例 #10
0
ファイル: main_menu.py プロジェクト: svitalsky/kontext
    def generate(self, disabled_items, save_items, corpus_dependent, ui_lang):
        """
        Generate menu items based on current
        action and user state.

        Args:

        disabled_items (list of MainMenuItemId): a list of items
        save
        """

        def custom_menu_items(section):
            return [item.to_dict() for item in plugins.runtime.MENU_ITEMS.instance.get_items(section.name, lang=ui_lang)]

        def is_disabled(menu_item):
            if isinstance(menu_item, MainMenuItemId):
                item_id = menu_item
                item = None
            elif isinstance(menu_item, AbstractMenuItem):
                item_id = menu_item.ident
                item = menu_item
            else:
                raise ValueError()
            if corpus_dependent is False and item is not None and item.corpus_dependent is True:
                return True
            for item in disabled_items:
                if item_id.matches(item):
                    return True
            return False

        def exp(section, *args):
            ans = []
            for item in args:
                if callable(item):
                    item = item()
                if item:
                    ans.append(item.filter_empty_args().set_disabled(
                        is_disabled(item)).create(self._args))
            return tuple(ans + custom_menu_items(section))

        items = [
            (MainMenu.NEW_QUERY.name, dict(
                label=te('Query'),
                fallback_action='query',
                items=exp(MainMenu.NEW_QUERY, self.new_query, self.recent_queries, self.word_list),
                disabled=is_disabled(MainMenu.NEW_QUERY)
            )),
            (MainMenu.CORPORA.name, dict(
                label=te('Corpora'),
                fallback_action='corpora/corplist',
                items=exp(MainMenu.CORPORA, self.avail_corpora,
                          self.my_subcorpora, self.public_subcorpora, self.create_subcorpus),
                disabled=is_disabled(MainMenu.CORPORA)
            )),
            (MainMenu.SAVE.name, dict(
                label=te('Save'),
                items=exp(MainMenu.SAVE, *save_items),
                disabled=is_disabled(MainMenu.SAVE)
            )),
            (MainMenu.CONCORDANCE.name, dict(
                label=te('Concordance'),
                items=exp(MainMenu.CONCORDANCE, self.curr_conc, self.sorting, self.shuffle, self.sample,
                          self.query_overview, self.query_save_as, self.archive_conc, self.query_undo),
                disabled=is_disabled(MainMenu.CONCORDANCE)
            )),
            (MainMenu.FILTER.name, dict(
                label=te('Filter'),
                items=exp(MainMenu.FILTER, self.filter_pos, self.filter_neg,
                          self.filter_subhits, self.filter_each_first),
                disabled=is_disabled(MainMenu.FILTER)
            )),
            (MainMenu.FREQUENCY.name, dict(
                label=te('Frequency'),
                items=exp(MainMenu.FREQUENCY, self.freq_lemmas, self.freq_node_forms_i,
                          self.freq_doc_ids, self.freq_text_types, self.freq_custom),
                disabled=is_disabled(MainMenu.FREQUENCY)
            )),
            (MainMenu.COLLOCATIONS.name, dict(
                label=te('Collocations'),
                items=exp(MainMenu.COLLOCATIONS, self.colloc_custom),
                disabled=is_disabled(MainMenu.COLLOCATIONS)
            )),
            (MainMenu.VIEW.name, dict(
                label=te('View'),
                items=exp(MainMenu.VIEW, self.view_mode_switch,
                          self.view_structs_attrs, self.view_global),
                disabled=is_disabled(MainMenu.VIEW)
            )),
            (MainMenu.HELP.name, dict(
                label=te('Help'),
                items=exp(MainMenu.HELP, self.how_to_cite_corpus, self.keyboard_shortcuts),
                disabled=is_disabled(MainMenu.HELP)
            ))
        ]
        return dict(submenuItems=items)
コード例 #11
0
ファイル: main_menu.py プロジェクト: svitalsky/kontext
    def __init__(self, tpl_data, args, plugin_api):
        self._args = OutData(tpl_data, args)

        # -------------------------- menu-new-query -------------------------------------

        self.new_query = (
            MenuItemInternal(MainMenu.NEW_QUERY('new-query'), te('Enter new query'), 'query')
            .add_args(
                ('corpname', self._args['corpname']),
                ('usesubcorp', self._args['usesubcorp']),
                *[('align', v) for v in self._args['align']]
                if len(self._args['align']) else [('align', None)])
            .mark_indirect()
        )

        self.recent_queries = (
            EventTriggeringItem(MainMenu.VIEW('history'), te('Recent queries'),
                                'MAIN_MENU_SHOW_QUERY_HISTORY')
            .mark_indirect()
        )

        self.word_list = (
            HideOnCustomCondItem(MainMenu.NEW_QUERY('wordlist'), te('Word List'), 'wordlist/form')
            .add_args(
                ('corpname', self._args['corpname']),
                ('include_nonwords', 1))
            .mark_indirect()
        )

        # ---------------------------- menu-corpora -------------------------------------

        self.avail_corpora = (
            MenuItemInternal(MainMenu.CORPORA('avail-corpora'),
                             te('Available corpora'), 'corpora/corplist')
            .mark_indirect()
        )

        with plugins.runtime.CORPARCH as corparch:
            corparch.mod_corplist_menu(plugin_api, self.avail_corpora)

        self.my_subcorpora = (
            MenuItemInternal(MainMenu.CORPORA('my-subcorpora'),
                             te('My subcorpora'), 'subcorpus/list')
            .mark_indirect()
        )

        self.public_subcorpora = (
            MenuItemInternal(MainMenu.CORPORA('public-subcorpora'),
                             te('Public subcorpora'), 'subcorpus/list_published')
        )

        self.create_subcorpus = (
            MenuItemInternal(MainMenu.CORPORA('create-subcorpus'),
                             te('Create new subcorpus'), 'subcorpus/new')
            .add_args(
                ('corpname', self._args['corpname']))
            .mark_indirect()
        )

        # -------------------------------- menu-save ------------------------------------

        # save items are generated dynamically during action processing
        # (see Kontext._add_save_menu_item())

        # ----------------------------- menu-concordance --------------------------------

        self.curr_conc = (
            ConcMenuItem(MainMenu.CONCORDANCE('current-concordance'),
                         te('Current concordance'), 'view')
        )

        self.sorting = (
            EventTriggeringItem(MainMenu.CONCORDANCE('sorting'), te('Sorting'),
                                'MAIN_MENU_SHOW_SORT', key_code=83).mark_indirect()
        )

        self.shuffle = (
            EventTriggeringItem(MainMenu.CONCORDANCE('shuffle'),
                                te('Shuffle'), 'MAIN_MENU_APPLY_SHUFFLE')
        )

        self.sample = EventTriggeringItem(MainMenu.CONCORDANCE('sample'),
                                          te('Sample'), 'MAIN_MENU_SHOW_SAMPLE', key_code=77).mark_indirect()

        self.query_overview = (
            EventTriggeringItem(MainMenu.CONCORDANCE('query-overview'),
                                te('Query overview'), 'MAIN_MENU_OVERVIEW_SHOW_QUERY_INFO')
        )

        self.query_save_as = (
            EventTriggeringItem(MainMenu.CONCORDANCE('query-save-as'),
                                te('Archive query'), 'MAIN_MENU_SHOW_SAVE_QUERY_AS_FORM').mark_indirect()
            .enable_if(lambda d: d.get('user_owns_conc', False))
        )

        self.archive_conc = lambda: (
            EventTriggeringItem(MainMenu.CONCORDANCE('archive-conc'),
                                te('Permanent link'), 'MAIN_MENU_MAKE_CONC_LINK_PERSISTENT')
            .mark_indirect()
            .enable_if(lambda d: d.get('user_owns_conc', False))
        ) if self._args['explicit_conc_persistence_ui'] else None

        self.query_undo = (
            EventTriggeringItem(MainMenu.CONCORDANCE('undo'), te(
                'Undo'), 'MAIN_MENU_UNDO_LAST_QUERY_OP')
            .enable_if(lambda d: len(d.get('undo_q', [])) > 0)
        )

        # ------------------------------------ menu-filter ------------------------------

        self.filter_pos = (
            EventTriggeringItem(MainMenu.FILTER('positive'), te('Positive'), 'MAIN_MENU_SHOW_FILTER',
                                key_code=70)  # key = 'f'
            .add_args(('pnfilter', 'p'))
            .mark_indirect()
        )

        self.filter_neg = (
            EventTriggeringItem(MainMenu.FILTER('negative'), te(
                'Negative'), 'MAIN_MENU_SHOW_FILTER')
            .add_args(('pnfilter', 'n'))
            .mark_indirect()
        )

        self.filter_subhits = (
            EventTriggeringItem(MainMenu.FILTER('subhits'),
                                te('Remove nested matches'), 'MAIN_MENU_FILTER_APPLY_SUBHITS_REMOVE')
        )

        self.filter_each_first = (
            EventTriggeringItem(MainMenu.FILTER('each-first'),
                                te('First hits in documents'),
                                'MAIN_MENU_FILTER_APPLY_FIRST_OCCURRENCES')
            .enable_if(lambda d: len(d.get('aligned_corpora', [])) == 0)
        )

        # ----------------------------------- menu-frequency ----------------------------

        self.freq_lemmas = (
            ConcMenuItem(MainMenu.FREQUENCY('lemmas'), te('Lemmas'), 'freqs')
            .add_args(
                ('fcrit', 'lemma/e 0~0>0'),
                ('ml', 0))
            .enable_if(lambda d: 'tag' in [x['n'] for x in self._args.get('AttrList', ())])
        )

        self.freq_node_forms_i = (
            ConcMenuItem(MainMenu.FREQUENCY('node-forms'), te('Node forms') + ' [A=a]', 'freqs',
                         hint=te('case insensitive'))
            .add_args(
                ('fcrit', 'word/ie 0~0>0'),
                ('ml', 0))
        )

        self.freq_doc_ids = (
            ConcMenuItem(MainMenu.FREQUENCY('doc-ids'), te('Doc IDs'), 'freqs')
            .add_args(
                ('fcrit', self._args['fcrit_shortref']),
                ('ml', 0))
            .enable_if(lambda d: 'fcrit_shortref' in self._args and
                       '.' in self._args['fcrit_shortref'].split('/')[0])
        )

        self.freq_text_types = (
            ConcMenuItem(MainMenu.FREQUENCY('text-types'), te('Text Types'), 'freqs')
            .add_args(*self._args.get('ttcrit', []))
            .add_args(('ml', 0))
            .enable_if(lambda d: bool(d['ttcrit']))
        )

        self.freq_custom = (
            EventTriggeringItem(MainMenu.FREQUENCY('custom'), te('Custom'), 'MAIN_MENU_SHOW_FREQ_FORM',
                                key_code=70, key_mod='shift')  # key = 'f'
            .mark_indirect()
        )

        # -------------------------------- menu-collocations ----------------------------

        self.colloc_custom = (
            EventTriggeringItem(MainMenu.COLLOCATIONS('custom'), te('Custom'),
                                'MAIN_MENU_SHOW_COLL_FORM', key_code=67, key_mod='shift')  # key = 'c'
            .mark_indirect()
        )

        # -------------------------------- menu-view ------------------------------------

        self.view_mode_switch = (
            EventTriggeringItem(MainMenu.VIEW('kwic-sent-switch'), te('KWIC/Sentence'),
                                'CONCORDANCE_SWITCH_KWIC_SENT_MODE', key_code=86)  # key = 'v'
        )

        self.view_structs_attrs = (
            EventTriggeringItem(MainMenu.VIEW('structs-attrs'), te('Corpus-specific settings'),
                                'MAIN_MENU_SHOW_ATTRS_VIEW_OPTIONS', key_code=79)  # key = 'o'
            .mark_corpus_dependent()
            .mark_indirect()
        )

        self.view_global = (
            EventTriggeringItem(MainMenu.VIEW('global-options'), te('General view options'),
                                'MAIN_MENU_SHOW_GENERAL_VIEW_OPTIONS', key_code=79, key_mod='shift')  # key = 'o'
            .mark_indirect()
        )

        # -------------------------------- menu-help ------------------------------------

        self.how_to_cite_corpus = (
            EventTriggeringItem(MainMenu.HELP('how-to-cite'),
                                'global__how_to_cite_corpus', 'OVERVIEW_SHOW_CITATION_INFO')
            .add_args(('corpusId', self._args['corpname']))
            .enable_if(lambda d: d['uses_corp_instance'])
            .mark_corpus_dependent()
        )

        self.keyboard_shortcuts = (
            EventTriggeringItem(MainMenu.HELP('keyboard-shortcuts'), 'global__keyboard_shortcuts',
                                'OVERVIEW_SHOW_KEY_SHORTCUTS', key_code=75, key_mod='shift')  # key = 'k')
        )
コード例 #12
0
ファイル: main_menu.py プロジェクト: czcorpus/kontext
    def generate(self, disabled_items, save_items, corpus_dependent, ui_lang):
        """
        Generate menu items based on current
        action and user state.

        Args:

        disabled_items (list of MainMenuItemId): a list of items
        save
        """

        def custom_menu_items(section):
            return map(
                lambda item: item.to_dict(),
                plugins.runtime.MENU_ITEMS.instance.get_items(section.name, lang=ui_lang)
            )

        def is_disabled(menu_item):
            if isinstance(menu_item, MainMenuItemId):
                item_id = menu_item
                item = None
            elif isinstance(menu_item, AbstractMenuItem):
                item_id = menu_item.ident
                item = menu_item
            else:
                raise ValueError()
            if corpus_dependent is False and item is not None and item.corpus_dependent is True:
                return True
            for item in disabled_items:
                if item_id.matches(item):
                    return True
            return False

        def exp(section, *args):
            ans = []
            for item in args:
                if callable(item):
                    item = item()
                if item:
                    ans.append(item.filter_empty_args().set_disabled(
                        is_disabled(item)).create(self._args))
            return tuple(ans + custom_menu_items(section))

        items = [
            (MainMenu.NEW_QUERY.name, dict(
                label=te('Query'),
                fallback_action='first_form',
                items=exp(MainMenu.NEW_QUERY, self.new_query, self.recent_queries, self.word_list),
                disabled=is_disabled(MainMenu.NEW_QUERY)
            )),
            (MainMenu.CORPORA.name, dict(
                label=te('Corpora'),
                fallback_action='corpora/corplist',
                items=exp(MainMenu.CORPORA, self.avail_corpora,
                          self.my_subcorpora, self.public_subcorpora, self.create_subcorpus),
                disabled=is_disabled(MainMenu.CORPORA)
            )),
            (MainMenu.SAVE.name, dict(
                label=te('Save'),
                items=exp(MainMenu.SAVE, *save_items),
                disabled=is_disabled(MainMenu.SAVE)
            )),
            (MainMenu.CONCORDANCE.name, dict(
                label=te('Concordance'),
                items=exp(MainMenu.CONCORDANCE, self.curr_conc, self.sorting, self.shuffle, self.sample,
                          self.query_overview, self.query_save_as, self.archive_conc, self.query_undo),
                disabled=is_disabled(MainMenu.CONCORDANCE)
            )),
            (MainMenu.FILTER.name, dict(
                label=te('Filter'),
                items=exp(MainMenu.FILTER, self.filter_pos, self.filter_neg,
                          self.filter_subhits, self.filter_each_first),
                disabled=is_disabled(MainMenu.FILTER)
            )),
            (MainMenu.FREQUENCY.name, dict(
                label=te('Frequency'),
                items=exp(MainMenu.FREQUENCY, self.freq_lemmas, self.freq_node_forms_i,
                          self.freq_doc_ids, self.freq_text_types, self.freq_custom),
                disabled=is_disabled(MainMenu.FREQUENCY)
            )),
            (MainMenu.COLLOCATIONS.name, dict(
                label=te('Collocations'),
                items=exp(MainMenu.COLLOCATIONS, self.colloc_custom),
                disabled=is_disabled(MainMenu.COLLOCATIONS)
            )),
            (MainMenu.VIEW.name, dict(
                label=te('View'),
                items=exp(MainMenu.VIEW, self.view_mode_switch,
                          self.view_structs_attrs, self.view_global),
                disabled=is_disabled(MainMenu.VIEW)
            )),
            (MainMenu.HELP.name, dict(
                label=te('Help'),
                items=exp(MainMenu.HELP, self.how_to_cite_corpus, self.keyboard_shortcuts),
                disabled=is_disabled(MainMenu.HELP)
            ))
        ]
        return dict(submenuItems=items)
コード例 #13
0
ファイル: main_menu.py プロジェクト: czcorpus/kontext
    def __init__(self, tpl_data, args, plugin_api):
        self._args = OutData(tpl_data, args)

        # -------------------------- menu-new-query -------------------------------------

        self.new_query = (
            MenuItemInternal(MainMenu.NEW_QUERY('new-query'), te('Enter new query'), 'first_form')
            .add_args(
                ('corpname', self._args['corpname']),
                ('usesubcorp', self._args['usesubcorp']),
                ('align', self._args['align'] if len(self._args['align']) else None))
            .mark_indirect()
        )

        self.recent_queries = (
            HideOnCustomCondItem(MainMenu.NEW_QUERY('history'), te(
                'Recent queries'), 'user/query_history')
            .add_args(
                ('corpname', self._args['corpname']))
            .mark_indirect()
        )

        self.word_list = (
            HideOnCustomCondItem(MainMenu.NEW_QUERY('wordlist'), te('Word List'), 'wordlist/form')
            .add_args(
                ('corpname', self._args['corpname']),
                ('include_nonwords', 1))
            .mark_indirect()
        )

        # ---------------------------- menu-corpora -------------------------------------

        self.avail_corpora = (
            MenuItemInternal(MainMenu.CORPORA('avail-corpora'),
                             te('Available corpora'), 'corpora/corplist')
            .mark_indirect()
        )

        with plugins.runtime.CORPARCH as corparch:
            corparch.mod_corplist_menu(plugin_api, self.avail_corpora)

        self.my_subcorpora = (
            MenuItemInternal(MainMenu.CORPORA('my-subcorpora'),
                             te('My subcorpora'), 'subcorpus/subcorp_list')
            .mark_indirect()
        )

        self.public_subcorpora = (
            MenuItemInternal(MainMenu.CORPORA('public-subcorpora'),
                             te('Public subcorpora'), 'subcorpus/list_published')
        )

        self.create_subcorpus = (
            MenuItemInternal(MainMenu.CORPORA('create-subcorpus'),
                             te('Create new subcorpus'), 'subcorpus/subcorp_form')
            .add_args(
                ('corpname', self._args['corpname']))
            .mark_indirect()
        )

        # -------------------------------- menu-save ------------------------------------

        # save items are generated dynamically during action processing
        # (see Kontext._add_save_menu_item())

        # ----------------------------- menu-concordance --------------------------------

        self.curr_conc = (
            ConcMenuItem(MainMenu.CONCORDANCE('current-concordance'),
                         te('Current concordance'), 'view')
        )

        self.sorting = (
            EventTriggeringItem(MainMenu.CONCORDANCE('sorting'), te('Sorting'),
                                'MAIN_MENU_SHOW_SORT', key_code=83).mark_indirect()
        )

        self.shuffle = (
            EventTriggeringItem(MainMenu.CONCORDANCE('shuffle'),
                                te('Shuffle'), 'MAIN_MENU_APPLY_SHUFFLE')
        )

        self.sample = EventTriggeringItem(MainMenu.CONCORDANCE('sample'),
                                          te('Sample'), 'MAIN_MENU_SHOW_SAMPLE', key_code=77).mark_indirect()

        self.query_overview = (
            EventTriggeringItem(MainMenu.CONCORDANCE('query-overview'),
                                te('Query overview'), 'MAIN_MENU_OVERVIEW_SHOW_QUERY_INFO')
        )

        self.query_save_as = (
            EventTriggeringItem(MainMenu.CONCORDANCE('query-save-as'),
                                te('Archive query'), 'MAIN_MENU_SHOW_SAVE_QUERY_AS_FORM').mark_indirect()
            .enable_if(lambda d: d.get('user_owns_conc', False))
        )

        self.archive_conc = lambda: (
            EventTriggeringItem(MainMenu.CONCORDANCE('archive-conc'),
                                te('Permanent link'), 'MAIN_MENU_MAKE_CONC_LINK_PERSISTENT')
            .mark_indirect()
            .enable_if(lambda d: d.get('user_owns_conc', False))
        ) if self._args['explicit_conc_persistence_ui'] else None

        self.query_undo = (
            EventTriggeringItem(MainMenu.CONCORDANCE('undo'), te(
                'Undo'), 'MAIN_MENU_UNDO_LAST_QUERY_OP')
            .enable_if(lambda d: len(d.get('undo_q', [])) > 0)
        )

        # ------------------------------------ menu-filter ------------------------------

        self.filter_pos = (
            EventTriggeringItem(MainMenu.FILTER('positive'), te('Positive'), 'MAIN_MENU_SHOW_FILTER',
                                key_code=70)  # key = 'f'
            .add_args(('pnfilter', 'p'))
            .mark_indirect()
        )

        self.filter_neg = (
            EventTriggeringItem(MainMenu.FILTER('negative'), te(
                'Negative'), 'MAIN_MENU_SHOW_FILTER')
            .add_args(('pnfilter', 'n'))
            .mark_indirect()
        )

        self.filter_subhits = (
            EventTriggeringItem(MainMenu.FILTER('subhits'),
                                te('Remove nested matches'), 'MAIN_MENU_FILTER_APPLY_SUBHITS_REMOVE')
        )

        self.filter_each_first = (
            EventTriggeringItem(MainMenu.FILTER('each-first'),
                                te('First hits in documents'),
                                'MAIN_MENU_FILTER_APPLY_FIRST_OCCURRENCES')
        )

        # ----------------------------------- menu-frequency ----------------------------

        self.freq_lemmas = (
            ConcMenuItem(MainMenu.FREQUENCY('lemmas'), te('Lemmas'), 'freqs')
            .add_args(
                ('fcrit', 'lemma/e 0~0>0'),
                ('ml', 0))
            .enable_if(lambda d: 'tag' in [x['n'] for x in self._args.get('AttrList', ())])
        )

        self.freq_node_forms_i = (
            ConcMenuItem(MainMenu.FREQUENCY('node-forms'), te('Node forms') + ' [A=a]', 'freqs',
                         hint=te('case insensitive'))
            .add_args(
                ('fcrit', 'word/ie 0~0>0'),
                ('ml', 0))
        )

        self.freq_doc_ids = (
            ConcMenuItem(MainMenu.FREQUENCY('doc-ids'), te('Doc IDs'), 'freqs')
            .add_args(
                ('fcrit', self._args['fcrit_shortref']),
                ('ml', 0))
            .enable_if(lambda d: 'fcrit_shortref' in self._args and
                       '.' in self._args['fcrit_shortref'].split('/')[0])
        )

        self.freq_text_types = (
            ConcMenuItem(MainMenu.FREQUENCY('text-types'), te('Text Types'), 'freqs')
            .add_args(*self._args.get('ttcrit', []))
            .add_args(('ml', 0))
            .enable_if(lambda d: bool(d['ttcrit']))
        )

        self.freq_custom = (
            EventTriggeringItem(MainMenu.FREQUENCY('custom'), te('Custom'), 'MAIN_MENU_SHOW_FREQ_FORM',
                                key_code=70, key_mod='shift')  # key = 'f'
            .mark_indirect()
        )

        # -------------------------------- menu-collocations ----------------------------

        self.colloc_custom = (
            EventTriggeringItem(MainMenu.COLLOCATIONS('custom'), te('Custom'),
                                'MAIN_MENU_SHOW_COLL_FORM', key_code=67, key_mod='shift')  # key = 'c'
            .mark_indirect()
        )

        # -------------------------------- menu-view ------------------------------------

        self.view_mode_switch = (
            EventTriggeringItem(MainMenu.VIEW('kwic-sent-switch'), te('KWIC/Sentence'),
                                'CONCORDANCE_SWITCH_KWIC_SENT_MODE', key_code=86)  # key = 'v'
        )

        self.view_structs_attrs = (
            EventTriggeringItem(MainMenu.VIEW('structs-attrs'), te('Corpus-specific settings'),
                                'MAIN_MENU_SHOW_ATTRS_VIEW_OPTIONS', key_code=79)  # key = 'o'
            .mark_corpus_dependent()
            .mark_indirect()
        )

        self.view_global = (
            EventTriggeringItem(MainMenu.VIEW('global-options'), te('General view options'),
                                'MAIN_MENU_SHOW_GENERAL_VIEW_OPTIONS', key_code=79, key_mod='shift')  # key = 'o'
            .mark_indirect()
        )

        # -------------------------------- menu-help ------------------------------------

        self.how_to_cite_corpus = (
            EventTriggeringItem(MainMenu.HELP('how-to-cite'),
                                'global__how_to_cite_corpus', 'OVERVIEW_SHOW_CITATION_INFO')
            .add_args(('corpusId', self._args['corpname']))
            .enable_if(lambda d: d['uses_corp_instance'])
            .mark_corpus_dependent()
        )

        self.keyboard_shortcuts = (
            EventTriggeringItem(MainMenu.HELP('keyboard-shortcuts'), 'global__keyboard_shortcuts',
                                'OVERVIEW_SHOW_KEY_SHORTCUTS', key_code=75, key_mod='shift')  # key = 'k')
        )
コード例 #14
0
def generate_main_menu(
        tpl_data: Dict[str, Any],
        args: Args,
        disabled_items: Union[List[MainMenuItemId], Tuple[MainMenuItemId, ...]],
        dynamic_items: List[AbstractMenuItem],
        corpus_dependent: bool,
        plugin_ctx: PluginCtx):
    """
    Generate main menu data based
    """

    merged_args = OutData(tpl_data, args)

    def modify_corpora(item: Corpora) -> Corpora:
        with plugins.runtime.CORPARCH as corparch:
            corparch.mod_corplist_menu(plugin_ctx, item.avail_corpora)
        return item

    def modify_save_items(save_item: Save) -> Save:
        for d_item in dynamic_items:
            if d_item.ident == MainMenu.SAVE:
                save_item.save_items.append(d_item)
        return save_item

    def modify_concordance(conc_item: ConcordanceDefault) -> Union[ConcordanceDefault, ConcordancePquery]:
        curr_action = merged_args.get('current_action', None)
        if curr_action == 'pquery/result':
            conc_item2 = ConcordancePquery()
            for d_item in dynamic_items:
                if d_item.ident == MainMenu.CONCORDANCE:
                    conc_item2.concordances.append(d_item)
            return conc_item2
        return conc_item

    def is_disabled(menu_item):
        if isinstance(menu_item, MainMenuItemId):
            item_id = menu_item
            item = None
        elif isinstance(menu_item, AbstractMenuItem):
            item_id = menu_item.ident
            item = menu_item
        else:
            raise ValueError()
        if corpus_dependent is False and item is not None and item.corpus_dependent is True:
            return True
        for item in disabled_items:
            if item_id.matches(item):
                return True
        return False

    def custom_menu_items(section):
        return [item.to_dict()
                for item in plugins.runtime.MENU_ITEMS.instance.get_items(section.name, lang=plugin_ctx.user_lang)]

    def exp(section, submenu, runtime_upd: Optional[Callable] = None):
        ans = []
        submenu_inst = submenu()
        if callable(runtime_upd):
            submenu_inst = runtime_upd(submenu_inst)
        smitems: Tuple[Field, ...] = fields(submenu_inst)
        for item in smitems:
            value = getattr(submenu_inst, item.name)
            if callable(value):
                value = value(merged_args)
            values = value if type(value) is list else [value]
            for value in values:
                if not value:
                    continue
                ans.append(value.filter_empty_args().set_disabled(
                    is_disabled(value)).create(merged_args))
        return tuple(ans + custom_menu_items(section))

    items = [
        (MainMenu.NEW_QUERY.name, dict(
            label=te('Query'),
            fallback_action='query',
            items=exp(MainMenu.NEW_QUERY, NewQuery),
            disabled=is_disabled(MainMenu.NEW_QUERY)
        )),
        (MainMenu.CORPORA.name, dict(
            label=te('Corpora'),
            fallback_action='corpora/corplist',
            items=exp(MainMenu.CORPORA, Corpora, modify_corpora),
            disabled=is_disabled(MainMenu.CORPORA)
        )),
        (MainMenu.SAVE.name, dict(
            label=te('Save'),
            items=exp(MainMenu.SAVE, Save, modify_save_items),
            disabled=is_disabled(MainMenu.SAVE)
        )),
        (MainMenu.CONCORDANCE.name, dict(
            label=te('Concordance'),
            items=exp(MainMenu.CONCORDANCE, ConcordanceDefault, modify_concordance),
            disabled=is_disabled(MainMenu.CONCORDANCE)
        )),
        (MainMenu.FILTER.name, dict(
            label=te('Filter'),
            items=exp(MainMenu.FILTER, Filter),
            disabled=is_disabled(MainMenu.FILTER)
        )),
        (MainMenu.FREQUENCY.name, dict(
            label=te('Frequency'),
            items=exp(MainMenu.FREQUENCY, Frequency),
            disabled=is_disabled(MainMenu.FREQUENCY)
        )),
        (MainMenu.COLLOCATIONS.name, dict(
            label=te('Collocations'),
            items=exp(MainMenu.COLLOCATIONS, Collocations),
            disabled=is_disabled(MainMenu.COLLOCATIONS)
        )),
        (MainMenu.VIEW.name, dict(
            label=te('View'),
            items=exp(MainMenu.VIEW, View),
            disabled=is_disabled(MainMenu.VIEW)
        )),
        (MainMenu.HELP.name, dict(
            label=te('Help'),
            items=exp(MainMenu.HELP, Help),
            disabled=is_disabled(MainMenu.HELP)
        ))
    ]
    return dict(submenuItems=items)