Permalink
Browse files

Changes, bug fixes, more builtins

  • Loading branch information...
Louis Sobel
Louis Sobel committed Apr 23, 2012
1 parent 4c86d16 commit 972e9d77afde84c8293cf8362d855ba16de3c854
Showing with 229 additions and 164 deletions.
  1. +23 −15 drapache/dbapiio.py
  2. +20 −11 drapache/dbapijinja.py
  3. +4 −2 drapache/dbapiserver.py
  4. +155 −94 drapache/dbpybuiltins.py
  5. +8 −33 drapache/dbpyexecute.py
  6. +18 −7 drapache/httpserver.py
  7. +1 −2 drapache/index_generator.py
View
@@ -36,6 +36,11 @@ def write_error(self,*args,**kwargs):
class LiveDropboxFile(StringIO.StringIO):
+ """
+ An in-memory file object representing a dropbox file
+ It is 'live' in the sense that it once it is closed, all changes made are
+ reflected to dropbox. So it's not really live, but under sufficient pre-conditions
+ (locking the file) it will be. A leaky abstraction I guess."""
def __init__(self,path,client,download=True):
@@ -50,22 +55,25 @@ def __init__(self,path,client,download=True):
else:
StringIO.StringIO.__init__(self)
+
+ def is_open(self):
+ return self.__open
+
def _update(self,client):
if self.__open:
self.seek(0)
client.put_file(self.path,self,overwrite=True)
- def close(self,locker=None):
+ def _close(self,locker):
if self.__open:
- if locker is not None:
- try:
- self._update(locker.client)
- finally:
- locker.release(self.path)
- self.__open = False
+ try:
+ self._update(locker.client)
+ finally:
+ locker.release(self.path)
+ self.__open = False
else:
pass
#this allows for mutliple accidental callings .close()
@@ -75,11 +83,11 @@ class WritableDropboxFile(LiveDropboxFile):
def __init__(self,path,client,download=True,mode='append'):
#mode is either write or append
- #if append, we have to download if download is true
+ #if append, we have to download if download is true, which generally will
+ #be set by the caller to True only if the file exists
#so we only have to download the file if download is true and the mode is append
-
do_download = (download and mode == 'append')
- LiveDropboxFile.__init__(self,path,client,do_download,mode)
+ LiveDropboxFile.__init__(self,path,client,do_download)
if mode == 'append':
self.seek(0,2)
@@ -95,7 +103,7 @@ def _update(self,client):
def write(self,what):
- if not self.__open:
+ if not self.is_open():
raise IOError('Cannot write to a closed file')
if self.mode == 'append':
@@ -107,7 +115,7 @@ def writeline(self,line):
def writelines(self,sequence):
- if not self.__open:
+ if not self.is_open():
raise IOError('Cannot write to a closed file!')
if self.mode == 'append':
@@ -134,7 +142,7 @@ def _update(self,client):
self.seek(0)
try:
- json.dump(self.json_object,self)
+ json.dump(self.json_object,self,indent=4)
self.truncate()
except TypeError:
self.seek(0)
@@ -223,7 +231,7 @@ def lock(self,path,timeout=None):
flag_list.sort()
if not flag_list:
- raise AssertionError('There should not be an empty list of flags at this point')
+ raise IOError('There should not be an empty list of flags at this point')
first_uid = flag_list[0][1]
@@ -261,7 +269,7 @@ def lock(self,path,timeout=None):
def close_all(self,):
for file_h in self.open_files:
- file_h.close(locker=self)
+ file_h._close(self)
def register_open_file(self,file_h):
self.open_files.append(file_h)
View
@@ -5,33 +5,42 @@
import os
import dropbox
+class TemplateNotFound(Exception):
+ pass
+
class DropboxLoader(jinja2.BaseLoader):
def __init__(self,client,search_root):
self.client = client
- self.search_root = search_root
-
-
- def get_source(self,environment,template):
-
- template_path = os.path.join(self.search_root,template)
+ self.search_root = search_root
+ def get_source(self,environment,path):
+
+
+ template_path = self.search_root + path
+
try:
f = self.client.get_file(template_path).read()
except dropbox.rest.ErrorResponse as e:
- raise jinja2.TemplateNotFound(template)
+ if e.status == 404:
+ raise jinja2.TemplateNotFound(template_path)
+ else:
+ raise IOError("Error connecting to dropbox to download template")
return f,template_path,True
-
-def render_dropbox_template(client,template,data,search_root):
+def render_dropbox_template(client,template_path,data):
+
+ search_root,path = template_path.rsplit('/',1)
+ search_root += '/'
+
env = jinja2.Environment(loader=DropboxLoader(client,search_root))
try:
- template = env.get_template(template)
+ template = env.get_template(path)
output = template.render(**data)
except jinja2.TemplateNotFound as e:
- output = "Template %s not found; looking in %s" % (template,search_root)
+ raise TemplateNotFound()
return output
View
@@ -97,6 +97,9 @@ def _serve_static(self,file_meta):
"""
path = file_meta['path']
f = self.client.get_file(path).read()
+ if f.startswith('#DBPYEXECUTE'):
+ param_dict = dict(client=self.client,request=self.request)
+ return dbpyexecute.execute(f,**param_dict)
headers = {'Content-type':self._get_content_type(file_meta)}
return ResponseObject(200,f,headers)
@@ -108,11 +111,10 @@ def _serve_python(self,file_meta):
#allows these files to be shared without getting executed
headers = {'Content-type':'text/plain'}
return ResponseObject(200,f,headers)
-
param_dict = dict(client=self.client,request=self.request)
-
return dbpyexecute.execute(f,**param_dict)
+
def _find_and_serve_index(self,directory_meta,path):
"""
Oops, something went wrong.

0 comments on commit 972e9d7

Please sign in to comment.