Skip to content

Commit

Permalink
additional bug fixes for v0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael R. Hines committed Jun 8, 2014
1 parent 8492c42 commit 028d556
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 25 deletions.
2 changes: 1 addition & 1 deletion common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if sys.getdefaultencoding() != "utf-8" :
print sys.getdefaultencoding()
print "FIXME! WE NEED THE CORRECT DEFAULT ENCODING! AHHHHHH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
#reload(sys).setdefaultencoding("utf-8")
reload(sys).setdefaultencoding("utf-8")

micalogger = False
txnlogger = False
Expand Down
41 changes: 19 additions & 22 deletions couch_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@

try :
from jnius import autoclass
String = autoclass('java.lang.String')
except ImportError, e :
mdebug("pyjnius not available. We must be on a desktop.")
mdebug("pyjnius not available. Probably on a server.")

class ResourceNotFound(Exception) :
def __init__(self, msg, e = False):
Expand Down Expand Up @@ -41,6 +42,15 @@ def __init__(self, msg, e = False):
def __str__(self) :
return self.msg

class NotImplementedError(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e

def __str__(self) :
return self.msg

class MicaDatabaseCouchDB(object) :
def __init__(self, db) :
self.db = db
Expand Down Expand Up @@ -111,15 +121,15 @@ def __init__(self, db) :

def __setitem__(self, name, doc) :
try :
err = self.db.put(self.dbname, name, json.dumps(doc))
err = self.db.put(self.dbname, name, String(json.dumps(doc)))
if err != "" :
raise CommunicationError("Error occured putting document: " + name + " " + err)
except Exception, e :
raise CommunicationError("Error occured putting document: " + str(e), e)

def __getitem__(self, name) :
try :
doc = self.db.get(self.dbname, name)
doc = self.db.get(String(self.dbname), String(name))
if doc == "" :
return False
if doc is not None :
Expand All @@ -132,7 +142,7 @@ def __getitem__(self, name) :

def __delitem__(self, name) :
try :
err = self.db.delete(self.dbname, name)
err = self.db.delete(String(self.dbname), String(name))
except Exception, e :
raise CommunicationError("Error occured deleting document: " + name + " " + str(e), e)
if err != "" :
Expand All @@ -143,24 +153,20 @@ def put_attachment(self, name, filename, contents, doc = False) :

def get_attachment(self, name, filename) :
try :
attach = self.db.get_attachment(self.dbname, name, filename)
attach = self.db.get_attachment(String(self.dbname), String(name), String(filename))
except Exception, e :
raise CommunicationError("Error getting attachment: " + name + " " + str(e), e)
if attach is None :
raise ResourceNotFound("Could not find attachment for document: " + name)
#mdebug("Result is of type: " + str(type(attach)))
#mdebug("Got " + str(len(attach)) + " bytes from java.")
# The ByteArray pyjnius is actually a 'memoryview' from java,
# which you can google about
joined = "".join(map(chr, attach))
#mdebug("Joining succeeded.")
decoded = joined.decode("utf-8")
#mdebug("Decoding succeeded.")
return decoded

def doc_exist(self, name) :
try :
result = self.db.doc_exist(self.dbname, name)
result = self.db.doc_exist(String(self.dbname), String(name))
if result == "error" :
raise CommunicationError("Error occured checking document existence: " + name)
return True if result == "true" else False
Expand All @@ -180,55 +186,47 @@ def view(self, name, startkey = False, endkey = False, keys = False, stale = Fal
vname = parts[1]
params = {}
if startkey :
mdebug("Adding: *" + str(startkey) + "*")
params["startkey"] = startkey
if endkey :
mdebug("Adding: *" + str(endkey) + "*")
params["endkey"] = endkey
if keys :
mdebug("Adding " + str(len(keys)) + " to view seed.")
uuid = str(uuid4.uuid4())
for key in keys :
assert(isinstance(key, str) or isinstance(key, unicode))
self.db.view_seed(uuid, username, key)
self.db.view_seed(String(uuid), String(username), String(key))
seed = True

params["keys"] = uuid
if stale :
mdebug("Adding: *" + str(stale) + "*")
params["stale"] = stale

if len(params) == 0 :
params = ""
else :
params = json.dumps(params)

mdebug("Final length of params input: " + str(len(params)))

it = self.db.view(self.dbname, design, vname, params, str(username))
it = self.db.view(String(self.dbname), String(design), String(vname), String(params), String(str(username)))
if it is None :
raise CommunicationError("Error occured for view: " + name)

while True :
has_next = self.db.view_has_next(it)

if not has_next :
mdebug("Iterator is exhausted. Returning")
break

result = self.db.view_next(it)
if result is None :
raise CommunicationError("Iteration error occured for view: " + name)
j = json.loads(result)
mdebug("Going to yield: " + str(j))
yield j["result"]
except Exception, err :
err_msg = "Error getting view: " + name + " " + str(err)
except CommunicationError, e :
err_msg = str(err)
finally :
if seed and uuid:
self.db.view_seed_cleanup(uuid)
self.db.view_seed_cleanup(String(uuid))
if err_msg :
raise CommunicationError(err_msg)

Expand All @@ -239,5 +237,4 @@ def __init__(self, db_already_local) :

def __getitem__(self, dbname) :
self.dbname = dbname
mdebug("Mobile Database " + dbname + " requested. Returning New Object")
return MicaDatabaseCouchbaseMobile(self.db)
3 changes: 1 addition & 2 deletions mica.py
Original file line number Diff line number Diff line change
Expand Up @@ -3195,11 +3195,10 @@ def go(params) :
pass
'''


mdebug("Initializing logging.")
mica_init_logging(params["log"])

if not params["tonefile"] :
if "tonefile" not in params or not params["tonefile"] :
params["tonefile"] = cwd + "/chinese.txt" # from https://github.com/lxyu/pinyin

mdebug("Building tone file")
Expand Down

0 comments on commit 028d556

Please sign in to comment.