Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pdfkit/source.py : 37%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2import os
3import io
6class Source(object):
7 def __init__(self, url_or_file, type_):
8 self.source = url_or_file
9 self.type = type_
11 if self.type is 'file':
12 self.checkFiles()
14 def isUrl(self):
15 return 'url' in self.type
17 def isFile(self, path=None):
18 # dirty hack to check where file is opened with codecs module
19 # (because it returns 'instance' type when encoding is specified
20 if path:
21 return isinstance(path, io.IOBase) or path.__class__.__name__ == 'StreamReaderWriter'
22 else:
23 return 'file' in self.type
25 def checkFiles(self):
26 if isinstance(self.source, list):
27 for path in self.source:
28 if not os.path.exists(path):
29 raise IOError('No such file: %s' % path)
30 else:
31 if not hasattr(self.source, 'read') and not os.path.exists(self.source):
32 raise IOError('No such file: %s' % self.source)
34 def isString(self):
35 return 'string' in self.type
37 def isFileObj(self):
38 return hasattr(self.source, 'read')
40 def to_s(self):
41 return self.source