Thread: SF.net SVN: fclient: [86] trunk/sandbox/fcp/fcp2_0_uri.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-01-30 13:19:44
|
Revision: 86
http://fclient.svn.sourceforge.net/fclient/?rev=86&view=rev
Author: jurner
Date: 2008-01-30 05:19:47 -0800 (Wed, 30 Jan 2008)
Log Message:
-----------
check keys to more depth
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_uri.py
Modified: trunk/sandbox/fcp/fcp2_0_uri.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_uri.py 2008-01-29 11:28:06 UTC (rev 85)
+++ trunk/sandbox/fcp/fcp2_0_uri.py 2008-01-30 13:19:47 UTC (rev 86)
@@ -1,82 +1,93 @@
-"""Freennet Client Protocol uri"""
+"""Freennet Client Protocol uri and related methods"""
+import base64
import re
+import urlparse
+#**************************************************************************************
+# freenet base64 for keys
+#**************************************************************************************
+def base64UrlsaveDecode(string):
+ """Decodes a base64 urlsave encoded string as encoded by freenet
+ @param string: string to decode
+ @return: decoded string
+
+ @raise TypeError: if the string can not be decoded
+ @note: this function handles non-standard encoding as used by freenet (see: freenet/support/base64.java)
+ """
+ # freenet uses - for + and ~ for /
+ altchars = '-~'
+
+ # padding may be ommitted or not
+ padding = 4 - len(string) % 4
+ if padding:
+ string += '=' * padding
+ return base64.b64decode(string, altchars)
+
+#****************************************************************************************
+# freenet keys
+#
+# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
+#
+# all byte components are base64 encoded. Freenet uses base64 without padding
+# and uses the following altchars for urlsave encode: - for + and ~ for /
+# see: freenet/support/base64.java
+#
+# so a key as the user gets it to see is:
+# KeyType@43 bytes, 43 bytes, 7 bytes ..of [A-Za-z0-9\-~]
+#
+#***************************************************************************************
+KeyPat = re.compile(
+r'''
+^(CHK | SSK | SVK | USK) @
+(
+ [a-z0-9\-~]{43},
+ [a-z0-9\-~]{43},
+ [a-z0-9\-~]{7}
+)
+''', re.I | re.X) #TODO: ignorecase?
+
+
+def keyType(uri):
+ """Returns the ky type of a freenet key or None if the type could not be determined"""
+ if uri.startswith('KSK@'):
+ return 'KSK'
+ result = KeyPat.match(uri)
+ if result is None:
+ return None
+ return result.group(1)
+
#*********************************************************************
#
#*********************************************************************
-
+def stripUri(uri):
+ """Strips scheme and location parts from an uri"""
+ result = urlparse.urlsplit(uri)[2]
+ result = result.lstrip('/')
+ return result
+
+
class Uri(object):
- """Wrapper class for freenet uris"""
+ KeyTypeSSK = 'SSK'
+ KeyTypeKSK = 'KSK'
+ KeyTypeCHK = 'CHK'
+ KeyTypeUSK = 'USK'
+ KeyTypeSVK = 'SVK'
+ KeyTypeInvalid = ''
+ KeysTypesAll = (KeyTypeSSK, KeyTypeKSK, KeyTypeCHK, KeyTypeUSK, KeyTypeSVK)
- KeySSK = 'SSK@'
- KeyKSK = 'KSK@'
- KeyCHK = 'CHK@'
- KeyUSK = 'USK@'
- KeySVK = 'SVK@'
- KeyUnknown = ''
- KeysAll = (KeySSK, KeyKSK, KeyCHK, KeyUSK, KeySVK)
-
- ReUriPattern = re.compile('(%s.*?)(?= |\Z)' % '.*?|'.join(KeysAll), re.I)
- ReKeyPattern = re.compile('(%s)' % '|'.join(KeysAll), re.I)
-
def __init__(self, uri):
- """
- @param uri: uri to wrap
- @param cvar ReUriPattern: pattern matching a freenet uri
- @param cvar ReKeyPattern: pattern matching the key type of a freenet uri
+ self.uri = stripUri(uri)
- @note: any dfecorations prefixing the freenet part of the uri uri are stripped if possible
+ result = keyType(self.uri)
+ self.keyType = self.KeyTypeInvalid if result is None else result
+
+ def __nonzero__(self):
+ return self.keyType != self.KeyTypeInvalid
- >>> uri = FcpUri('freenet:SSK@foo/bar')
- >>> str(uri)
- 'SSK@foo/bar'
- >>> uri.keyType() == FcpUri.KeySSK
- True
- >>> uri.split()
- ('SSK@foo', 'bar')
- >>> uri.fileName()
- 'bar'
- >>> uri = FcpUri('http://SSK@foo/bar')
- >>> str(uri)
- 'SSK@foo/bar'
-
- # uris not containing freenet keys are left unchanged
- >>> uri = FcpUri('http://foo/bar')
- >>> str(uri)
- 'http://foo/bar'
- >>> uri.keyType() == FcpUri.KeyUnknown
- True
- >>> uri.split()
- ('http://foo/bar', '')
- >>> uri.fileName()
- 'http://foo/bar'
-
- """
- self.uri = uri
-
- result = self.ReUriPattern.search(uri)
- if result is not None:
- self.uri = result.group(0)
-
- def __str__(self):
- return str(self.uri)
-
- def __unicode__(self):
- return unicode(self.uri)
-
- def keyType(self):
- """Retuns the key type of the uri
- @return: one of the Key* consts
- """
- result = self.ReKeyPattern.search(self.uri)
- if result is not None:
- return result.group(0).upper()
- return self.KeyUnknown
-
def split(self):
"""Splits the uri
@return: tuple(freenet-key, file-name)
@@ -86,6 +97,7 @@
return head, tail
return self.uri, ''
+
def fileName(self):
"""Returns the filename part of the uri
@return: str
@@ -94,4 +106,6 @@
if tail:
return tail
return self.uri
-
+
+
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ju...@us...> - 2008-02-01 14:43:04
|
Revision: 98
http://fclient.svn.sourceforge.net/fclient/?rev=98&view=rev
Author: jurner
Date: 2008-02-01 06:43:03 -0800 (Fri, 01 Feb 2008)
Log Message:
-----------
some fixes
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_uri.py
Modified: trunk/sandbox/fcp/fcp2_0_uri.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-01 14:42:18 UTC (rev 97)
+++ trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-01 14:43:03 UTC (rev 98)
@@ -3,6 +3,8 @@
import base64
import re
import urlparse
+
+import fcp2_0_consts as consts
#**************************************************************************************
# freenet base64 for keys
#**************************************************************************************
@@ -38,7 +40,7 @@
#***************************************************************************************
KeyPat = re.compile(
r'''
-^(CHK | SSK | SVK | USK) @
+^(CHK | SSK | SVK | USK @)
(
[a-z0-9\-~]{43},
[a-z0-9\-~]{43},
@@ -49,8 +51,8 @@
def keyType(uri):
"""Returns the ky type of a freenet key or None if the type could not be determined"""
- if uri.startswith('KSK@'):
- return 'KSK'
+ if uri.startswith(consts.KeyType.KSK):
+ return consts.KeyType.KSK
result = KeyPat.match(uri)
if result is None:
return None
@@ -67,32 +69,26 @@
class Uri(object):
+
+ KeyType = consts.KeyType
+
- KeyTypeSSK = 'SSK'
- KeyTypeKSK = 'KSK'
- KeyTypeCHK = 'CHK'
- KeyTypeUSK = 'USK'
- KeyTypeSVK = 'SVK'
- KeyTypeInvalid = ''
- KeysTypesAll = (KeyTypeSSK, KeyTypeKSK, KeyTypeCHK, KeyTypeUSK, KeyTypeSVK)
-
-
def __init__(self, uri):
self.uri = stripUri(uri)
result = keyType(self.uri)
- self.keyType = self.KeyTypeInvalid if result is None else result
+ self.keyType = self.KeyType.Invalid if result is None else result
def __nonzero__(self):
- return self.keyType != self.KeyTypeInvalid
+ return self.keyType != self.KeyType.Invalid
def split(self):
"""Splits the uri
@return: tuple(freenet-key, file-name)
"""
- if self.keyType() != self.KeyUnknown:
+ if self.keyType() != self.KeyType.Invalid:
head, sep, tail = self.uri.partition('/')
return head, tail
return self.uri, ''
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ju...@us...> - 2008-02-04 03:16:43
|
Revision: 132
http://fclient.svn.sourceforge.net/fclient/?rev=132&view=rev
Author: jurner
Date: 2008-02-03 19:16:48 -0800 (Sun, 03 Feb 2008)
Log Message:
-----------
docs
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_uri.py
Modified: trunk/sandbox/fcp/fcp2_0_uri.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-04 03:16:21 UTC (rev 131)
+++ trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-04 03:16:48 UTC (rev 132)
@@ -69,18 +69,27 @@
class Uri(object):
-
+ """Class wrappinf a freenet Uri
+
+ @ivar keyType: L{consts.KeyType} of the uri
+ @ivar uri: (str) uri contained in the instance
+ """
+
KeyType = consts.KeyType
def __init__(self, uri):
+ """
+ @param uri: (str) freenet uri (may be an uri like http://..CHK@ or
+ freenet:CHK@ or whatever or a a freenet key)
+ """
self.uri = stripUri(uri)
-
result = keyType(self.uri)
self.keyType = self.KeyType.Invalid if result is None else result
def __nonzero__(self):
+ """Checks if the uri contained in the instance is a vaild freenet uri"""
return self.keyType != self.KeyType.Invalid
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <ju...@us...> - 2008-02-18 22:01:05
|
Revision: 234
http://fclient.svn.sourceforge.net/fclient/?rev=234&view=rev
Author: jurner
Date: 2008-02-18 14:00:55 -0800 (Mon, 18 Feb 2008)
Log Message:
-----------
a few fixes
Modified Paths:
--------------
trunk/sandbox/fcp/fcp2_0_uri.py
Modified: trunk/sandbox/fcp/fcp2_0_uri.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-18 22:00:15 UTC (rev 233)
+++ trunk/sandbox/fcp/fcp2_0_uri.py 2008-02-18 22:00:55 UTC (rev 234)
@@ -14,7 +14,7 @@
@return: decoded string
@raise TypeError: if the string can not be decoded
- @note: this function handles non-standard encoding as used by freenet (see: freenet/support/base64.java)
+ @note: this function handles non-standard encoding as used by freenet (see: freenet/src/support/base64.java)
"""
# freenet uses - for + and ~ for /
altchars = '-~'
@@ -31,12 +31,14 @@
# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
#
# all byte components are base64 encoded. Freenet uses base64 without padding
-# and uses the following altchars for urlsave encode: - for + and ~ for /
+# along with the following altchars for urlsave encode: - for + and ~ for /
# see: freenet/support/base64.java
#
# so a key as the user gets it to see is:
# KeyType@43 bytes, 43 bytes, 7 bytes ..of [A-Za-z0-9\-~]
#
+# see: [freenet/src/support/base64.java]
+#
#***************************************************************************************
KeyPat = re.compile(
r'''
@@ -97,7 +99,7 @@
"""Splits the uri
@return: tuple(freenet-key, file-name)
"""
- if self.keyType() != self.KeyType.Invalid:
+ if self.keyType != self.KeyType.Invalid:
head, sep, tail = self.uri.partition('/')
return head, tail
return self.uri, ''
@@ -110,7 +112,8 @@
head, tail = self.split()
if tail:
return tail
- return self.uri
+ return ''
+
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|