Commits

Dhananjay Nene  committed 09edfdd

Brought in a few more changes

  • Participants
  • Parent commits 37e5837

Comments (0)

Files changed (8)

+*.py[cod]
+
+# C extensions
+*.so
+
+# Packages
+*.egg
+*.egg-info
+dist
+build
+eggs
+parts
+bin
+var
+sdist
+develop-eggs
+.installed.cfg
+lib
+lib64
+
+# Installer logs
+pip-log.txt
+
+# Unit test / coverage reports
+.coverage
+.tox
+nosetests.xml
+
+# Translations
+*.mo
+
+# Mr Developer
+.mr.developer.cfg
+.project
+.pydevproject
+*.egg-info/

File data/files/python.png

Added
New image

File data/files/ubuntu.png

Added
New image

File src/pybuckets/botos3.py

 from boto.s3.key import Key
 from pybuckets import Bucket, BucketServer, DuplicateBucketNameException, \
     BucketsException, server_registry
+from pybuckets import build_kw_from_params_and_object
 import operator
 
 # S3CreateError - body, box_usage, bucket, error_code, error_message,message,reason,request_id,status
         k = Key(self.s3bucket)
         k.key = name
         return k.set_contents_from_string(value)
-
-def build_kw_from_params_and_object(params,obj,candidates) :
-    kw = {}
-    for key in candidates :
-        if key in params : kw[key] = params[key]
-        elif hasattr(obj,key) : kw[key] = getattr(obj,key)
-    return kw
+    
+    def delete_key(self,name):
+        k = Key(self.s3bucket)
+        k.key = name
+        k.delete()
 
 class BotoS3Server(BucketServer):
     def __init__(self,**config):
-        super(BotoS3Server,self).__init__()
-        for key,val in config.items() :
-            if key in ('aws_access_key_id','aws_secret_access_key','host','port','is_secure', 'debug','calling_format','headers','location','policy') :
-                self.__dict__[key] = val
+        super(BotoS3Server,self).__init__(('aws_access_key_id','aws_secret_access_key','host','port','is_secure', 'debug','calling_format','headers','location','policy'),**config)
         
         self.connection = S3Connection(
                     **build_kw_from_params_and_object({},self,

File src/pybuckets/local.py

         
 class LocalFSServer(BucketServer):
     def __init__(self,**config):
-        super(LocalFSServer,self).__init__()
-        self.path = config['path']
-        self.config['_remove_non_empty_buckets'] = config.get('_remove_non_empty_buckets',False)
+        super(LocalFSServer,self).__init__(('path'),**config)
         if not os.path.isdir(self.path) :
-            raise LocalFSException('%s must be a directory' % self.path)
+            raise LocalFSException('%s must be a directory' % self.path,None)
         
-    def get_bucket_names(self):
+    def keys(self):
         return os.listdir(self.path)
     
     def get(self,name):
         return LocalFSBucket(name,self)
     
     def delete_bucket(self,key):
-        if self.config['_remove_non_empty_buckets'] :
-            shutil.rmtree(os.path.join(self.path,key))
-        else :
-            os.rmdir(os.path.join(self.path,key))
+        os.rmdir(os.path.join(self.path,key))
     
     def create_bucket(self,name,**kwargs):
         try :

File src/pybuckets/pybuckets.py

 bucket_registry = {}
 """ Registry for autoregistration of various component libraries supporting just a bucket"""
 
+def build_kw_from_params_and_object(params,obj,candidates) :
+    kw = {}
+    for key in candidates :
+        if key in params : kw[key] = params[key]
+        elif hasattr(obj,key) : kw[key] = getattr(obj,key)
+    return kw
+
 class BucketsException(Exception):
     """Exception class to wrap various exceptions"""
     def __init__(self,e):
         
 class BucketServer(object):
     "A bucket server"
-    def __init__(self):
-        self.config = {}
+    def __init__(self,valid_keys,**config):
+        for key,val in config.items() :
+            if key in valid_keys :
+                self.__dict__[key] = val
+        
     def __len__(self):
         return len(self.keys())
     def __iter__(self):
         return self.get(key)
     def __delitem__(self,key):
         return self.delete_bucket(key)
+    def supports_mime_type(self,mime_type):
+        return True
+    def supported_mime_types(self):
+        return ['*/*']
     @classmethod
     def get_server(cls,name,**creds):
         return server_registry[name](**creds)

File src/pybuckets/rscloudfiles.py

         
 class CloudfilesServer(BucketServer):
     def __init__(self,**config):
-        super(CloudfilesServer,self).__init__()
-        self.username = config['username']
-        self.api_key = config['api_key']
+        super(CloudfilesServer,self).__init__(('username','api_key'),**config)
         self.connection = cloudfiles.get_connection(self.username, self.api_key)
         
     def keys(self):

File test/test.py

+from pybuckets.pybuckets import BucketServer
+from unittest import TestCase
+import pybuckets.botos3
+import pybuckets.rscloudfiles
+import pybuckets.local
+import pybuckets.mongodb
+import pybuckets.pyb_couchdb
+import datetime
+import json
+import filecmp
+import os
+from conf import *
+
+##############################################################
+#   NOTE :
+#   You will need a separate conf.py in the same directory as
+#   test.py with the following variables defined
+#
+##############################################################
+# aws_access_key_id = ...
+# aws_secret_access_key = ...
+# bucket_name = ...
+#
+#
+# cf_username=...
+# cf_api_key=...
+##############################################################
+
+class TestBuckets(TestCase):
+    
+    def test_one(self):            
+#        bucket_server = BucketServer.get_server('botos3',aws_access_key_id= aws_access_key_id,aws_secret_access_key=aws_secret_access_key)
+#        bucket_server = BucketServer.get_server('cloudfiles',username= cf_username,api_key=cf_api_key)
+        bucket_server = BucketServer.get_server('localfs',path='../data/localfs')
+
+        for current_bucket_name in bucket_server.keys() :
+            print 'Exists bucket %s' % current_bucket_name
+            
+        if not bucket_name in bucket_server.keys() : bucket_server.create_bucket(bucket_name)
+        
+        self.assertEquals(bucket_name in bucket_server.keys(),True)
+        
+        bucket = bucket_server[bucket_name]
+        
+        for key in bucket :
+            print 'Deleting existing bucket key %s' % key
+            del bucket[key]
+        keys_added = []
+            
+        self.assertEquals(bucket.keys(),[])
+        
+        if bucket_server.supports_mime_type('text/plain') :
+            bucket['foo'] = 'bar'
+            self.assertEquals(bucket['foo'],'bar')
+            keys_added.append('foo')
+            
+        if bucket_server.supports_mime_type('image/png') :
+            src, target = (('../data/%s/python.png' % dir) 
+                           for dir in ('files','tmp'))
+            bucket('python_logo') << src
+            bucket('python_logo') >> target
+            self.assertEquals(filecmp.cmp(src,target),True)
+            os.remove(target)
+            keys_added.append('python_logo')
+
+            src, target = (('../data/%s/ubuntu.png' % dir) 
+                           for dir in ('files','tmp'))
+            with open(src,'r') as f:
+                bucket('ubuntu_logo') << f 
+            with open(target,'w') as f:
+                bucket('ubuntu_logo') >> f 
+            self.assertEquals(filecmp.cmp(src,target),True)
+            os.remove(target)
+            keys_added.append('ubuntu_logo')
+        
+        if bucket_server.supports_mime_type('application/json') :
+            json_str = json.dumps([{'hello':'world','foo':'bar'},'baz',['lorem','ipsum']])
+            bucket['json_str'] = json_str
+            self.assertEquals(bucket['json_str'],json_str)
+            keys_added.append('json_str')
+
+        self.assertEquals(sorted(keys_added),sorted(bucket.keys()))
+        
+        for key in bucket.keys() :
+            del bucket[key]
+            
+        self.assertEquals(len(bucket.keys()),0)
+
+
+    def testTwo(self):
+#        bucket_server = BucketServer.get_server('mongodb',host='localhost',port=27017,database_name='testdb')
+        bucket_server = BucketServer.get_server('couchdb',url='http://localhost:5984')
+        for current_bucket_name in bucket_server.keys() :
+            print 'Exists bucket %s' % current_bucket_name
+            
+        if not bucket_name in bucket_server.keys() : bucket_server.create_bucket(bucket_name)
+        self.assertEquals(bucket_name in bucket_server.keys(),True)
+        
+        bucket = bucket_server[bucket_name]
+        
+        for key in bucket :
+            print 'Deleting existing bucket key %s' % key
+            del bucket[key]
+        keys_added = []
+
+        self.assertEquals(len(bucket.keys()),0)
+        
+        if bucket_server.supports_mime_type('text/plain') :
+            bucket['foo'] = 'bar'
+            self.assertEquals(bucket['foo'],'bar')
+            keys_added.append('foo')
+            
+        if bucket_server.supports_mime_type('application/json') :
+            obj = {'hello':'world','foo':'bar'}
+            json_str = json.dumps([{'hello':'world','foo':'bar'},'baz',['lorem','ipsum']])
+            bucket['json_str'] = json.dumps(obj)
+            self.assertEquals(bucket['json_str'],obj)
+            keys_added.append('json_str')
+
+        self.assertEquals(sorted(keys_added),sorted(bucket.keys()))
+        
+       
+