Robot Framework Integrated Development Environment (RIDE)
librarydatabase.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 import os
17 import sqlite3
18 import time
19 
20 from ..preferences.settings import SETTINGS_DIRECTORY
21 from ..spec.iteminfo import LibraryKeywordInfo
22 from ..lib.robot.utils import system_decode
23 
24 CREATION_SCRIPT = """\
25 CREATE TABLE libraries (id INTEGER PRIMARY KEY,
26  name TEXT,
27  doc_format TEXT,
28  arguments TEXT,
29  last_updated REAL);
30 CREATE TABLE keywords (name TEXT,
31  doc TEXT,
32  arguments TEXT,
33  library_name TEXT,
34  library INTEGER,
35  FOREIGN KEY(library) REFERENCES libraries(id));
36 """
37 
38 DATABASE_FILE = os.path.join(system_decode(SETTINGS_DIRECTORY),
39  'librarykeywords.db')
40 
41 
43  print('Creating librarykeywords database to "%s"' % DATABASE_FILE)
44 
45  connection = sqlite3.connect(DATABASE_FILE)
46  connection.executescript(CREATION_SCRIPT)
47  connection.commit()
48  connection.close()
49 
50 
52  connection = sqlite3.connect(DATABASE_FILE)
53  try:
54  connection.execute('select id, name, doc_format, arguments,'
55  ' last_updated from libraries')
56  connection.execute('select name, doc, arguments, library_name,'
57  ' library from keywords')
58  finally:
59  connection.close()
60 
61 
63  if not os.path.exists(SETTINGS_DIRECTORY):
64  os.makedirs(SETTINGS_DIRECTORY)
65  if not os.path.exists(DATABASE_FILE):
67  else:
68  try:
70  except sqlite3.DatabaseError as err:
71  print('removing database "%s"' % DATABASE_FILE)
72  print('error during database validation "%s"' % err)
73  try:
74  os.remove(DATABASE_FILE)
75  except Exception as err:
76  print('failed to remove database "%s"' % DATABASE_FILE)
77  raise err
79 
80 
82 
83  def __init__(self, database):
84  self._connection_connection = sqlite3.connect(database, timeout=30.0)
85 
86  def create_database(self):
87  self._cursor_cursor().executescript(CREATION_SCRIPT)
88  self._connection_connection.commit()
89 
90  def _cursor(self):
91  return self._connection_connection.cursor()
92 
93  def close(self):
94  self._connection_connection.close()
95 
96  def insert_library_keywords(self, library_name, library_arguments,
97  keywords):
98  library_doc_format = "ROBOT"
99  if len(keywords) > 0:
100  library_doc_format = keywords[0].doc_format
101  # if any(x.doc_format != library_doc_format for x in keywords):
102  # print("debug: keywords doc format not
103  # consistent within library")
104 
105  cur = self._cursor_cursor()
106  old_versions = cur.execute('select id from libraries where name = ? '
107  'and arguments = ?',
108  (library_name,
109  str(library_arguments))).fetchall()
110  cur.executemany('delete from keywords where library = ?', old_versions)
111  cur.executemany('delete from libraries where id = ?', old_versions)
112  lib = self._insert_library_insert_library(library_name, library_doc_format,
113  library_arguments, cur)
114  keyword_values = [[kw.name, kw.doc, u' | '.join(kw.arguments),
115  kw.source,
116  lib[0]] for kw in keywords if kw is not None]
117  self._insert_library_keywords_insert_library_keywords(keyword_values, cur)
118  self._connection_connection.commit()
119 
120  def update_library_timestamp(self, name, arguments, milliseconds=None):
121  self._cursor_cursor().execute('update libraries set last_updated = ?'
122  ' where name = ? and arguments = ?',
123  (milliseconds or time.time(), name,
124  str(arguments)))
125  self._connection_connection.commit()
126 
127  def fetch_library_keywords(self, library_name, library_arguments):
128  lib = self._fetch_lib_fetch_lib(library_name, library_arguments, self._cursor_cursor())
129  if lib is None:
130  return []
131  return [LibraryKeywordInfo(name, doc, lib[2], library_name,
132  arguments.split(u' | ') if arguments else [])
133  for name, doc, arguments, library_name in
134  self._connection_connection.execute('select name, doc, arguments,'
135  ' library_name from keywords where'
136  ' library = ?', [lib[0]])]
137 
138  def library_exists(self, library_name, library_arguments):
139  return self._fetch_lib_fetch_lib(library_name, library_arguments,
140  self._cursor_cursor()) is not None
141 
142  def get_library_last_updated(self, library_name, library_arguments):
143  lib = self._fetch_lib_fetch_lib(library_name, library_arguments, self._cursor_cursor())
144  if not lib:
145  return 0.0
146  return lib[4]
147 
148  def _insert_library(self, name, doc_format, arguments, cursor):
149  cursor.execute('insert into libraries values (null, ?, ?, ?, ?)',
150  (name, doc_format, str(arguments), time.time()))
151  return self._fetch_lib_fetch_lib(name, arguments, cursor)
152 
153  def _fetch_lib(self, name, arguments, cursor):
154  t = cursor.execute('select max(last_updated) from libraries where name'
155  ' = ? and arguments = ?',
156  (name, str(arguments))).fetchone()[0]
157  return cursor.execute('select * from libraries where name = ?'
158  ' and arguments = ? and last_updated = ?',
159  (name, str(arguments), t)).fetchone()
160 
161  def _insert_library_keywords(self, data, cursor):
162  cursor.executemany('insert into keywords values (?, ?, ?, ?, ?)', data)
def library_exists(self, library_name, library_arguments)
def fetch_library_keywords(self, library_name, library_arguments)
def insert_library_keywords(self, library_name, library_arguments, keywords)
def get_library_last_updated(self, library_name, library_arguments)
def _insert_library(self, name, doc_format, arguments, cursor)
def _fetch_lib(self, name, arguments, cursor)
def update_library_timestamp(self, name, arguments, milliseconds=None)
def system_decode(string)
Decodes bytes from system (e.g.
Definition: encoding.py:81