Skip to content

Instantly share code, notes, and snippets.

@pop
Created October 21, 2016 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pop/09b3b38cd4f911d135aa8707e5800e1a to your computer and use it in GitHub Desktop.
Save pop/09b3b38cd4f911d135aa8707e5800e1a to your computer and use it in GitHub Desktop.
This script has a very specific set of skills
"""
Title: make-it-fit.py
Description: Transforms a very specific csv into a very specific json format
Owner: Elijah C. Voigt <elijah.caine@coreos.com>
Usage: ``python make-it-fit.py > my-output-file.json``
CSV Format: ``Timestamp(optional),Name (First and Last),Start date at CoreOS,End date at CoreOS(optional),Github handle,Primary Coreos email,Other emails used for commits(optional)``
CSV Example Line: ``1/10/2016 12:00:00,Foo Bazington,1/10/2016,1/10/2017,bazington84,foo.bazington@coreos.com,foobazington@someschool.edu,foobazington@yahoo.net``
"""
import json
import datetime
from sys import argv
INPUT_FILE = ''
def blacklist():
return []
def organizations():
return {}
def identity(line, index):
"""
['Timestamp', 'Name (First and Last)', 'Start date at CoreOS',
'Github handle', 'Primary Coreos email', 'Other emails used for commits']
"""
global INPUT_FILE
line_arr = line.split(',')
emails = [x.strip().strip('"') for x in line_arr[4:]]
for email in emails:
if ' ' in email:
emails.remove(email)
emails += email.split(' ')
if '@' not in email:
emails.remove(email)
return {'enrollments': [
{
'end': None,
'organization': 'CoreOS', # TODO: extract this from email?
'start': datetime.datetime.strptime(line_arr[2], '%m/%d/%Y').isoformat().replace('T',' ') if line_arr[2] else None,
'uuid': str(index)
}
],
'identities': [
{
'email': email,
'id': str(index),
'name': line_arr[1],
'soruce': INPUT_FILE,
'username': line_arr[3],
'uuid': str(index)+str(i)
} for i, email in enumerate(emails, 0) ],
'profile': None,
'uuid': str(index)
}
def uidentities():
"""
returns:
{
#: {
enrollments: []
identities: [
{
"email": null,
"id": "0",
"name": null,
"source": "example_csv",
"username": "ncopernico",
"uuid": "0"
},
],
"profile": null,
"uuid": "0"
}
"""
global INPUT_FILE
with open(INPUT_FILE,'r') as f:
f.readline().strip().split(',')
return {int(index): identity(line, index) for index, line in enumerate(f, 0)}
if __name__ == '__main__':
try:
INPUT_FILE= argv[1]
obj = { 'blacklist': blacklist(),
'organizations': organizations(),
'soruce': INPUT_FILE,
'time': datetime.datetime.now().isoformat().replace('T',' '),
'uidentities': uidentities()
}
print(json.dumps(obj,
sort_keys=True,
indent=2,
separators=(',', ': ')))
except IndexError:
print("Usage:\n\tpython make-it-fit.py input_file.csv > output_file.json")
except IOError:
print("Couldn't find `{}`.\nDid you spell it right?".format(INPUT_FILE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment