Skip to content

Commit ad97198

Browse files
author
Marius Schlueter
committed
added initialization of sqlite
1 parent 4c13cf7 commit ad97198

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

pytrackunit/tucache.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
"""tucache module"""
22

3+
import sqlite3
4+
import os.path
35
from .webcache import WebCache
46

57
class TuCache:
68
"""tucache class"""
7-
def __init__(self,auth=None,_dir=None,verbose=False):
9+
def __init__(self,auth=None,_dir=None,use_sqlite=False,verbose=False):
810
self.cache = WebCache(auth=auth,_dir=_dir,verbose=verbose)
9-
self._verbose = verbose
11+
self.use_sqlite = use_sqlite
12+
if self.use_sqlite:
13+
self.web_db_path = os.path.join(_dir,"webdb.db")
14+
self._db = sqlite3.connect(self.web_db_path)
15+
cur = self._db.cursor()
16+
_res = list(cur.execute(
17+
'''
18+
SELECT name FROM sqlite_master WHERE type="table" AND name="history"
19+
'''))
20+
if len(_res) == 0:
21+
cur.execute('''CREATE TABLE history
22+
(unit text not null, time text not null, event int, keyId text, location text, address text, heading int, speed real,
23+
km real, run1 real,run2 real,run3 real,run4 real,runOdo real,temperature1 real,temperature2 real,
24+
input1 int,input2 int,input3 int,input4 int,input5 int,input6 int,input7 int,input8 int,input9 int,input10 int,
25+
output1 int,output2 int,output3 int,output4 int,output5 int,
26+
analogInput1 real,analogInput2 real,analogInput4 real,
27+
Input1ChangeCounter INT,Input2ChangeCounter INT,Input3ChangeCounter INT,Input4ChangeCounter INT,
28+
batteryLevel real,externalPower real,
29+
primary key (unit,time))''')
30+
self._db.commit()
1031
def clean(self):
1132
"""deletes all cached data"""
1233
self.cache.clean()
@@ -19,4 +40,7 @@ def get(self,url):
1940
return []
2041
if self.cache.dont_read_files and len(data) == 0:
2142
return []
43+
# if self.use_sqlite and "api.trackunit.com/public/Report/UnitHistory" in url:
44+
# cur = self.db.cursor()
45+
# _data = list(map(lambda x: (x["time"])))
2246
return data.get('list')

tests/test_tucache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ def test_Get_return_hashes():
5757
data = cache.get(DUMMY_URL)
5858
assert len(data) == 1
5959
assert data[0] == "70551c7a431274e4617c94ad307346d2.json"
60+
def test_db():
61+
cache = TuCache(_dir="pytest-web-cache",use_sqlite=True)
62+

tests/test_webcache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
from os.path import isfile, join
66

77
DUMMY_URL = 'https://pokeapi.co/api/v2/pokemon/ditto'
8-
DUMMY_RESPONSE_HASH = "3b28bef695a954335cadf285a6220b374bf5e947ef55102bea38823df53d2865"
9-
DATA_LEN = 21779
8+
DUMMY_RESPONSE_HASH = "493d9c6b65e19a64514d98a0515418f577c3a035300dc11423e2747591109764"
9+
DATA_LEN = 23291
1010

1111
def get_hash(x):
1212
return hashlib.sha256(x.encode('ascii')).hexdigest()

0 commit comments

Comments
 (0)