コード例 #1
0
 def run(self, tt_query, cql, path, publish_path):
     """
     returns:
     True in case of success
     In case of an empty subcorus, EmptySubcorpusException is thrown
     """
     conc = conclib.get_conc(self._corp, self._user_id, q=cql, asnc=0)
     if conc.size() == 0:
         raise EmptySubcorpusException('Empty subcorpus')
     ans = corplib.subcorpus_from_conc(path, conc)
     if ans is False:
         raise EmptySubcorpusException(
             'Failed to create the subcorpus from a concordance')
     if not os.path.isfile(
             path):  # this should not happen but it looks like it did
         logging.getLogger(__name__).warning(
             'Sync. called conc. file not created (path: {})'.format(path))
         time.sleep(5)
     # we must set write perms for group as this is created by Celery and we won't be
     # able to create hardlinks otherwise
     os.chmod(path, 0o664)
     if publish_path:
         corplib.mk_publish_links(path, publish_path, self._author,
                                  self._description)
     return ans
コード例 #2
0
ファイル: subcorpus.py プロジェクト: anukat2015/kontext
    def _create_subcorpus(self, request):
        """
        req. arguments:
        subcname -- name of new subcorpus
        create -- bool, sets whether to create new subcorpus
        cql -- custom within condition
        """
        subcname = request.form['subcname']
        within_json = request.form.get('within_json')
        raw_cql = request.form.get('cql')
        corp_encoding = self._corp().get_conf('ENCODING')

        if raw_cql:
            tt_query = ()
            within_cql = raw_cql
            full_cql = 'aword,[] %s' % raw_cql
            imp_cql = (full_cql,)
        elif within_json:  # user entered a subcorpus query manually
            tt_query = ()
            within_cql = self._deserialize_custom_within(json.loads(within_json))
            full_cql = 'aword,[] %s' % within_cql
            imp_cql = (full_cql,)
        else:
            tt_query = TextTypeCollector(self._corp(), request).get_query()
            full_cql = ' within '.join(['<%s %s />' % item for item in tt_query])
            full_cql = 'aword,[] within %s' % full_cql
            full_cql = import_string(full_cql, from_encoding=corp_encoding)
            imp_cql = (full_cql,)
        basecorpname = self.args.corpname.split(':')[0]
        if not subcname:
            raise UserActionException(_('No subcorpus name specified!'))
        path = self.prepare_subc_path(basecorpname, subcname)

        if type(path) == unicode:
            path = path.encode('utf-8')

        if len(tt_query) == 1:
            result = corplib.create_subcorpus(path, self._corp(), tt_query[0][0], tt_query[0][1])
        elif len(tt_query) > 1 or within_cql:
            conc = conclib.get_conc(self._corp(), self._session_get('user', 'user'), q=imp_cql)
            conc.sync()
            struct = self._corp().get_struct(tt_query[0][0]) if len(tt_query) == 1 else None
            result = corplib.subcorpus_from_conc(path, conc, struct)
        else:
            raise UserActionException(_('Nothing specified!'))

        if result:
            if plugins.has_plugin('subc_restore'):
                try:
                    plugins.get('subc_restore').store_query(user_id=self._session_get('user', 'id'),
                                                            corpname=self.args.corpname,
                                                            subcname=subcname,
                                                            cql=full_cql.split('[]')[-1])
                except Exception as e:
                    logging.getLogger(__name__).warning('Failed to store subcorpus query: %s' % e)
                    self.add_system_message('warning',
                                            _('Subcorpus created but there was a problem saving a backup copy.'))
            return {}
        else:
            raise ConcError(_('Empty subcorpus!'))
コード例 #3
0
ファイル: subc_calc.py プロジェクト: petrduda/kontext
 def run(self, tt_query, cql, path):
     """
     returns:
     True in case of success
     In case of an empty subcorus, EmptySubcorpusException is thrown
     """
     conc = conclib.get_conc(self._corp, self._user_id, q=cql, async=0)
     ans = corplib.subcorpus_from_conc(path, conc)
     if ans is False:
         raise EmptySubcorpusException('Empty subcorpus')
     return ans