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""" 

2 pyexcel_io.manager 

3 ~~~~~~~~~~~~~~~~~~~ 

4 

5 Control file streams 

6 

7 :copyright: (c) 2014-2020 by Onni Software Ltd. 

8 :license: New BSD License, see LICENSE for more details 

9""" 

10from pyexcel_io._compact import BytesIO, StringIO 

11 

12MIME_TYPES = {} 

13FILE_TYPES = () 

14TEXT_STREAM_TYPES = [] 

15BINARY_STREAM_TYPES = [] 

16 

17 

18def register_stream_type(file_type, stream_type): 

19 """ 

20 keep track of stream type for different file formats 

21 """ 

22 if stream_type == "text": 

23 TEXT_STREAM_TYPES.append(file_type) 

24 elif stream_type == "binary": 

25 BINARY_STREAM_TYPES.append(file_type) 

26 

27 

28def get_io(file_type): 

29 """A utility function to help you generate a correct io stream 

30 

31 :param file_type: a supported file type 

32 :returns: a appropriate io stream, None otherwise 

33 """ 

34 __file_type = None 

35 if file_type: 

36 __file_type = file_type.lower() 

37 

38 if __file_type in TEXT_STREAM_TYPES: 

39 return StringIO() 

40 

41 elif __file_type in BINARY_STREAM_TYPES: 

42 return BytesIO() 

43 

44 else: 

45 return None 

46 

47 

48def get_io_type(file_type): 

49 """A utility function to help you generate a correct io stream 

50 

51 :param file_type: a supported file type 

52 :returns: a appropriate io stream, None otherwise 

53 """ 

54 __file_type = None 

55 if file_type: 

56 __file_type = file_type.lower() 

57 

58 if __file_type in TEXT_STREAM_TYPES: 

59 return "string" 

60 

61 elif __file_type in BINARY_STREAM_TYPES: 

62 return "bytes" 

63 

64 else: 

65 return None 

66 

67 

68def register_a_file_type(file_type, stream_type, mime_type): 

69 """ 

70 keep track of file format supports by this library 

71 """ 

72 global FILE_TYPES 

73 FILE_TYPES += (file_type,) 

74 stream_type = stream_type 

75 if mime_type is not None: 

76 MIME_TYPES[file_type] = mime_type 

77 register_stream_type(file_type, stream_type)