1 | # -*- coding: ISO-8859-1 -*- |
---|
2 | |
---|
3 | ################################## |
---|
4 | # @program smon |
---|
5 | # @description simulation monitor |
---|
6 | # @copyright Copyright â(c)2009 Centre National de la Recherche Scientifique CNRS. |
---|
7 | # All Rights Reservedâ |
---|
8 | # @svn_file $Id: repo_io.py 2599 2013-03-24 19:01:23Z jripsl $ |
---|
9 | # @version $Rev: 2599 $ |
---|
10 | # @lastrevision $Date: 2013-03-24 20:01:23 +0100 (Sun, 24 Mar 2013) $ |
---|
11 | # @license CeCILL (http://dods.ipsl.jussieu.fr/jripsl/smon/LICENSE) |
---|
12 | ################################## |
---|
13 | |
---|
14 | import sys |
---|
15 | import sqlite3 |
---|
16 | |
---|
17 | import types |
---|
18 | |
---|
19 | _conn=None |
---|
20 | timeout=1 |
---|
21 | |
---|
22 | def connect(): |
---|
23 | global _conn |
---|
24 | |
---|
25 | if _conn is not None: |
---|
26 | raise Exception() |
---|
27 | |
---|
28 | _conn=sqlite3.connect(":memory:",timeout) |
---|
29 | |
---|
30 | create_tables() |
---|
31 | |
---|
32 | def free(): |
---|
33 | global _conn |
---|
34 | |
---|
35 | if _conn is None: |
---|
36 | raise Exception() |
---|
37 | |
---|
38 | _conn.close() |
---|
39 | |
---|
40 | _conn=None |
---|
41 | |
---|
42 | def create_tables(): |
---|
43 | |
---|
44 | _conn.execute("create table simulation (id INTEGER PRIMARY KEY, name TEXT)") |
---|
45 | _conn.execute("create unique index idx_simulation on simulation (name)") |
---|
46 | |
---|
47 | _conn.execute("create table message (id INTEGER PRIMARY KEY, simulation_id TEXT)") # TODO: check how to use INT datatype for simulation_id column |
---|
48 | |
---|
49 | def populate_tables_with_sample(): |
---|
50 | |
---|
51 | rows = [('SIMU-001',), |
---|
52 | ('SIMU-002',), |
---|
53 | ('SIMU-003',),] |
---|
54 | _conn.executemany('INSERT INTO simulation (name) VALUES (?)', rows) |
---|
55 | |
---|
56 | def retrieve_simulations(): |
---|
57 | li=[] |
---|
58 | c=_conn.cursor() |
---|
59 | |
---|
60 | c.execute("select name,id from simulation") |
---|
61 | |
---|
62 | rs=c.fetchone() |
---|
63 | while rs is not None: |
---|
64 | li.append(types.Simulation(name=rs[0],id=rs[1])) |
---|
65 | rs=c.fetchone() |
---|
66 | |
---|
67 | return li |
---|
68 | |
---|
69 | def retrieve_simulation(name): |
---|
70 | c=_conn.cursor() |
---|
71 | |
---|
72 | c.execute("select name,id from simulation where name = ?",(name,)) |
---|
73 | |
---|
74 | rs=c.fetchone() |
---|
75 | |
---|
76 | if rs is None: |
---|
77 | raise Exception() |
---|
78 | |
---|
79 | return types.Simulation(id=rs[1]) |
---|
80 | |
---|
81 | def delete_simulation(simulation): |
---|
82 | _conn.execute("delete from simulation where name = ?",(simulation.name,)) |
---|
83 | |
---|
84 | def create_simulation(simulation): |
---|
85 | _conn.execute("insert into simulation (name) values (?)",(simulation.name,)) |
---|
86 | |
---|
87 | def update_simulation_status(simulation): |
---|
88 | _conn.execute("update simulation set status=? where name = ?",(simulation.status,simulation.name)) |
---|
89 | |
---|
90 | def retrieve_messages(simulation): |
---|
91 | li=[] |
---|
92 | |
---|
93 | c=_conn.cursor() |
---|
94 | |
---|
95 | _conn.execute("select id from message where simulation_id = ?",(simulation.id,)) |
---|
96 | |
---|
97 | rs=c.fetchone() |
---|
98 | while rs is not None: |
---|
99 | li.append(types.Message(id=rs[0])) |
---|
100 | rs=c.fetchone() |
---|
101 | |
---|
102 | return li |
---|
103 | |
---|
104 | def delete_messages(simulation): |
---|
105 | _conn.execute("delete from message where simulation_id = ?",(simulation.id,)) |
---|
106 | |
---|
107 | def create_message(message): |
---|
108 | _conn.execute("insert into message (simulation_id) values (?)",(message.simuid,)) |
---|
109 | |
---|
110 | def retrieve_last_message(simulation): |
---|
111 | c=_conn.cursor() |
---|
112 | |
---|
113 | _conn.execute("select id, content from message where simulation_id=? order by id desc limit 1",(simulation.id,)) |
---|
114 | |
---|
115 | rs=c.fetchone() |
---|
116 | |
---|
117 | if rs is None: |
---|
118 | raise Exception() |
---|
119 | |
---|
120 | return types.Message(id=rs[0]) |
---|