1""" 2 pyexcel.source 3 ~~~~~~~~~~~~~~~~~~~~~~~~ 4 5 Generic data source definition 6 7 :copyright: (c) 2015-2020 by Onni Software Ltd. 8 :license: New BSD License 9""" 10from pyexcel import constants as constants 11 12 13class AbstractSource(object): 14 """ 15 Define a data source for use with the signature functions 16 17 This can be used to extend the function parameters once the custom 18 class inherit this and register it with corresponding source registry 19 """ 20 21 fields = [constants.SOURCE] 22 attributes = [] 23 targets = [] 24 actions = [] 25 key = constants.SOURCE 26 27 def __init__(self, **keywords): 28 self._keywords = keywords 29 30 def get_source_info(self): 31 """return filename and path, otherwise not useful 32 33 see also `:meth:pyexcel.internal.core.get_book_stream` 34 """ 35 return (None, None) 36 37 @classmethod 38 def is_my_business(cls, action, **keywords): 39 """ 40 If all required keys are present, this source is activated 41 """ 42 statuses = [_has_field(field, keywords) for field in cls.fields] 43 results = [status for status in statuses if status is False] 44 return len(results) == 0 45 46 def write_data(self, content): 47 """Write data to a data source""" 48 raise NotImplementedError("") 49 50 def get_data(self): 51 """Get data from a data source""" 52 raise NotImplementedError("") 53 54 55class MemorySourceMixin(object): 56 """A memory source should an internal memory stream 57 58 And it is desirable to get its internal stream 59 """ 60 61 def get_content(self): 62 """Get memory repsentation of the formatted data 63 64 e.g. StringIO instance which contains the csv formatted data 65 """ 66 return self._content 67 68 69def _has_field(field, keywords): 70 return field in keywords and keywords[field] is not None 71