'Provide intelligent behavoir for Tab key press'

def intelligent_tab(self):

    leading_text = self.get_text('sol', 'cursor')
    if ((not leading_text.strip()) or leading_text.endswith('#')):
        self.indent_or_replace()
    elif (self.in_comment_or_string() and (not leading_text.endswith('    '))):
        self.do_completion()
    elif (leading_text.endswith('import    ') or (leading_text[(-1)] == '.')):
        self.do_completion()
    elif ((leading_text.split()[0] in ['from', 'import']) and (not (';' in leading_text))):
        self.do_completion()
    elif ((leading_text[(-1)] in '(,') or leading_text.endswith(',    ')):
        self.indent_or_replace()
    elif leading_text.endswith('    '):
        self.indent_or_replace()
    elif re.search('[^\\d\\W]\\w*\\Z', leading_text, re.UNICODE):
        self.do_completion()
    else:
        self.indent_or_replace()
