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

Paulo Ricardo Paz Vital pvital at linux.vnet.ibm.com
Wed Mar 23 13:44:14 UTC 2016


First of all, what's the reason to have this method instead of use
TemplateModel.lookup(name) ?

On 03/22/2016 04:20 PM, Ramon Medeiros wrote:
> Signed-off-by: Ramon Medeiros <ramonn at linux.vnet.ibm.com>
> ---
>  utils.py | 30 ++++++++++++++++++++++++++++++
>  1 file changed, 30 insertions(+)
> 
> diff --git a/utils.py b/utils.py
> index f43f26e..0a3d02d 100644
> --- a/utils.py
> +++ b/utils.py
> @@ -35,6 +35,36 @@ 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'"

I don't like the idea to pass as argument a '*' mark. My suggestion is
set the method signature to get_template_by_name(name=all) and them, do
something like:

sql = "SELECT json from objects where type == 'template'"
if name != 'all':
    sql += " and id=='%s'" % name

In addition, I think that search by all templates is not coherent with
the method name :-P

> +
> +    # 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
>      m = re.match(expr, uri)
> 




More information about the Kimchi-devel mailing list