Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Updated docloader to handle proper JSON and no _id.

Change-Id: I61a397408c06460250d59ee86f6690bf916ea467
  • Loading branch information...
commit d022a9fbf66e7a57513c0113b8b86cd10bc901a1 1 parent 2a8b9ba
@ingenthr ingenthr authored
Showing with 31 additions and 23 deletions.
  1. +26 −18 docloader
  2. +5 −5 gamesim-sample/design_docs/players.json
View
44 docloader
@@ -6,6 +6,7 @@ import getopt
import json
import ast
import tempfile
+import re
from couchbase import client
@@ -48,18 +49,22 @@ def parse_args(args):
return user, pswd, host, bucket, args
-def save_doc(bucket,fp, views):
+def save_doc(bucket, dockey, fp, views):
buf = fp.read()
- result = ast.literal_eval(buf)
+ result = json.loads(buf)
if isinstance(result, dict):
- try:
- doc_id = bucket.save(result)
- except:
- doc_id = "_design/testing"
- if result['_id'] and 'views' in result:
- for key in result['views'].iterkeys():
- viewpath = result['_id'] + '/_view/' + key
- views.append(viewpath)
+ if '_id' not in result:
+ bucket.set(dockey, 0, 0, json.dumps(result))
+ else:
+ try:
+ doc_id = bucket.save(result)
+ print "just now saving", doc_id
+ except:
+ doc_id = "_design/testing"
+ if result['_id'] and 'views' in result:
+ for key in result['views'].iterkeys():
+ viewpath = result['_id'] + '/_view/' + key
+ views.append(viewpath)
def listFiles(bucket, dir, views):
basedir = dir
@@ -68,8 +73,8 @@ def listFiles(bucket, dir, views):
for item in os.listdir(dir):
if os.path.isfile(os.path.join(basedir, item)):
with open(os.path.join(basedir, item), 'r') as fp:
- #print item
- save_doc(bucket, fp, views)
+ print "working with ", item
+ save_doc(bucket, dockey, fp, views)
else:
subdirlist.append(os.path.join(basedir, item))
for subdir in subdirlist:
@@ -78,16 +83,19 @@ def listFiles(bucket, dir, views):
def unzip_file_and_upload(bucket,file, views):
zfobj = zipfile.ZipFile(file)
for name in zfobj.namelist():
- if name.endswith('/'):
- print "dir:", name
- else:
+ #if name.endswith('/'):
+ #print "dir:", name
+ #else:
#print 'file:', name
+ fileslashed = re.split(".json", name)
+ fileinlist = re.split("/", fileslashed[0])
+ file_for_key = fileinlist.pop()
temp = tempfile.NamedTemporaryFile(delete=False)
fname = temp.name
temp.write(zfobj.read(name))
temp.close()
with open(fname, 'r') as fp:
- save_doc(bucket, fp, views)
+ save_doc(bucket, file_for_key, fp, views)
os.remove(fname)
def populate_docs(bucket, dir, views):
@@ -114,7 +122,7 @@ def main():
# execute views at least once
for viewpath in views:
rows = newbucket.view(viewpath)
- for row in rows:
- print row
+ #for row in rows:
+ #print row
if __name__ == '__main__': main()
View
10 gamesim-sample/design_docs/players.json
@@ -1,11 +1,11 @@
{
- '_id': '_design/players',
- 'language': 'javascript',
- 'views': {
- 'leaderboard': {
+ "_id": "_design/players",
+ "language": "javascript",
+ "views": {
+ "leaderboard": {
"map": "function (doc) {\n if (doc.jsonType == \"player\") {\n emit(doc.experience, null);\n }\n}"
},
- 'playerlist': {
+ "playerlist": {
"map": "function (doc) {\n if (doc.jsonType == \"player\") {\n emit(doc._id, null);\n }\n}"
}
}

0 comments on commit d022a9f

Please sign in to comment.
Something went wrong with that request. Please try again.