Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from ..registry import check
from ..error import Error
# Module API
@check('extra-value')
class ExtraValue(object):
def __init__(self, **options):
self._num_columns = None
def check_row(self, cells):
errors = []
# Check that all rows have the same number of columns
if self._num_columns is None:
self._num_columns = len(cells)
elif len(cells) > self._num_columns:
extra_cells = cells[self._num_columns:]
for cell in extra_cells:
error = Error('extra-value', cell)
errors.append(error)
cells.remove(cell)
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from simpleeval import simple_eval
from ...registry import check
from ...error import Error
# Module API
@check('custom-constraint', type='custom', context='body')
class CustomConstraint(object):
# Public
def __init__(self, constraint, **options):
self.__constraint = constraint
def check_row(self, cells):
# Prepare names
names = {}
for cell in cells:
if None not in [cell.get('header'), cell.get('value')]:
try:
names[cell['header']] = float(cell['value'])
except ValueError:
pass
@check('duplicate-header')
def duplicate_header(cells, sample=None):
errors = []
rindex = {}
cell_by_column_number = {}
for cell in cells:
# Skip if not header
if 'header' not in cell:
continue
header_indexes = rindex.get(cell['header'], set())
header_indexes.add(cell['column-number'])
rindex[cell['header']] = header_indexes
cell_by_column_number[cell['column-number']] = cell
@check('missing-value')
def missing_value(cells):
"""
missing-value: A row has less columns than the header.
"""
errors = []
for cell in copy(cells):
# Skip if cell has value
# There is a difference between:
# - not having value at all - there is no `value` key
# - having a value which is falsy (None, False, '', etc)
# (so we don't use something like `if cell.get('value')`)
if 'value' in cell:
continue
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from copy import copy
from tableschema import Schema
from ..registry import check
from ..error import Error
# Module API
@check('extra-header')
class ExtraHeader(object):
# Public
def __init__(self, infer_fields=False, **options):
self.__infer_fields = infer_fields
def check_headers(self, cells, sample):
errors = []
for cell in copy(cells):
# Skip if cell has field
if 'field' in cell and cell['field'] is not None:
continue
# Infer field
@check('required-constraint')
def required_constraint(cells):
errors = []
for cell in copy(cells):
field = cell.get('field')
value = cell.get('value')
# Skip if cell has no field
if field is None:
continue
# Check constraint
valid = True
if field.required or field.descriptor.get('primaryKey'):
# TODO: remove this hack after:
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
from ...registry import check
from ...error import Error
# Module API
@check('blacklisted-value', type='custom', context='body')
class BlacklistedValue(object):
# Public
def __init__(self, column, blacklist, **options):
self.__column = column
self.__blacklist = blacklist
def check_row(self, cells):
# Get cell
cell = None
for item in cells:
if self.__column in [item['column-number'], item['header']]:
cell = item
break
@check('minimum-length-constraint')
def minimum_length_constraint(cells):
check_constraint = create_check_constraint('minimum-length-constraint', 'minLength')
return check_constraint(cells)
@check('type-or-format-error')
def type_or_format_error(cells):
errors = []
for cell in copy(cells):
field = cell.get('field')
value = cell.get('value')
# Skip if cell has no field
if field is None:
continue
# Cast value
try:
valid = True
cell['value'] = field.cast_value(value, constraints=False)
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import
from __future__ import unicode_literals
import statistics
from ...registry import check
from ...error import Error
from ... import exceptions
# Module API
@check('deviated-value', type='custom', context='body')
class DeviatedValue(object):
# Public
def __init__(self, column, average='mean', interval=3, **options):
# Set attributes
self.__column = column
self.__interval = interval
self.__column_cells = []
self.__average_function = _AVERAGE_FUNCTIONS.get(average)
self.__code = 'deviated-value'
self.__cell = None
# Validate average
if not self.__average_function: