def publish(self, dataset, datadir, watch): dataset = dataset.lower() provider_info = self.mkt_contract.functions.getDataProviderInfo( Web3.toHex(dataset.encode()) ).call() if not provider_info[4]: raise MarketplaceDatasetNotFound(dataset=dataset) match = next( (l for l in self.addresses if l['pubAddr'] == provider_info[0]), None ) if not match: raise MarketplaceNoAddressMatch( dataset=dataset, address=provider_info[0]) print('Using address: {} to publish this dataset.'.format( provider_info[0])) if 'key' in match: key = match['key'] secret = match['secret'] else: key, secret = get_key_secret(provider_info[0], match['wallet']) filenames = glob.glob(os.path.join(datadir, '*.csv')) if not filenames: raise MarketplaceNoCSVFiles(datadir=datadir) def read_file(pathname): with open(pathname, 'rb') as f: return f.read() files = [] for idx, file in enumerate(filenames): log.info('Uploading file {} of {}: {}'.format( idx + 1, len(filenames), file)) files.append(('file', (os.path.basename(file), read_file(file)))) headers = get_signed_headers(dataset, key, secret) r = requests.post('{}/marketplace/publish'.format(AUTH_SERVER), files=files, headers=headers) if r.status_code != 200: raise MarketplaceHTTPRequest(request='upload file', error=r.status_code) if 'error' in r.json(): raise MarketplaceHTTPRequest(request='upload file', error=r.json()['error']) log.info('File processed successfully.') print('\nDataset {} uploaded and processed successfully.'.format( dataset))
def publish(self, dataset, datadir, watch): dataset = dataset.lower() provider_info = self.mkt_contract.functions.getDataProviderInfo( Web3.toHex(dataset) ).call() if not provider_info[4]: raise MarketplaceDatasetNotFound(dataset=dataset) match = next( (l for l in self.addresses if l['pubAddr'] == provider_info[0]), None ) if not match: raise MarketplaceNoAddressMatch( dataset=dataset, address=provider_info[0]) print('Using address: {} to publish this dataset.'.format( provider_info[0])) if 'key' in match: key = match['key'] secret = match['secret'] else: key, secret = get_key_secret(provider_info[0]) headers = get_signed_headers(dataset, key, secret) filenames = glob.glob(os.path.join(datadir, '*.csv')) if not filenames: raise MarketplaceNoCSVFiles(datadir=datadir) files = [] for file in filenames: files.append(('file', open(file, 'rb'))) r = requests.post('{}/marketplace/publish'.format(AUTH_SERVER), files=files, headers=headers) if r.status_code != 200: raise MarketplaceHTTPRequest(request='upload file', error=r.status_code) if 'error' in r.json(): raise MarketplaceHTTPRequest(request='upload file', error=r.json()['error']) print('Dataset {} uploaded successfully.'.format(dataset))