<commit_before>import json, urllib2
from copy import deepcopy
import operator, unicodedata
import bibserver.dao
import bibserver.config

class IOManager(object):
    def __init__(self, results, args):
        self.results = results
        self.config = bibserver.config.Config()
        self.args = args if args is not None else {}
        self.facet_fields = {}
        #print self.results['facets']
        for facet,data in self.results['facets'].items():
            self.facet_fields[facet.replace(self.config.facet_field,'')] = data["terms"]
            #facet = facet.replace(self.config.facet_field,'')
            #self.facet_fields[facet] = {}
            #for termdict in data['terms']:
            #    self.facet_fields[facet][termdict['term']] = termdict['count']

    def get_q(self):
        return self.args.get('q','')
    
    def get_safe_terms_object(self):
        terms = {}
        for term in self.args["terms"]:
            if term.replace(self.config.facet_field,'') not in self.args["path"]:
                terms[term.replace(self.config.facet_field,'')] = '[' + ','.join('"{0}"'.format(i) for i in self.args['terms'][term]) + ']'
        return terms    


    # run the desired method on a field content, to alter it for display
    # see the bottom of this file for the collection of methods to run
    def get_field_display(self, field, value):
        if self.config.display_value_functions.has_key(field):
            d = self.config.display_value_functions[field]
            func_name = d.keys()[0]
            args = d[func_name]
            args["field"] = field
            func = globals()[func_name]
            return func(value, args)
        else:
            return value

    def get_path_params(self,myargs):
        param = '/' + myargs["path"] + '?' if (myargs["path"] != '') else self.config.base_url + '?'
        if 'q' in myargs:
            param += 'q=' + myargs['q'] + '&'
        if 'terms' in myargs:
            for term in myargs['terms']:
                if term.replace(self.config.facet_field,'') not in self.args["path"]:
                    val = '[' + ",".join(urllib2.quote('"{0}"'.format(i.encode('utf-8'))) for i in myargs['terms'][term]) + ']'
                    param += term.replace(self.config.facet_field,'') + '=' + val + '&'
        return param

    def get_add_url(self, field, value):
        myargs = deepcopy(self.args)
        field += self.config.facet_field
        if myargs['terms'].has_key(field):
            if value not in myargs['terms'][field]:
                myargs['terms'][field].append(value)
        else:
            myargs['terms'][field] = [value]
        return self.get_path_params(myargs)
        
    def get_delete_url(self, field, value=None):
        myargs = deepcopy(self.args)
        if value is not None:
            field += self.config.facet_field
            myargs['terms'][field].remove(value)
            if len(myargs['terms'][field]) == 0:
                del myargs['terms'][field]
        else:
            del myargs['terms'][field]
        return self.get_path_params(myargs)


    def in_args(self, facet, value):
        return self.args['terms'].has_key(facet + self.config.facet_field) and value in self.args['terms'][facet + self.config.facet_field]
            
    def has_values(self, facet):
        return facet in self.config.facet_fields and facet in self.facet_fields

    def get_display_fields(self):
        return self.config.display_fields

    def get_facet_fields(self):
        return self.config.facet_fields

    def get_rpp_options(self):
        return self.config.results_per_page_options

    def numFound(self):
        return int(self.results['hits']['total'])

    def page_size(self):
        return int(self.args.get("size",10))

    def start(self):
        return int(self.args.get('start',0))

    def set(self):
        '''Return list of search result items'''
        return [rec['_source'] for rec in self.results['hits']['hits']]

    def get_str(self, result, field, raw=False):
        if result.get(field) is None:
            return ""
        if raw:
            if hasattr(result.get(field), "append"):
                return ", ".join([val for val in result.get(field)])
            else:
                return result.get(field)
        if hasattr(result.get(field), "append"):
            return ", ".join([self.get_field_display(field, val) for val in result.get(field)])
        else:
            return self.get_field_display(field, result.get(field))
        
    def get_meta(self):
        try:
            coll = self.results['hits']['hits'][0]["_source"]["collection"]
            if isinstance(coll,list):
                coll = coll[0]
            res = bibserver.dao.Record.query(q='collection' + self.config.facet_field + ':' + coll + ' AND type:collection')
            rec = res["hits"]["hits"][0]["_source"]
            meta = "<p>"
            if "source" in rec:
                meta = 'The source of this collection is <a href="'
                meta += rec["source"] + '">' + rec["source"] + '</a>. '
            if "received" in rec:
                meta += 'This collection was last updated on ' + rec["received"] + '. '
            if "source" in rec:
                meta += '<br />If changes have been made to the source file since then, '
                meta += '<a href="/upload?source=' + rec["source"] + '&collection=' + rec["collection"]
                meta += '">refresh this collection</a>.'
            meta += '</p>'
            return meta
        except:
            return ""
        



# the following methods can be called by get_field_display
# to perform various functions upon a field for display

def wrap(value, dict):
    return dict['start'] + value + dict['end']
    
def doiify(value, dict):
    # dois may start with:
    # 10. - prefix http://dx.doi.org/
    # doi: - strip doi: and replace with http://dx.doi.org/
    # http://dx.doi.org/ already done, just linkify<commit_msg>adding quotes around meta search<commit_after>import json, urllib2
from copy import deepcopy
import operator, unicodedata
import bibserver.dao
import bibserver.config

class IOManager(object):
    def __init__(self, results, args):
        self.results = results
        self.config = bibserver.config.Config()
        self.args = args if args is not None else {}
        self.facet_fields = {}
        #print self.results['facets']
        for facet,data in self.results['facets'].items():
            self.facet_fields[facet.replace(self.config.facet_field,'')] = data["terms"]
            #facet = facet.replace(self.config.facet_field,'')
            #self.facet_fields[facet] = {}
            #for termdict in data['terms']:
            #    self.facet_fields[facet][termdict['term']] = termdict['count']

    def get_q(self):
        return self.args.get('q','')
    
    def get_safe_terms_object(self):
        terms = {}
        for term in self.args["terms"]:
            if term.replace(self.config.facet_field,'') not in self.args["path"]:
                terms[term.replace(self.config.facet_field,'')] = '[' + ','.join('"{0}"'.format(i) for i in self.args['terms'][term]) + ']'
        return terms    


    # run the desired method on a field content, to alter it for display
    # see the bottom of this file for the collection of methods to run
    def get_field_display(self, field, value):
        if self.config.display_value_functions.has_key(field):
            d = self.config.display_value_functions[field]
            func_name = d.keys()[0]
            args = d[func_name]
            args["field"] = field
            func = globals()[func_name]
            return func(value, args)
        else:
            return value

    def get_path_params(self,myargs):
        param = '/' + myargs["path"] + '?' if (myargs["path"] != '') else self.config.base_url + '?'
        if 'q' in myargs:
            param += 'q=' + myargs['q'] + '&'
        if 'terms' in myargs:
            for term in myargs['terms']:
                if term.replace(self.config.facet_field,'') not in self.args["path"]:
                    val = '[' + ",".join(urllib2.quote('"{0}"'.format(i.encode('utf-8'))) for i in myargs['terms'][term]) + ']'
                    param += term.replace(self.config.facet_field,'') + '=' + val + '&'
        return param

    def get_add_url(self, field, value):
        myargs = deepcopy(self.args)
        field += self.config.facet_field
        if myargs['terms'].has_key(field):
            if value not in myargs['terms'][field]:
                myargs['terms'][field].append(value)
        else:
            myargs['terms'][field] = [value]
        return self.get_path_params(myargs)
        
    def get_delete_url(self, field, value=None):
        myargs = deepcopy(self.args)
        if value is not None:
            field += self.config.facet_field
            myargs['terms'][field].remove(value)
            if len(myargs['terms'][field]) == 0:
                del myargs['terms'][field]
        else:
            del myargs['terms'][field]
        return self.get_path_params(myargs)


    def in_args(self, facet, value):
        return self.args['terms'].has_key(facet + self.config.facet_field) and value in self.args['terms'][facet + self.config.facet_field]
            
    def has_values(self, facet):
        return facet in self.config.facet_fields and facet in self.facet_fields

    def get_display_fields(self):
        return self.config.display_fields

    def get_facet_fields(self):
        return self.config.facet_fields

    def get_rpp_options(self):
        return self.config.results_per_page_options

    def numFound(self):
        return int(self.results['hits']['total'])

    def page_size(self):
        return int(self.args.get("size",10))

    def start(self):
        return int(self.args.get('start',0))

    def set(self):
        '''Return list of search result items'''
        return [rec['_source'] for rec in self.results['hits']['hits']]

    def get_str(self, result, field, raw=False):
        if result.get(field) is None:
            return ""
        if raw:
            if hasattr(result.get(field), "append"):
                return ", ".join([val for val in result.get(field)])
            else:
                return result.get(field)
        if hasattr(result.get(field), "append"):
            return ", ".join([self.get_field_display(field, val) for val in result.get(field)])
        else:
            return self.get_field_display(field, result.get(field))
        
    def get_meta(self):
        try:
            coll = self.results['hits']['hits'][0]["_source"]["collection"]
            if isinstance(coll,list):
                coll = coll[0]
            res = bibserver.dao.Record.query(q='collection' + self.config.facet_field + ':"' + coll + '" AND type:collection')
            rec = res["hits"]["hits"][0]["_source"]
            meta = "<p>"
            if "source" in rec:
                meta = 'The source of this collection is <a href="'
                meta += rec["source"] + '">' + rec["source"] + '</a>. '
            if "received" in rec:
                meta += 'This collection was last updated on ' + rec["received"] + '. '
            if "source" in rec:
                meta += '<br />If changes have been made to the source file since then, '
                meta += '<a href="/upload?source=' + rec["source"] + '&collection=' + rec["collection"]
                meta += '">refresh this collection</a>.'
            meta += '</p>'
            return meta
        except:
            return ""
        



# the following methods can be called by get_field_display
# to perform various functions upon a field for display

def wrap(value, dict):
    return dict['start'] + value + dict['end']
    
def doiify(value, dict):
    # dois may start with:
    # 10. - prefix http://dx.doi.org/
    # doi: - strip doi: and replace with http://dx.doi.org/
    # http://dx.doi.org/ already done, just linkify
{'fraction_non_alphanumeric': 0.14277403806199618, 'fraction_numerical': 0.0024931438544003987, 'mean_word_length': 2.2271386430678466, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7449098', 'n_tokens_mistral': 3690, 'n_tokens_neox': 3575, 'n_words': 867}