1818"""
1919
2020from io import BytesIO
21- import base64
2221import gzip
2322import json
2423import logging
2524import sys
2625import time
2726import os
28- from socket import gethostbyname , gaierror
29- from datetime import datetime
27+ from socket import gaierror
3028import requests
3129
32- _BINDING_VERSION = "1.1"
30+ _BINDING_VERSION = "1.1.1 "
3331_GZIP_BYTEARRAY = bytearray ([0x1F , 0x8b , 0x08 ])
3432
3533_IsPy3 = sys .version_info [0 ] == 3
@@ -145,14 +143,18 @@ def __getitem__(self, key):
145143 def validate (self ):
146144 pass
147145
148- def serialize (self ):
146+ def serialize (self , options ):
149147 self .validate ()
150148 v = {}
151149 for (key , val ) in self .__params .items ():
152150 if val is None :
153151 pass
154152 else :
155153 v [key ] = val
154+
155+ if options is not None and len (options ) > 0 :
156+ v ['options' ] = options
157+
156158 return v
157159
158160
@@ -203,10 +205,10 @@ def validate(self):
203205 "Cannot supply both Content and ContentUri" ,
204206 "bad arguments" )
205207
206- def serialize (self ):
208+ def serialize (self , options ):
207209 """Internal. Do not use."""
208210 self .validate ()
209- slz = super (DocumentParameters , self ).serialize ()
211+ slz = super (DocumentParameters , self ).serialize (options )
210212 return slz
211213
212214 def load_document_file (self , path ):
@@ -229,18 +231,6 @@ def load_document_string(self, s):
229231 self ["content" ] = s
230232
231233
232- class RelationshipsParameters (DocumentParameters ):
233-
234- """Parameter object for relationships endpoint. Inherits from L(DocumentParameters), but allows the user
235- to specify the relationships-unique options parameter."""
236-
237- def __init__ (self ):
238- """Create a L{RelationshipsParameters} object."""
239- self .useMultipart = False
240- _DocumentParamSetBase .__init__ (
241- self , ("content" , "contentUri" , "language" , "options" , "genre" ))
242-
243-
244234class NameTranslationParameters (_DocumentParamSetBase ):
245235 """Parameter object for C{name-translation} endpoint.
246236 The following values may be set by the indexing (i.e.,C{ parms["name"]}) operator. The values are all
@@ -348,7 +338,6 @@ def __init__(self, api, suburl):
348338 self .user_key = api .user_key
349339 self .logger = api .logger
350340 self .useMultipart = False
351- self .checker = lambda : api .check_version ()
352341 self .suburl = suburl
353342 self .debug = api .debug
354343 self .api = api
@@ -376,39 +365,26 @@ def info(self):
376365 @return: A dictionary telling server version and other
377366 identifying data."""
378367 url = self .service_url + "info"
368+ headers = {'Accept' : 'application/json' , 'X-RosetteAPI-Binding' : 'python' , 'X-RosetteAPI-Binding-Version' : _BINDING_VERSION }
379369 if self .debug :
380370 headers ['X-RosetteAPI-Devel' ] = 'true'
381371 self .logger .info ('info: ' + url )
382- headers = {'Accept' : 'application/json' }
383372 if self .user_key is not None :
384373 headers ["X-RosetteAPI-Key" ] = self .user_key
385374 r = self .api ._get_http (url , headers = headers )
386375 return self .__finish_result (r , "info" )
387376
388- def checkVersion (self ):
389- """Issues a special "info" request to the L{EndpointCaller}'s specific endpoint.
390- @return: A dictionary containing server version as well as version check"""
391- url = self .service_url + "info?clientVersion=" + _BINDING_VERSION
392- if self .debug :
393- headers ["X-RosetteAPI-Devel" ] = 'true'
394- self .logger .info ('info: ' + url )
395- headers = {'Accept' : 'application/json' }
396- if self .user_key is not None :
397- headers ["X-RosetteAPI-Key" ] = self .user_key
398- r = self .api ._post_http (url , None , headers )
399- return self .__finish_result (r , "info" )
400-
401377 def ping (self ):
402378 """Issues a "ping" request to the L{EndpointCaller}'s (server-wide) endpoint.
403379 @return: A dictionary if OK. If the server cannot be reached,
404380 or is not the right server or some other error occurs, it will be
405381 signalled."""
406382
407383 url = self .service_url + 'ping'
384+ headers = {'Accept' : 'application/json' , 'X-RosetteAPI-Binding' : 'python' , 'X-RosetteAPI-Binding-Version' : _BINDING_VERSION }
408385 if self .debug :
409386 headers ['X-RosetteAPI-Devel' ] = 'true'
410387 self .logger .info ('Ping: ' + url )
411- headers = {'Accept' : 'application/json' }
412388 if self .user_key is not None :
413389 headers ["X-RosetteAPI-Key" ] = self .user_key
414390 r = self .api ._get_http (url , headers = headers )
@@ -421,7 +397,7 @@ def call(self, parameters):
421397 endpoints except C{name-translation} and C{name-similarity}, it must be a L{DocumentParameters}
422398 object or a string; for C{name-translation}, it must be an L{NameTranslationParameters} object;
423399 for C{name-similarity}, it must be an L{NameSimilarityParameters} object. For relationships,
424- it may be an L(DocumentParameters) or an L(RelationshipsParameters) .
400+ it may be an L(DocumentParameters).
425401
426402 In all cases, the result is returned as a python dictionary
427403 conforming to the JSON object described in the endpoint's entry
@@ -445,14 +421,14 @@ def call(self, parameters):
445421 "Text-only input only works for DocumentParameter endpoints" ,
446422 self .suburl )
447423
448- self .checker ()
449-
450424 self .useMultipart = parameters .useMultipart
451425 url = self .service_url + self .suburl
452- params_to_serialize = parameters .serialize ()
426+ params_to_serialize = parameters .serialize (self . api . options )
453427 headers = {}
454428 if self .user_key is not None :
455429 headers ["X-RosetteAPI-Key" ] = self .user_key
430+ headers ["X-RosetteAPI-Binding" ] = "python"
431+ headers ["X-RosetteAPI-Binding-Version" ] = _BINDING_VERSION
456432 if self .useMultipart :
457433 params = dict (
458434 (key ,
@@ -515,7 +491,6 @@ def __init__(
515491 self .logger = logging .getLogger ('rosette.api' )
516492 self .logger .info ('Initialized on ' + self .service_url )
517493 self .debug = debug
518- self .version_checked = False
519494
520495 if (retries < 1 ):
521496 retries = 1
@@ -526,6 +501,7 @@ def __init__(
526501 self .reuse_connection = reuse_connection
527502 self .connection_refresh_duration = refresh_duration
528503 self .http_connection = None
504+ self .options = {}
529505
530506 def _connect (self , parsedUrl ):
531507 """ Simple connection method
@@ -587,7 +563,7 @@ def _make_request(self, op, url, data, headers):
587563 raise RosetteException (code , message , url )
588564 except :
589565 raise
590- except (httplib .BadStatusLine , gaierror ) as e :
566+ except (httplib .BadStatusLine , gaierror ):
591567 raise RosetteException (
592568 "ConnectionError" ,
593569 "Unable to establish connection to the Rosette API server" ,
@@ -631,22 +607,36 @@ def _post_http(self, url, data, headers):
631607
632608 return _ReturnObject (_my_loads (rdata , response_headers ), status )
633609
634- def check_version (self ):
610+ def setOption (self , name , value ):
635611 """
636- Info call to check binding version against the current Rosette API
612+ Sets an option
613+
614+ @param name: name of option
615+ @param value: value of option
637616 """
638- if self .version_checked :
639- return True
640- op = EndpointCaller (self , None )
641- result = op .checkVersion ()
642- if 'versionChecked' not in result or result ['versionChecked' ] is False :
643- raise RosetteException (
644- "incompatibleVersion" ,
645- "The server version is not compatible with binding version " +
646- _BINDING_VERSION ,
647- '' )
648- self .version_checked = True
649- return True
617+ if value is None :
618+ self .options .pop (name , None )
619+ else :
620+ self .options [name ] = value
621+
622+ def getOption (self , name ):
623+ """
624+ Gets an option
625+
626+ @param name: name of option
627+
628+ @return: value of option
629+ """
630+ if name in self .options .keys ():
631+ return self .options [name ]
632+ else :
633+ return None
634+
635+ def clearOptions (self ):
636+ """
637+ Clears all options
638+ """
639+ self .options .clear ()
650640
651641 def ping (self ):
652642 """
@@ -702,19 +692,19 @@ def morphology(self, parameters, facet=MorphologyOutput.COMPLETE):
702692 @return: A python dictionary containing the results of morphological analysis."""
703693 return EndpointCaller (self , "morphology/" + facet ).call (parameters )
704694
705- def entities (self , parameters , linked = False ):
695+ def entities (self , parameters , resolve_entities = False ):
706696 """
707697 Create an L{EndpointCaller} to identify named entities found in the texts
708698 to which it is applied and call it. Linked entity information is optional, and
709699 its need must be specified at the time the operator is created.
710700 @param parameters: An object specifying the data,
711701 and possible metadata, to be processed by the entity identifier.
712702 @type parameters: L{DocumentParameters} or L{str}
713- @param linked : Specifies whether or not linked entity information will
703+ @param resolve_entities : Specifies whether or not linked entity information will
714704 be wanted.
715- @type linked : Boolean
705+ @type resolve_entities : Boolean
716706 @return: A python dictionary containing the results of entity extraction."""
717- if linked :
707+ if resolve_entities :
718708 return EndpointCaller (self , "entities/linked" ).call (parameters )
719709 else :
720710 return EndpointCaller (self , "entities" ).call (parameters )
@@ -749,7 +739,7 @@ def relationships(self, parameters):
749739 which it is applied and call it.
750740 @param parameters: An object specifying the data,
751741 and possible metadata, to be processed by the relationships identifier.
752- @type parameters: L{DocumentParameters}, L(RelationshipsParameters), or L{str}
742+ @type parameters: L{DocumentParameters} or L{str}
753743 @return: A python dictionary containing the results of relationship extraction."""
754744 return EndpointCaller (self , "relationships" ).call (parameters )
755745
0 commit comments