def add_error(component, method, category=None, message=None, exception=None): if exception is not None: if isinstance(exception, urllib2.URLError) and hasattr( exception, 'reason') and isinstance( exception.reason, socket.timeout): exception = exception.reason if isinstance(exception, socket.timeout): category = 'timeout' message = 'The server did not respond' elif isinstance(exception, urllib2.HTTPError): category = 'server' message = unicode( getattr(exception, 'reason', 'The server did not respond as expected')) elif isinstance(exception, urllib2.URLError): category = 'connection' message = unicode( getattr(exception, 'reason', 'The server could not be found')) error = spineapi.Annotation() error['concept'] = 'Error' error['property:component'] = component error['property:method'] = method error['property:category'] = category if message is not None: error['property:message'] = message document.addAnnotation(error, 'errors.metadata')
def add_success(component, method): error = spineapi.Annotation() error['concept'] = 'Success' error['property:component'] = component error['property:method'] = method error['property:category'] = 'success' document.addAnnotation(error, 'errors.metadata')
def on_activate_event(self, document): text = document.text().encode('utf8') text_hash = hashlib.md5(text).hexdigest() url = 'http://beta.sciencewise.info/api/utopia' payload = urllib.urlencode({'text': text, 'chksum': text_hash}) response = urllib2.urlopen(url, payload, timeout=8).read() results = json.loads(response) annotations = [] for result in results: before = result.get('context', {}).get('before', '') term = result.get('value', '') after = result.get('context', {}).get('after', '') link = result.get('link') definitions = [] for definition in result.get('definitions', []): definitions.append( (definition.get('url'), definition.get('title'))) if len(term) > 0 and len(before) + len(term) + len( after) > 0 and link is not None: matches = document.findInContext(before, term, after) if len(matches) > 0: annotation = spineapi.Annotation() annotation['concept'] = 'ScienceWISE' annotation['property:webpageUrl'] = link annotation['property:term'] = term annotation['property:name'] = 'Definitions of {0}'.format( term) annotation[ 'property:description'] = 'ScienceWISE ontology definitions' annotation['property:sourceDatabase'] = 'sciencewise' annotation[ 'property:sourceDescription'] = '<p><a href="http://sciencewise.info/">ScienceWISE</a> provides phycists with article annotation and scientific bookmarking.</p>' for url, title in definitions: annotation.insertProperty('property:definitions', '{0} {1}'.format(url, title)) for match in matches: annotation.addExtent(match) annotations.append(annotation) if len(annotations) > 0: document.addAnnotations(annotations)