Permalink
Browse files

Refactored csvjson into library. Wrote tests. Toward #96. Also squash…

…ed output bug.
  • Loading branch information...
1 parent 83db467 commit 733ed4d11ea29c582106ca7f794538cbcfe32eb2 @onyxfish onyxfish committed Aug 19, 2011
View
46 csvjson
@@ -1,47 +1,7 @@
#!/usr/bin/env python
-import json
-import sys
-import codecs
+from csvkit.utilities.csvjson import CSVJSON
-from csvkit import CSVKitReader
-from csvkit.cli import CSVKitUtility
-
-class CSVJSON(CSVKitUtility):
- description = 'Convert a CSV file into JSON.'
-
- def add_arguments(self):
- self.argparser.add_argument('-i', '--indent', dest='indent', type=int, default=None,
- help='Indent the output JSON this many spaces. Disabled by default.')
- self.argparser.add_argument('-k', '--key', dest='key', type=str, default=None,
- help='Output JSON as an array of objects keyed by a given column, KEY, rather than as a list. All values in the column must be unique.')
-
- def main(self):
- """
- Convert CSV to JSON.
- """
- rows = CSVKitReader(self.args.file, **self.reader_kwargs)
- column_names = rows.next()
-
- stream = codecs.getwriter('utf-8')(sys.stdout)
-
- if self.args.key:
- output = {}
-
- for row in rows:
- row_dict = dict(zip(column_names, row))
- k = row_dict[self.args.key]
-
- if k in output:
- sys.exit('Value %s is not unique in the key column.' % k)
-
- output[k] = row_dict
- else:
- output = [dict(zip(column_names, row)) for row in rows]
-
- json.dump(output, stream, ensure_ascii=False, indent=self.args.indent, encoding='utf-8')
-
-if __name__ == "__main__":
- utility = CSVJSON()
- utility.main()
+if __name__ == '__main__':
+ CSVJSON().main()
@@ -0,0 +1,46 @@
+#!/usr/bin/env python
+
+import json
+import sys
+import codecs
+
+from csvkit import CSVKitReader
+from csvkit.cli import CSVKitUtility
+
+class CSVJSON(CSVKitUtility):
+ description = 'Convert a CSV file into JSON.'
+
+ def add_arguments(self):
+ self.argparser.add_argument('-i', '--indent', dest='indent', type=int, default=None,
+ help='Indent the output JSON this many spaces. Disabled by default.')
+ self.argparser.add_argument('-k', '--key', dest='key', type=str, default=None,
+ help='Output JSON as an array of objects keyed by a given column, KEY, rather than as a list. All values in the column must be unique.')
+
+ def main(self):
+ """
+ Convert CSV to JSON.
+ """
+ rows = CSVKitReader(self.args.file, **self.reader_kwargs)
+ column_names = rows.next()
+
+ stream = codecs.getwriter('utf-8')(self.output_file)
+
+ if self.args.key:
+ output = {}
+
+ for row in rows:
+ row_dict = dict(zip(column_names, row))
+ k = row_dict[self.args.key]
+
+ if k in output:
+ sys.exit('Value %s is not unique in the key column.' % k)
+
+ output[k] = row_dict
+ else:
+ output = [dict(zip(column_names, row)) for row in rows]
+
+ json.dump(output, stream, ensure_ascii=False, indent=self.args.indent, encoding='utf-8')
+
+if __name__ == "__main__":
+ CSVJSON().main()
+
File renamed without changes.
View
@@ -1,6 +1,6 @@
#!/usr/bin/env python
-from csvkit.utilities.stack import CSVStack
+from csvkit.utilities.csvstack import CSVStack
if __name__ == '__main__':
CSVStack().main()
@@ -0,0 +1,35 @@
+#!/usr/bin/env python
+
+import StringIO
+import unittest
+
+from csvkit.utilities.csvjson import CSVJSON
+
+class TestCSVStack(unittest.TestCase):
+ def test_simple(self):
+ args = ['examples/dummy.csv']
+ output_file = StringIO.StringIO()
+
+ utility = CSVJSON(args, output_file)
+ utility.main()
+
+ self.assertEqual(output_file.getvalue(), '[{"a": "1", "c": "3", "b": "2"}]')
+
+ def test_indentation(self):
+ args = ['-i', '4', 'examples/dummy.csv']
+ output_file = StringIO.StringIO()
+
+ utility = CSVJSON(args, output_file)
+ utility.main()
+
+ self.assertEqual(output_file.getvalue(), '[\n {\n "a": "1", \n "c": "3", \n "b": "2"\n }\n]')
+
+ def test_keying(self):
+ args = ['-k', 'a', 'examples/dummy.csv']
+ output_file = StringIO.StringIO()
+
+ utility = CSVJSON(args, output_file)
+ utility.main()
+
+ self.assertEqual(output_file.getvalue(), '{"1": {"a": "1", "c": "3", "b": "2"}}')
+
@@ -4,7 +4,7 @@
import unittest
from csvkit import CSVKitReader
-from csvkit.utilities.stack import CSVStack
+from csvkit.utilities.csvstack import CSVStack
class TestCSVStack(unittest.TestCase):
def test_explicit_grouping(self):

0 comments on commit 733ed4d

Please sign in to comment.