Skip to content

Instantly share code, notes, and snippets.

@cdent
Forked from FND/.gitignore
Created March 24, 2010 11:16
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 cdent/342192 to your computer and use it in GitHub Desktop.
Save cdent/342192 to your computer and use it in GitHub Desktop.
import csv
def dictify(filename):
"""
turn data from CSV file into a list of dictionaries
"""
f = open(filename, "rU")
items = []
for row in unicode_csv_reader(f):
try:
item = dict([(k, row[i]) for i, k in enumerate(keys)])
item["_source"] = filename # XXX: not a generic requirement
items.append(item)
except NameError: # first row
keys = row
f.close()
return items
# Unicode fixes as per documentation
def unicode_csv_reader(data, dialect=csv.excel, **kwargs):
"""
expects Unicode input, temporarily encoding it as UTF-8
"""
reader = csv.reader(utf8_encoder(data), dialect=dialect, **kwargs)
for row in reader:
yield [unicode(cell, "utf-8", 'replace') for cell in row]
def utf8_encoder(data):
for line in data:
yield line
#!/usr/bin/env python
"""
Usage:
$ ./csv2tiddlers.py <filename> <titlefields> <textfields> <fieldmap> <tagsmap>
Example:
$ ./csv2tiddlers.py sample.csv "colA;colC" "colD;colE" "colB=foo;colF=bar"
"""
import sys
from csv2dict import dictify
from tiddler import put
HOST = "http://localhost:8080"
BAG = "common"
def main(args):
"""
arguments are filename, titlefields, textfields and fieldmap
fields are a list of semicolon-separated column names whose values will be
concatenated for the respective tiddler attribute
fieldmap provides a way to translate column names to tiddler fields
tagsmap provides a way to translate column values into tags
"""
args = [unicode(arg, "utf-8", 'replace') for arg in args]
try:
filename = args[1]
titlefields = args[2].split(";")
textfields = args[3].split(";")
except IndexError, exc:
print "ERROR: missing argument", exc
return False
try:
fieldmap = dict([i.split("=") for i in args[4].split(";")])
except IndexError, exc:
fieldmap = {}
try:
tagsmap = args[5].split(";")
except IndexError, exc:
tagsmap = []
items = dictify(filename)
put_tiddlers(items, titlefields, textfields, fieldmap, tagsmap)
return True
def put_tiddlers(items, titlefields, textfields, fieldmap, tagsmap):
for item in items:
values = []
for field in titlefields:
values.append(item[field])
title = "_".join(values)
try:
values = []
for field in textfields: # TODO: DRY
values.append(item[field])
text = "\n\n".join(values)
except KeyError:
text = None # XXX: invalid?
for column, field in fieldmap.items(): # XXX: inefficient!?
try: # XXX: temporary workaround
item[field] = item[column]
del item[column]
except KeyError:
pass
try:
tagsCol, tagsDelim = tagsmap
tags = item[tagsCol].split(tagsDelim)
except (ValueError, KeyError), exc:
tags = []
tiddler = {
"text": text,
"tags": tags,
"fields": item
}
message = "sending tiddler: %s ..." % title.encode('utf-8')
sys.stdout.write(message)
status = put(HOST, BAG, title, tiddler)
print "\t[%s]" % status
if __name__ == "__main__":
status = not main(sys.argv)
sys.exit(status)
"""
PUT a tiddler.
"""
import json
import urllib2
import httplib2
def put(host, bag, title, tiddler):
"""
send a dictionary representing a tiddler to a TiddlyWeb server
"""
http = httplib2.Http()
bag = urllib2.quote(bag.encode('utf-8'))
title = urllib2.quote(title.encode('utf-8'))
uri = "%s/bags/%s/tiddlers/%s" % (host, bag, title)
response, content = http.request(
uri,
method="PUT",
headers={ "Content-Type": "application/json" },
body=json.dumps(tiddler)
)
return response["status"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment