[Kimchi-devel] [Kimchi 2/4] Method to retrieve stored templates at object store

Ramon Medeiros ramonn at linux.vnet.ibm.com
Wed Dec 23 15:12:03 UTC 2015


Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
---
 utils.py | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/utils.py b/utils.py
index 5a3f209..5f576f7 100644
--- a/utils.py
+++ b/utils.py
@@ -34,6 +34,34 @@ from wok.xmlutils.utils import xpath_get_text
 
 MAX_REDIRECTION_ALLOWED = 5
 
+def get_template_by_name(name):
+
+    conn = sqlite3.connect(config.get_object_store(), timeout=10)
+    cursor = conn.cursor()
+
+    # if * is passed: select all fields
+    sql = "SELECT json from objects where type == 'template' and id == '%s'" % name
+    if name == "*":
+        sql = "SELECT json from objects where type == 'template'"
+
+    # execute and fetch results
+    cursor.execute(sql)
+    content = cursor.fetchall()
+
+    # no record: return nothing
+    if len(content) == 0:
+        return {}
+
+    # sqllite returns a tuple of strings
+    # iterate over it and return a list of dictonaries
+    if len(content[0]) == 1:
+        return eval(content[0][0])
+
+    result = []
+    for dictonary in content[0]:
+        result.append(eval(dictonary))
+
+    return result
 
 def _uri_to_name(collection, uri):
     expr = '/plugins/kimchi/%s/(.*?)$' % collection
-- 
2.1.0




More information about the Kimchi-devel mailing list