Permalink
Browse files

Handle edge cases in csvpy.

  • Loading branch information...
1 parent baf76e8 commit bd5627ee438b4ad4453efaeed01508b8d8514868 @onyxfish onyxfish committed Aug 14, 2012
View
@@ -55,7 +55,7 @@ class CSVFileType(object):
An argument factory like argparse.FileType with compression support.
"""
- def __init__(self, mode = "rb"):
+ def __init__(self, mode='rb'):
"""
Initialize the factory.
"""
@@ -65,22 +65,20 @@ def __call__(self, path):
"""
Build a file-like object from the specified path.
"""
- if path == "-":
- if "r" in self._mode:
+ if path == '-':
+ if 'r' in self._mode:
return sys.stdin
- elif "w" in self._mode:
+ elif 'w' in self._mode:
return sys.stdout
else:
- raise ValueError("invalid path \"-\" with mode {0}".format(self._mode))
+ raise ValueError('Invalid path "-" with mode {0}'.format(self._mode))
else:
(_, extension) = os.path.splitext(path)
- if extension == ".gz":
- #return gzip.open(path, self._mode)
+ if extension == '.gz':
return LazyFile(gzip.open, path, self._mode)
- if extension == ".bz2":
+ if extension == '.bz2':
return LazyFile(bz2.BZ2File, path, self._mode)
- #return bz2.BZ2File(path, self._mode)
else:
return LazyFile(open, path, self._mode)
@@ -66,3 +66,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -66,3 +66,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -107,3 +107,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -48,3 +48,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -50,3 +50,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -1,11 +1,11 @@
#!/usr/bin/env python
-import os
+import sys
from csvkit import CSVKitReader
from csvkit.cli import CSVKitUtility
-class CSVLook(CSVKitUtility):
+class CSVPy(CSVKitUtility):
description = 'Load a CSV file into a CSVKitReader object and then drops into a Python shell.'
override_flags = 'l'
@@ -15,6 +15,12 @@ def add_arguments(self):
pass
def main(self):
+ if self.args.file == sys.stdin:
+ raise NotImplementedError('csvpy does not currently support input via pipes. Sorry!')
+ else:
+ # Attempt reading filename, will cause lazy loader to access file and raise error if it does not exist
+ filename = self.args.file.name
+
reader = CSVKitReader(self.args.file, **self.reader_kwargs)
try:
@@ -26,7 +32,7 @@ def main(self):
code.interact(self.welcome_message, local={ 'reader': reader })
def launch_new_instance():
- utility = CSVLook()
+ utility = CSVPy()
utility.main()
if __name__ == "__main__":
@@ -49,3 +49,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -93,3 +93,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -59,3 +59,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -220,3 +220,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+
@@ -73,3 +73,4 @@ def launch_new_instance():
if __name__ == "__main__":
launch_new_instance()
+

0 comments on commit bd5627e

Please sign in to comment.