2011-04-26 08:02:44 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
"""
|
2013-10-10 13:41:10 +02:00
|
|
|
OpenRefine Facets, Engine, and Facet Responses.
|
2011-04-26 08:02:44 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
# Copyright (c) 2011 Paul Makepeace, Real Programmers. All rights reserved.
|
|
|
|
|
2011-05-01 19:24:12 +02:00
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
import json
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
def to_camel(attr):
|
|
|
|
"""convert this_attr_name to thisAttrName."""
|
|
|
|
# Do lower case first letter
|
|
|
|
return (attr[0].lower() +
|
|
|
|
re.sub(r'_(.)', lambda x: x.group(1).upper(), attr[1:]))
|
|
|
|
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
def from_camel(attr):
|
|
|
|
"""convert thisAttrName to this_attr_name."""
|
|
|
|
# Don't add an underscore for capitalized first letter
|
|
|
|
return re.sub(r'(?<=.)([A-Z])', lambda x: '_' + x.group(1), attr).lower()
|
|
|
|
|
|
|
|
|
|
|
|
class Facet(object):
|
2013-10-09 20:04:24 +02:00
|
|
|
def __init__(self, column, facet_type, **options):
|
|
|
|
self.type = facet_type
|
2011-04-26 08:02:44 +02:00
|
|
|
self.name = column
|
|
|
|
self.column_name = column
|
|
|
|
for k, v in options.items():
|
|
|
|
setattr(self, k, v)
|
|
|
|
|
|
|
|
def as_dict(self):
|
|
|
|
return dict([(to_camel(k), v) for k, v in self.__dict__.items()
|
|
|
|
if v is not None])
|
|
|
|
|
|
|
|
|
|
|
|
class TextFilterFacet(Facet):
|
2011-04-27 00:56:59 +02:00
|
|
|
def __init__(self, column, query, **options):
|
2011-04-27 01:09:52 +02:00
|
|
|
super(TextFilterFacet, self).__init__(
|
2013-10-09 20:04:24 +02:00
|
|
|
column, query=query, case_sensitive=False, facet_type='text',
|
2011-04-27 01:09:52 +02:00
|
|
|
mode='text', **options)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TextFacet(Facet):
|
2011-04-27 01:09:52 +02:00
|
|
|
def __init__(self, column, selection=None, expression='value',
|
2013-10-09 20:04:24 +02:00
|
|
|
omit_blank=False, omit_error=False, select_blank=False,
|
|
|
|
select_error=False, invert=False, **options):
|
2011-04-26 08:02:44 +02:00
|
|
|
super(TextFacet, self).__init__(
|
|
|
|
column,
|
2013-10-09 20:04:24 +02:00
|
|
|
facet_type='list',
|
2011-04-26 08:02:44 +02:00
|
|
|
omit_blank=omit_blank,
|
|
|
|
omit_error=omit_error,
|
|
|
|
select_blank=select_blank,
|
|
|
|
select_error=select_error,
|
|
|
|
invert=invert,
|
|
|
|
**options)
|
2011-04-27 01:09:52 +02:00
|
|
|
self.expression = expression
|
2011-04-26 08:02:44 +02:00
|
|
|
self.selection = []
|
|
|
|
if selection is None:
|
|
|
|
selection = []
|
|
|
|
elif not isinstance(selection, list):
|
|
|
|
selection = [selection]
|
|
|
|
for value in selection:
|
|
|
|
self.include(value)
|
|
|
|
|
|
|
|
def include(self, value):
|
|
|
|
for s in self.selection:
|
|
|
|
if s['v']['v'] == value:
|
|
|
|
return
|
|
|
|
self.selection.append({'v': {'v': value, 'l': value}})
|
|
|
|
return self
|
|
|
|
|
|
|
|
def exclude(self, value):
|
|
|
|
self.selection = [s for s in self.selection
|
|
|
|
if s['v']['v'] != value]
|
|
|
|
return self
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.selection = []
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
|
|
|
class BoolFacet(TextFacet):
|
|
|
|
def __init__(self, column, expression=None, selection=None):
|
|
|
|
if selection is not None and not isinstance(selection, bool):
|
|
|
|
raise ValueError('selection must be True or False.')
|
|
|
|
if expression is None:
|
|
|
|
raise ValueError('Missing expression')
|
2013-10-09 20:04:24 +02:00
|
|
|
super(BoolFacet, self).__init__(
|
|
|
|
column, expression=expression, selection=selection)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class StarredFacet(BoolFacet):
|
|
|
|
def __init__(self, selection=None):
|
2013-10-09 20:04:24 +02:00
|
|
|
super(StarredFacet, self).__init__(
|
|
|
|
'', expression='row.starred', selection=selection)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class FlaggedFacet(BoolFacet):
|
|
|
|
def __init__(self, selection=None):
|
2013-10-09 20:04:24 +02:00
|
|
|
super(FlaggedFacet, self).__init__(
|
|
|
|
'', expression='row.flagged', selection=selection)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class BlankFacet(BoolFacet):
|
|
|
|
def __init__(self, column, selection=None):
|
2013-10-09 20:04:24 +02:00
|
|
|
super(BlankFacet, self).__init__(
|
|
|
|
column, expression='isBlank(value)', selection=selection)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
|
2011-06-10 16:31:05 +02:00
|
|
|
class ReconJudgmentFacet(TextFacet):
|
|
|
|
def __init__(self, column, **options):
|
2013-10-09 20:04:24 +02:00
|
|
|
super(ReconJudgmentFacet, self).__init__(
|
|
|
|
column,
|
2011-06-10 16:31:05 +02:00
|
|
|
expression=('forNonBlank(cell.recon.judgment, v, v, '
|
|
|
|
'if(isNonBlank(value), "(unreconciled)", "(blank)"))'),
|
|
|
|
**options)
|
|
|
|
|
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
# Capitalize 'From' to get around python's reserved word.
|
2013-10-09 20:04:24 +02:00
|
|
|
#noinspection PyPep8Naming
|
2011-04-26 08:02:44 +02:00
|
|
|
class NumericFacet(Facet):
|
2011-06-09 12:50:13 +02:00
|
|
|
def __init__(self, column, From=None, to=None, expression='value',
|
|
|
|
select_blank=True, select_error=True, select_non_numeric=True,
|
|
|
|
select_numeric=True, **options):
|
2011-04-26 08:02:44 +02:00
|
|
|
super(NumericFacet, self).__init__(
|
|
|
|
column,
|
2011-04-27 01:24:32 +02:00
|
|
|
From=From,
|
|
|
|
to=to,
|
2011-04-27 01:31:10 +02:00
|
|
|
expression=expression,
|
2013-10-09 20:04:24 +02:00
|
|
|
facet_type='range',
|
2011-04-26 08:02:44 +02:00
|
|
|
select_blank=select_blank,
|
|
|
|
select_error=select_error,
|
|
|
|
select_non_numeric=select_non_numeric,
|
|
|
|
select_numeric=select_numeric,
|
|
|
|
**options)
|
|
|
|
|
2011-04-27 01:31:10 +02:00
|
|
|
def reset(self):
|
|
|
|
self.From = None
|
|
|
|
self.to = None
|
|
|
|
return self
|
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
class FacetResponse(object):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""Class for unpacking an individual facet response."""
|
2011-04-26 08:02:44 +02:00
|
|
|
def __init__(self, facet):
|
2013-10-09 20:04:24 +02:00
|
|
|
self.name = None
|
2011-04-26 08:02:44 +02:00
|
|
|
for k, v in facet.items():
|
|
|
|
if isinstance(k, bool) or isinstance(k, basestring):
|
|
|
|
setattr(self, from_camel(k), v)
|
|
|
|
self.choices = {}
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-26 08:02:44 +02:00
|
|
|
class FacetChoice(object):
|
|
|
|
def __init__(self, c):
|
|
|
|
self.count = c['c']
|
|
|
|
self.selected = c['s']
|
|
|
|
|
|
|
|
if 'choices' in facet:
|
|
|
|
for choice in facet['choices']:
|
|
|
|
self.choices[choice['v']['v']] = FacetChoice(choice)
|
|
|
|
if 'blankChoice' in facet:
|
|
|
|
self.blank_choice = FacetChoice(facet['blankChoice'])
|
|
|
|
else:
|
|
|
|
self.blank_choice = None
|
|
|
|
if 'bins' in facet:
|
|
|
|
self.bins = facet['bins']
|
|
|
|
self.base_bins = facet['baseBins']
|
|
|
|
|
|
|
|
|
|
|
|
class FacetsResponse(object):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""FacetsResponse unpacking the compute-facets response.
|
|
|
|
|
|
|
|
It has two attributes: facets & mode. Mode is either 'row-based' or
|
|
|
|
'record-based'. facets is a list of facets produced by compute-facets, in
|
|
|
|
the same order as they were specified in the Engine. By coupling the engine
|
|
|
|
object with a custom container it's possible to look up the computed facet
|
|
|
|
by the original facet's object.
|
|
|
|
"""
|
|
|
|
def __init__(self, engine, facets):
|
|
|
|
class FacetResponseContainer(object):
|
|
|
|
facets = None
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-27 05:52:13 +02:00
|
|
|
def __init__(self, facet_responses):
|
|
|
|
self.facets = [FacetResponse(fr) for fr in facet_responses]
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-27 05:52:13 +02:00
|
|
|
def __iter__(self):
|
|
|
|
for facet in self.facets:
|
|
|
|
yield facet
|
2013-10-09 20:04:24 +02:00
|
|
|
|
2011-04-27 05:52:13 +02:00
|
|
|
def __getitem__(self, index):
|
|
|
|
if not isinstance(index, int):
|
|
|
|
index = engine.facet_index_by_id[id(index)]
|
|
|
|
assert self.facets[index].name == engine.facets[index].name
|
|
|
|
return self.facets[index]
|
|
|
|
|
|
|
|
self.facets = FacetResponseContainer(facets['facets'])
|
2011-04-26 08:02:44 +02:00
|
|
|
self.mode = facets['mode']
|
|
|
|
|
|
|
|
|
|
|
|
class Engine(object):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""An Engine keeps track of Facets, and responses to facet computation."""
|
|
|
|
|
2011-06-23 11:49:25 +02:00
|
|
|
def __init__(self, *facets, **kwargs):
|
2013-10-10 12:32:56 +02:00
|
|
|
self.facets = []
|
|
|
|
self.facet_index_by_id = {} # dict of facets by Facet object id
|
2011-06-23 11:49:25 +02:00
|
|
|
self.set_facets(*facets)
|
|
|
|
self.mode = kwargs.get('mode', 'row-based')
|
2011-04-26 22:55:07 +02:00
|
|
|
|
2011-06-23 11:49:25 +02:00
|
|
|
def set_facets(self, *facets):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""facets may be a Facet or list of Facets."""
|
|
|
|
self.remove_all()
|
|
|
|
for facet in facets:
|
|
|
|
self.add_facet(facet)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
2011-04-27 05:52:13 +02:00
|
|
|
def facets_response(self, response):
|
|
|
|
"""Unpack a compute-facets response."""
|
|
|
|
return FacetsResponse(self, response)
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.facets)
|
|
|
|
|
|
|
|
def as_json(self):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""Return a JSON string suitable for use as a POST parameter."""
|
|
|
|
return json.dumps({
|
|
|
|
'facets': [f.as_dict() for f in self.facets], # XXX how with json?
|
|
|
|
'mode': self.mode,
|
|
|
|
})
|
2011-04-26 08:02:44 +02:00
|
|
|
|
|
|
|
def add_facet(self, facet):
|
2011-04-27 05:52:13 +02:00
|
|
|
# Record the facet's object id so facet response can be looked up by id
|
|
|
|
self.facet_index_by_id[id(facet)] = len(self.facets)
|
2011-04-26 08:02:44 +02:00
|
|
|
self.facets.append(facet)
|
|
|
|
|
|
|
|
def remove_all(self):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""Remove all facets."""
|
2011-06-23 11:48:16 +02:00
|
|
|
self.facet_index_by_id = {}
|
2011-04-26 08:02:44 +02:00
|
|
|
self.facets = []
|
|
|
|
|
|
|
|
def reset_all(self):
|
2011-04-27 05:52:13 +02:00
|
|
|
"""Reset all facets."""
|
2011-04-26 08:02:44 +02:00
|
|
|
for facet in self.facets:
|
|
|
|
facet.reset()
|
|
|
|
|
|
|
|
|
|
|
|
class Sorting(object):
|
|
|
|
"""Class representing the current sorting order for a project.
|
|
|
|
|
|
|
|
Used in RefineProject.get_rows()"""
|
|
|
|
def __init__(self, criteria=None):
|
|
|
|
self.criteria = []
|
|
|
|
if criteria is None:
|
|
|
|
criteria = []
|
|
|
|
if not isinstance(criteria, list):
|
|
|
|
criteria = [criteria]
|
|
|
|
for criterion in criteria:
|
2011-04-27 05:52:13 +02:00
|
|
|
# A string criterion defaults to a string sort on that column
|
2011-04-26 08:02:44 +02:00
|
|
|
if isinstance(criterion, basestring):
|
|
|
|
criterion = {
|
|
|
|
'column': criterion,
|
|
|
|
'valueType': 'string',
|
|
|
|
'caseSensitive': False,
|
|
|
|
}
|
2011-04-27 05:52:13 +02:00
|
|
|
criterion.setdefault('reverse', False)
|
|
|
|
criterion.setdefault('errorPosition', 1)
|
|
|
|
criterion.setdefault('blankPosition', 2)
|
2011-04-26 08:02:44 +02:00
|
|
|
self.criteria.append(criterion)
|
|
|
|
|
|
|
|
def as_json(self):
|
|
|
|
return json.dumps({'criteria': self.criteria})
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self.criteria)
|