-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathImportExportHandler.py
More file actions
52 lines (44 loc) · 1.72 KB
/
ImportExportHandler.py
File metadata and controls
52 lines (44 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from PyQt6 import QtWidgets
from components import Database as db
import csv
def getCSVFile(type):
fileName = QtWidgets.QFileDialog().getOpenFileName(None, 'Import CSV File', '', 'CSV File (*.csv)')
if not fileName[0]:
return False
file = open(fileName[0], 'r')
with file:
fileContent = csv.reader(file)
content = []
for index, row in enumerate(fileContent):
if not index and row[0] != type:
return False
content.append(row)
if not fileContent.line_num:
return False
return content
def saveAs():
fileName = QtWidgets.QFileDialog.getSaveFileName(None, 'Save GAS Scenario', '', 'GAS Scenario (*.gas)')
if not fileName[0]:
return False
with open(fileName[0], 'w+') as file:
conn = db.getConnection()
for line in conn.iterdump():
file.write('{}\n'.format(line))
conn.close()
def load():
fileName = QtWidgets.QFileDialog().getOpenFileName(None, 'Load GAS Scenario', '', 'GAS Scenario (*.gas)')
if not fileName[0]:
return False
with open(fileName[0], 'r') as file:
conn = db.getConnection()
cursor = conn.cursor()
tables = list(cursor.execute("SELECT name FROM sqlite_master WHERE type IS 'table'"))
cursor.executescript(';'.join(['DROP TABLE IF EXISTS {}'.format(table[0]) for table in tables]))
cursor.executescript(file.read())
conn.close()
def removeTables():
conn = db.getConnection()
cursor = conn.cursor()
tables = list(cursor.execute("SELECT name FROM sqlite_master WHERE type IS 'table'"))
cursor.executescript(';'.join(['DROP TABLE IF EXISTS {}'.format(table[0]) for table in tables]))
conn.close()