some improvements

This commit is contained in:
ingvar1995
2016-03-29 17:11:30 +03:00
parent 3b5e869055
commit ce7f6e03f5
5 changed files with 39 additions and 22 deletions

View File

@ -1,9 +1,11 @@
# coding=utf-8
from sqlite3 import connect
from settings import Settings
import profile as pr
from os import chdir
PAGE_SIZE = 42
MESSAGE_OWNER = {
'ME': 0,
'FRIEND': 1
@ -13,7 +15,7 @@ MESSAGE_OWNER = {
class History(object):
def __init__(self, name):
self._name = name
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(name + '.hstr')
cursor = db.cursor()
cursor.execute('CREATE TABLE IF NOT EXISTS friends('
@ -22,7 +24,7 @@ class History(object):
db.close()
def export(self, directory):
path = Settings.get_default_path() + self._name + '.hstr'
path = pr.ProfileHelper.get_path() + self._name + '.hstr'
new_path = directory + self._name + '.hstr'
with open(path, 'rb') as fin:
data = fin.read()
@ -31,7 +33,7 @@ class History(object):
print 'History exported to: {}'.format(new_path)
def add_friend_to_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -51,7 +53,7 @@ class History(object):
db.close()
def delete_friend_from_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -65,7 +67,7 @@ class History(object):
db.close()
def friend_exists_in_db(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
cursor = db.cursor()
cursor.execute('SELECT 0 FROM friends WHERE tox_id=?', (tox_id, ))
@ -74,7 +76,7 @@ class History(object):
return result is not None
def save_messages_to_db(self, tox_id, messages_iter):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -88,7 +90,7 @@ class History(object):
db.close()
def delete_messages(self, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
db = connect(self._name + '.hstr')
try:
cursor = db.cursor()
@ -105,7 +107,7 @@ class History(object):
class MessageGetter(object):
def __init__(self, name, tox_id):
chdir(Settings.get_default_path())
chdir(pr.ProfileHelper.get_path())
self._db = connect(name + '.hstr')
self._cursor = self._db.cursor()
self._cursor.execute('SELECT message, owner, unix_time, message_type FROM id' + tox_id +