def __init__(self, ctx):
        self.closed = 0
        self.ctx = ctx  # component context
        self._client = MendeleyHttpClient()

        self._formattedCitationsResponse = MendeleyHttpClient.ResponseBody()

        self.citationClusters = []
        self.citationStyleUrl = ""
        self.formattedBibliography = []

        self._previousResultLength = 0
    def getUserUuid(self):
        response = self._client.userUuid()

        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.uuid
    def getDesktopSelectedStyleId(self):
        response = self._client.citationStyle_selected()

        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.citationStyleId
Пример #4
0
    def getCitationStylePresentationType(self):
        response = self._client.citationStylePresentationType()

        if response.status != 200:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.presentationType
    def mendeleyDesktopInfo(self):
        response = self._client.mendeleyDesktopInfo()
        try:
            assert (response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        result = {"processId": response.body.processId}
        return result
Пример #6
0
 def citation_undoManualFormat(self, fieldCode):
     citationCluster = self._citationClusterFromFieldCode(fieldCode)
     response = self._client.citation_undoManualFormat(citationCluster)
     try:
         assert(response.status == 200)
         fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
     except:
         raise MendeleyHttpClient.UnexpectedResponse(response)
     return fieldCode
Пример #7
0
 def getFieldCodeFromUuid(self, documentUuid):
     response = self._client.testMethods_citationCluster_getFromUuid(
         {"documentUuid": documentUuid})
     try:
         assert(response.status == 200)
         fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
     except:
         raise MendeleyHttpClient.UnexpectedResponse(response)
     return fieldCode
Пример #8
0
 def citation_edit_interactive(self, fieldCode, hintText):
     citationCluster = self._citationClusterFromFieldCode(fieldCode)
     citationCluster["citationEditorHint"] = hintText
     response = self._client.citation_edit_interactive(citationCluster)
     try:
         assert(response.status == 200)
         fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
     except:
         raise MendeleyHttpClient.UnexpectedResponse(response)
     return fieldCode
Пример #9
0
    def citation_choose_interactive(self, hintText):
        response = self._client.citation_choose_interactive(
            {"citationEditorHint": hintText})
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return fieldCode
Пример #10
0
    def citation_update_interactive(self, fieldCode, formattedText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["formattedText"] = formattedText

        response = self._client.citation_update_interactive(citationCluster)
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode
Пример #11
0
    def wordProcessor_set(self, wordProcessor, version):
        response = self._client.wordProcessor_set({
            "wordProcessor": wordProcessor,
            "version": version
        })

        try:
            assert (response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return ""
    def __init__(self, ctx):
        self.closed = 0
        self.ctx = ctx # component context
        self._client = MendeleyHttpClient()
        
        self._formattedCitationsResponse = MendeleyHttpClient.ResponseBody()

        self.citationClusters = []
        self.citationStyleUrl = ""
        self.formattedBibliography = []

        self._previousResultLength = 0
Пример #13
0
    def citations_merge(self, *fieldCodes):
        clusters = []

        for fieldCode in fieldCodes:
            clusters.append(self._citationClusterFromFieldCode(fieldCode))

        response = self._client.citations_merge({"citationClusters": clusters})
        try:
            assert (response.status == 200)
            mergedFieldCode = \
                self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return mergedFieldCode
Пример #14
0
class MendeleyDesktopAPI(unohelper.Base, XJob):
    def __init__(self, ctx):
        self.closed = 0
        self.ctx = ctx  # component context
        self._client = MendeleyHttpClient()

        self._formattedCitationsResponse = MendeleyHttpClient.ResponseBody()

        self.citationClusters = []
        self.citationStyleUrl = ""
        self.formattedBibliography = []

        self._previousResultLength = 0

    def resetCitations(self):
        self.citationClusters = []

    def _citationClusterFromFieldCode(self, fieldCode):
        # remove ADDIN and CSL_CITATION from start
        pattern = re.compile("CSL_CITATION[ ]*({.*$)")
        match = pattern.search(fieldCode)

        if match == None:
            result = {"fieldCode": fieldCode.decode('string_escape')}
        else:
            bareJson = match.group(1)
            citationCluster = json.loads(bareJson)
            result = {"citationCluster": citationCluster}
        return result

    def addCitationCluster(self, fieldCode):
        self.citationClusters.append(
            self._citationClusterFromFieldCode(fieldCode))

    def addFormattedCitation(self, formattedCitation):
        self.citationClusters[len(self.citationClusters) -
                              1]["formattedText"] = formattedCitation

    def setCitationStyle(self, citationStyleUrl):
        self.citationStyleUrl = citationStyleUrl

    def getCitationStyleId(self):
        return self.citationStyleUrl

    def formatCitationsAndBibliography(self):
        self._formattedCitationsResponse = \
            self._client.formattedCitationsAndBibliography_Interactive(
            self.citationStyleUrl, self.citationClusters).body

        return json.dumps(self._formattedCitationsResponse.__dict__)

    def getCitationCluster(self, index):
        return "ADDIN CSL_CITATION " + json.dumps(
            self._formattedCitationsResponse.citationClusters[int(
                index)]["citationCluster"])


#        return "citation cluster"

    def getFormattedCitation(self, index):
        return self._formattedCitationsResponse.citationClusters[int(
            index)]["formattedText"]

    def getFormattedBibliography(self):
        # a single string is interpreted as a file name
        if (type(self._formattedCitationsResponse.bibliography)
                == type(u"unicode string")
                or type(self._formattedCitationsResponse.bibliography)
                == type("string")):
            return self._formattedCitationsResponse.bibliography
        else:
            return "<br/>".join(self._formattedCitationsResponse.bibliography)

    def getUserAccount(self):
        response = self._client.userAccount()

        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.account

    def getUserUuid(self):
        response = self._client.userUuid()

        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.uuid

    def citationStyle_choose_interactive(self, styleId):
        return self._client.citationStyle_choose_interactive({
            "currentStyleUrl":
            styleId
        }).body.citationStyleUrl

    def citation_choose_interactive(self, hintText):
        response = self._client.citation_choose_interactive(
            {"citationEditorHint": hintText})
        try:
            assert (response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(
                response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return fieldCode

    def citation_edit_interactive(self, fieldCode, hintText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["citationEditorHint"] = hintText
        response = self._client.citation_edit_interactive(citationCluster)
        try:
            assert (response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(
                response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def setDisplayedText(self, displayedText):
        self.formattedText = displayedText

    def citation_update_interactive(self, fieldCode, formattedText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["formattedText"] = formattedText

        response = self._client.citation_update_interactive(citationCluster)
        try:
            assert (response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(
                response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def getFieldCodeFromUuid(self, documentUuid):
        response = self._client.testMethods_citationCluster_getFromUuid(
            {"documentUuid": documentUuid})
        try:
            assert (response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(
                response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def getDesktopSelectedStyleId(self):
        response = self._client.citationStyle_selected()

        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.citationStyleId

    def _fieldCodeFromCitationCluster(self, citationCluster):
        if ("citationItems" in citationCluster):
            if (len(citationCluster["citationItems"]) == 0):
                return ""

        return "ADDIN CSL_CITATION " + json.dumps(citationCluster,
                                                  sort_keys=True)

    def citation_undoManualFormat(self, fieldCode):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        response = self._client.citation_undoManualFormat(citationCluster)
        try:
            assert (response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(
                response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def citations_merge(self, *fieldCodes):
        clusters = []

        for fieldCode in fieldCodes:
            clusters.append(self._citationClusterFromFieldCode(fieldCode))

        response = self._client.citations_merge({"citationClusters": clusters})
        try:
            assert (response.status == 200)
            mergedFieldCode = \
                self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return mergedFieldCode

    def wordProcessor_set(self, wordProcessor, version):
        response = self._client.wordProcessor_set({
            "wordProcessor": wordProcessor,
            "version": version
        })

        try:
            assert (response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return ""

    def mendeleyDesktopInfo(self):
        response = self._client.mendeleyDesktopInfo()
        try:
            assert (response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        result = {"processId": response.body.processId}
        return result

    def isMendeleyDesktopRunningStr(self):
        try:
            response = self._client.mendeleyDesktopInfo()
            return str(response.status == 200)
        except:
            return False

    # for testing
    def setNumberTest(self, number):
        self.number = number.decode('string_escape')
        return ""

    # for testing
    def getNumberTest(self):
        return str(self.number)

    # for testing
    def concatenateStringsTest(self, string1, string2):
        return str(string1) + str(string2)

    def previousSuccess(self):
        previousResponse = self._client.previousResponse

        return str(previousResponse.status == 200)

    def previousErrorMessage(self):
        previousResponse = self._client.previousResponse

        downloadInstructions = "Please download the latest version " + \
                "of Mendeley Desktop here: \n" + \
                "http://www.mendeley.com/download-mendeley-desktop"

        if (previousResponse.status == 406 or previousResponse.status == 415):
            if (previousResponse.contentType.startswith(
                    "application/vnd.mendeley.typeDeprecatedError")):
                # TODO: insert link to plugin download
                return "Deprecated type error. Please update this plugin to work with the " + \
                    "current version of Mendeley Desktop"
            else:
                return "Unknown type error. " + downloadInstructions

        if (previousResponse.status == 404):
            return "Page not found. " + downloadInstructions

        if (previousResponse.status != 200):
            return "Unknown error\n" + json.dumps(previousResponse.__dict__)

        return ""

    def previousResultLength(self):
        return self._previousResultLength

    def previousResponse(self):
        return json.dumps(self.previousResponse.__dict__)

    def execute(self, args):
        functionName = str(args[0].Value)
        statement = 'self.' + functionName + '('
        for arg in range(1, len(args)):
            statement += '"'
            data = codecs.getencoder('unicode_escape')(args[arg].Value)[0]
            if python3:
                data = data.decode('utf-8')
            statement += data.replace('"', '\\"')
            statement += '"'
            if arg < len(args) - 1:
                statement += ', '
        statement += ')'

        if hasattr(self, functionName):
            try:
                result = eval(statement)
                self._previousResultLength = len(unicode(result))
                return result
            except MendeleyHttpClient.UnexpectedResponse:
                return ""
        else:
            raise Exception("ERROR: Function " + functionName +
                            " doesn't exist")
class MendeleyDesktopAPI(unohelper.Base, XJob):
    def __init__(self, ctx):
        self.closed = 0
        self.ctx = ctx # component context
        self._client = MendeleyHttpClient()
        
        self._formattedCitationsResponse = MendeleyHttpClient.ResponseBody()

        self.citationClusters = []
        self.citationStyleUrl = ""
        self.formattedBibliography = []

        self._previousResultLength = 0

    def resetCitations(self):
        self.citationClusters = []

    def _citationClusterFromFieldCode(self, fieldCode):
        # remove ADDIN and CSL_CITATION from start
        pattern = re.compile("CSL_CITATION[ ]*({.*$)")
        match = pattern.search(fieldCode)

        if match == None:
            result = {"fieldCode" : fieldCode.decode('string_escape')}
        else:
            bareJson = match.group(1)
            citationCluster = json.loads(bareJson)
            result = {"citationCluster" : citationCluster}
        return result

    def addCitationCluster(self, fieldCode):
        self.citationClusters.append(self._citationClusterFromFieldCode(fieldCode))

    def addFormattedCitation(self, formattedCitation):
        self.citationClusters[len(self.citationClusters)-1]["formattedText"] = formattedCitation
    
    def setCitationStyle(self, citationStyleUrl):
        self.citationStyleUrl = citationStyleUrl

    def getCitationStyleId(self):
        return self.citationStyleUrl

    def formatCitationsAndBibliography(self):
        self._formattedCitationsResponse = \
            self._client.formattedCitationsAndBibliography_Interactive(
            self.citationStyleUrl, self.citationClusters).body

        return json.dumps(self._formattedCitationsResponse.__dict__)

    def getCitationCluster(self, index):
        return "ADDIN CSL_CITATION " + json.dumps(self._formattedCitationsResponse.citationClusters[int(index)]["citationCluster"])
    
    def getCitationClusterUUIDs(self, index):
        cites = self._formattedCitationsResponse.citationClusters[int(index)]["citationCluster"]["citationItems"]
        uris = []
        if len(cites) == 1:
            uris.append(cites[0]["uris"])
        else:
            uris.extend([cite["uris"] for cite in cites])

        pattern = re.compile(".*\?uuid=(.*)']")
        matches = [pattern.search(str(uri)).group(1) for uri in uris]
        return matches

    def getCitationClusterDOIs(self, index):
        cites = self._formattedCitationsResponse.citationClusters[int(index)]["citationCluster"]["citationItems"]
        dois = []

        if len(cites) == 1:
            try:
                dois.append(str(cites[0]['itemData']["DOI"]))
            except KeyError:
                raise KeyError
        else:
            dois.extend([str(cite['itemData']["DOI"]) for cite in cites])

        return dois

    def getLocalURLs(self, index): 
        return ["mendeley://library/document/"+uuid for uuid in self.getCitationClusterUUIDs(index)]

    def getDOIURLs(self, index, addUUID=False):
        dois = self.getCitationClusterDOIs(index)
        if addUUID: 
            uuids = self.getCitationClusterUUIDs(index)
            return ["http://dx.doi.org/"+doi+"?uuid="+uuid for doi, uuid in zip(dois, uuids)]
        else:
            return ["http://dx.doi.org/"+doi for doi in dois]

    def getFormattedCitation(self, index):
        return self._formattedCitationsResponse.citationClusters[int(index)]["formattedText"]

    def getFormattedCitations(self):
        return [c["formattedText"] for c in self._formattedCitationsResponse.citationClusters]

    def getFormattedCitationLength(self):
        return len(self._formattedCitationsResponse.citationClusters)

    def getFormattedBibliography(self):
        # a single string is interpreted as a file name
        if (type(self._formattedCitationsResponse.bibliography) == type(u"unicode string")
                or type(self._formattedCitationsResponse.bibliography) == type("string")):
            return self._formattedCitationsResponse.bibliography;
        else:
            return "<br/>".join(self._formattedCitationsResponse.bibliography)
        
    def getUserAccount(self):
        response = self._client.userAccount()
        
        if (response.status != 200):
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.account

    def citationStyle_choose_interactive(self, styleId):
        return self._client.citationStyle_choose_interactive(
            {"currentStyleUrl": styleId}).body.citationStyleUrl

    def citation_choose_interactive(self, hintText):
        response = self._client.citation_choose_interactive(
            {"citationEditorHint": hintText})
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return fieldCode
    
    def citation_edit_interactive(self, fieldCode, hintText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["citationEditorHint"] = hintText
        response = self._client.citation_edit_interactive(citationCluster)
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode
    
    def setDisplayedText(self, displayedText):
        self.formattedText = displayedText

    def citation_update_interactive(self, fieldCode, formattedText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["formattedText"] = formattedText

        response = self._client.citation_update_interactive(citationCluster)
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def getFieldCodeFromUuid(self, documentUuid):
        response = self._client.testMethods_citationCluster_getFromUuid(
            {"documentUuid": documentUuid})
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def _fieldCodeFromCitationCluster(self, citationCluster):
        if ("citationItems" in citationCluster):
            if (len(citationCluster["citationItems"]) == 0):
                return ""

        return "ADDIN CSL_CITATION " + json.dumps(citationCluster, sort_keys=True)

    def citation_undoManualFormat(self, fieldCode):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        response = self._client.citation_undoManualFormat(citationCluster)
        try:
            assert(response.status == 200)
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def citations_merge(self, *fieldCodes):
        clusters = []

        for fieldCode in fieldCodes:
            clusters.append(self._citationClusterFromFieldCode(fieldCode))

        response = self._client.citations_merge({"citationClusters": clusters})
        try:
            assert(response.status == 200)
            mergedFieldCode = \
                self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        
        return mergedFieldCode

    def wordProcessor_set(self, wordProcessor, version):
        response = self._client.wordProcessor_set(
            {
                "wordProcessor": wordProcessor,
                "version": version
            })

        try:
            assert(response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return ""

    def mendeleyDesktopInfo(self):
        response = self._client.mendeleyDesktopInfo()
        try:
            assert(response.status == 200)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        result = {"processId": response.body.processId}
        return result

    def isMendeleyDesktopRunningStr(self):
        try:
            response = self._client.mendeleyDesktopInfo()
            return str(response.status == 200)
        except:
            return False

    # for testing
    def setNumberTest(self, number):
        self.number = number.decode('string_escape')
        return ""

    # for testing
    def getNumberTest(self):
        return str(self.number)

    # for testing
    def concatenateStringsTest(self, string1, string2):
        return str(string1) + str(string2)

    def previousSuccess(self):
        previousResponse = self._client.previousResponse
        
        return str(previousResponse.status == 200)

    def previousErrorMessage(self):
        previousResponse = self._client.previousResponse
        
        downloadInstructions = "Please download the latest version " + \
                "of Mendeley Desktop here: \n" + \
                "http://www.mendeley.com/download-mendeley-desktop"

        if (previousResponse.status == 406 or 
            previousResponse.status == 415):
            if (previousResponse.contentType.startswith(
                "application/vnd.mendeley.typeDeprecatedError")):
                # TODO: insert link to plugin download
                return "Deprecated type error. Please update this plugin to work with the " + \
                    "current version of Mendeley Desktop"
            else:
                return "Unknown type error. " + downloadInstructions

        if (previousResponse.status == 404):
            return "Page not found. " + downloadInstructions

        if (previousResponse.status != 200):
            return "Unknown error\n" + json.dumps(previousResponse.__dict__)

        return ""

    def previousResultLength(self):
        return self._previousResultLength

    def previousResponse(self):
        return json.dumps(self.previousResponse.__dict__)

    def execute(self, args):
        functionName = str(args[0].Value)
        statement = 'self.' + functionName + '('
        for arg in range(1, len(args)):
            statement += '"'
            data = codecs.getencoder('unicode_escape')(args[arg].Value)[0]
            if python3:
                data = data.decode('utf-8')
            statement += data.replace('"', '\\"')
            statement += '"'
            if arg < len(args) - 1:
                statement += ', '
        statement += ')'

        if hasattr(self, functionName):
            try:
                result = eval(statement)
                self._previousResultLength = len(unicode(result))
                return result
            except MendeleyHttpClient.UnexpectedResponse:
                return ""
        else:
            raise Exception("ERROR: Function " + functionName + " doesn't exist")
class MendeleyDesktopAPI(unohelper.Base, XJob):
    def __init__(self, ctx):
        self.closed = 0
        self.ctx = ctx  # component context
        self._client = MendeleyHttpClient()

        self._formattedCitationsResponse = MendeleyHttpClient.ResponseBody()

        self.citationClusters = []
        self.citationStyleUrl = ""
        self.formattedBibliography = []

        self._previousResultLength = 0

    def resetCitations(self):
        self.citationClusters = []

    def _citationClusterFromFieldCode(self, fieldCode):
        # remove ADDIN and CSL_CITATION from start
        pattern = re.compile("CSL_CITATION[ ]*({.*$)")
        match = pattern.search(fieldCode)

        if match == None:
            result = {"fieldCode": fieldCode.decode("string_escape")}
        else:
            bareJson = match.group(1)
            citationCluster = json.loads(bareJson)
            result = {"citationCluster": citationCluster}
        return result

    def addCitationCluster(self, fieldCode):
        self.citationClusters.append(self._citationClusterFromFieldCode(fieldCode))

    def addFormattedCitation(self, formattedCitation):
        self.citationClusters[len(self.citationClusters) - 1]["formattedText"] = formattedCitation

    def setCitationStyle(self, citationStyleUrl):
        self.citationStyleUrl = citationStyleUrl

    def getCitationStyleId(self):
        return self.citationStyleUrl

    def formatCitationsAndBibliography(self):
        self._formattedCitationsResponse = self._client.formattedCitationsAndBibliography_Interactive(
            self.citationStyleUrl, self.citationClusters
        ).body

        return json.dumps(self._formattedCitationsResponse.__dict__)

    def getCitationCluster(self, index):
        return "ADDIN CSL_CITATION " + json.dumps(
            self._formattedCitationsResponse.citationClusters[int(index)]["citationCluster"]
        )

    #        return "citation cluster"

    def getFormattedCitation(self, index):
        return self._formattedCitationsResponse.citationClusters[int(index)]["formattedText"]

    def getFormattedBibliography(self):
        # a single string is interpreted as a file name
        if type(self._formattedCitationsResponse.bibliography) == type(u"unicode string") or type(
            self._formattedCitationsResponse.bibliography
        ) == type("string"):
            return self._formattedCitationsResponse.bibliography
        else:
            return "<br/>".join(self._formattedCitationsResponse.bibliography)

    def getUserAccount(self):
        response = self._client.userAccount()

        if response.status != 200:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.account

    def getUserUuid(self):
        response = self._client.userUuid()

        if response.status != 200:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.uuid

    def citationStyle_choose_interactive(self, styleId):
        return self._client.citationStyle_choose_interactive({"currentStyleUrl": styleId}).body.citationStyleUrl

    def citation_choose_interactive(self, hintText):
        response = self._client.citation_choose_interactive({"citationEditorHint": hintText})
        try:
            assert response.status == 200
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return fieldCode

    def citation_edit_interactive(self, fieldCode, hintText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["citationEditorHint"] = hintText
        response = self._client.citation_edit_interactive(citationCluster)
        try:
            assert response.status == 200
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def setDisplayedText(self, displayedText):
        self.formattedText = displayedText

    def citation_update_interactive(self, fieldCode, formattedText):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        citationCluster["formattedText"] = formattedText

        response = self._client.citation_update_interactive(citationCluster)
        try:
            assert response.status == 200
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def getFieldCodeFromUuid(self, documentUuid):
        response = self._client.testMethods_citationCluster_getFromUuid({"documentUuid": documentUuid})
        try:
            assert response.status == 200
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def getDesktopSelectedStyleId(self):
        response = self._client.citationStyle_selected()

        if response.status != 200:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return response.body.citationStyleId

    def _fieldCodeFromCitationCluster(self, citationCluster):
        if "citationItems" in citationCluster:
            if len(citationCluster["citationItems"]) == 0:
                return ""

        return "ADDIN CSL_CITATION " + json.dumps(citationCluster, sort_keys=True)

    def citation_undoManualFormat(self, fieldCode):
        citationCluster = self._citationClusterFromFieldCode(fieldCode)
        response = self._client.citation_undoManualFormat(citationCluster)
        try:
            assert response.status == 200
            fieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)
        return fieldCode

    def citations_merge(self, *fieldCodes):
        clusters = []

        for fieldCode in fieldCodes:
            clusters.append(self._citationClusterFromFieldCode(fieldCode))

        response = self._client.citations_merge({"citationClusters": clusters})
        try:
            assert response.status == 200
            mergedFieldCode = self._fieldCodeFromCitationCluster(response.body.citationCluster)
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return mergedFieldCode

    def wordProcessor_set(self, wordProcessor, version):
        response = self._client.wordProcessor_set({"wordProcessor": wordProcessor, "version": version})

        try:
            assert response.status == 200
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        return ""

    def mendeleyDesktopInfo(self):
        response = self._client.mendeleyDesktopInfo()
        try:
            assert response.status == 200
        except:
            raise MendeleyHttpClient.UnexpectedResponse(response)

        result = {"processId": response.body.processId}
        return result

    def isMendeleyDesktopRunningStr(self):
        try:
            response = self._client.mendeleyDesktopInfo()
            return str(response.status == 200)
        except:
            return False

    # for testing
    def setNumberTest(self, number):
        self.number = number.decode("string_escape")
        return ""

    # for testing
    def getNumberTest(self):
        return str(self.number)

    # for testing
    def concatenateStringsTest(self, string1, string2):
        return str(string1) + str(string2)

    def previousSuccess(self):
        previousResponse = self._client.previousResponse

        return str(previousResponse.status == 200)

    def previousErrorMessage(self):
        previousResponse = self._client.previousResponse

        downloadInstructions = (
            "Please download the latest version "
            + "of Mendeley Desktop here: \n"
            + "http://www.mendeley.com/download-mendeley-desktop"
        )

        if previousResponse.status == 406 or previousResponse.status == 415:
            if previousResponse.contentType.startswith("application/vnd.mendeley.typeDeprecatedError"):
                # TODO: insert link to plugin download
                return (
                    "Deprecated type error. Please update this plugin to work with the "
                    + "current version of Mendeley Desktop"
                )
            else:
                return "Unknown type error. " + downloadInstructions

        if previousResponse.status == 404:
            return "Page not found. " + downloadInstructions

        if previousResponse.status != 200:
            return "Unknown error\n" + json.dumps(previousResponse.__dict__)

        return ""

    def previousResultLength(self):
        return self._previousResultLength

    def previousResponse(self):
        return json.dumps(self.previousResponse.__dict__)

    def execute(self, args):
        functionName = str(args[0].Value)
        statement = "self." + functionName + "("
        for arg in range(1, len(args)):
            statement += '"'
            data = codecs.getencoder("unicode_escape")(args[arg].Value)[0]
            if python3:
                data = data.decode("utf-8")
            statement += data.replace('"', '\\"')
            statement += '"'
            if arg < len(args) - 1:
                statement += ", "
        statement += ")"

        if hasattr(self, functionName):
            try:
                result = eval(statement)
                self._previousResultLength = len(unicode(result))
                return result
            except MendeleyHttpClient.UnexpectedResponse:
                return ""
        else:
            raise Exception("ERROR: Function " + functionName + " doesn't exist")