SF.net SVN: fclient: [247] trunk/sandbox/fcp/fcp2_0_requests.py
Status: Pre-Alpha
Brought to you by:
jurner
|
From: <ju...@us...> - 2008-02-24 11:52:31
|
Revision: 247
http://fclient.svn.sourceforge.net/fclient/?rev=247&view=rev
Author: jurner
Date: 2008-02-24 03:52:23 -0800 (Sun, 24 Feb 2008)
Log Message:
-----------
too much trouble, split requests up into client methods. Del module.
Removed Paths:
-------------
trunk/sandbox/fcp/fcp2_0_requests.py
Deleted: trunk/sandbox/fcp/fcp2_0_requests.py
===================================================================
--- trunk/sandbox/fcp/fcp2_0_requests.py 2008-02-24 11:50:43 UTC (rev 246)
+++ trunk/sandbox/fcp/fcp2_0_requests.py 2008-02-24 11:52:23 UTC (rev 247)
@@ -1,263 +0,0 @@
-"""fcp request objects"""
-
-import fcp2_0_consts as consts
-#******************************************************************************************************************
-#
-#******************************************************************************************************************
-#TODO: if only one item is added its name is ignored so far. How to handle self.targetName vs. item['Name']
-#TODO: Metadata.ContentType vs. ContentType is pretty inconsistent
-class Upload(object):
- """Wrapper class to manage a freenet uploads
-
- @ivar defaultItem: (int) for directories or multiple uploads the name of this item is used as
- default item when a page is displayed
- @ivar directoryAdded: (bool) True if the upload contains a directory, False otherwise
- @ivar items: (list) of dicts containing paramaters for each upload
- @ivar keyType: L{consts.KeyType} the type of key to upload
- @ivar params: (dict) request parameters
- @ivar privateKey: (str) private keey (required for SSK or USK uploads)
- @ivar targetName: (str) for CHKs the name to be appended to the CHK. For SSKs and USKs the
- (target-name/version) part of the key
- @ivar targetNames: (list) of names already in use (for multi-uploads)
-
- @note: you can upload one or more items of any kind (data, files, redirects), except from directories
- wich have to be the only item to upload
- @note: if you want to upload a single redirect keyType has to be set to L{consts.KeyType.KSK}
- """
-
- PrivateParamNames = ('Data', 'RequestType', 'Name')
-
-
- def __init__(self,
- keyType,
- dontCompress=None,
- earlyEncode=None,
- maxRetries=None,
- persistence=consts.Persistence.Connection,
- priorityClass=consts.Priority.Medium,
-
- privateKey=None,
- targetName=None,
- ):
- """
- @param keyType: (L{consts.KeyType}) the desired key type to upload
- @param dontCompress:
- @param earlyEncode:
- @param maxRetries:
- @param persistence:
- @param priorityClass:
-
- @param privateKey: (str) privateKey if keyType is SSK or USK
- @param targetName:
- """
- if keyType not in consts.KeyType.TypesAll:
- raise ValueError('Invalid key type: %r' % keyType)
- if keyType in (consts.KeyType.SSK, consts.KeyType.USK) :
- if privateKey is None:
- raise ValueError('For %s a public key is required' % keyType)
- if not targetName:
- raise ValueError('For %s a target name is required' % keyType)
- elif keyType in (consts.KeyType.CHK, consts.KeyType.KSK) and privateKey is not None:
- raise ValueError('For %s no public key is required' % keyType)
-
- self.defaultItem = 0
- self.directoryAdded = False
- self.items = []
- self.keyType = keyType
- self.params = {
- 'DontCompress': dontCompress,
- 'EarlyEncode': earlyEncode,
- 'MaxRetries': maxRetries,
- 'Persistence': persistence,
- 'PriorityClass': priorityClass,
- }
- self.privateKey = privateKey
- self.targetName = targetName
- self.targetNames = [] # record to check if names are unique (assert order is arbitrary)
-
-
- def _addItem(self, isDefault=False, **params):
- """Private method to add an upload item"""
- if self.directoryAdded:
- raise ValueError('A directory has already been added, no other items allowed')
-
- if params['RequestType'] == consts.RequestType.PutDir:
- if self.directoryAdded:
- raise ValueError('An item has already been added, no additional directory allowed')
- else:
- self.directoryAdded = True
-
- name = params['Name']
- if name in self.targetNames:
- raise ValueError('Target name already exists: %r' % name)
-
- self.targetNames.append(name)
- self.items.append(params)
-
- if isDefault:
- self.defaultItem =len(self.items) -1
-
-
- def addData(self, name, data, contentType=None, isDefault=False):
- """Adds data to be uploaded
- @param name: target name
- @param data: (str) data to upload
- @param contentType: (str) content type of the data (if desired)
- @param isDefault: (bool) if True, the item is the default item
- """
- return self._addItem(
- isDefault=isDefault,
-
- RequestType=consts.RequestType.PutData,
- Data=data,
- ContentType=contentType,
- DataLength=len(data),
- Name=name,
- UploadFrom=consts.UploadFrom.Direct
- )
-
-
- def addDirectory(self, name, directory, allowUnreadableFiles=False, defaultName=None):
- """Adds a directory to be uploaded
- @param name: target name
- @param directory: (abspath) of the directory to be uploaded
- @param defaultName: (for SSKs and USKs, the file to display when the key is requested)
-
- @note: if you add a directory, every attempt to add anything else will fail
- """
- return self._addItem(
- RequestType=consts.RequestType.PutDir,
- AllowUnreadableFiles=allowUnreadableFiles,
- Filename=directory,
- Name=name,
- DefaultName=defaultName,
- )
-
-
- def addFile(self, name, filename, contentType=None, isDefault=False):
- """Adds a file to be uploaded
- @param name: target name
- @param filename: (abspath) of the file to be uploaded
- @param contentType: (str) content type of the file (if desired)
- @param isDefault: (bool) if True, the item is the default item
- """
- return self._addItem(
- isDefault=isDefault,
-
- RequestType=consts.RequestType.PutFile,
- ContentType=contentType,
- Filename=filename,
- Name=name,
- UploadFrom=consts.UploadFrom.Disk,
- )
-
-
- def addRedirect(self, name, redirect, isDefault=False):
- """Adds a redirect to be uploaded
- @param name: target name
- @param redirect: (freenet-key) to redirect to
- @param isDefault: (bool) if True, the item is the default item
- """
- return self._addItem(
- isDefault=isDefault,
-
- RequestType=consts.RequestType.PutRedirect,
- Name=name,
- TargetURI=redirect,
- UploadFrom=consts.UploadFrom.Redirect
- )
-
-
- def getMessage(self, messageClass):
- """Returns the message for the request, ready to send the request to the node
- @param messageClass: (L{fcp2_0_message.Message}) class to fill in
- @return: (tuple) (L{consts.RequestType}, L{fcp2_0_message.Message}) or (None, None)
- if ther is nothing to upload
- """
- n = len(self.items)
- if n == 0:
- return None, None
-
- elif n == 1:
- requestType = self.items[0]['RequestType']
-
- if requestType == consts.RequestType.PutDir:
- msg = messageClass(consts.Message.ClientPutDiskDir)
- params = self.items[0]
- for param, value in params.items():
- if value is None or param in self.PrivateParamNames:
- continue
- msg[param] = value
-
- item = self.items[self.defaultItem]
- msg['DefaultName'] = item['Name']
-
- else:
- msg = messageClass(consts.Message.ClientPut)
- item = self.items[0]
- for param, value in item.items():
- if value is None or param in self.PrivateParamNames:
- continue
- if param == 'ContentType':
- param = 'Metadata.ContentType'
- msg[param] = value
-
- # handle some specilal cases
- if requestType == consts.RequestType.PutData:
- msg.data = item['Data']
- elif requestType == consts.RequestType.PutRedirect:
- if self.keyType != consts.KeyType.KSK:
- raise ValueError('For redirects keyType must be %s' % consts.KeyType.KSK)
-
- else:
- requestType = consts.RequestType.PutMultiple
- msg = messageClass(consts.Message.ClientPutComplexDir)
- data = None
- for n, itemParams in enumerate(self.items):
- for param, value in itemParams.items():
- if param == 'Data':
- if data is None:
- data = ''
- data += value
- continue
-
- if param == 'Name':
- param = 'Files.%s.%s' % (n, param)
- msg[param] = value
- continue
-
- if value is None or param in self.PrivateParamNames:
- continue
-
- if param == 'ContentType':
- param = 'Metadata.ContentType'
- param = 'Files.%s.%s' % (n, param)
- msg[param] = value
-
- item = self.items[self.defaultItem]
- msg['DefaultName'] = item['Name']
- if data is not None:
- msg.data = data
-
- # determine Uri
- if self.keyType == consts.KeyType.CHK:
- msg['URI'] = consts.KeyType.CHK
- if self.targetName:
- msg['TargetFilename'] = self.targetName
- elif self.keyType == consts.KeyType.KSK:
- msg['URI'] = consts.KeyType.KSK + self.targetName
- elif self.keyType == consts.KeyType.SSK:
- msg['URI'] = self.privateKey + self.targetName
- elif self.keyType == consts.KeyType.USK:
- msg['URI'] = self.privateKey + self.targetName
-
- # set params and some defaults
- msg['Global'] = False
- msg['Verbosity'] = consts.Verbosity.ReportProgress | consts.Verbosity.ReportCompression
- for param, value in self.params.items():
- if value is None:
- continue
- msg[param] = value
-
- return requestType, msg
-
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|