Skip to content

Commit

Permalink
[archive,sqlite-] guess sqlite/tar/zip filetypes confidently
Browse files Browse the repository at this point in the history
And never guess that empty files are tarfiles.
  • Loading branch information
midichef authored and anjakefala committed Dec 1, 2024
1 parent c137dcf commit a8e754a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions visidata/loaders/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
from visidata.loaders import unzip_http

from visidata import vd, VisiData, asyncthread, Sheet, Progress, Menu, options
from visidata import ColumnAttr, Column, Path
from visidata import ColumnAttr, Column, Path, filesize
from visidata.type_date import date

@VisiData.api
def guess_zip(vd, p):
if not p.is_url() and zipfile.is_zipfile(p.open_bytes()):
return dict(filetype='zip')
return dict(filetype='zip', _likelihood=10)

@VisiData.api
def guess_tar(vd, p):
# an empty file will pass is_tarfile(), but can't be opened by tarfile.open()
if filesize(p) == 0:
return None
if tarfile.is_tarfile(p.open_bytes()):
return dict(filetype='tar')
return dict(filetype='tar', _likelihood=10)

@VisiData.api
def open_zip(vd, p):
Expand Down
2 changes: 1 addition & 1 deletion visidata/loaders/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def requery(url, **kwargs):
@VisiData.api
def guess_sqlite(vd, p):
if p.open_bytes().read(16).startswith(b'SQLite format'):
return dict(filetype='sqlite')
return dict(filetype='sqlite', _likelihood=10)


@VisiData.api
Expand Down

0 comments on commit a8e754a

Please sign in to comment.