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

Ramon Medeiros ramonn at linux.vnet.ibm.com
Wed Mar 23 20:50:50 UTC 2016



On 03/23/2016 05:06 PM, Aline Manera wrote:
>
>
> On 03/23/2016 11:10 AM, Ramon Medeiros wrote:
>>
>>
>> On 03/23/2016 10:44 AM, Paulo Ricardo Paz Vital wrote:
>>> First of all, what's the reason to have this method instead of use
>>> TemplateModel.lookup(name) ?
>> lookup uses the session object, which is not available at templates.
>
> Sorry! Why the objectstore is not available at templates?
> It should be available for the whole application.
how i can get it? I tried to pass the object store by parameter via 
LibvirtTemplate, but not successful
>
>>> 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
>> we can change the *
>>>
>>>> +
>>>> +    # 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)
>>>>
>>> _______________________________________________
>>> Kimchi-devel mailing list
>>> Kimchi-devel at ovirt.org
>>> http://lists.ovirt.org/mailman/listinfo/kimchi-devel
>>>
>>
>

-- 

Ramon Nunes Medeiros
Kimchi Developer
Linux Technology Center Brazil
IBM Systems & Technology Group
Phone : +55 19 2132 7878
ramonn at br.ibm.com




More information about the Kimchi-devel mailing list