(filepath, check, autofix)
return success, lines
Validate imports are coming from the correct base package.
Validate imports are coming from the correct base package.
Validate imports are coming from the correct base package .
def validate_import(filepath, check, autofix):
    """Validate imports are coming from the correct base package."""
    # almost every case is of the form `from datadog_checks.. import ..`
    # we want to ensure that the imports are from `datadog_checks.base...`
    # except for cases where its importing from actual check code

    success = True
    lines = []

    with open(filepath) as f:
        for num, line in enumerate(f):
            if all(
                (
                    'import' in line,
                    'datadog_checks' in line,
                    'datadog_checks.base' not in line,
                    f'datadog_checks.{check}' not in line,
                    'datadog_checks.dev' not in line,
                )
            ):
                success = False
                lines.append((num, line))

    if autofix and not success:
        with open(filepath, 'r') as f:
            data = f.readlines()

        for num, _ in lines:
            data[num] = data[num].replace('datadog_checks', 'datadog_checks.base')

        with open(filepath, 'w') as f:
            f.write(''.join(data))

    return success, lines
def validate_import ( filepath , check , autofix ) : # almost every case is of the form `from datadog_checks.. import ..` # we want to ensure that the imports are from `datadog_checks.base...` # except for cases where its importing from actual check code success = True lines = [ ] with open ( filepath ) as f : for num , line in enumerate ( f ) : if all ( ( 'import' in line , 'datadog_checks' in line , 'datadog_checks.base' not in line , f'datadog_checks.{check}' not in line , 'datadog_checks.dev' not in line , ) ) : success = False lines . append ( ( num , line ) ) if autofix and not success : with open ( filepath , 'r' ) as f : data = f . readlines ( ) for num , _ in lines : data [ num ] = data [ num ] . replace ( 'datadog_checks' , 'datadog_checks.base' ) with open ( filepath , 'w' ) as f : f . write ( '' . join ( data ) ) return success , lines