def __init__(self, fields=None, required=('name', 'description', 'url'), whoosh_index=None): """ - fields : list of fields to use, or None to calculate dynamically - required : required data (strings) - whoosh_index : directory to keep whoosh index in """ self.required = set(required) # reserved fields self.reserved = self.required.copy() self.reserved.update(['modified']) # last modified, a computed value self.search = WhooshSearch(whoosh_index=whoosh_index) # classifier fields self._fields = fields self.field_set = set(fields or ())
class ProjectsModel(object): """ abstract base class for toolbox tools """ def __init__(self, fields=None, required=('name', 'description', 'url'), whoosh_index=None): """ - fields : list of fields to use, or None to calculate dynamically - required : required data (strings) - whoosh_index : directory to keep whoosh index in """ self.required = set(required) # reserved fields self.reserved = self.required.copy() self.reserved.update(['modified']) # last modified, a computed value self.search = WhooshSearch(whoosh_index=whoosh_index) # classifier fields self._fields = fields self.field_set = set(fields or ()) def update_search(self, project): """update the search index""" assert self.required.issubset( project.keys()) # XXX should go elsewhere fields = dict([(field, project[field]) for field in self.fields() if field in project]) # keys must be strings, not unicode, on some systems f = dict([(str(i), j) for i, j in fields.items()]) self.search.update(name=project['name'], description=project['description'], **f) def fields(self): """what fields does the model support?""" if self._fields is not None: return self._fields return list(self.field_set) def projects(self): """list of all projects""" return [i['name'] for i in self.get()] def export(self, other): """export the current model to another model instance""" for project in self.get(): other.update(project) def rename_field_value(self, field, from_value, to_value): projects = self.get(None, **{field: from_value}) for project in projects: project[field].remove(from_value) project[field].append(to_value) self.update(project) ### implementor methods def update(self, project): """update a project""" raise NotImplementedError def get(self, search=None, **query): """ get a list of projects matching a query the query should be key, value pairs to match; if the value is single, it should be a string; if the value is multiple, it should be a set which will be ANDed together """ raise NotImplementedError def project(self, name): """get a project of a particular name, or None if there is none""" raise NotImplementedError def field_query(self, field): """get projects according to a particular field, or None""" raise NotImplementedError def delete(self, project): raise NotImplementedError
class ProjectsModel(object): """ abstract base class for toolbox tools """ def __init__(self, fields=None, required=('name', 'description', 'url'), whoosh_index=None): """ - fields : list of fields to use, or None to calculate dynamically - required : required data (strings) - whoosh_index : directory to keep whoosh index in """ self.required = set(required) # reserved fields self.reserved = self.required.copy() self.reserved.update(['modified']) # last modified, a computed value self.search = WhooshSearch(whoosh_index=whoosh_index) # classifier fields self._fields = fields self.field_set = set(fields or ()) def update_search(self, project): """update the search index""" assert self.required.issubset(project.keys()) # XXX should go elsewhere fields = dict([(field, project[field]) for field in self.fields() if field in project]) # keys must be strings, not unicode, on some systems f = dict([(str(i), j) for i, j in fields.items()]) self.search.update(name=project['name'], description=project['description'], **f) def fields(self): """what fields does the model support?""" if self._fields is not None: return self._fields return list(self.field_set) def projects(self): """list of all projects""" return [i['name'] for i in self.get()] def export(self, other): """export the current model to another model instance""" for project in self.get(): other.update(project) def rename_field_value(self, field, from_value, to_value): projects = self.get(None, **{field: from_value}) for project in projects: project[field].remove(from_value) project[field].append(to_value) self.update(project) ### implementor methods def update(self, project): """update a project""" raise NotImplementedError def get(self, search=None, **query): """ get a list of projects matching a query the query should be key, value pairs to match; if the value is single, it should be a string; if the value is multiple, it should be a set which will be ANDed together """ raise NotImplementedError def project(self, name): """get a project of a particular name, or None if there is none""" raise NotImplementedError def field_query(self, field): """get projects according to a particular field, or None""" raise NotImplementedError def delete(self, project): raise NotImplementedError