PSKManagement.py
author Edouard Tisserant <edouard.tisserant@gmail.com>
Fri, 08 May 2020 16:47:52 +0200
branchsvghmi
changeset 2948 faaf677ea99d
parent 2492 7dd551ac2fa0
child 3750 f62625418bff
permissions -rw-r--r--
SVGHMI: Add an exslt function to collect used widget types, in order to only include necessary JS definitions in furure changes.
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
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    17
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
    18
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
    19
    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
    20
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    21
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
    22
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
    23
    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
    24
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    25
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    26
def _ensurePSKdir(project_path):
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    27
    pskpath = _pskpath(project_path)
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    28
    if not os.path.exists(pskpath):
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    29
        os.mkdir(pskpath)
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    30
    return pskpath
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    31
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    32
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    33
def _default(ID):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    34
    return [ID,
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    35
            '',  # default description
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    36
            None,  # last known URI
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
    37
            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
    38
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    39
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    40
def _dataByID(data):
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    41
    return {row[COL_ID]: row for row in data}
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    42
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    43
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
    44
def _LoadData(project_path):
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    45
    """ 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
    46
    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
    47
        _path = _mgtpath(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    48
        if os.path.exists(_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    49
            return json.loads(open(_path).read())
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    50
    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
    51
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    52
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    53
def _filterData(psk_files, data_input):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    54
    input_by_ID = _dataByID(data_input)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    55
    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
    56
    # 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
    57
    # 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
    58
    # 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
    59
    # 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
    60
    for filename in psk_files:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    61
        if filename.endswith('.secret'):
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    62
            ID = filename[:-7]  # strip filename extension
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    63
            output.append(input_by_ID.get(ID, _default(ID)))
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    64
    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
    65
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    66
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    67
def GetData(project_path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    68
    loaded_data = _LoadData(project_path)
2429
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    69
    if loaded_data:
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    70
        psk_files = os.listdir(_pskpath(project_path))
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    71
        return _filterData(psk_files, loaded_data)
15f18dc8b56a Merge, with surprizingly little conflicts
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2428
diff changeset
    72
    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
    73
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    74
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
    75
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
    76
    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
    77
    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
    78
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    79
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    80
def SaveData(project_path, data):
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    81
    _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
    82
    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
    83
        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
    84
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
    85
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
    86
def UpdateID(project_path, ID, secret, URI):
2461
9624575a9cac Cosmetic change in PSKManagement.py
Edouard Tisserant
parents: 2460
diff changeset
    87
    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
    88
    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
    89
        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
    90
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
    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
    92
    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
    93
        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
    94
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    95
    # 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
    96
    data = _LoadData(project_path)
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
    97
    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
    98
    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
    99
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
   100
    _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
   101
    if _is_new_ID:
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
   102
        dataForID = _default(ID)
2460
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
   103
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
   104
    dataForID[COL_URI] = URI
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
   105
    # FIXME : could store time instead os a string and use DVC model's cmp
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
   106
    # 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
   107
    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
   108
89abeece2c71 Fixed ID,last URI, or last connection date not being updated when connnecting to target.
Edouard Tisserant
parents: 2429
diff changeset
   109
    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
   110
        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
   111
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   112
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   113
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
   114
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   115
def ExportIDs(project_path, export_zip):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   116
    with ZipFile(export_zip, 'w') as zf:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   117
        path = _pskpath(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   118
        for nm in os.listdir(path):
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   119
            if nm.endswith('.secret') or nm == 'management.json':
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   120
                zf.write(os.path.join(path, nm), nm)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   121
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
   122
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   123
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
   124
    zf = ZipFile(import_zip, 'r')
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   125
    data = GetData(project_path)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   126
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   127
    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
   128
    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
   129
    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
   130
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   131
    idata = _dataByID(data)
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
    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
   134
    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
   135
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   136
    for imported_row in zip_filtered_data:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   137
        ID = imported_row[COL_ID]
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   138
        existing_row = idata.get(ID, None)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   139
        if existing_row is None:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   140
            data.append(imported_row)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   141
        else:
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   142
            # 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
   143
            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
   144
                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
   145
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
            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
   147
                return
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
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
            if result in [REPLACE_ALL, REPLACE]:
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   150
                # replace with imported
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   151
                existing_row[:] = imported_row
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   152
                # 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
   153
                keys_to_import.append(ID)
2492
7dd551ac2fa0 check_sources.sh makes me become even less productive
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 2461
diff changeset
   154
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
   155
    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
   156
        zf.extract(ID+".secret", _pskpath(project_path))
2340
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   157
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   158
    SaveData(project_path, data)
decf52efb7f7 IDManager: added import/export plus little cosmetic enhancements.
Edouard Tisserant
parents: 2339
diff changeset
   159
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
   160
    return data