Changeset 4240


Ignore:
Timestamp:
29/09/08 09:49:12 (11 years ago)
Author:
cbyrom
Message:

Extend Person object to include comparison method for object equality
checks + add enum style 'type' constants and update references to use
these to make code clearer + add addAuthors() method to ensure Person
data is added and updated correctly + add string cast to eXistInterface
method to avoid problems sending DOS characters to eXist + add extra
logging and checks for setting up eXist connection.

Location:
exist/trunk/python/ndgUtils
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/eXistConnector.py

    r4229 r4240  
    9090        '''Execute an xquery string, return session and summary information''' 
    9191        logging.debug("Executing xquery on eXist:\n%s" %xquery) 
    92         xquery=xmlrpclib.Binary(xquery) 
     92        xquery=xmlrpclib.Binary(str(xquery)) 
    9393        id = self.xmlrpc.executeQuery(xquery, params) 
    9494        summary = self.xmlrpc.querySummary(id) 
  • exist/trunk/python/ndgUtils/eXistInterface.py

    r4229 r4240  
    66    ''' Adds ndg methods to a "standard" exist Connector ''' 
    77    def __init__(self,db='glue.badc.rl.ac.uk',passwordFile='passwords.txt'): 
    8         logging.debug("Initialising connection to eXist DB") 
     8        logging.debug("Initialising connection to eXist DB ('%s')" %db) 
    99        try: 
    1010            f=file(passwordFile,'r') 
  • exist/trunk/python/ndgUtils/models/Atom.py

    r4236 r4240  
    3535    Class representing atom author type data - with name, uri and role attributes 
    3636    ''' 
    37     def __init__(self, tagName = "author", namespace = None): 
    38         self.tagName = tagName 
     37    AUTHOR_TYPE = 0 
     38    CONTRIBUTOR_TYPE = 1 
     39    RESPONSIBLE_PARTY_TYPE = 2 
     40    ELEMENT_NAMES = ["author", "contributor", "responsibleParty"] 
     41     
     42    def __init__(self, personType = AUTHOR_TYPE, namespace = None): 
     43        self.type = personType 
    3944        self.ns = namespace 
    4045        self.name = "" 
    4146        self.uri = "" 
    4247        self.role = "" 
    43  
     48         
     49        # NB, responsible party data is always stored in the moles section 
     50        if self.type == self.RESPONSIBLE_PARTY_TYPE: 
     51            self.ns = 'moles' 
     52 
     53    def __str__(self): 
     54        if self.name or self.uri or self.role: 
     55            return self.name + " | " + self.uri + " | " + self.role 
     56        return "" 
     57     
    4458    def fromString(self, personString): 
    4559        (self.name, self.uri, self.role) = getTripleData(personString) 
     
    5771            prefix = self.ns + ':' 
    5872 
    59         author = ET.Element(prefix + self.tagName) 
     73        author = ET.Element(prefix + self.ELEMENT_NAMES[self.type]) 
    6074 
    6175        if self.name: 
     
    7387        return author 
    7488     
     89    def __cmp__(self, person1): 
     90        ''' 
     91        Override comparison to allow proper object comparison when checking 
     92        if Person objects are in an array already - i.e. if person in personArray... 
     93        ''' 
     94        if self is person1: 
     95            return 0 
     96        elif self.uri == person1.uri and self.name == person1.name and \ 
     97                self.role == person1.role and self.type == person1.type: 
     98            return 0 
     99        return 1 
    75100 
    76101class Link(): 
     
    196221        # if inputs passed in as dict, add these now 
    197222        if inputs: 
     223            logging.info("Adding info to atom from input dict") 
     224            logging.debug(inputs) 
    198225            self.__dict__.update(inputs) 
    199226             
     227            # NB, this doesn't trigger the Summary Property, so do this 
     228            # explicitly, if need be 
     229            if inputs.get('Summary'): 
     230                self.Summary = inputs.get('Summary') 
     231             
     232            # also pass any moles data up to the moles entity object 
    200233            if inputs.get('providerID'): 
    201234                self.ME.providerID = inputs.get('providerID') 
    202  
    203235 
    204236        if self.atomTypeID: 
     
    262294        self.ME.createdDate = getISO8601Date(object_creation_time) 
    263295        logging.debug('Moles entity information added') 
     296 
     297 
     298    def addAuthors(self, authors): 
     299        ''' 
     300        Add author data appropriately to the atom 
     301        NB, these will overwrite any existing authors of the same type 
     302        @param authors: list of Person objects with the author data 
     303        ''' 
     304        logging.debug('Adding authors data to Atom') 
     305        isFirstAuthor = {} 
     306        authorArray = None 
     307        for author in authors: 
     308            # NB, we're only allowed one atom author 
     309            if author.type == Person.AUTHOR_TYPE: 
     310                self.author = author 
     311                if isFirstAuthor.has_key(author.type): 
     312                    raise AtomError("Error: an atom can only have one author specified") 
     313                isFirstAuthor[author.type] = 1 
     314                continue 
     315            elif author.type == Person.CONTRIBUTOR_TYPE: 
     316                authorArray = self.contributors 
     317            elif author.type == Person.RESPONSIBLE_PARTY_TYPE: 
     318                authorArray = self.ME.responsibleParties 
     319                 
     320            # check if this is the first addition - if so, clear out the 
     321            # array in advance 
     322            if not isFirstAuthor.has_key(author.type): 
     323                logging.debug("Clearing out author array") 
     324                # NB, need to be careful to clear the array, not create a ref 
     325                # to a new array 
     326                del authorArray[:] 
     327                isFirstAuthor[author.type] = 1 
     328 
     329            if str(author) != "" and author not in authorArray: 
     330                logging.debug("Adding author (type:'%s', name:'%s', uri:'%s', role:'%s')" \ 
     331                              %(author.type, author.name, author.uri, author.role)) 
     332                authorArray.append(author) 
     333 
     334        logging.debug('Finished adding authors data') 
    264335 
    265336 
     
    360431        root.append(self.author.toXML()) 
    361432             
    362         # NB, only the first author in the list is the author; the rest are contrinbutors 
    363433        for contributor in self.contributors: 
    364434            root.append(contributor.toXML()) 
     
    366436        # add the moles entity section, if it is required 
    367437        if self.ME: 
    368             # add any authors info 
    369             for author in self.atomAuthors: 
    370                 if author not in self.ME.responsibleParties: 
    371                     self.ME.responsibleParties.append(author) 
    372438            root.append(self.ME.toXML()) 
    373439 
     
    380446                     
    381447        summary = ET.SubElement(root, "summary") 
    382         summary.text = self.Summary 
     448        summary.text = escapeSpecialCharacters(self.Summary) 
    383449 
    384450        # add link to content, if required - NB, can only have one content element in atom 
     
    473539        for contributorElement in contributorElements: 
    474540            logging.debug("Adding contributor data") 
    475             contributor = Person(tagName = 'contributor') 
     541            contributor = Person(personType = Person.CONTRIBUTOR_TYPE) 
    476542            contributor.fromETElement(contributorElement) 
    477543            self.contributors.append(contributor) 
     
    480546        if molesElement: 
    481547            self.ME.fromET(molesElement) 
    482             for author in self.ME.responsibleParties: 
    483                 self.atomAuthors.append(author) 
    484548                 
    485549        self.atomID = tree.findtext('id') 
     
    700764            if len(attributeValue) > 1: 
    701765                setattr(self, "contributors", attributeValue[1:]) 
     766        elif attributeName == "atomAuthors": 
     767            self.ME.responsibleParties.append(attributeValue) 
    702768        else: 
    703769            setattr(self, attributeName, attributeValue) 
  • exist/trunk/python/ndgUtils/models/MolesEntity.py

    r4236 r4240  
    144144         
    145145        for vals in partyVals: 
    146             rp = Person(tagName = 'responsibleParty', namespace = 'moles') 
     146            rp = Person(personType = Person.RESPONSIBLE_PARTY_TYPE) 
    147147            rp.fromString(vals) 
    148148            self.responsibleParties.append(rp) 
     
    271271        for authorElement in authorElements: 
    272272            logging.debug("Adding atom author data") 
    273             author = Person(tagName = 'responsibleParty', namespace = 'moles') 
     273            author = Person(personType = Person.RESPONSIBLE_PARTY_TYPE) 
    274274            author.fromETElement(authorElement) 
    275275            self.responsibleParties.append(author) 
  • exist/trunk/python/ndgUtils/models/existdbclient.py

    r4236 r4240  
    2121class eXistDBClient: 
    2222     
    23      
    2423    def __init__(self, configFile = None, eXistDBHostname = None): 
    2524        ''' 
     
    2827        logging.info("Initialising connection to eXist DB") 
    2928        self.eXistDBHostname = eXistDBHostname 
    30          
     29        logging.debug("- connecting to DB, '%s', with config file, '%s'" \ 
     30                      %(eXistDBHostname or 'Default', configFile or 'Default')) 
    3131        inputs = {} 
    32         # Firstly load in config data 
     32         
     33        # NB, there are two routes through here: if a config file is specified 
     34        # without a hostname, the host will be taken to be the first entry in 
     35        # the config file; if a hostname is specified, it will be used explicitly 
    3336        if configFile: 
    34             self._loadDBDetails(configFile) 
     37            if not self.eXistDBHostname: 
     38                self._loadDBDetails(configFile) 
    3539            inputs['passwordFile'] = configFile 
    3640             
     
    3943             
    4044        # Now set up the connection 
     45        logging.debug(inputs) 
    4146        self.xmldb = ndg_eXist(**inputs) 
    4247         
Note: See TracChangeset for help on using the changeset viewer.