eric6/Plugins/CheckerPlugins/CodeStyleChecker/DocStyleChecker.py

changeset 7637
c878e8255972
parent 7628
f904d0eef264
child 7639
422fd05e9c91
equal deleted inserted replaced
7636:61566f35ab22 7637:c878e8255972
38 38
39 @param source source code of the context (list of string or string) 39 @param source source code of the context (list of string or string)
40 @param startLine line number the context starts in the source (integer) 40 @param startLine line number the context starts in the source (integer)
41 @param contextType type of the context object (string) 41 @param contextType type of the context object (string)
42 """ 42 """
43 if sys.version_info[0] == 2: 43 if isinstance(source, str):
44 stringTypes = (str, unicode) # __IGNORE_WARNING__
45 else:
46 stringTypes = str
47 if isinstance(source, stringTypes):
48 self.__source = source.splitlines(True) 44 self.__source = source.splitlines(True)
49 else: 45 else:
50 self.__source = source[:] 46 self.__source = source[:]
51 self.__start = startLine 47 self.__start = startLine
52 self.__indent = "" 48 self.__indent = ""
358 if not self.__checkers: 354 if not self.__checkers:
359 # don't do anything, if no codes were selected 355 # don't do anything, if no codes were selected
360 return 356 return
361 357
362 source = "".join(self.__source) 358 source = "".join(self.__source)
363 # Check type for py2: if not str it's unicode
364 if sys.version_info[0] == 2:
365 try:
366 source = source.encode('utf-8')
367 except UnicodeError:
368 pass
369 try: 359 try:
370 compile(source, self.__filename, 'exec', ast.PyCF_ONLY_AST) 360 compile(source, self.__filename, 'exec', ast.PyCF_ONLY_AST)
371 except (SyntaxError, TypeError): 361 except (SyntaxError, TypeError):
372 self.__reportInvalidSyntax() 362 self.__reportInvalidSyntax()
373 return 363 return
443 else: 433 else:
444 lineno = 2 434 lineno = 2
445 summaries.append(line2) 435 summaries.append(line2)
446 return summaries, lineno 436 return summaries, lineno
447 437
448 if sys.version_info[0] < 3: 438 def __getArgNames(self, node):
449 def __getArgNames(self, node): 439 """
450 """ 440 Private method to get the argument names of a function node.
451 Private method to get the argument names of a function node. 441
452 442 @param node AST node to extract arguments names from
453 @param node AST node to extract arguments names from 443 @return tuple of two list of argument names, one for arguments
454 @return tuple of two list of argument names, one for arguments 444 and one for keyword arguments (tuple of list of string)
455 and one for keyword arguments (tuple of list of string) 445 """
456 """ 446 arguments = []
457 def unpackArgs(args): 447 arguments.extend([arg.arg for arg in node.args.args])
458 """ 448 if node.args.vararg is not None:
459 Local helper function to unpack function argument names. 449 if sys.version_info < (3, 4, 0):
460
461 @param args list of AST node arguments
462 @return list of argument names (list of string)
463 """
464 ret = []
465 for arg in args:
466 if isinstance(arg, ast.Tuple):
467 ret.extend(unpackArgs(arg.elts))
468 else:
469 ret.append(arg.id)
470 return ret
471
472 arguments = unpackArgs(node.args.args)
473 if node.args.vararg is not None:
474 arguments.append(node.args.vararg) 450 arguments.append(node.args.vararg)
475 kwarguments = [] 451 else:
476 if node.args.kwarg is not None: 452 arguments.append(node.args.vararg.arg)
453
454 kwarguments = []
455 kwarguments.extend([arg.arg for arg in node.args.kwonlyargs])
456 if node.args.kwarg is not None:
457 if sys.version_info < (3, 4, 0):
477 kwarguments.append(node.args.kwarg) 458 kwarguments.append(node.args.kwarg)
478 return arguments, kwarguments 459 else:
479 else: 460 kwarguments.append(node.args.kwarg.arg)
480 def __getArgNames(self, node): # __IGNORE_WARNING__ 461 return arguments, kwarguments
481 """
482 Private method to get the argument names of a function node.
483
484 @param node AST node to extract arguments names from
485 @return tuple of two list of argument names, one for arguments
486 and one for keyword arguments (tuple of list of string)
487 """
488 arguments = []
489 arguments.extend([arg.arg for arg in node.args.args])
490 if node.args.vararg is not None:
491 if sys.version_info < (3, 4, 0):
492 arguments.append(node.args.vararg)
493 else:
494 arguments.append(node.args.vararg.arg)
495
496 kwarguments = []
497 kwarguments.extend([arg.arg for arg in node.args.kwonlyargs])
498 if node.args.kwarg is not None:
499 if sys.version_info < (3, 4, 0):
500 kwarguments.append(node.args.kwarg)
501 else:
502 kwarguments.append(node.args.kwarg.arg)
503 return arguments, kwarguments
504 462
505 ################################################################## 463 ##################################################################
506 ## Parsing functionality below 464 ## Parsing functionality below
507 ################################################################## 465 ##################################################################
508 466

eric ide

mercurial