close db after use

This commit is contained in:
j 2016-08-24 14:15:12 +02:00
parent bb8f91bbd8
commit a6aa9afe63

View file

@ -216,6 +216,7 @@ class Client(object):
for i in db: for i in db:
c.execute(i) c.execute(i)
conn.commit() conn.commit()
conn.close()
def load_plugins(self, base=os.path.join(utils.basedir(), 'client.d')): def load_plugins(self, base=os.path.join(utils.basedir(), 'client.d')):
global parse_path, example_path, ignore_file, encode global parse_path, example_path, ignore_file, encode
@ -248,14 +249,18 @@ class Client(object):
def get(self, key, default=None): def get(self, key, default=None):
conn, c = self._conn() conn, c = self._conn()
c.execute(u'SELECT value FROM setting WHERE key = ?', (key, )) c.execute(u'SELECT value FROM setting WHERE key = ?', (key, ))
value = default
for row in c: for row in c:
return row[0] value = row[0]
return default break
conn.close()
return value
def set(self, key, value): def set(self, key, value):
conn, c = self._conn() conn, c = self._conn()
c.execute(u'INSERT OR REPLACE INTO setting values (?, ?)', (key, str(value))) c.execute(u'INSERT OR REPLACE INTO setting values (?, ?)', (key, str(value)))
conn.commit() conn.commit()
conn.close()
def info(self, oshash): def info(self, oshash):
conn, c = self._conn() conn, c = self._conn()
@ -264,6 +269,7 @@ class Client(object):
for row in c: for row in c:
info = json.loads(row[0]) info = json.loads(row[0])
break break
conn.close()
return info return info
def get_info(self, oshash, prefix=None): def get_info(self, oshash, prefix=None):
@ -300,6 +306,7 @@ class Client(object):
for row in c: for row in c:
path = row[0] path = row[0]
paths.append(path) paths.append(path)
conn.close()
return paths return paths
def online(self): def online(self):
@ -334,6 +341,7 @@ class Client(object):
conn, c = self._conn() conn, c = self._conn()
c.execute(u'DELETE FROM encode WHERE site = ?', (site, )) c.execute(u'DELETE FROM encode WHERE site = ?', (site, ))
conn.commit() conn.commit()
conn.close()
self.add_encodes(site, files) self.add_encodes(site, files)
def get_encodes(self, site, status=''): def get_encodes(self, site, status=''):
@ -341,13 +349,16 @@ class Client(object):
sql = u'SELECT oshash FROM encode WHERE site = ? AND status = ?' sql = u'SELECT oshash FROM encode WHERE site = ? AND status = ?'
args = [site, status] args = [site, status]
c.execute(sql, tuple(args)) c.execute(sql, tuple(args))
return [row[0] for row in c] encodes = [row[0] for row in c]
conn.close()
return encodes
def add_encodes(self, site, files): def add_encodes(self, site, files):
conn, c = self._conn() conn, c = self._conn()
for oshash in files: for oshash in files:
c.execute(u'INSERT INTO encode VALUES (?, ?, ?, 0)', (oshash, site, '')) c.execute(u'INSERT INTO encode VALUES (?, ?, ?, 0)', (oshash, site, ''))
conn.commit() conn.commit()
conn.close()
def update_encodes(self, add=False): def update_encodes(self, add=False):
# send empty list to get updated list of requested info/files/data # send empty list to get updated list of requested info/files/data
@ -361,6 +372,7 @@ class Client(object):
c.execute(sql, (site, )) c.execute(sql, (site, ))
known = [row[0] for row in c] known = [row[0] for row in c]
files = list(set(files) - set(known)) files = list(set(files) - set(known))
conn.close()
self.add_encodes(site, files) self.add_encodes(site, files)
else: else:
self.set_encodes(site, files) self.set_encodes(site, files)
@ -395,6 +407,7 @@ class Client(object):
c.execute(u'INSERT OR REPLACE INTO file values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)', c.execute(u'INSERT OR REPLACE INTO file values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
t) t)
conn.commit() conn.commit()
conn.close()
return 'error' not in info return 'error' not in info
def get_resolution(self, info): def get_resolution(self, info):
@ -508,6 +521,7 @@ class Client(object):
conn, c = self._conn() conn, c = self._conn()
c.execute(u'SELECT path FROM file WHERE path LIKE ? AND deleted < 0', [u"%s%%" % path]) c.execute(u'SELECT path FROM file WHERE path LIKE ? AND deleted < 0', [u"%s%%" % path])
known_files = [r[0] for r in c.fetchall()] known_files = [r[0] for r in c.fetchall()]
conn.close()
files = [] files = []
unknown = [] unknown = []
@ -554,6 +568,7 @@ class Client(object):
for f in deleted_files: for f in deleted_files:
c.execute(u'UPDATE file SET deleted=? WHERE path=?', (deleted, f)) c.execute(u'UPDATE file SET deleted=? WHERE path=?', (deleted, f))
conn.commit() conn.commit()
conn.close()
''' '''
print("scanned volume %s: %s files, %s new, %s deleted, %s ignored, %s unsupported" % ( print("scanned volume %s: %s files, %s new, %s deleted, %s ignored, %s unsupported" % (
@ -563,7 +578,6 @@ class Client(object):
name, len(files), len(new_files), len(deleted_files), len(ignored))) name, len(files), len(new_files), len(deleted_files), len(ignored)))
def extract(self, args): def extract(self, args):
conn, c = self._conn()
if args: if args:
if args[0] == 'offline': if args[0] == 'offline':
files = self.get_encodes(self._config['url']) files = self.get_encodes(self._config['url'])
@ -650,7 +664,6 @@ class Client(object):
if not self.user: if not self.user:
print("you need to login") print("you need to login")
return return
conn, c = self._conn()
documents = [] documents = []
if args: if args:
data = [] data = []
@ -748,7 +761,6 @@ class Client(object):
if not self.user: if not self.user:
print("you need to login") print("you need to login")
return return
conn, c = self._conn()
for oshash in args: for oshash in args:
info = self.info(oshash) info = self.info(oshash)
if info and 'error' not in info: if info and 'error' not in info:
@ -820,7 +832,6 @@ class Client(object):
if not self.user: if not self.user:
print("you need to login") print("you need to login")
return return
conn, c = self._conn()
for f in args: for f in args:
r = self._add_document(f) r = self._add_document(f)
if not r: if not r:
@ -853,6 +864,7 @@ class Client(object):
'ctime': row[4], 'ctime': row[4],
'mtime': row[5], 'mtime': row[5],
}) })
conn.close()
return files return files
def clean(self, args): def clean(self, args):