Thread: SF.net SVN: fclient: [460] trunk/fcp2/src/fcp2/key.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <jU...@us...> - 2008-07-01 11:27:25
|
Revision: 460
http://fclient.svn.sourceforge.net/fclient/?rev=460&view=rev
Author: jUrner
Date: 2008-07-01 04:27:35 -0700 (Tue, 01 Jul 2008)
Log Message:
-----------
for ease of use, all necessary symbols are now imported to main
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-01 11:27:28 UTC (rev 459)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-01 11:27:35 UTC (rev 460)
@@ -16,17 +16,14 @@
from fcp2 import consts
-del hack
+del hack, _RelImportHack
#<-- rel import hack
#**************************************************************************************
# consts
#**************************************************************************************
-ReMatchExact = '\A%s\Z'
+_ReMatchExact = '\A%s\Z'
KeyTypesAll = {}
-# for testing, arbitrary but valid key data
-DummyKeyData = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA'
-
#**************************************************************************************
# freenet base64 for keys
#**************************************************************************************
@@ -73,7 +70,7 @@
# see: [freenet/src/support/base64.java]
#
#***************************************************************************************
-class KeyMeta(type):
+class _KeyMeta(type):
"""Metaclass for freenet keys"""
def __new__(klass, name, bases, kws):
@@ -90,7 +87,7 @@
#
#**************************************************************************************
#TODO: too bad, can not move this to types.py ...cross import
-class FcpTypeKey(object):
+class TypeKey(object):
"""key type for type conversions
>>> key = FcpTypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
@@ -146,20 +143,20 @@
return key.toString()
-def key(string):
+def Key(string):
"""creates a key object from a string
@return: L{KeyBase}
"""
- return FcpTypeKey.fcpToPython(string)
+ return TypeKey.fcpToPython(string)
#**************************************************************************************
#
#**************************************************************************************
-class KeyBase(object):
+class _KeyBase(object):
"""Base class for freenet keys
"""
- __metaclass__ = KeyMeta
+ __metaclass__ = _KeyMeta
KeyType = None
def __eq__(self, other):
@@ -179,7 +176,7 @@
raise NotImplementedError()
-class CHK(KeyBase):
+class KeyCHK(_KeyBase):
""""""
_key_pattern_ = '''
(?P<keyType>CHK@)
@@ -190,9 +187,9 @@
)
(?: / (?P<filename>[^/]+?)? (?: /)?)?
'''
- KeyType = consts.KeyType.CHK
+ KeyType = consts.ConstKeyType.CHK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(ReMatchExact % _key_pattern_, re.I | re.X)
+ ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
def __init__(self, keyData=None, filename=None):
"""Creates a CHK key
@@ -218,9 +215,10 @@
d = result.groupdict()
key = clss(d['keyData'], filename=d['filename'])
return key
-
+
+
-class SSK(KeyBase):
+class KeySSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>SSK@)
(?P<keyData>
@@ -233,9 +231,9 @@
(?: /)?
)?
'''
- KeyType = consts.KeyType.SSK
+ KeyType = consts.ConstKeyType.SSK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(ReMatchExact % _key_pattern_, re.I | re.X)
+ ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
def __init__(self, keyData, filename=None):
self.filename = filename
@@ -255,17 +253,18 @@
if result is not None:
d = result.groupdict()
return clss(d['keyData'], d['filename'])
-
-class KSK(KeyBase):
+
+
+class KeyKSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>KSK@)
(?P<filename>[^/]+?)
(?: /)?
'''
- KeyType = consts.KeyType.KSK
+ KeyType = consts.ConstKeyType.KSK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(ReMatchExact % _key_pattern_, re.I | re.X)
+ ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
def __init__(self, filename):
@@ -283,9 +282,10 @@
if result is not None:
d = result.groupdict()
return clss(filename=d['filename'])
-
+
-class USK(KeyBase):
+
+class KeyUSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>USK@)
(?P<keyData>
@@ -298,9 +298,9 @@
(?: /)?
'''
- KeyType = consts.KeyType.USK
+ KeyType = consts.ConstKeyType.USK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(ReMatchExact % _key_pattern_, re.I | re.X)
+ ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
def __init__(self, keyData, filename=None, edition=0):
"""Creates a USK key
@@ -330,6 +330,8 @@
d = result.groupdict()
return clss(d['keyData'], d['filename'], edition=d['edition'])
+
+__all__ = [i for i in dir() if i[0].isupper() and not i.startswith('_')]
#*****************************************************************************
#
#*****************************************************************************
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jU...@us...> - 2008-07-02 07:05:19
|
Revision: 482
http://fclient.svn.sourceforge.net/fclient/?rev=482&view=rev
Author: jUrner
Date: 2008-07-02 00:05:23 -0700 (Wed, 02 Jul 2008)
Log Message:
-----------
fixed broken docstrings
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-02 07:05:11 UTC (rev 481)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-02 07:05:23 UTC (rev 482)
@@ -145,7 +145,7 @@
def Key(string):
"""creates a key object from a string
- @return: L{KeyBase}
+ @return: (L{_KeyBase})
"""
return TypeKey.fcpToPython(string)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jU...@us...> - 2008-07-02 09:59:39
|
Revision: 485
http://fclient.svn.sourceforge.net/fclient/?rev=485&view=rev
Author: jUrner
Date: 2008-07-02 02:59:48 -0700 (Wed, 02 Jul 2008)
Log Message:
-----------
beautify
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-02 07:05:37 UTC (rev 484)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-02 09:59:48 UTC (rev 485)
@@ -54,34 +54,6 @@
if result.startswith('/'):
result = result[1:]
return result
-
-#****************************************************************************************
-# freenet keys
-#
-# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
-#
-# all byte components are base64 encoded. Freenet uses base64 without padding
-# 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]
-#
-#***************************************************************************************
-class _KeyMeta(type):
- """Metaclass for freenet keys"""
-
- def __new__(klass, name, bases, kws):
- """Registers a key type to L{KeyTypesAll}
- @note: if KeyType of the the key type is None it will not get registered
- """
-
- newClass = type.__new__(klass, name, bases, kws)
- if newClass.KeyType is not None:
- KeyTypesAll[newClass.KeyType] = newClass
- return newClass
#**************************************************************************************
#
@@ -141,6 +113,34 @@
@return: (str) fcp key
"""
return key.toString()
+
+#****************************************************************************************
+# freenet keys
+#
+# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
+#
+# all byte components are base64 encoded. Freenet uses base64 without padding
+# 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]
+#
+#***************************************************************************************
+class _KeyMeta(type):
+ """Metaclass for freenet keys"""
+
+ def __new__(klass, name, bases, kws):
+ """Registers a key type to L{KeyTypesAll}
+ @note: if KeyType of the the key type is None it will not get registered
+ """
+
+ newClass = type.__new__(klass, name, bases, kws)
+ if newClass.KeyType is not None:
+ KeyTypesAll[newClass.KeyType] = newClass
+ return newClass
def Key(string):
@@ -216,7 +216,6 @@
key = clss(d['keyData'], filename=d['filename'])
return key
-
class KeySSK(_KeyBase):
_key_pattern_ = '''
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jU...@us...> - 2008-07-02 10:00:08
|
Revision: 486
http://fclient.svn.sourceforge.net/fclient/?rev=486&view=rev
Author: jUrner
Date: 2008-07-02 03:00:16 -0700 (Wed, 02 Jul 2008)
Log Message:
-----------
fixed doctests
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-02 09:59:48 UTC (rev 485)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-02 10:00:16 UTC (rev 486)
@@ -62,31 +62,31 @@
class TypeKey(object):
"""key type for type conversions
- >>> key = FcpTypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
+ >>> key = TypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
>>> key.toString()
'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = FcpTypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
+ >>> key = TypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
>>> key.toString()
'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = FcpTypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
>>> key.toString()
'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = FcpTypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
>>> key.toString()
'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = FcpTypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0')
+ >>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0')
>>> key.toString()
'USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/'
- >>> key = FcpTypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/')
+ >>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/')
>>> key.toString()
'USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/'
- >>> key = FcpTypeKey.fcpToPython('KSK@abcde')
+ >>> key = TypeKey.fcpToPython('KSK@abcde')
>>> key.toString()
'KSK@abcde/'
- >>> key = FcpTypeKey.fcpToPython('KSK@abcde/')
+ >>> key = TypeKey.fcpToPython('KSK@abcde/')
>>> key.toString()
'KSK@abcde/'
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jU...@us...> - 2008-07-04 08:25:48
|
Revision: 487
http://fclient.svn.sourceforge.net/fclient/?rev=487&view=rev
Author: jUrner
Date: 2008-07-04 01:25:43 -0700 (Fri, 04 Jul 2008)
Log Message:
-----------
combed over keys. many a changes..
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-02 10:00:16 UTC (rev 486)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-04 08:25:43 UTC (rev 487)
@@ -24,6 +24,9 @@
_ReMatchExact = '\A%s\Z'
KeyTypesAll = {}
+# save url chars as defined in [freenet/src/support/URLEncoder.java]
+SafeURLChars = '*-_./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
+
#**************************************************************************************
# freenet base64 for keys
#**************************************************************************************
@@ -39,9 +42,7 @@
altchars = '-~'
# padding may be ommitted or not
- padding = 4 - len(string) % 4
- if padding:
- string += '=' * padding
+ string += '=' * (len(string) % 4)
return base64.b64decode(string, altchars)
@@ -56,7 +57,13 @@
return result
#**************************************************************************************
-#
+# freenet keys
+#
+# KeyType@base64(routingbKey), base64(cryptoKey), base64(extra)
+#
+# Freenet seems to use base64 without padding along with the following altchars for urlsave
+# encode: - for + and ~ for / see: [freenet/support/base64.java]
+#
#**************************************************************************************
#TODO: too bad, can not move this to types.py ...cross import
class TypeKey(object):
@@ -69,12 +76,12 @@
>>> key.toString()
'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1')
>>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
+ 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
+ >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/')
>>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
+ 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
>>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0')
>>> key.toString()
@@ -115,19 +122,7 @@
return key.toString()
#****************************************************************************************
-# freenet keys
#
-# KeyType@32 bytes hash, 32 bytes encryption key, 5 bytes extra
-#
-# all byte components are base64 encoded. Freenet uses base64 without padding
-# 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]
-#
#***************************************************************************************
class _KeyMeta(type):
"""Metaclass for freenet keys"""
@@ -181,31 +176,31 @@
_key_pattern_ = '''
(?P<keyType>CHK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
- (?: / (?P<filename>[^/]+?)? (?: /)?)?
+ (?: / (?P<docName>[^/]+?)? (?: /)?)?
'''
KeyType = consts.ConstKeyType.CHK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData=None, filename=None):
+ def __init__(self, keyData=None, docName=None):
"""Creates a CHK key
@param keyData: (str) key data or None
- @param filename: (str) filename to add to the key or None
+ @param docName: (str) docName to add to the key or None
"""
self.keyData = keyData
- self.filename = filename
+ self.docName = docName
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
return out
@classmethod
@@ -213,7 +208,7 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- key = clss(d['keyData'], filename=d['filename'])
+ key = clss(d['keyData'], docName=d['docName'])
return key
@@ -221,29 +216,34 @@
_key_pattern_ = '''
(?P<keyType>SSK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
(
- (?: / (?P<filename>[^/]+?))
- (?: /)?
+ (?: / (?P<docName>[^/]+?))
+ -
+ (?: (?P<edition>[\d]+))
)?
+ (?: /)?
'''
KeyType = consts.ConstKeyType.SSK
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData, filename=None):
- self.filename = filename
+ def __init__(self, keyData, docName=None, edition=0):
+ self.docName = docName
+ self.edition = edition
self.keyData = keyData
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ if self.edition is None:
+ raise ValueError('no edition number specified')
+ out += self.docName + '-' + str(self.edition) + '/'
return out
@classmethod
@@ -251,14 +251,17 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(d['keyData'], d['filename'])
+ edition = d['edition']
+ if edition is not None:
+ edition = int(edition)
+ return clss(d['keyData'], d['docName'], edition=edition)
class KeyKSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>KSK@)
- (?P<filename>[^/]+?)
+ (?P<docName>[^/]+?)
(?: /)?
'''
KeyType = consts.ConstKeyType.KSK
@@ -266,13 +269,13 @@
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, filename):
- self.filename = filename
+ def __init__(self, docName):
+ self.docName = docName
def toString(self):
out = self.KeyType
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
return out
@classmethod
@@ -280,20 +283,22 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(filename=d['filename'])
+ return clss(docName=d['docName'])
-
+#TODO: is docName obligatory?
class KeyUSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>USK@)
(?P<keyData>
- (?P<hash>[a-z0-9\-~]{43}),
- (?P<cryptoKey>[a-z0-9\-~]{43}),
- (?P<extra>[a-z0-9\-~]{7})
+ (?P<routingKey>[a-z0-9\-~]+?),
+ (?P<cryptoKey>[a-z0-9\-~]+?),
+ (?P<extra>[a-z0-9\-~]+?)
)
- (?: / (?P<filename>[^/]+?) )
- (?: / (?P<edition>[\d]+))
+ (
+ (?: / (?P<docName>[^/]+?) )
+ (?: / (?P<edition>-?[\d]+))
+ )?
(?: /)?
'''
@@ -301,23 +306,23 @@
KeyPattern = re.compile(_key_pattern_, re.I | re.X)
ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
- def __init__(self, keyData, filename=None, edition=0):
+ def __init__(self, keyData, docName=None, edition=0):
"""Creates a USK key
@param keyData: (str) public key
- @param filename: (str) filename
+ @param docName: (str) docName
@param edition: (int) edition number
"""
self.edition = edition
- self.filename = filename
+ self.docName = docName
self.keyData = keyData
def toString(self):
out = self.KeyType
if self.keyData is not None:
out += self.keyData + '/'
- if self.filename is not None:
- out += self.filename + '/'
+ if self.docName is not None:
+ out += self.docName + '/'
if self.edition is not None:
out += str(self.edition) + '/'
return out
@@ -327,7 +332,10 @@
result = clss.ExactKeyPattern.match(stripKey(string))
if result is not None:
d = result.groupdict()
- return clss(d['keyData'], d['filename'], edition=d['edition'])
+ edition = d['edition']
+ if edition is not None:
+ edition = int(edition)
+ return clss(d['keyData'], d['docName'], edition=edition)
__all__ = [i for i in dir() if i[0].isupper() and not i.startswith('_')]
@@ -336,4 +344,4 @@
#*****************************************************************************
if __name__ == '__main__':
import doctest
- print 'doctests failed: %s/%s' % doctest.testmod()
+ #print 'doctests failed: %s/%s' % doctest.testmod()
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <jU...@us...> - 2008-07-05 10:44:43
|
Revision: 493
http://fclient.svn.sourceforge.net/fclient/?rev=493&view=rev
Author: jUrner
Date: 2008-07-05 03:44:51 -0700 (Sat, 05 Jul 2008)
Log Message:
-----------
another rework of the key module
Modified Paths:
--------------
trunk/fcp2/src/fcp2/key.py
Modified: trunk/fcp2/src/fcp2/key.py
===================================================================
--- trunk/fcp2/src/fcp2/key.py 2008-07-04 08:27:51 UTC (rev 492)
+++ trunk/fcp2/src/fcp2/key.py 2008-07-05 10:44:51 UTC (rev 493)
@@ -2,7 +2,9 @@
import os, sys
import base64
+import posixpath
import re
+import urllib
import urlparse
#--> rel import hack
@@ -24,9 +26,6 @@
_ReMatchExact = '\A%s\Z'
KeyTypesAll = {}
-# save url chars as defined in [freenet/src/support/URLEncoder.java]
-SafeURLChars = '*-_./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz'
-
#**************************************************************************************
# freenet base64 for keys
#**************************************************************************************
@@ -46,14 +45,15 @@
return base64.b64decode(string, altchars)
-def stripKey(key):
- """Strips all uri stuff from a key
- @param key: (str) key to strip
- @return: (str) key
- """
- result = urlparse.urlsplit(key)[2]
- if result.startswith('/'):
- result = result[1:]
+def keyNormkey(string):
+ """"""
+ result = urlparse.urlsplit(string)[2]
+ while result.startswith('/'):
+ result = result[1: ]
+ while result.endswith('/'):
+ result = result[ :-1]
+ if result:
+ result += '/'
return result
#**************************************************************************************
@@ -65,40 +65,19 @@
# encode: - for + and ~ for / see: [freenet/support/base64.java]
#
#**************************************************************************************
-#TODO: too bad, can not move this to types.py ...cross import
-class TypeKey(object):
- """key type for type conversions
+class TypeKey(type):
+ """Metaclass for freenet keys"""
- >>> key = TypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo')
- >>> key.toString()
- 'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
- >>> key = TypeKey.fcpToPython('CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/')
- >>> key.toString()
- 'CHK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/'
-
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1')
- >>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
- >>> key = TypeKey.fcpToPython('SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/')
- >>> key.toString()
- 'SSK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo-1/'
-
- >>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0')
- >>> key.toString()
- 'USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/'
- >>> key = TypeKey.fcpToPython('USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/')
- >>> key.toString()
- 'USK@aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,AAAAAAA/foo/0/'
-
- >>> key = TypeKey.fcpToPython('KSK@abcde')
- >>> key.toString()
- 'KSK@abcde/'
- >>> key = TypeKey.fcpToPython('KSK@abcde/')
- >>> key.toString()
- 'KSK@abcde/'
-
- """
-
+ def __new__(klass, name, bases, kws):
+ """Registers a key type to L{KeyTypesAll}
+ @note: if KeyType of the the key type is None it will not get registered
+ """
+
+ newClass = type.__new__(klass, name, bases, kws)
+ if newClass.KeyType is not None:
+ KeyTypesAll[newClass.KeyType] = newClass
+ return newClass
+
@classmethod
def fcpToPython(clss, string):
"""converts a fcp key to a python key
@@ -107,9 +86,8 @@
@raise ValueError: if the string can not be converted
@note: use this method to convert an arbirary key to the corrosponding python key object
"""
- key = stripKey(string)
for clssKeyType in KeyTypesAll.values():
- result = clssKeyType.fromString(key)
+ result = clssKeyType.fromString(string)
if result is not None:
return result
raise ValueError('Invalid key: %s' % string)
@@ -120,24 +98,10 @@
@return: (str) fcp key
"""
return key.toString()
-
-#****************************************************************************************
-#
-#***************************************************************************************
-class _KeyMeta(type):
- """Metaclass for freenet keys"""
-
- def __new__(klass, name, bases, kws):
- """Registers a key type to L{KeyTypesAll}
- @note: if KeyType of the the key type is None it will not get registered
- """
-
- newClass = type.__new__(klass, name, bases, kws)
- if newClass.KeyType is not None:
- KeyTypesAll[newClass.KeyType] = newClass
- return newClass
-
+#**************************************************************************************
+#
+#**************************************************************************************
def Key(string):
"""creates a key object from a string
@return: (L{_KeyBase})
@@ -151,7 +115,7 @@
"""Base class for freenet keys
"""
- __metaclass__ = _KeyMeta
+ __metaclass__ = TypeKey
KeyType = None
def __eq__(self, other):
@@ -161,8 +125,10 @@
return self.pytoString() != other.toString()
@classmethod
- def fromString(clss, string):
+ def fromString(clss, string, isQuoted=True):
"""should create a key object from a string
+ @param string:
+ @param isQuoted: (bool) if True, the string is assumed to be already quoted
"""
raise NotImplementedError()
@@ -170,6 +136,7 @@
"""should return the key as string"""
raise NotImplementedError()
+
class KeyCHK(_KeyBase):
""""""
@@ -180,36 +147,46 @@
(?P<cryptoKey>[a-z0-9\-~]+?),
(?P<extra>[a-z0-9\-~]+?)
)
- (?: / (?P<docName>[^/]+?)? (?: /)?)?
+ /
+ (
+ (?P<docName> (?: [^/]+?)) /
+ ((?P<tail>.+) /)?
+ )?
'''
+ KeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
KeyType = consts.ConstKeyType.CHK
- KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
-
- def __init__(self, keyData=None, docName=None):
+
+ def __init__(self, keyData=None, docName=None, tail=None):
"""Creates a CHK key
@param keyData: (str) key data or None
@param docName: (str) docName to add to the key or None
+ @param tail: (str) for containers, path to item in container or None
"""
self.keyData = keyData
self.docName = docName
+ self.tail = tail
def toString(self):
- out = self.KeyType
- if self.keyData is not None:
- out += self.keyData + '/'
+ if self.keyData is None:
+ out = [self.KeyType, ]
+ else:
+ out = [self.KeyType + self.keyData, ]
if self.docName is not None:
- out += self.docName + '/'
- return out
+ out.append(urllib.quote(self.docName))
+ if self.tail is not None:
+ out.append(urllib.quote(self.tail))
+ return posixpath.join(*out)
@classmethod
def fromString(clss, string):
- result = clss.ExactKeyPattern.match(stripKey(string))
+ key = keyNormkey(string)
+ key = urllib.unquote(key)
+ result = clss.KeyPattern.match(key)
if result is not None:
d = result.groupdict()
- key = clss(d['keyData'], docName=d['docName'])
- return key
+ return clss(d['keyData'], docName=d['docName'], tail=d['tail'])
+
class KeySSK(_KeyBase):
@@ -220,73 +197,89 @@
(?P<cryptoKey>[a-z0-9\-~]+?),
(?P<extra>[a-z0-9\-~]+?)
)
+ /
(
- (?: / (?P<docName>[^/]+?))
- -
- (?: (?P<edition>[\d]+))
+ (?: (?P<docName>[^/]+?)) - (?: (?P<edition>[\d]+)) /
+ ((?P<tail>.+) /)?
)?
- (?: /)?
'''
+ KeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
KeyType = consts.ConstKeyType.SSK
- KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
+
+ def __init__(self, keyData=None, docName=None, edition=0, tail=None):
+ """Creates a SSK key
- def __init__(self, keyData, docName=None, edition=0):
+ @param keyData: (str) key data or None
+ @param docName: (str) docName to add to the key or None
+ @param edition: (int) desired edition
+ @param tail: (str) for containers, path to item in container or None
+ """
self.docName = docName
self.edition = edition
self.keyData = keyData
+ self.tail = tail
def toString(self):
- out = self.KeyType
- if self.keyData is not None:
- out += self.keyData + '/'
+ if self.keyData is None:
+ out = [self.KeyType, ]
+ else:
+ out = [self.KeyType + self.keyData, ]
if self.docName is not None:
if self.edition is None:
raise ValueError('no edition number specified')
- out += self.docName + '-' + str(self.edition) + '/'
- return out
-
+ out.append(urllib.quote(self.docName + '-' + str(self.edition)))
+ if self.tail is not None:
+ out.append(urllib.quote(self.tail))
+ return posixpath.join(*out)
+
@classmethod
def fromString(clss, string):
- result = clss.ExactKeyPattern.match(stripKey(string))
+ key = keyNormkey(string)
+ key = urllib.unquote(key)
+ result = clss.KeyPattern.match(key)
if result is not None:
d = result.groupdict()
edition = d['edition']
if edition is not None:
edition = int(edition)
- return clss(d['keyData'], d['docName'], edition=edition)
+ return clss(d['keyData'], docName=d['docName'], edition=edition, tail=d['tail'])
+
+
-
-
+#TODO: current impl is to not allow slashes in KSKs. have to check if the node enforces this
class KeyKSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>KSK@)
- (?P<docName>[^/]+?)
- (?: /)?
+ (?P<docName>[^/]+?) /
'''
+ KeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
KeyType = consts.ConstKeyType.KSK
- KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
+
-
def __init__(self, docName):
+ """Creates a KSK key
+
+ @param docName: (str) docName to add to the key or None
+ """
self.docName = docName
def toString(self):
- out = self.KeyType
- if self.docName is not None:
- out += self.docName + '/'
- return out
+ if self.docName is None:
+ return self.KeyType
+ return self.KeyType + urllib.quote(self.docName)
+
@classmethod
- def fromString(clss, string):
- result = clss.ExactKeyPattern.match(stripKey(string))
+ def fromString(clss, string, isQuoted=True):
+ key = keyNormkey(string)
+ key = urllib.unquote(key)
+ result = clss.KeyPattern.match(key)
if result is not None:
d = result.groupdict()
return clss(docName=d['docName'])
-#TODO: is docName obligatory?
+
class KeyUSK(_KeyBase):
_key_pattern_ = '''
(?P<keyType>USK@)
@@ -295,53 +288,58 @@
(?P<cryptoKey>[a-z0-9\-~]+?),
(?P<extra>[a-z0-9\-~]+?)
)
+ /
(
- (?: / (?P<docName>[^/]+?) )
- (?: / (?P<edition>-?[\d]+))
+ (?P<docName>[^/]+?) /
+ (?P<edition>-?\d+) /
+ ((?P<tail>.+) /)?
)?
- (?: /)?
'''
-
+ KeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
KeyType = consts.ConstKeyType.USK
- KeyPattern = re.compile(_key_pattern_, re.I | re.X)
- ExactKeyPattern = re.compile(_ReMatchExact % _key_pattern_, re.I | re.X)
-
- def __init__(self, keyData, docName=None, edition=0):
+
+ def __init__(self, keyData=None, docName=None, edition=-1, tail=None):
"""Creates a USK key
- @param keyData: (str) public key
- @param docName: (str) docName
- @param edition: (int) edition number
+
+ @param keyData: (str) key data or None
+ @param docName: (str) docName to add to the key or None
+ @param edition: (int) desired edition
+ @param tail: (str) for containers, path to item in container or None
"""
self.edition = edition
self.docName = docName
self.keyData = keyData
+ self.tail = tail
def toString(self):
- out = self.KeyType
- if self.keyData is not None:
- out += self.keyData + '/'
+ if self.keyData is None:
+ out = [self.KeyType, ]
+ else:
+ out = [self.KeyType + self.keyData, ]
if self.docName is not None:
- out += self.docName + '/'
- if self.edition is not None:
- out += str(self.edition) + '/'
- return out
+ if self.edition is None:
+ raise ValueError('no edition number specified')
+ out.append(urllib.quote(self.docName))
+ out.append(urllib.quote(str(self.edition)))
+ if self.tail is not None:
+ out.append(urllib.quote(self.tail))
+ return posixpath.join(*out)
@classmethod
- def fromString(clss, string):
- result = clss.ExactKeyPattern.match(stripKey(string))
+ def fromString(clss, string, isQuoted=True):
+ key = keyNormkey(string)
+ key = urllib.unquote(key)
+ result = clss.KeyPattern.match(key)
if result is not None:
d = result.groupdict()
edition = d['edition']
if edition is not None:
edition = int(edition)
- return clss(d['keyData'], d['docName'], edition=edition)
+ return clss(d['keyData'], docName=d['docName'], edition=edition, tail=d['tail'])
+
__all__ = [i for i in dir() if i[0].isupper() and not i.startswith('_')]
-#*****************************************************************************
-#
-#*****************************************************************************
-if __name__ == '__main__':
- import doctest
- #print 'doctests failed: %s/%s' % doctest.testmod()
+__all__.append('base64UrlsaveDecode')
+__all__.append('keyNormkey')
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|