<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def get_historical_accounts():
 """Fetches valid accounts from SWAG if enabled or a list accounts."""

    if os.environ.get('SWAG_BUCKET', False):
        swag_opts = {
            'swag.type': 's3',
            'swag.bucket_name': os.environ['SWAG_BUCKET'],
            'swag.data_file': os.environ.get('SWAG_DATA_FILE', 'accounts.json'),
            'swag.region': os.environ.get('SWAG_REGION', 'us-east-1')
        }
        swag = SWAGManager(**parse_swag_config_options(swag_opts))
        search_filter = f"[?provider=='aws' && owner=='{os.environ['SWAG_OWNER']}' && account_status!='deleted'"

        if parse_boolean(os.environ.get('TEST_ACCOUNTS_ONLY')):
            search_filter += " && environment=='test'"

        search_filter += ']'

        accounts = swag.get_service_enabled('historical', search_filter=search_filter)
    else:
        accounts = [{'id': account_id} for account_id in os.environ['ENABLED_ACCOUNTS'].split(',')]

    return accounts