summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSylvain Th?nault <sylvain.thenault@logilab.fr>2009-12-18 11:34:04 +0100
committerSylvain Th?nault <sylvain.thenault@logilab.fr>2009-12-18 11:34:04 +0100
commit8847a3a202afe2f7edf9acd89a30d81126c96e37 (patch)
tree24a4aae6cfec0ffafdc63005c0d48dfa180c440b
parentaad18c7062f98f4b1b2dc76ca5748af6deddbe33 (diff)
downloadpylint-8847a3a202afe2f7edf9acd89a30d81126c96e37.tar.gz
include vincent patch for #5821
-rw-r--r--doc/manual.txt13
-rwxr-xr-xepylint.py55
2 files changed, 67 insertions, 1 deletions
diff --git a/doc/manual.txt b/doc/manual.txt
index eb40cdf..64032a4 100644
--- a/doc/manual.txt
+++ b/doc/manual.txt
@@ -182,6 +182,19 @@ This should open a window where you can enter the name of the package
or module to check, at pylint messages will be displayed in the user
interface.
+It is also possible to call Pylint from an other Python program,
+thanks ``py_run()`` function in ``lint`` module,
+assuming Pylint options are stored in ``pylint_options`` string, as ::
+
+ from pylint import lint
+ lint.py_run( pylint_options)
+
+To silently run Pylint on a ``module_name.py`` module,
+and get its standart output and error::
+
+ from pylint import lint
+ (pylint_stdout, pylint_stderr) = lint.py_run( 'module_name.py', True)
+
Pylint output
-------------
diff --git a/epylint.py b/epylint.py
index 6c56409..b2c127a 100755
--- a/epylint.py
+++ b/epylint.py
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8; mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:ft=python:et:sw=4:ts=4:sts=4
-
"""Emacs and Flymake compatible Pylint.
This script is for integration with emacs and is compatible with flymake mode.
@@ -27,6 +26,8 @@ For example:
- As this script will be invoked by emacs within the directory of the file we are checking
we need to traverse down out of it to avoid these false positives.
+
+You may also use py_run to run pylint with desired options and get back (or not) its output.
"""
import sys, os, re
@@ -91,6 +92,58 @@ def lint(filename):
def Run():
lint(sys.argv[1])
+
+def py_run(command_options='', return_std=False, stdout=None, stderr=None,
+ script='pylint'):
+ """Run pylint from python (needs Python >= 2.4).
+
+ ``command_options`` is a string containing ``pylint`` command line options;
+ ``return_std`` (boolean) indicates return of created standart output
+ and error (see below);
+ ``stdout`` and ``stderr`` are 'file-like' objects in which standart output
+ could be written.
+
+ Calling agent is responsible for stdout/err management (creation, close).
+ Default standart output and error are those from sys,
+ or standalone ones (``subprocess.PIPE``) are used
+ if they are not set and ``return_std``.
+
+ If ``return_std`` is set to ``True``, this function returns a 2-uple
+ containing standart output and error related to created process,
+ as follows: ``(stdout, stderr)``.
+
+ A trivial usage could be as follows:
+ >>> py_run( '--version')
+ No config file found, using default configuration
+ pylint 0.18.1,
+ ...
+
+ To silently run Pylint on a module, and get its standart output and error:
+ >>> (pylint_stdout, pylint_stderr) = py_run( 'module_name.py', True)
+ """
+ # Create command line to call pylint
+ if os.name == 'nt':
+ script += '.bat'
+ command_line = script + ' ' + command_options
+ # Providing standart output and/or error if not set
+ if stdout is None:
+ if return_std:
+ stdout = PIPE
+ else:
+ stdout = sys.stdout
+ if stderr is None:
+ if return_std:
+ stderr = PIPE
+ else:
+ stderr = sys.stderr
+ # Call pylint in a subprocess
+ p = Popen(command_line, shell=True, stdout=stdout, stderr=stderr)
+ p.wait()
+ # Return standart output and error
+ if return_std:
+ return (p.stdout, p.stderr)
+
+
if __name__ == '__main__':
lint(sys.argv[1])