Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

clean up export/merge scripts: use absolute paths and os.path.join

  • Loading branch information...
commit 0b75be025f3f14103f5ad6935b16772188f1c2d7 1 parent 67e3187
straup authored
Showing with 35 additions and 18 deletions.
  1. +14 −9 bin/add_media.py
  2. +18 −8 bin/export_objects.py
  3. +3 −1 bin/utils.py
23 bin/add_media.py
View
@@ -1,12 +1,10 @@
#!/usr/bin/env python
-# THIS IS NOT FINISHED. IT WORKS BUT IS UGLY.
-# (20120225/straup)
-
"""
Append the URLs for the files in media.csv to individual object JSON files.
"""
+import sys
import csv
import json
import utils
@@ -14,18 +12,25 @@
if __name__ == '__main__' :
+ whoami = os.path.abspath(sys.argv[0])
+ bindir = os.path.dirname(whoami)
+ collection = os.path.dirname(bindir)
- path = '../media.csv'
- fh = open(path, 'r')
+ objects = os.path.join(collection, 'objects')
- reader = csv.DictReader(fh)
+ media_csv = os.path.join(collection, 'media.csv')
+ media_fh = open(media_csv, 'r')
- for row in reader:
+ reader = csv.DictReader(media_fh)
- root = "../objects/" + utils.id2path(row['id']) + "/"
+ for row in reader:
+
fname = "%s.json" % row['id']
- obj_path = root + fname
+ root = utils.id2path(row['id'])
+ root = os.path.join(objects, root)
+
+ obj_path = os.path.join(root, fname)
if not os.path.exists(obj_path):
continue
26 bin/export_objects.py
View
@@ -1,12 +1,13 @@
#!/usr/bin/env python
-# THIS IS NOT FINISHED. IT WORKS BUT IS UGLY.
+# I think there might still be some weirdness in the UTF-8 wrangling
# (20120225/straup)
"""
Export the contents of objects.csv in to individual JSON files.
"""
+import sys
import json
import csv
import utils
@@ -43,17 +44,27 @@ def __init__(self, f, encoding="utf-8", fieldnames=None, **kwds):
if __name__ == '__main__':
- path = '../objects.csv'
- fh = open(path, 'r')
+ whoami = os.path.abspath(sys.argv[0])
+ bindir = os.path.dirname(whoami)
+ collection = os.path.dirname(bindir)
- r = UnicodeDictReader(fh)
+ objects = os.path.join(collection, 'objects')
- for row in r:
+ obj_csv = os.path.join(collection, 'objects.csv')
+ obj_fh = open(obj_csv, 'r')
+
+ reader = UnicodeDictReader(obj_fh)
+
+ for row in reader:
- root = "../objects/" + utils.id2path(row['id']) + "/"
fname = "%s.json" % row['id']
- out = root + fname
+ root = utils.id2path(row['id'])
+ root = os.path.join(objects, root)
+
+ out = os.path.join(root, fname)
+ print out
+ continue
if not os.path.exists(root):
os.makedirs(root)
@@ -61,5 +72,4 @@ def __init__(self, f, encoding="utf-8", fieldnames=None, **kwds):
out_fh = open(out, 'w')
json.dump(row, out_fh, indent=2)
-
print out
4 bin/utils.py
View
@@ -1,3 +1,5 @@
+import os.path
+
def id2path(id):
tmp = str(id)
@@ -10,4 +12,4 @@ def id2path(id):
if len(tmp):
parts.append(tmp)
- return "/".join(parts)
+ return os.path.join(*parts)
Please sign in to comment.
Something went wrong with that request. Please try again.