|
1 | 1 | #!/usr/bin/env python |
2 | | -''' |
| 2 | +""" |
3 | 3 | pass2csv: Exports passwords from pass to CSV format |
4 | 4 |
|
5 | 5 | This software is distributed by qualIP Software under the GNU GENERAL PUBLIC |
6 | 6 | LICENSE v3 in the hope that others may find it useful too. |
7 | 7 |
|
8 | 8 | The original source and a copy of the GPLv3 license can be found here: |
9 | 9 | https://github.com/qualIP/qip-pass-export |
10 | | -''' |
| 10 | +""" |
11 | 11 |
|
12 | | -# flake8: noqa: E501 |
13 | | - |
14 | | -from pathlib import Path |
15 | 12 | import argparse |
16 | 13 | import csv |
17 | 14 | import io |
18 | 15 | import os |
19 | 16 | import re |
20 | 17 | import sys |
21 | 18 | import types |
| 19 | +from pathlib import Path |
22 | 20 |
|
23 | 21 | try: |
24 | 22 | from ._version import __version__ |
|
28 | 26 |
|
29 | 27 | def main(): |
30 | 28 | parser = argparse.ArgumentParser( |
31 | | - prog='pass2csv', |
32 | | - description='Exports passwords from pass to CSV format', |
| 29 | + prog="pass2csv", |
| 30 | + description="Exports passwords from pass to CSV format", |
33 | 31 | ) |
34 | | - parser.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__) |
35 | | - parser.add_argument('passfiles', metavar="passfile", nargs='+', type=Path, help='Password file(s) (*.gpg) to export') |
| 32 | + parser.add_argument("-V", "--version", |
| 33 | + action="version", version="%(prog)s " + __version__) |
| 34 | + parser.add_argument("passfiles", |
| 35 | + metavar="passfile", nargs=argparse.ONE_ORMORE, type=Path, |
| 36 | + help="Password file(s) (*.gpg) to export") |
36 | 37 | args = parser.parse_args() |
37 | 38 |
|
38 | 39 | try: |
39 | 40 | import gnupg |
40 | 41 | except ImportError as err: |
41 | 42 | print(err, file=sys.stderr) |
42 | | - print('The python-gnupg module is required to run this software.', file=sys.stderr) |
| 43 | + print("The python-gnupg module is required to run this software.", file=sys.stderr) |
43 | 44 | sys.exit(1) |
44 | 45 | gpg = gnupg.GPG() |
45 | | - gpg.encoding = 'utf-8' |
| 46 | + gpg.encoding = "utf-8" |
46 | 47 |
|
47 | 48 | # https://help.passbolt.com/faq/start/import-passwords |
48 | 49 | # Using Csv (BitWarden) format |
49 | 50 | csv_fields = ( |
50 | | - 'description', # YES |
51 | | - 'folder', # YES |
52 | | - 'favorite', # no |
53 | | - 'type', # no |
54 | | - 'name', # YES |
55 | | - 'notes', # YES |
56 | | - 'fields', # no |
57 | | - 'reprompt', # no |
58 | | - 'login_uri', # YES |
59 | | - 'login_username', # YES |
60 | | - 'login_password', # YES |
61 | | - 'login_totp', # no |
| 51 | + "description", |
| 52 | + "folder", |
| 53 | + "favorite", # unused |
| 54 | + "type", # unused |
| 55 | + "name", |
| 56 | + "notes", |
| 57 | + "fields", # unused |
| 58 | + "reprompt", # unused |
| 59 | + "login_uri", |
| 60 | + "login_username", |
| 61 | + "login_password", |
| 62 | + "login_totp", # unused |
62 | 63 | ) |
63 | 64 | csvwriter = csv.DictWriter(sys.stdout, fieldnames=csv_fields) |
64 | 65 | csvwriter.writeheader() |
65 | 66 |
|
66 | 67 | for passfile in args.passfiles: |
67 | 68 | relfile = passfile |
68 | 69 | try: |
69 | | - relfile = relfile.absolute().relative_to(Path.home() / '.password-store') |
| 70 | + relfile = relfile.absolute().relative_to(Path.home() / ".password-store") |
70 | 71 | except ValueError: |
71 | 72 | try: |
72 | 73 | relfile = relfile.absolute().relative_to(Path.home()) |
73 | 74 | except ValueError: |
74 | 75 | pass |
75 | | - if passfile.suffix == '.gpg': |
76 | | - with open(passfile, 'rb') as fp: |
| 76 | + if passfile.suffix == ".gpg": |
| 77 | + with open(passfile, "rb") as fp: |
77 | 78 | indata = gpg.decrypt_file(fp) |
78 | 79 | assert indata.ok |
79 | 80 | indata = str(indata) |
80 | 81 | indata = io.StringIO(indata) |
81 | | - csvrow = types.SimpleNamespace(**{k: '' for k in csv_fields}) |
| 82 | + csvrow = types.SimpleNamespace(**{k: "" for k in csv_fields}) |
82 | 83 | csvrow.notes = [] |
83 | 84 | for iline, line in enumerate(indata, start=1): |
84 | | - line = line.rstrip('\r\n') |
| 85 | + line = line.rstrip("\r\n") |
85 | 86 | if iline == 1: |
86 | 87 | csvrow.login_password = line |
87 | 88 | continue |
88 | | - m = not csvrow.login_uri \ |
89 | | - and re.match(r'(?i)^(?:URL|URI)(?: *1)? *: *(?P<content>.*)$', line) |
| 89 | + m = not csvrow.login_uri and re.match( |
| 90 | + r"(?i)^(?:URL|URI)(?: *1)? *: *(?P<content>.*)$", line |
| 91 | + ) |
90 | 92 | if m: |
91 | | - content = m.group('content').strip() |
| 93 | + content = m.group("content").strip() |
92 | 94 | if content: |
93 | | - if not re.match(r'(?i)^[a-z]+://', content): |
94 | | - content = 'https://' + content |
| 95 | + if not re.match(r"(?i)^[a-z]+://", content): |
| 96 | + content = "https://" + content |
95 | 97 | csvrow.login_uri = content |
96 | 98 | continue |
97 | | - m = not csvrow.login_username \ |
98 | | - and re.match(r'(?i)^(?:Login|Username) *: *(?P<content>.*)$', line) |
| 99 | + m = not csvrow.login_username and re.match( |
| 100 | + r"(?i)^(?:Login|Username) *: *(?P<content>.*)$", line |
| 101 | + ) |
99 | 102 | if m: |
100 | | - content = m.group('content').strip() |
| 103 | + content = m.group("content").strip() |
101 | 104 | if content: |
102 | 105 | csvrow.login_username = content |
103 | 106 | continue |
104 | 107 | csvrow.notes.append(line) |
105 | 108 | csvrow.name = relfile.stem |
106 | 109 | csvrow.folder = os.fspath(relfile.parent) |
107 | | - csvrow.notes = '\n'.join(csvrow.notes) |
| 110 | + csvrow.notes = "\n".join(csvrow.notes) |
108 | 111 | csvwriter.writerow(csvrow.__dict__) |
109 | 112 | csvrow = None |
110 | 113 | else: |
111 | 114 | raise NotImplementedError(os.fspath(passfile)) |
112 | 115 |
|
113 | 116 |
|
114 | | -if __name__ == '__main__': |
| 117 | +if __name__ == "__main__": |
115 | 118 | main() |
0 commit comments