diff options
Diffstat (limited to 'tablib/formats')
| -rw-r--r-- | tablib/formats/__init__.py | 2 | ||||
| -rw-r--r-- | tablib/formats/_csv.py | 9 | ||||
| -rw-r--r-- | tablib/formats/_json.py | 9 | ||||
| -rw-r--r-- | tablib/formats/_yaml.py | 13 |
4 files changed, 31 insertions, 2 deletions
diff --git a/tablib/formats/__init__.py b/tablib/formats/__init__.py index b22a959..69eada7 100644 --- a/tablib/formats/__init__.py +++ b/tablib/formats/__init__.py @@ -8,4 +8,4 @@ import _json as json import _xls as xls import _yaml as yaml -FORMATS = (csv, json, xls, yaml) +FORMATS = (json, xls, yaml, csv) diff --git a/tablib/formats/_csv.py b/tablib/formats/_csv.py index 8b19da7..27d2e0d 100644 --- a/tablib/formats/_csv.py +++ b/tablib/formats/_csv.py @@ -40,3 +40,12 @@ def import_set(dset, in_stream, headers=True): dset.headers = row else: dset.append(row) + + +def detect(stream): + """Returns True if given stream is valid CSV.""" + try: + rows = dialect = csv.Sniffer().sniff(stream) + return True + except csv.Error: + return False
\ No newline at end of file diff --git a/tablib/formats/_json.py b/tablib/formats/_json.py index 1f92b58..f7c88ee 100644 --- a/tablib/formats/_json.py +++ b/tablib/formats/_json.py @@ -36,3 +36,12 @@ def import_book(dbook, in_stream): data.title = sheet['title'] data.dict = sheet['data'] dbook.add_sheet(data) + + +def detect(stream): + """Returns True if given stream is valid JSON.""" + try: + json.loads(stream) + return True + except json.decoder.JSONDecodeError: + return False
\ No newline at end of file diff --git a/tablib/formats/_yaml.py b/tablib/formats/_yaml.py index 4cac8aa..57d63d7 100644 --- a/tablib/formats/_yaml.py +++ b/tablib/formats/_yaml.py @@ -39,4 +39,15 @@ def import_book(dbook, in_stream): data = tablib.core.Dataset() data.title = sheet['title'] data.dict = sheet['data'] - dbook.add_sheet(data)
\ No newline at end of file + dbook.add_sheet(data) + +def detect(stream): + """Returns True if given stream is valid YAML.""" + try: + _yaml = yaml.load(stream) + if isinstance(_yaml, (list, tuple, dict)): + return True + else: + return False + except yaml.parser.ParserError: + return False
\ No newline at end of file |
