Changeset 22

Show
Ignore:
Timestamp:
01/09/07 23:51:12 (5 years ago)
Author:
nicolast
Message:

Updated to new spec, included doctest

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/djangoid/microidutils.py

    r14 r22  
    2020from HTMLParser import HTMLParser, HTMLParseError 
    2121 
    22 def microid(c, p): 
    23         return sha.new(sha.new(c).hexdigest() + sha.new(p).hexdigest()).hexdigest() 
     22def microid(c, p, newstyle = True, hash = "sha1"): 
     23        """ 
     24        This function generates a MicroID digest based on claimer URI and property URI 
     25 
     26        >>> microid("http://nicolast.be", "http://www.eikke.com", False) 
     27        'a6a18b671b0239ed85a32543ec487f443582e926' 
     28        >>> microid("http://nicolast.be", "http://blog.eikke.com") 
     29        'http+http:sha1:207ec367c4f64dc9d37c47fb490ed9c2f686f875' 
     30        >>> microid("mailto:spambox@nicolast.be", "http://nicolast.be") 
     31        'mailto+http:sha1:6646f18368bc40c4095092d61807ae98de9ef19a' 
     32        """ 
     33        hashfunc = None 
     34        if hash == "sha1": 
     35                hashfunc = sha.new 
     36        else: 
     37                raise Exception, "Only sha1 hashing is supported for now" 
     38 
     39        digest = hashfunc(hashfunc(c).hexdigest() + hashfunc(p).hexdigest()).hexdigest() 
     40 
     41        if newstyle: 
     42                s1 = c.split(":")[0] 
     43                s2 = p.split(":")[0] 
     44                digest = "%(firsturi)s+%(seconduri)s:%(hash)s:%(microid)s" % {"firsturi": s1, "seconduri": s2, "hash": hash, "microid": digest} 
     45        return digest 
    2446 
    2547class MicroIDParser(HTMLParser): 
     
    6587 
    6688        return ret 
     89 
     90def _test(): 
     91    import doctest 
     92    doctest.testmod() 
     93 
     94if __name__ == "__main__": 
     95    _test()