|
| 1 | +from hashlib import md5 |
| 2 | + |
| 3 | +from dataflows import Flow, PackageWrapper |
| 4 | +from dataflows import load, concatenate, join, set_type, checkpoint,\ |
| 5 | + dump_to_path |
| 6 | + |
| 7 | +from .config import Config |
| 8 | +from .context import Context |
| 9 | +from ..genera.consts import CONFIG_MODEL_MAPPING, RESOURCE_NAME |
| 10 | + |
| 11 | + |
| 12 | +class BaseEnricher: |
| 13 | + |
| 14 | + def __init__(self, config: Config): |
| 15 | + self.config = config |
| 16 | + self.prepare() |
| 17 | + |
| 18 | + def prepare(self): |
| 19 | + pass |
| 20 | + |
| 21 | + def test(self): |
| 22 | + return False |
| 23 | + |
| 24 | + def preflow(self): |
| 25 | + return None |
| 26 | + |
| 27 | + def postflow(self): |
| 28 | + return None |
| 29 | + |
| 30 | + |
| 31 | +class ColumnTypeTester(BaseEnricher): |
| 32 | + |
| 33 | + # REQUIRED_COLUMN_TYPES = [] |
| 34 | + # PROHIBITED_COLUMN_TYPES = [] |
| 35 | + |
| 36 | + def test(self): |
| 37 | + all_cts = [ |
| 38 | + x['columnType'] |
| 39 | + for x in self.config.get(CONFIG_MODEL_MAPPING) |
| 40 | + if 'columnType' in x |
| 41 | + ] |
| 42 | + if not all(x in all_cts for x in self.REQUIRED_COLUMN_TYPES): |
| 43 | + return False |
| 44 | + if any(x in all_cts for x in self.PROHIBITED_COLUMN_TYPES): |
| 45 | + return False |
| 46 | + return True |
| 47 | + |
| 48 | + |
| 49 | +def rename_last_resource(name): |
| 50 | + |
| 51 | + def func(package: PackageWrapper): |
| 52 | + package.pkg.descriptor['resources'][-1]['name'] = name |
| 53 | + num_resources = len(package.pkg.descriptor['resources']) |
| 54 | + |
| 55 | + yield package.pkg |
| 56 | + |
| 57 | + for i, res in enumerate(iter(package)): |
| 58 | + if i == (num_resources - 1): |
| 59 | + yield res.it |
| 60 | + else: |
| 61 | + yield res |
| 62 | + |
| 63 | + return func |
| 64 | + |
| 65 | + |
| 66 | +class DatapackageJoiner(BaseEnricher): |
| 67 | + |
| 68 | + # REF_DATAPACKAGE = '' |
| 69 | + # REF_KEY_FIELDS = [''] |
| 70 | + # REF_FETCH_FIELDS = [''] |
| 71 | + # SOURCE_KEY_FIELDS = [''] |
| 72 | + # TARGET_FIELD_COLUMNTYPES = [''] |
| 73 | + |
| 74 | + def prepare(self): |
| 75 | + self.ref_hash = md5(self.REF_DATAPACKAGE.encode('utf8')).hexdigest() |
| 76 | + self.key = self.__class__.__name__ |
| 77 | + |
| 78 | + Flow(load(self.REF_DATAPACKAGE), |
| 79 | + rename_last_resource(self.ref_hash), |
| 80 | + dump_to_path('.enrichments/{}'.format(self.ref_hash)), |
| 81 | + checkpoint(self.ref_hash)).process() |
| 82 | + print('DONE PREPARING', self.key) |
| 83 | + |
| 84 | + def preflow(self): |
| 85 | + f = Flow( |
| 86 | + load('.enrichments/{}/datapackage.json'.format(self.ref_hash)), |
| 87 | + concatenate( |
| 88 | + fields=dict( |
| 89 | + (f, []) |
| 90 | + for f in self.REF_KEY_FIELDS + self.REF_FETCH_FIELDS |
| 91 | + ), |
| 92 | + target=dict( |
| 93 | + name=self.key, |
| 94 | + path=self.key+'.csv' |
| 95 | + ), |
| 96 | + resources=self.ref_hash |
| 97 | + ), |
| 98 | + ) |
| 99 | + return f |
| 100 | + |
| 101 | + def postflow(self): |
| 102 | + target_field_names = [ct.replace(':', '-') for ct in self.TARGET_FIELD_COLUMNTYPES] |
| 103 | + steps = [ |
| 104 | + join( |
| 105 | + self.key, self.REF_KEY_FIELDS, |
| 106 | + RESOURCE_NAME, self.SOURCE_KEY_FIELDS, |
| 107 | + dict( |
| 108 | + ( |
| 109 | + target_field_name, |
| 110 | + dict(name=fetch_field) |
| 111 | + ) |
| 112 | + for target_field_name, fetch_field |
| 113 | + in zip(target_field_names, self.REF_FETCH_FIELDS) |
| 114 | + ) |
| 115 | + ), |
| 116 | + ] |
| 117 | + steps.extend([ |
| 118 | + set_type(target_field_name, |
| 119 | + resources=RESOURCE_NAME, |
| 120 | + columnType=target_field_columntype) |
| 121 | + for target_field_name, target_field_columntype |
| 122 | + in zip(target_field_names, self.TARGET_FIELD_COLUMNTYPES) |
| 123 | + ]) |
| 124 | + f = Flow(*steps) |
| 125 | + return f |
| 126 | + |
| 127 | + |
| 128 | +def enrichments_flow(config: Config, context: Context, *classes): |
| 129 | + active_enrichments = [e(config) for e in classes] |
| 130 | + active_enrichments = [e for e in active_enrichments if e.test()] |
| 131 | + |
| 132 | + steps = [] |
| 133 | + |
| 134 | + for e in active_enrichments: |
| 135 | + f = e.preflow() |
| 136 | + if f: |
| 137 | + steps.append(f) |
| 138 | + |
| 139 | + steps.extend([ |
| 140 | + load(context.enricher_dir), |
| 141 | + ]) |
| 142 | + |
| 143 | + for e in active_enrichments: |
| 144 | + f = e.postflow() |
| 145 | + if f: |
| 146 | + steps.append(f) |
| 147 | + |
| 148 | + f = Flow( |
| 149 | + *steps |
| 150 | + ) |
| 151 | + return f |
0 commit comments