Save dumy records to tmp
This commit is contained in:
parent
2624e58004
commit
058ef6a81e
73
1export.py
73
1export.py
@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
from collections import namedtuple
|
||||||
import subprocess as sp
|
import subprocess as sp
|
||||||
import pprint
|
import pprint
|
||||||
import json
|
import json
|
||||||
@ -15,23 +16,31 @@ logging.basicConfig(level=logging.INFO)
|
|||||||
OP_BIN = 'op'
|
OP_BIN = 'op'
|
||||||
ZIP_BIN = 'zip'
|
ZIP_BIN = 'zip'
|
||||||
|
|
||||||
|
Record = namedtuple('Record', [
|
||||||
|
'title',
|
||||||
|
'username',
|
||||||
|
'password',
|
||||||
|
'url',
|
||||||
|
])
|
||||||
|
|
||||||
|
Vault = namedtuple('Vault', ['title', 'records'])
|
||||||
|
|
||||||
|
DUMMY_RECORD_SET = [
|
||||||
|
Record(
|
||||||
|
title='title' + str(i),
|
||||||
|
username='username' + str(i),
|
||||||
|
password='password' + str(i),
|
||||||
|
url='url' + str(i),
|
||||||
|
)
|
||||||
|
for i in range(10)
|
||||||
|
]
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
dir_path = tempfile.mkdtemp(suffix='password-export')
|
# dir_path = tempfile.mkdtemp(suffix='password-export')
|
||||||
logging.info('Store temp files in ' + dir_path)
|
dir_path = './tmp'
|
||||||
vaults = retrieve_vaults()
|
vaults = [Vault(title='test', records=DUMMY_RECORD_SET)]
|
||||||
for vault in vaults:
|
for vault in vaults:
|
||||||
vault_name, records = process_vault(vault)
|
save_vault(dir_path, vault)
|
||||||
with open(os.path.join(dir_path, vault_name.lower()), 'w') as csvfile:
|
|
||||||
record_writer = csv.writer(csvfile)
|
|
||||||
for record in records:
|
|
||||||
record_writer.writerow([
|
|
||||||
record['title'],
|
|
||||||
record['username'],
|
|
||||||
record['password'],
|
|
||||||
record['url']
|
|
||||||
])
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def process_vault(vault):
|
def process_vault(vault):
|
||||||
@ -46,36 +55,48 @@ def process_vault(vault):
|
|||||||
extracted = extract_item_fields(item_data)
|
extracted = extract_item_fields(item_data)
|
||||||
records.append(extracted)
|
records.append(extracted)
|
||||||
logging.info('Vault "{}": {}/{}'.format(vault_name, index + 1, len(items)))
|
logging.info('Vault "{}": {}/{}'.format(vault_name, index + 1, len(items)))
|
||||||
return vault_name, records
|
return Vault(title=vault_name, records=records)
|
||||||
|
|
||||||
|
|
||||||
|
def save_vault(base_dir, vault):
|
||||||
|
file_name = os.path.join(base_dir, vault.title.lower())
|
||||||
|
with open(file_name, 'w') as csvfile:
|
||||||
|
record_writer = csv.writer(csvfile)
|
||||||
|
for record in vault.records:
|
||||||
|
record_writer.writerow([
|
||||||
|
record.title,
|
||||||
|
record.username,
|
||||||
|
record.password,
|
||||||
|
record.url
|
||||||
|
])
|
||||||
|
|
||||||
|
|
||||||
def retrieve_vaults():
|
def retrieve_vaults():
|
||||||
data = catch_op_json(['op', 'list', 'vaults'])
|
data = catch_op_json([OP_BIN, 'list', 'vaults'])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def retrieve_items(vault_uuid):
|
def retrieve_items(vault_uuid):
|
||||||
data = catch_op_json(['op', 'list', 'items', '--vault=' + vault_uuid])
|
data = catch_op_json([OP_BIN, 'list', 'items', '--vault=' + vault_uuid])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def retrieve_item(item_uuid):
|
def retrieve_item(item_uuid):
|
||||||
data = catch_op_json(['op', 'get', 'item', item_uuid])
|
data = catch_op_json([OP_BIN, 'get', 'item', item_uuid])
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
def extract_item_fields(item_data):
|
def extract_item_fields(item_data):
|
||||||
details = item_data.get('details', {})
|
details = item_data.get('details', {})
|
||||||
fields = details.get('fields', [])
|
fields = details.get('fields', [])
|
||||||
title = item_data.get('overview', {}).get('title', '')
|
overview = item_data.get('overview', {})
|
||||||
username = value_from_fields(fields, 'username')
|
username = value_from_fields(fields, 'username')
|
||||||
password = value_from_fields(fields, 'password') or details.get('password', '')
|
password = value_from_fields(fields, 'password') or details.get('password', '')
|
||||||
url = item_data.get('overview', {}).get('url', '')
|
return Record(
|
||||||
return {
|
title=overview.get('title', ''),
|
||||||
'title': title,
|
username=username,
|
||||||
'username': username,
|
password=password,
|
||||||
'password': password,
|
url=overview.get('url', ''),
|
||||||
'url': url,
|
)
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def value_from_fields(item_fields, key):
|
def value_from_fields(item_fields, key):
|
||||||
|
Loading…
Reference in New Issue
Block a user