PSKManagement.py
changeset 2339 48b4eba13064
child 2340 decf52efb7f7
equal deleted inserted replaced
2338:2c3222433244 2339:48b4eba13064
       
     1 #!/usr/bin/env python
       
     2 # -*- coding: utf-8 -*-
       
     3 
       
     4 # See COPYING file for copyrights details.
       
     5 
       
     6 from __future__ import absolute_import
       
     7 import os
       
     8 import time
       
     9 import json
       
    10 
       
    11 COL_ID,COL_URI,COL_DESC,COL_LAST = range(4)
       
    12 
       
    13 def _pskpath(project_path):
       
    14     return os.path.join(project_path, 'psk')
       
    15 
       
    16 def _mgtpath(project_path):
       
    17     return os.path.join(_pskpath(project_path), 'management.json')
       
    18 
       
    19 def _default():
       
    20     return ['', # default description
       
    21             None, # last known URI
       
    22             None]  # last connection date
       
    23 
       
    24 def _LoadData(project_path):
       
    25     if os.path.isdir(_pskpath(project_path)):
       
    26         _path = _mgtpath(project_path)
       
    27         # load known keys metadata
       
    28         # {ID:(Desc, LastKnownURI, LastConnect)}
       
    29         return json.loads(open(_path).read()) \
       
    30                if os.path.exists(_path) else {}
       
    31     return {}
       
    32 
       
    33 def GetData(project_path):
       
    34     # [(ID, Desc, LastKnownURI, LastConnect)
       
    35     data = []
       
    36     loaded_data = _LoadData(project_path)
       
    37     # go through all secret files available an build data
       
    38     # out of data recoverd from json and list of secret.
       
    39     # this implicitly filters IDs out of metadata who's
       
    40     # secret is missing
       
    41     psk_files = os.listdir(_pskpath(project_path))
       
    42     for filename in psk_files:
       
    43        if filename.endswith('.secret'):
       
    44            ID = filename[:-7]  # strip filename extension
       
    45            meta = loaded_data.get(ID,_default())                  
       
    46            data.append([ID]+meta)
       
    47     return data
       
    48 
       
    49 
       
    50 def DeleteID(project_path, ID):
       
    51     secret_path = os.path.join(_pskpath(project_path), ID+'.secret')
       
    52     os.remove(secret_path)
       
    53 
       
    54 def _StoreData(project_path, data):
       
    55     pskpath = _pskpath(project_path)
       
    56     if not os.path.isdir(pskpath):
       
    57         os.mkdir(pskpath)
       
    58     with open(_mgtpath(project_path), 'w') as f:
       
    59         f.write(json.dumps(data))
       
    60 
       
    61 def SaveData(project_path, data):
       
    62     to_store = {row[0]:row[1:] for row in data}
       
    63     _StoreData(project_path, to_store)
       
    64 
       
    65 def UpdateID(project_path, ID, secret, URI):
       
    66     pskpath = _pskpath(project_path)
       
    67     if not os.path.exists(pskpath):
       
    68         os.mkdir(pskpath)
       
    69 
       
    70     secpath = os.path.join(pskpath, ID+'.secret')
       
    71     with open(secpath, 'w') as f:
       
    72         f.write(ID+":"+secret)
       
    73 
       
    74     data = _LoadData(project_path)
       
    75     dataForID = [ID] + (data.get(ID, _default()) if data else _default())
       
    76     dataForID[COL_URI] = URI
       
    77     # FIXME : could store time instead os a string and use DVC model's cmp 
       
    78     # then date display could be smarter, etc - sortable sting hack for now
       
    79     dataForID[COL_LAST] = time.strftime('%y/%M/%d-%H:%M:%S')
       
    80     data[ID] = dataForID[1:]
       
    81     _StoreData(project_path, data)