validata_util.py 979 Bytes
Newer Older
1 2 3 4 5 6 7
""" Call validation code """

import logging
from io import BytesIO

from validata_core.source_helpers import build_tabulator_params

8

9 10 11 12 13 14
log = logging.getLogger(__name__)


class ValidataSource():
    """ Handy class to handle different sort of data source """

Pierre Dittgen's avatar
wip  
Pierre Dittgen committed
15
    def __init__(self, type_, name, source):
16
        """ Initialization """
Pierre Dittgen's avatar
wip  
Pierre Dittgen committed
17
        self.type = type_
18 19 20 21 22 23 24 25
        self.name = name
        self.source = source

        info = build_tabulator_params(type, name, source)
        self.source = info.get('source')
        self.format = info.get('format')
        self.scheme = info.get('scheme')

Pierre Dittgen's avatar
wip  
Pierre Dittgen committed
26 27
    def is_url(self):
        return self.type == 'url'
28

Pierre Dittgen's avatar
wip  
Pierre Dittgen committed
29 30 31 32 33 34 35 36
    def get_url(self):
        return self.source

    def get_filename(self):
        return self.name

    def get_filecontent(self):
        return self.source
37 38 39 40 41 42 43 44


def bytes_data(f):
    """Gets bytes data from Werkzeug FileStorage instance"""
    iob = BytesIO()
    f.save(iob)
    iob.seek(0)
    return iob.getvalue()