5 |
5 |
6 from __future__ import absolute_import |
6 from __future__ import absolute_import |
7 import os |
7 import os |
8 import wx |
8 import wx |
9 import wx.dataview as dv |
9 import wx.dataview as dv |
10 import json |
10 import PSKManagement as PSK |
11 |
11 from PSKManagement import COL_ID,COL_URI,COL_DESC,COL_LAST |
12 def _GetInitialData(psk_path): |
|
13 # [(ID, Desc, LastKnownURI, LastConnect) |
|
14 data = [] |
|
15 |
|
16 data_path = os.path.join(psk_path, 'management.json') |
|
17 |
|
18 if os.path.isdir(psk_path): |
|
19 # load known keys metadata |
|
20 # {ID:(Desc, LastKnownURI, LastConnect)} |
|
21 recovered_data = json.loads(open(data_path).read()) \ |
|
22 if os.path.exists(data_path) else {} |
|
23 |
|
24 # go through all secret files available an build data |
|
25 # out of data recoverd from json and list of secret. |
|
26 # this implicitly filters IDs out of metadata who's |
|
27 # secret is missing |
|
28 psk_files = os.listdir(psk_path) |
|
29 for filename in psk_files: |
|
30 if filename.endswith('.secret'): |
|
31 ID = filename[:-7] # strip filename extension |
|
32 meta = recovered_data.get(ID, |
|
33 ['', # default description |
|
34 None, # last known URI |
|
35 None]) # last connection date |
|
36 |
|
37 data.append([ID]+meta) |
|
38 return data |
|
39 |
|
40 def _DeleteID(psk_path, ID): |
|
41 secret_path = os.path.join(psk_path, ID+'.secret') |
|
42 os.remove(secret_path) |
|
43 |
|
44 def _SaveData(psk_path, data): |
|
45 if not os.path.isdir(psk_path): |
|
46 os.mkdir(psk_path) |
|
47 data_path = os.path.join(psk_path, 'management.json') |
|
48 to_store = {row[0]:row[1:] for row in data} |
|
49 with open(data_path, 'w') as f: |
|
50 f.write(json.dumps(to_store)) |
|
51 |
12 |
52 class IDBrowserModel(dv.PyDataViewIndexListModel): |
13 class IDBrowserModel(dv.PyDataViewIndexListModel): |
53 def __init__(self, psk_path, columncount): |
14 def __init__(self, psk_path, columncount): |
54 self.psk_path = psk_path |
15 self.psk_path = psk_path |
55 self.columncount = columncount |
16 self.columncount = columncount |
56 self.data = _GetInitialData(psk_path) |
17 self.data = PSK.GetData(psk_path) |
57 dv.PyDataViewIndexListModel.__init__(self, len(self.data)) |
18 dv.PyDataViewIndexListModel.__init__(self, len(self.data)) |
58 |
19 |
59 def _saveData(self): |
20 def _saveData(self): |
60 _SaveData(self.psk_path, self.data) |
21 PSK.SaveData(self.psk_path, self.data) |
61 |
22 |
62 def GetColumnType(self, col): |
23 def GetColumnType(self, col): |
63 return "string" |
24 return "string" |
64 |
25 |
65 def GetValueByRow(self, row, col): |
26 def GetValueByRow(self, row, col): |
96 def DeleteRows(self, rows): |
57 def DeleteRows(self, rows): |
97 rows = list(rows) |
58 rows = list(rows) |
98 rows.sort(reverse=True) |
59 rows.sort(reverse=True) |
99 |
60 |
100 for row in rows: |
61 for row in rows: |
|
62 PSK.DeleteID(self.psk_path, self.data[row][COL_ID]) |
101 del self.data[row] |
63 del self.data[row] |
102 _DeleteID(self.psk_path, ID) |
|
103 self.RowDeleted(row) |
64 self.RowDeleted(row) |
104 self._saveData() |
65 self._saveData() |
105 |
66 |
106 def AddRow(self, value): |
67 def AddRow(self, value): |
107 self.data.append(value) |
68 self.data.append(value) |
108 self.RowAppended() |
69 self.RowAppended() |
109 self._saveData() |
70 self._saveData() |
110 |
71 |
111 colflags = dv.DATAVIEW_COL_RESIZABLE|dv.DATAVIEW_COL_SORTABLE |
72 colflags = dv.DATAVIEW_COL_RESIZABLE|dv.DATAVIEW_COL_SORTABLE |
112 COL_ID,COL_URI,COL_DESC,COL_LAST = range(4) |
|
113 |
73 |
114 class IDBrowser(wx.Panel): |
74 class IDBrowser(wx.Panel): |
115 def __init__(self, parent, ctr, SelectURICallBack=None, SelectIDCallBack=None, **kwargs): |
75 def __init__(self, parent, ctr, SelectURICallBack=None, SelectIDCallBack=None, **kwargs): |
116 wx.Panel.__init__(self, parent, -1, size=(400,200)) |
76 wx.Panel.__init__(self, parent, -1, size=(400,200)) |
117 |
77 |
135 if self.isManager |
95 if self.isManager |
136 else dv.DATAVIEW_CELL_INERT), |
96 else dv.DATAVIEW_CELL_INERT), |
137 args(_("Last connection"), COL_LAST, width = 100), |
97 args(_("Last connection"), COL_LAST, width = 100), |
138 ] |
98 ] |
139 |
99 |
140 self.model = IDBrowserModel( |
100 self.model = IDBrowserModel(ctr.ProjectPath, len(ColumnsDesc)) |
141 os.path.join(str(ctr.ProjectPath), 'psk'), |
|
142 len(ColumnsDesc)) |
|
143 self.dvc.AssociateModel(self.model) |
101 self.dvc.AssociateModel(self.model) |
144 |
102 |
145 for a,k in ColumnsDesc: |
103 for a,k in ColumnsDesc: |
146 self.dvc.AppendTextColumn(*a,**dict(k, flags = colflags)) |
104 self.dvc.AppendTextColumn(*a,**dict(k, flags = colflags)) |
147 |
105 |