PSKManagement.py
author Edouard Tisserant
Mon, 26 Nov 2018 15:12:18 +0100
changeset 2460 89abeece2c71
parent 2429 15f18dc8b56a
child 2461 9624575a9cac
permissions -rw-r--r--
Fixed ID,last URI, or last connection date not being updated when connnecting to target.
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
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    23
def _default(ID):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    24
    return [ID,
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    25
            '', # 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
    26
            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
    27
            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
    28
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    29
def _dataByID(data):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    30
    return {row[COL_ID]:row for row in data}
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    31
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
def _LoadData(project_path):
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    33
    """ 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
    34
    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
    35
        _path = _mgtpath(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    36
        if os.path.exists(_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    37
            return json.loads(open(_path).read())
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    38
    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
    39
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    40
def _filterData(psk_files, data_input):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    41
    input_by_ID = _dataByID(data_input)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    42
    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
    43
    # 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
    44
    # 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
    45
    # 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
    46
    # 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
    47
    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
    48
       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
    49
           ID = filename[:-7]  # strip filename extension
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    50
           output.append(input_by_ID.get(ID,_default(ID)))
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    51
    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
    52
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    53
def GetData(project_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    54
    loaded_data = _LoadData(project_path)
2429
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    55
    if loaded_data:
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    56
        psk_files = os.listdir(_pskpath(project_path))
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    57
        return _filterData(psk_files, loaded_data)
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    58
    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
    59
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
    60
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
    61
    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
    62
    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
    63
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    64
def SaveData(project_path, data):
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
    pskpath = _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
    66
    if not os.path.isdir(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
    67
        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
    68
    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
    69
        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
    70
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
    71
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
def UpdateID(project_path, ID, secret, 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
    73
    pskpath = _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
    74
    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
    75
        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
    76
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
    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
    78
    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
    79
        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
    80
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    81
    # 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
    82
    data = _LoadData(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    83
    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
    84
    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
    85
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    86
    _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
    87
    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
    88
       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
    89
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
    90
    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
    91
    # 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
    92
    # 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
    93
    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
    94
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
    95
    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
    96
        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
    97
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    98
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    99
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   100
def ExportIDs(project_path, export_zip):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   101
    with ZipFile(export_zip, 'w') as zf:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   102
        path = _pskpath(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   103
        for nm in os.listdir(path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   104
            if nm.endswith('.secret') or nm == 'management.json':
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   105
                zf.write(os.path.join(path, nm), nm)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   106
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   107
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
   108
    zf = ZipFile(import_zip, 'r')
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   109
    data = GetData(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   110
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   111
    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
   112
    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
   113
    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
   114
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   115
    idata = _dataByID(data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   116
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
   117
    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
   118
    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
   119
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   120
    for imported_row in zip_filtered_data:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   121
        ID = imported_row[COL_ID]
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   122
        existing_row = idata.get(ID, None)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   123
        if existing_row is None:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   124
            data.append(imported_row)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   125
        else:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   126
            # 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
   127
            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
   128
                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
   129
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 == 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
   131
                return
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   132
            
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
   133
            if result in [REPLACE_ALL, REPLACE]:
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   134
                # replace with imported
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   135
                existing_row[:] = imported_row
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   136
                # 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
   137
                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
   138
    
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
   139
    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
   140
        zf.extract(ID+".secret", _pskpath(project_path))
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   141
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   142
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   143
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
   144
    return data
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   145
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
   146