flake8/flake8/util.py
Ian Cordasco a6fc242c5e Slightly simplify our conditionals
Test warnings by default
2015-03-07 20:21:58 -06:00

68 lines
1.8 KiB
Python

# -*- coding: utf-8 -*-
import os
try:
import ast
iter_child_nodes = ast.iter_child_nodes
except ImportError: # Python 2.5
import _ast as ast
if 'decorator_list' not in ast.ClassDef._fields:
# Patch the missing attribute 'decorator_list'
ast.ClassDef.decorator_list = ()
ast.FunctionDef.decorator_list = property(lambda s: s.decorators)
def iter_child_nodes(node):
"""
Yield all direct child nodes of *node*, that is, all fields that
are nodes and all items of fields that are lists of nodes.
"""
if not node._fields:
return
for name in node._fields:
field = getattr(node, name, None)
if isinstance(field, ast.AST):
yield field
elif isinstance(field, list):
for item in field:
if isinstance(item, ast.AST):
yield item
class OrderedSet(list):
"""List without duplicates."""
__slots__ = ()
def add(self, value):
if value not in self:
self.append(value)
def is_flag(val):
"""Guess if the value could be an on/off toggle"""
val = str(val)
return val.upper() in ('1', '0', 'F', 'T', 'TRUE', 'FALSE', 'ON', 'OFF')
def is_windows():
"""Determine if the system is Windows."""
return os.name == 'nt'
def is_using_stdin(paths):
"""Determine if we're running checks on stdin."""
return '-' in paths
def warn_when_using_jobs(options):
return (options.verbose and options.jobs and options.jobs.isdigit() and
int(options.jobs) > 1)
def force_disable_jobs(styleguide):
return is_windows() or is_using_stdin(styleguide.paths)
def flag_on(val):
"""Return true if flag is on"""
return str(val).upper() in ('1', 'T', 'TRUE', 'ON')