Add argument to specify base path for grouping of passwords

This commit is contained in:
Rupus Reinefjord 2020-11-01 17:59:54 +01:00
parent 6606d31eac
commit 4418d476f7

View file

@ -93,19 +93,12 @@ class CSVExporter:
def parse(self, basepath, path, data): def parse(self, basepath, path, data):
p = pathlib.Path(path) p = pathlib.Path(path)
name = p.stem name = p.stem
group = os.path.dirname(os.path.os.path.relpath(path, basepath)) self.logger.info("Processing %s", name)
if not group: group = os.path.dirname(os.path.relpath(path, basepath))
try:
parts = list(p.parts[:-1])
i = parts.index('.password-store')
group = os.path.join(*parts[i+1:])
except ValueError:
pass
split_data = data.split('\n', maxsplit=1) split_data = data.split('\n', maxsplit=1)
password = split_data[0] password = split_data[0]
# Perform if/else in case there are no notes for a field # Perform if/else in case there are no notes for a field
notes = split_data[1] if len(split_data) > 1 else "" notes = split_data[1] if len(split_data) > 1 else ""
self.logger.info("Processing %s", name)
if self.kpx_format: if self.kpx_format:
# We are using the advanced format; try extracting user and url # We are using the advanced format; try extracting user and url
user, url, notes = self.get_metadata(notes) user, url, notes = self.get_metadata(notes)
@ -115,7 +108,7 @@ class CSVExporter:
return [group, name, password, notes] return [group, name, password, notes]
def main(gpgbinary, use_agent, pass_path, def main(gpgbinary, use_agent, pass_path, base_path,
kpx_format, login_fields, get_url, exclude_rows, outfile): kpx_format, login_fields, get_url, exclude_rows, outfile):
exporter = CSVExporter(kpx_format, login_fields, get_url, exclude_rows) exporter = CSVExporter(kpx_format, login_fields, get_url, exclude_rows)
gpg = gnupg.GPG(use_agent=use_agent, gpgbinary=gpgbinary) gpg = gnupg.GPG(use_agent=use_agent, gpgbinary=gpgbinary)
@ -127,7 +120,9 @@ def main(gpgbinary, use_agent, pass_path,
data = str(gpg.decrypt_file(f)) data = str(gpg.decrypt_file(f))
if len(data) == 0: if len(data) == 0:
logger.warning("Could not decrypt %s or it is empty.", file_path) logger.warning("Could not decrypt %s or it is empty.", file_path)
csv_data.append(exporter.parse(pass_path, file_path, data)) base = base_path if base_path else pass_path
parsed = exporter.parse(base, file_path, data)
csv_data.append(parsed)
writer = csv.writer(outfile, delimiter=',') writer = csv.writer(outfile, delimiter=',')
writer.writerows(csv_data) writer.writerows(csv_data)
@ -153,6 +148,13 @@ class OptionsParser(ArgumentParser):
help="path to the password-store folder to export", help="path to the password-store folder to export",
) )
self.add_argument(
'-b', '--base',
type=str,
help="path to use as base for grouping passwords",
dest='base_path'
)
self.add_argument( self.add_argument(
'-a', '--agent', '-a', '--agent',
action='store_true', action='store_true',
@ -161,7 +163,7 @@ class OptionsParser(ArgumentParser):
) )
self.add_argument( self.add_argument(
'-b', '--gpgbinary', '-g', '--gpgbinary',
type=str, type=str,
help="path to the gpg binary you wish to use", help="path to the gpg binary you wish to use",
dest='gpgbinary', dest='gpgbinary',