Return the one-line summary of a file object, if present
(file)
| 322 | return False |
| 323 | |
| 324 | def source_synopsis(file): |
| 325 | """Return the one-line summary of a file object, if present""" |
| 326 | |
| 327 | string = '' |
| 328 | try: |
| 329 | tokens = tokenize.generate_tokens(file.readline) |
| 330 | for tok_type, tok_string, _, _, _ in tokens: |
| 331 | if tok_type == tokenize.STRING: |
| 332 | string += tok_string |
| 333 | elif tok_type == tokenize.NEWLINE: |
| 334 | with warnings.catch_warnings(): |
| 335 | # Ignore the "invalid escape sequence" warning. |
| 336 | warnings.simplefilter("ignore", SyntaxWarning) |
| 337 | docstring = ast.literal_eval(string) |
| 338 | if not isinstance(docstring, str): |
| 339 | return None |
| 340 | return docstring.strip().split('\n')[0].strip() |
| 341 | elif tok_type == tokenize.OP and tok_string in ('(', ')'): |
| 342 | string += tok_string |
| 343 | elif tok_type not in (tokenize.COMMENT, tokenize.NL, tokenize.ENCODING): |
| 344 | return None |
| 345 | except (tokenize.TokenError, UnicodeDecodeError, SyntaxError): |
| 346 | return None |
| 347 | return None |
| 348 | |
| 349 | def synopsis(filename, cache={}): |
| 350 | """Get the one-line summary out of a module file.""" |