Ejemplo n.º 1
0
    def export(self, id=None):
        """Export the BLD Files matching the search criteria
        as a .zip archive.  ."""
        # Get the Files that match the search and get their full path names
        try:
            result = session['fileSearchValues']
            file_q = meta.Session.query(model.File)
            file_q = h.filterSearchQuery(result, file_q, 'File')
            files = file_q.all()
        except KeyError:
            files = meta.Session.query(model.File).all()

        fileNames = [
            os.path.join(config['app_conf']['permanent_store'], file.name)
            for file in files
        ]

        # Create the .zip file and write the Files to it
        #  Python 2.5 was raising a UnicodeDecodeError when ZipFile.write was
        #  passed unicode arguments, so I've str()-ed them
        zfileName = str('%s_%s_file_export.zip' % \
            (session['user_firstName'].lower(), session['user_lastName'].lower()))
        zfileName = str(
            os.path.join(config['app_conf']['temporary_store'], zfileName))
        zout = zipfile.ZipFile(zfileName, "w")
        for fileName in fileNames:
            zout.write(str(fileName), os.path.basename(str(fileName)))
        zout.close()
        c.zfileName = os.path.basename(zfileName)
        c.zfileSize = os.path.getsize(zfileName)
        c.fileNames = [(file.name,
                        os.path.getsize(
                            os.path.join(config['app_conf']['permanent_store'],
                                         file.name))) for file in files]
        return render('/derived/file/export.html')
Ejemplo n.º 2
0
    def export(self, id=None):
        """Export the BLD Files matching the search criteria
        as a .zip archive.  ."""
        # Get the Files that match the search and get their full path names
        try:
            result = session['fileSearchValues']
            file_q = meta.Session.query(model.File)
            file_q = h.filterSearchQuery(result, file_q, 'File')
            files = file_q.all()
        except KeyError:
            files = meta.Session.query(model.File).all()

        fileNames = [os.path.join(config['app_conf']['permanent_store'], file.name) for file in files]

        # Create the .zip file and write the Files to it
        #  Python 2.5 was raising a UnicodeDecodeError when ZipFile.write was
        #  passed unicode arguments, so I've str()-ed them
        zfileName = str('%s_%s_file_export.zip' % \
            (session['user_firstName'].lower(), session['user_lastName'].lower()))
        zfileName = str(
            os.path.join(config['app_conf']['temporary_store'], zfileName))
        zout = zipfile.ZipFile(zfileName, "w")
        for fileName in fileNames:
            zout.write(str(fileName), os.path.basename(str(fileName)))
        zout.close()
        c.zfileName = os.path.basename(zfileName)
        c.zfileSize = os.path.getsize(zfileName)
        c.fileNames = [(file.name, os.path.getsize(os.path.join(config['app_conf']['permanent_store'], file.name))) for file in files]
        return render('/derived/file/export.html')
Ejemplo n.º 3
0
    def get_form_list_from_last_search(self):
        """Whenever a OLD Form search is effected, the values of the HTML Form Search
        form are stored in the session under the key 'formSearchValues'.  Use these
        values to recreate the last search and return the resultant Forms.

        """
        form_search = session.get("formSearchValues", None)
        if form_search:
            return (
                h.filterSearchQuery(form_search, meta.Session.query(model.Form), "Form").order_by(model.Form.id).all()
            )
        return []
Ejemplo n.º 4
0
    def get_form_list_from_last_search(self):
        """Whenever a OLD Form search is effected, the values of the HTML Form Search
        form are stored in the session under the key 'formSearchValues'.  Use these
        values to recreate the last search and return the resultant Forms.

        """
        form_search = session.get('formSearchValues', None)
        if form_search:
            return h.filterSearchQuery(
                form_search, meta.Session.query(model.Form), 'Form').\
                order_by(model.Form.id).all()
        return []
Ejemplo n.º 5
0
 def results(self):
     """Results action uses the filterSearchQuery helper function to build
     a query based on the values entered by the user in the search file."""
     if 'fileSearchValues' in session:
         result = session['fileSearchValues']
         file_q = meta.Session.query(model.File)
         file_q = h.filterSearchQuery(result, file_q, 'File')
     else:
         file_q = meta.Session.query(model.File)
     c.paginator = paginate.Page(
         file_q,
         page=int(request.params.get('page', 1)),
         items_per_page=app_globals.file_items_per_page)
     return render('/derived/file/results.html')
Ejemplo n.º 6
0
 def results(self):
     """Results action uses the filterSearchQuery helper function to build
     a query based on the values entered by the user in the search file."""
     if 'fileSearchValues' in session:
         result = session['fileSearchValues']
         file_q = meta.Session.query(model.File)
         file_q = h.filterSearchQuery(result, file_q, 'File')
     else:
         file_q = meta.Session.query(model.File)
     c.paginator = paginate.Page(
         file_q,
         page=int(request.params.get('page', 1)),
         items_per_page = app_globals.file_items_per_page
     )
     return render('/derived/file/results.html')
Ejemplo n.º 7
0
    def results(self):
        """Results action uses the filterSearchQuery helper function to build
        a query based on the values entered by the user in the search
        collection.

        """

        collection_q = meta.Session.query(model.Collection)
        if 'collectionSearchValues' in session:
            result = session['collectionSearchValues']
            collection_q = h.filterSearchQuery(result, collection_q,
                                               'Collection')

        c.paginator = paginate.Page(
            collection_q,
            page=int(request.params.get('page', 1)),
            items_per_page=app_globals.collection_items_per_page)

        return render('/derived/collection/results.html')
Ejemplo n.º 8
0
    def results(self):
        """Results action uses the filterSearchQuery helper function to build
        a query based on the values entered by the user in the search
        collection.

        """

        collection_q = meta.Session.query(model.Collection)
        if 'collectionSearchValues' in session:
            result = session['collectionSearchValues']
            collection_q = h.filterSearchQuery(
                result, collection_q, 'Collection')

        c.paginator = paginate.Page(
            collection_q,
            page=int(request.params.get('page', 1)),
            items_per_page = app_globals.collection_items_per_page
        )

        return render('/derived/collection/results.html')
Ejemplo n.º 9
0
    def applyphonologytodb(self):

        def getVariants(word):
            deCapped = word[0].lower() + word[1:]
            return list(set([removeExtraneousPunctuation(word),
                    removeExtraneousPunctuation(deCapped)]))

        def getReport(mb, tr, phonologizations):
            report = u''
            if tr in phonologizations or \
            set(getVariants(tr)) & set(phonologizations):
                span = '<span style="color: green;">'
                report += u'<p>%s%s \u2192 %s</span></p>' % (span, mb, tr)
            else:
                span = '<span style="color: red;">'
                report += u'<p>%s%s \u219B %s</span></p>\n<ul>' % (span, mb, tr)
                for ph in phonologizations:
                    report += u'<li>%s</li>' % ph
                report += u'</ul>'
            return report

        output = u''

        # Get forms based on the search/filter provided by the user
        values = urllib.unquote_plus(unicode(request.body, 'utf-8'))
        values = json.loads(values)

        schema = SearchFormForm()
        try:
            result = schema.to_python(values)
        except Invalid:
            return 'Unable to validate form data'

        form_q = meta.Session.query(model.Form)
        form_q = h.filterSearchQuery(result, form_q, 'Form')
        if 'limit' in result and result['limit']:
            form_q = form_q.limit(int(result['limit']))
        forms = form_q.all()
        log.debug(len(forms))

        correct = incorrect = wordsFound = 0
        mb2phonologized = {}
        output += u'<p>%d Forms match your criteria</p>' % len(forms)
        for form in forms:
            tr = form.transcription
            mb = form.morphemeBreak
            if form.morphemeBreak:
                if len(tr.split()) == len(mb.split()):
                    words = zip(tr.split(), mb.split())
                    for w in words:
                        tr = w[0]
                        mb = w[1]
                        try:
                            phonologizations = mb2phonologized[mb]
                        except KeyError:
                            phonologizations = list(set(applyFomaPhonology(
                                mb, 'inverse')))
                            mb2phonologized[mb] = phonologizations
                        wordsFound += 1
                        if tr in phonologizations or \
                        set(getVariants(tr)) & set(phonologizations):
                            correct += 1
                        else:
                            print '%s is not in %s' % (getVariants(tr), str(phonologizations))
                        output += getReport(w[1], w[0], phonologizations)
        try:
            percentCorrect = 100 * correct / float(wordsFound)
        except ZeroDivisionError:
            percentCorrect = 0.0
        output += u'<p>%0.2f%% accuracy.</p>' % percentCorrect
        output += u'<p>(%d words found in %d Forms).</p>' % (wordsFound, len(forms))
        return output
Ejemplo n.º 10
0
    def applyphonologytodb(self):
        def getVariants(word):
            deCapped = word[0].lower() + word[1:]
            return list(
                set([
                    removeExtraneousPunctuation(word),
                    removeExtraneousPunctuation(deCapped)
                ]))

        def getReport(mb, tr, phonologizations):
            report = u''
            if tr in phonologizations or \
            set(getVariants(tr)) & set(phonologizations):
                span = '<span style="color: green;">'
                report += u'<p>%s%s \u2192 %s</span></p>' % (span, mb, tr)
            else:
                span = '<span style="color: red;">'
                report += u'<p>%s%s \u219B %s</span></p>\n<ul>' % (span, mb,
                                                                   tr)
                for ph in phonologizations:
                    report += u'<li>%s</li>' % ph
                report += u'</ul>'
            return report

        output = u''

        # Get forms based on the search/filter provided by the user
        values = urllib.unquote_plus(unicode(request.body, 'utf-8'))
        values = json.loads(values)

        schema = SearchFormForm()
        try:
            result = schema.to_python(values)
        except Invalid:
            return 'Unable to validate form data'

        form_q = meta.Session.query(model.Form)
        form_q = h.filterSearchQuery(result, form_q, 'Form')
        if 'limit' in result and result['limit']:
            form_q = form_q.limit(int(result['limit']))
        forms = form_q.all()
        log.debug(len(forms))

        correct = incorrect = wordsFound = 0
        mb2phonologized = {}
        output += u'<p>%d Forms match your criteria</p>' % len(forms)
        for form in forms:
            tr = form.transcription
            mb = form.morphemeBreak
            if form.morphemeBreak:
                if len(tr.split()) == len(mb.split()):
                    words = zip(tr.split(), mb.split())
                    for w in words:
                        tr = w[0]
                        mb = w[1]
                        try:
                            phonologizations = mb2phonologized[mb]
                        except KeyError:
                            phonologizations = list(
                                set(applyFomaPhonology(mb, 'inverse')))
                            mb2phonologized[mb] = phonologizations
                        wordsFound += 1
                        if tr in phonologizations or \
                        set(getVariants(tr)) & set(phonologizations):
                            correct += 1
                        else:
                            print '%s is not in %s' % (getVariants(tr),
                                                       str(phonologizations))
                        output += getReport(w[1], w[0], phonologizations)
        try:
            percentCorrect = 100 * correct / float(wordsFound)
        except ZeroDivisionError:
            percentCorrect = 0.0
        output += u'<p>%0.2f%% accuracy.</p>' % percentCorrect
        output += u'<p>(%d words found in %d Forms).</p>' % (wordsFound,
                                                             len(forms))
        return output