File: helpmod.py

package info (click to toggle)
python-scipy 0.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 32,016 kB
  • ctags: 46,675
  • sloc: cpp: 124,854; ansic: 110,614; python: 108,664; fortran: 76,260; objc: 424; makefile: 384; sh: 10
file content (131 lines) | stat: -rw-r--r-- 4,184 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
## Automatically adapted for scipy Oct 31, 2005 by

# Copyright (c) 1996, 1997, The Regents of the University of California.
# All rights reserved.  See Legal.htm for full text and disclaimer.

# A general purpose, crude but hopefully effective help function.
# This expects to find help files on the sys.path named "<module>.help",
# internally formatted as follows:
#
#   <some_feature>:
#     Documentation for some_feature. The word <some_feature> begins in
#     column 1 and ends with ``:'' and a newline. To find <some_feature>
#     type ``help("some_feature")''. Notice the indentation of this text
#     inward from <some_feature>. This is not strictly necessary, but it
#     helps keep clear which things are features.
#
#   <some_other_feature>:
#     To find this particular feature, the help function would be
#     called with help("some_other_feature").  It would construct the
#     search pattern '^some_other_feature:' and pass that to the "more"
#     program.
#
# You can call help with three general forms:
#   from help import help
#   1) help("module.feature"): Look for a given feature in "module.help".
#   2) help("module."): Open "module.help" at line 1. (Notice the dot.)
#   3) help("feature"): Look for a feature in any help file, beginning
#        with the one last accessed.

import os, sys, string

_current_module = ""
_current_dir = ""

#*************
# This function should be the only external object in this module.
def help (subj = "help.help"):
    global _current_module, _current_dir
    sys.path.append(os.environ['GISTPATH'])

    if string.find (subj,'.') == -1: # subject pattern has no dot:

        rexp = "^" + subj + ":"
        p = _find_module_with_feature(rexp)

    else: # subject pattern does have a dot, e.g. help("foo.bar")

        [ module, feature ] = string.splitfields (subj, '.')
        p = _find_module(module)
        if feature == "":
            rexp = "+1 "
        else:
            rexp = "^" + feature + ":"

    if p != "" and _ispresent(rexp,p):
        _pageit (rexp, p)

    del sys.path[-1]

#*************
# Look in all help files for a particular feature
def _find_module_with_feature (rexp):
    global _current_module, _current_dir

    # Look first in the cached help file.
    p = os.path.join (_current_dir, _current_module) + ".help"
    if _ispresent(rexp, p):
        return p

    # Extend the search to all help files.
    for d in sys.path:
        xp = os.path.join (d, "*.help")
        s = "egrep -l '" + rexp + "' " + xp + " 2>/dev/null"
        pipe = os.popen (s)
        line = pipe.readline()
        pipe.close()
        line = string.strip(line)
        if line != '':      # Found one
            _current_dir = d
            _current_module = line[0:-5]
            return os.path.join(d, line)

    # Failure return
    print "could not find pattern", rexp, "in any help file on your sys.path"
    return ""

#*************
# Look for a particular help file "m.help" in user's sys.path
def _find_module (m):
    global _current_module, _current_dir

    # Check cache first.
    f = m + ".help"
    if m == _current_module:
        return os.path.join(_current_dir, f)

    # Extend the search over all components.
    for d in sys.path:
        p = os.path.join(d,f)
        if os.path.exists (p):
            _current_dir = d
            _current_module = m
            return p

    # Failure return
    print "could not find", f, "on your sys.path"
    _current_dir = _current_module = ""
    return ""

#*************
# Check a given file to see if it contains a given regular expression
def _ispresent (rexp, file):
    if rexp == "+1 ":
        return 1
    s = "egrep -l '" + rexp + "' " + file + ">/dev/null 2>&1"
    sts = os.system (s)
    if sts == 0:
        return 1
    # Failure return
    return 0

#*************
# Page the given file, starting at the given regular expression.
# This function should always succeed.
def _pageit( rexp, file):
    if rexp != "+1 ":
        rexp = "+/'" + rexp + "' "
    s = "${PAGER-more} " + rexp + file
    sts = os.system (s)
    if sts != 0:
        print "Warning: Pager returned non-zero status:", sts, "on file", file