1 # Copyright 2013 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 # This file is expected to be used under another directory to use,
6 # so we disable checking import path of GAE tools from this directory.
7 # pylint: disable=F0401,E0611,W0232
11 from google
.appengine
.ext
import blobstore
12 from google
.appengine
.ext
import ndb
15 class Profiler(ndb
.Model
):
16 """Profiler entity to store json data. Use run_id as its key.
17 Json data will be stored at blobstore, but can be referred by BlobKey."""
18 blob_key
= ndb
.BlobKeyProperty()
21 class Template(ndb
.Model
):
22 """Template to breakdown profiler with multiple tags.
23 Use content as its key."""
24 content
= ndb
.JsonProperty()
27 def CreateProfiler(blob_info
):
28 """Create Profiler entity in database of uploaded file. Return run_id."""
29 json_str
= blob_info
.open().read()
30 json_obj
= json
.loads(json_str
)
32 # Check the uniqueness of data run_id and store new one.
33 run_id
= json_obj
['run_id']
34 prof_key
= ndb
.Key('Profiler', run_id
)
35 if not prof_key
.get():
36 # Profile for this run_id does not exist
37 profiler
= Profiler(id=run_id
, blob_key
=blob_info
.key())
43 def GetProfiler(run_id
):
44 """Get Profiler entity from database of given run_id."""
46 profiler
= ndb
.Key('Profiler', run_id
).get()
47 return blobstore
.BlobReader(profiler
.blob_key
).read()
50 def CreateTemplates(blob_info
):
51 """Create Template entities for all templates of uploaded file. Return ndb.Key
52 of default template or None if not indicated or found in templates."""
53 json_str
= blob_info
.open().read()
54 json_obj
= json
.loads(json_str
)
56 # Return None when no default template indicated.
57 if 'default_template' not in json_obj
:
59 # Return None when no default template found in templates.
60 if json_obj
['default_template'] not in json_obj
['templates']:
63 # Check the uniqueness of template content and store new one.
64 for tag
, content
in json_obj
['templates'].iteritems():
65 content_str
= json
.dumps(content
)
66 tmpl_key
= ndb
.Key('Template', content_str
)
67 if tag
== json_obj
['default_template']:
68 default_key
= tmpl_key
69 if not tmpl_key
.get():
70 # Template of the same content does not exist.
71 template
= Template(id=content_str
, content
=content
)
77 def CreateTemplate(content
):
78 """Create Template entity for user to share."""
79 content_str
= json
.dumps(content
)
80 tmpl_key
= ndb
.Key('Template', content_str
)
81 if not tmpl_key
.get():
82 # Template of the same content does not exist.
83 template
= Template(id=content_str
, content
=content
)
89 def GetTemplate(tmpl_id
):
90 """Get Template entity of given tmpl_id generated by ndb.Key."""
92 template
= ndb
.Key(urlsafe
=tmpl_id
).get()
93 return json
.dumps(template
.content
)