PSKManagement.py
author Edouard Tisserant
Tue, 27 Nov 2018 10:54:13 +0100
changeset 2461 9624575a9cac
parent 2460 89abeece2c71
child 2492 7dd551ac2fa0
permissions -rw-r--r--
Cosmetic change in PSKManagement.py
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     1
#!/usr/bin/env python
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     2
# -*- coding: utf-8 -*-
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     3
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     4
# See COPYING file for copyrights details.
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     5
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     6
from __future__ import absolute_import
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     7
import os
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     8
import time
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
     9
import json
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    10
from zipfile import ZipFile
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    11
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    12
# PSK Management Data model :
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    13
# [[ID,Desc, LastKnownURI, LastConnect]]
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
    14
COL_ID, COL_URI, COL_DESC, COL_LAST = range(4)
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
    15
REPLACE, REPLACE_ALL, KEEP, KEEP_ALL, CANCEL = range(5)
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    16
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    17
def _pskpath(project_path):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    18
    return os.path.join(project_path, 'psk')
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    19
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    20
def _mgtpath(project_path):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    21
    return os.path.join(_pskpath(project_path), 'management.json')
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    22
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    23
def _ensurePSKdir(project_path):
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    24
    pskpath = _pskpath(project_path)
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    25
    if not os.path.exists(pskpath):
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    26
        os.mkdir(pskpath)
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    27
    return pskpath
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    28
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    29
def _default(ID):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    30
    return [ID,
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    31
            '', # default description
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    32
            None, # last known URI
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    33
            None]  # last connection date
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    34
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    35
def _dataByID(data):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    36
    return {row[COL_ID]:row for row in data}
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    37
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    38
def _LoadData(project_path):
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    39
    """ load known keys metadata """
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    40
    if os.path.isdir(_pskpath(project_path)):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    41
        _path = _mgtpath(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    42
        if os.path.exists(_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    43
            return json.loads(open(_path).read())
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    44
    return []
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    45
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    46
def _filterData(psk_files, data_input):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    47
    input_by_ID = _dataByID(data_input)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    48
    output = []
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    49
    # go through all secret files available an build data
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    50
    # out of data recoverd from json and list of secret.
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    51
    # this implicitly filters IDs out of metadata who's
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    52
    # secret is missing
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    53
    for filename in psk_files:
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    54
       if filename.endswith('.secret'):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    55
           ID = filename[:-7]  # strip filename extension
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    56
           output.append(input_by_ID.get(ID,_default(ID)))
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    57
    return output
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    58
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    59
def GetData(project_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    60
    loaded_data = _LoadData(project_path)
2429
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    61
    if loaded_data:
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    62
        psk_files = os.listdir(_pskpath(project_path))
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    63
        return _filterData(psk_files, loaded_data)
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    64
    return []
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    65
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    66
def DeleteID(project_path, ID):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    67
    secret_path = os.path.join(_pskpath(project_path), ID+'.secret')
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    68
    os.remove(secret_path)
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    69
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    70
def SaveData(project_path, data):
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    71
    _ensurePSKdir(project_path)
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    72
    with open(_mgtpath(project_path), 'w') as f:
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    73
        f.write(json.dumps(data))
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    74
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    75
def UpdateID(project_path, ID, secret, URI):
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    76
    pskpath = _ensurePSKdir(project_path)
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    77
    if not os.path.exists(pskpath):
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    78
        os.mkdir(pskpath)
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    79
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    80
    secpath = os.path.join(pskpath, ID+'.secret')
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    81
    with open(secpath, 'w') as f:
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    82
        f.write(ID+":"+secret)
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    83
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    84
    # here we directly use _LoadData, avoiding filtering that could be long
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    85
    data = _LoadData(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    86
    idata = _dataByID(data)
2460
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    87
    dataForID = idata.get(ID, None) if data else None
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    88
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    89
    _is_new_ID = dataForID is None
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    90
    if _is_new_ID:
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    91
       dataForID = _default(ID)
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    92
2339
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    93
    dataForID[COL_URI] = URI
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    94
    # FIXME : could store time instead os a string and use DVC model's cmp 
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    95
    # then date display could be smarter, etc - sortable sting hack for now
48b4eba13064 IDManager : refactored a bit, moved some code into PSKManagement.py. Now captures URI and PSK on new PYRO(S) and propose them when editing URI. Import/export still to be implemented.
Edouard Tisserant
parents:
diff changeset
    96
    dataForID[COL_LAST] = time.strftime('%y/%M/%d-%H:%M:%S')
2460
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    97
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    98
    if _is_new_ID:
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    99
        data.append(dataForID)
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
   100
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   101
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   102
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   103
def ExportIDs(project_path, export_zip):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   104
    with ZipFile(export_zip, 'w') as zf:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   105
        path = _pskpath(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   106
        for nm in os.listdir(path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   107
            if nm.endswith('.secret') or nm == 'management.json':
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   108
                zf.write(os.path.join(path, nm), nm)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   109
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   110
def ImportIDs(project_path, import_zip, should_I_replace_callback):
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   111
    zf = ZipFile(import_zip, 'r')
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   112
    data = GetData(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   113
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   114
    zip_loaded_data = json.loads(zf.open('management.json').read())
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   115
    name_list = zf.namelist()
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   116
    zip_filtered_data = _filterData(name_list, zip_loaded_data)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   117
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   118
    idata = _dataByID(data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   119
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   120
    keys_to_import = []
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   121
    result = None
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   122
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   123
    for imported_row in zip_filtered_data:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   124
        ID = imported_row[COL_ID]
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   125
        existing_row = idata.get(ID, None)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   126
        if existing_row is None:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   127
            data.append(imported_row)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   128
        else:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   129
            # callback returns the selected list for merge or none if canceled
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   130
            if result not in [REPLACE_ALL, KEEP_ALL]:
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   131
                result = should_I_replace_callback(existing_row, imported_row)
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   132
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   133
            if result == CANCEL:
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   134
                return
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   135
            
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   136
            if result in [REPLACE_ALL, REPLACE]:
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   137
                # replace with imported
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   138
                existing_row[:] = imported_row
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   139
                # copy the key of selected
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   140
                keys_to_import.append(ID)
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   141
    
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   142
    for ID in keys_to_import:
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   143
        zf.extract(ID+".secret", _pskpath(project_path))
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   144
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   145
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   146
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   147
    return data
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   148
2428
e0f16317668e IDManager : finished Import/Export. Added merging capability to import (asks if particular ID is replaced during import). Added ESC as closing shortcut to IDManager dialog, and adjusted its size.
Edouard Tisserant
parents: 2340
diff changeset
   149