Skip to content

Commit

Permalink
Add is_pdf from scratch
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandrogallo committed Aug 21, 2018
1 parent fef5216 commit e09733d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions papis/filetype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import sys
import os
import re


def is_pdf(filepath):

if not os.path.exists(filepath):
return False

with open(filepath, 'rb') as fd:
magic = fd.read(8)
return re.match(r'%PDF-.\..', magic.decode()) is not None


13 changes: 13 additions & 0 deletions tests/filetype.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from papis.filetype import is_pdf
import tempfile
import os

def test_is_pdf():
tmp = tempfile.mktemp()

assert not os.path.exists(tmp)
assert not is_pdf(tmp)

with open(tmp, 'w+') as fd:
fd.write('%PDF-1.8\n')
assert is_pdf(tmp)

0 comments on commit e09733d

Please sign in to comment.