Repository :
http://git.fedorahosted.org/cgit/cura-tools.git
On branch : master
---------------------------------------------------------------
commit c6b3f05a2adc1b41e2f938be27ec880143412593
Author: Peter Hatina <phatina(a)redhat.com>
Date: Fri Aug 9 11:01:10 2013 +0200
code cleanup, LMIFormatter, which, get_pager
---------------------------------------------------------------
cli/lmi/shell/LMIFormatter.py | 86 ++++++++++++++++++++++-------------------
1 files changed, 46 insertions(+), 40 deletions(-)
diff --git a/cli/lmi/shell/LMIFormatter.py b/cli/lmi/shell/LMIFormatter.py
index 5e139cc..cd4d3bf 100644
--- a/cli/lmi/shell/LMIFormatter.py
+++ b/cli/lmi/shell/LMIFormatter.py
@@ -26,6 +26,51 @@ from LMIUtil import lmi_raise_or_dump_exception
from LMIExceptions import LMINoPagerError
+def _is_executable(fpath):
+ """
+ Returns True, if provided path is executable, False otherwise.
+
+ Arguments:
+ fpath -- string containing a path to executable
+ """
+ return os.path.exists(fpath) and os.access(fpath, os.X_OK)
+
+def _which(program):
+ """
+ Returns a full path of a selected binary.
+
+ Arguments:
+ program -- string containing a path to executable
+ """
+ (fpath, fname) = os.path.split(program)
+ if fpath:
+ if _is_executable(program):
+ return program
+ else:
+ for path in os.environ["PATH"].split(os.pathsep):
+ exe_file = os.path.join(path, program)
+ if _is_executable(exe_file):
+ return exe_file
+ return None
+
+def _get_pager_with_params():
+ """
+ Returns a list containing a binary with its CLI arguments.
+ """
+ if "PAGER" in os.environ:
+ path = _which(os.environ["PAGER"])
+ if path:
+ return [path]
+ for p in ["less", "more"]:
+ path = _which(p)
+ if not path:
+ continue
+ elif p == "less":
+ return [path, "-S"]
+ return [path]
+ lmi_raise_or_dump_exception(LMINoPagerError("No default pager found"))
+ return []
+
class LMITextFormatter(object):
"""
Text formatter class. Used when printing a block of text to output stream.
@@ -86,45 +131,6 @@ class LMIFormatter(object):
"""
pass
- @staticmethod
- def __which(program):
- """
- Returns a full path of a selected binary. Similar to LMIShell's which.
-
- Arguments:
- program -- executable name
- """
- is_exe = lambda fpath: os.path.isfile(fpath) and os.access(fpath, os.X_OK)
- fpath, fname = os.path.split(program)
- if fpath:
- if is_exe(program):
- return program
- else:
- for path in os.environ["PATH"].split(os.pathsep):
- exe_file = os.path.join(path, program)
- if is_exe(exe_file):
- return exe_file
- return None
-
- @staticmethod
- def __get_pager():
- """
- Returns a list containing a binary with its CLI arguments.
- """
- if "PAGER" in os.environ:
- pager = os.environ["PAGER"].split()
- if os.path.exists(pager[0]) and os.access(pager[0], os.X_OK):
- return pager
- for p in ["less", "more"]:
- path = LMIFormatter.__which(p)
- if not path:
- continue
- elif p == "less":
- return [path, "-S"]
- return [path]
- lmi_raise_or_dump_exception(LMINoPagerError("No default pager found"))
- return []
-
def fancy_format(self, interactive):
"""
Formats a block of text. If the LMIShell is running in interactive
@@ -139,7 +145,7 @@ class LMIFormatter(object):
f = os.fdopen(tmpfile[0], "w")
self.format(0, 0, self._width, f)
f.close()
- subprocess_params = LMIFormatter.__get_pager()
+ subprocess_params = _get_pager_with_params()
if not subprocess_params:
return
subprocess_params.append(tmpfile[1])