Hide keyboard shortcuts

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 

4 

5 

6class Source(object): 

7 def __init__(self, url_or_file, type_): 

8 self.source = url_or_file 

9 self.type = type_ 

10 

11 if self.type is 'file': 

12 self.checkFiles() 

13 

14 def isUrl(self): 

15 return 'url' in self.type 

16 

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 

24 

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) 

33 

34 def isString(self): 

35 return 'string' in self.type 

36 

37 def isFileObj(self): 

38 return hasattr(self.source, 'read') 

39 

40 def to_s(self): 

41 return self.source