summaryrefslogtreecommitdiff
path: root/megapixels/commands/datasets/add_uuid.py
blob: 9c14c0e3eb619449b69b6ca8bb97dde1b0227cbb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import click

from app.settings import types
from app.utils import click_utils
from app.settings import app_cfg as cfg
from app.utils.logger_utils import Logger

log = Logger.getLogger()

@click.command()
@click.option('-i', '--input', 'opt_fp_in', required=True,
  help='Input directory')
@click.option('-o', '--output', 'opt_fp_out',
  help='Output directory')
@click.option('-f', '--force', 'opt_force', is_flag=True,
  help='Force overwrite file')
@click.pass_context
def cli(ctx, opt_fp_in, opt_fp_out, opt_force):
  """Appends UUID to records CSV"""
  
  from glob import glob
  from os.path import join
  from pathlib import Path
  import base64
  import uuid

  from tqdm import tqdm
  import pandas as pd
  
  if not opt_force and Path(opt_fp_out).exists():
    log.error('File exists. Use "-f / --force" to overwite')
    return

  # load names
  df_records = pd.read_csv(opt_fp_in)
  records = df_records.to_dict('index')
  # append a UUID to every entry
  for idx, item in records.items():
    records[idx]['uuid'] = uuid.uuid4()
  # save to csv
  df_uuid = pd.DataFrame.from_dict(list(records.values()))  # ignore the indices
  df_uuid.to_csv(opt_fp_out, index=False)

  log.info('done')