Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/ShExBy/shex_roo.../lib64/python2..../idlelib
File: help.py
""" help.py: Implement the Idle help menu.
[0] Fix | Delete
Contents are subject to revision at any time, without notice.
[1] Fix | Delete
[2] Fix | Delete
[3] Fix | Delete
Help => About IDLE: diplay About Idle dialog
[4] Fix | Delete
[5] Fix | Delete
<to be moved here from aboutDialog.py>
[6] Fix | Delete
[7] Fix | Delete
[8] Fix | Delete
Help => IDLE Help: Display help.html with proper formatting.
[9] Fix | Delete
Doc/library/idle.rst (Sphinx)=> Doc/build/html/library/idle.html
[10] Fix | Delete
(help.copy_strip)=> Lib/idlelib/help.html
[11] Fix | Delete
[12] Fix | Delete
HelpParser - Parse help.html and render to tk Text.
[13] Fix | Delete
[14] Fix | Delete
HelpText - Display formatted help.html.
[15] Fix | Delete
[16] Fix | Delete
HelpFrame - Contain text, scrollbar, and table-of-contents.
[17] Fix | Delete
(This will be needed for display in a future tabbed window.)
[18] Fix | Delete
[19] Fix | Delete
HelpWindow - Display HelpFrame in a standalone window.
[20] Fix | Delete
[21] Fix | Delete
copy_strip - Copy idle.html to help.html, rstripping each line.
[22] Fix | Delete
[23] Fix | Delete
show_idlehelp - Create HelpWindow. Called in EditorWindow.help_dialog.
[24] Fix | Delete
"""
[25] Fix | Delete
from HTMLParser import HTMLParser
[26] Fix | Delete
from os.path import abspath, dirname, isdir, isfile, join
[27] Fix | Delete
from platform import python_version
[28] Fix | Delete
from Tkinter import Tk, Toplevel, Frame, Text, Scrollbar, Menu, Menubutton
[29] Fix | Delete
import tkFont as tkfont
[30] Fix | Delete
from idlelib.configHandler import idleConf
[31] Fix | Delete
[32] Fix | Delete
use_ttk = False # until available to import
[33] Fix | Delete
if use_ttk:
[34] Fix | Delete
from tkinter.ttk import Menubutton
[35] Fix | Delete
[36] Fix | Delete
## About IDLE ##
[37] Fix | Delete
[38] Fix | Delete
[39] Fix | Delete
## IDLE Help ##
[40] Fix | Delete
[41] Fix | Delete
class HelpParser(HTMLParser):
[42] Fix | Delete
"""Render help.html into a text widget.
[43] Fix | Delete
[44] Fix | Delete
The overridden handle_xyz methods handle a subset of html tags.
[45] Fix | Delete
The supplied text should have the needed tag configurations.
[46] Fix | Delete
The behavior for unsupported tags, such as table, is undefined.
[47] Fix | Delete
If the tags generated by Sphinx change, this class, especially
[48] Fix | Delete
the handle_starttag and handle_endtags methods, might have to also.
[49] Fix | Delete
"""
[50] Fix | Delete
def __init__(self, text):
[51] Fix | Delete
HTMLParser.__init__(self)
[52] Fix | Delete
self.text = text # text widget we're rendering into
[53] Fix | Delete
self.tags = '' # current block level text tags to apply
[54] Fix | Delete
self.chartags = '' # current character level text tags
[55] Fix | Delete
self.show = False # used so we exclude page navigation
[56] Fix | Delete
self.hdrlink = False # used so we don't show header links
[57] Fix | Delete
self.level = 0 # indentation level
[58] Fix | Delete
self.pre = False # displaying preformatted text
[59] Fix | Delete
self.hprefix = '' # prefix such as '25.5' to strip from headings
[60] Fix | Delete
self.nested_dl = False # if we're in a nested <dl>
[61] Fix | Delete
self.simplelist = False # simple list (no double spacing)
[62] Fix | Delete
self.toc = [] # pair headers with text indexes for toc
[63] Fix | Delete
self.header = '' # text within header tags for toc
[64] Fix | Delete
[65] Fix | Delete
def indent(self, amt=1):
[66] Fix | Delete
self.level += amt
[67] Fix | Delete
self.tags = '' if self.level == 0 else 'l'+str(self.level)
[68] Fix | Delete
[69] Fix | Delete
def handle_starttag(self, tag, attrs):
[70] Fix | Delete
"Handle starttags in help.html."
[71] Fix | Delete
class_ = ''
[72] Fix | Delete
for a, v in attrs:
[73] Fix | Delete
if a == 'class':
[74] Fix | Delete
class_ = v
[75] Fix | Delete
s = ''
[76] Fix | Delete
if tag == 'div' and class_ == 'section':
[77] Fix | Delete
self.show = True # start of main content
[78] Fix | Delete
elif tag == 'div' and class_ == 'sphinxsidebar':
[79] Fix | Delete
self.show = False # end of main content
[80] Fix | Delete
elif tag == 'p' and class_ != 'first':
[81] Fix | Delete
s = '\n\n'
[82] Fix | Delete
elif tag == 'span' and class_ == 'pre':
[83] Fix | Delete
self.chartags = 'pre'
[84] Fix | Delete
elif tag == 'span' and class_ == 'versionmodified':
[85] Fix | Delete
self.chartags = 'em'
[86] Fix | Delete
elif tag == 'em':
[87] Fix | Delete
self.chartags = 'em'
[88] Fix | Delete
elif tag in ['ul', 'ol']:
[89] Fix | Delete
if class_.find('simple') != -1:
[90] Fix | Delete
s = '\n'
[91] Fix | Delete
self.simplelist = True
[92] Fix | Delete
else:
[93] Fix | Delete
self.simplelist = False
[94] Fix | Delete
self.indent()
[95] Fix | Delete
elif tag == 'dl':
[96] Fix | Delete
if self.level > 0:
[97] Fix | Delete
self.nested_dl = True
[98] Fix | Delete
elif tag == 'li':
[99] Fix | Delete
s = '\n* ' if self.simplelist else '\n\n* '
[100] Fix | Delete
elif tag == 'dt':
[101] Fix | Delete
s = '\n\n' if not self.nested_dl else '\n' # avoid extra line
[102] Fix | Delete
self.nested_dl = False
[103] Fix | Delete
elif tag == 'dd':
[104] Fix | Delete
self.indent()
[105] Fix | Delete
s = '\n'
[106] Fix | Delete
elif tag == 'pre':
[107] Fix | Delete
self.pre = True
[108] Fix | Delete
if self.show:
[109] Fix | Delete
self.text.insert('end', '\n\n')
[110] Fix | Delete
self.tags = 'preblock'
[111] Fix | Delete
elif tag == 'a' and class_ == 'headerlink':
[112] Fix | Delete
self.hdrlink = True
[113] Fix | Delete
elif tag == 'h1':
[114] Fix | Delete
self.tags = tag
[115] Fix | Delete
elif tag in ['h2', 'h3']:
[116] Fix | Delete
if self.show:
[117] Fix | Delete
self.header = ''
[118] Fix | Delete
self.text.insert('end', '\n\n')
[119] Fix | Delete
self.tags = tag
[120] Fix | Delete
if self.show:
[121] Fix | Delete
self.text.insert('end', s, (self.tags, self.chartags))
[122] Fix | Delete
[123] Fix | Delete
def handle_endtag(self, tag):
[124] Fix | Delete
"Handle endtags in help.html."
[125] Fix | Delete
if tag in ['h1', 'h2', 'h3']:
[126] Fix | Delete
self.indent(0) # clear tag, reset indent
[127] Fix | Delete
if self.show:
[128] Fix | Delete
self.toc.append((self.header, self.text.index('insert')))
[129] Fix | Delete
elif tag in ['span', 'em']:
[130] Fix | Delete
self.chartags = ''
[131] Fix | Delete
elif tag == 'a':
[132] Fix | Delete
self.hdrlink = False
[133] Fix | Delete
elif tag == 'pre':
[134] Fix | Delete
self.pre = False
[135] Fix | Delete
self.tags = ''
[136] Fix | Delete
elif tag in ['ul', 'dd', 'ol']:
[137] Fix | Delete
self.indent(amt=-1)
[138] Fix | Delete
[139] Fix | Delete
def handle_data(self, data):
[140] Fix | Delete
"Handle date segments in help.html."
[141] Fix | Delete
if self.show and not self.hdrlink:
[142] Fix | Delete
d = data if self.pre else data.replace('\n', ' ')
[143] Fix | Delete
if self.tags == 'h1':
[144] Fix | Delete
self.hprefix = d[0:d.index(' ')]
[145] Fix | Delete
if self.tags in ['h1', 'h2', 'h3'] and self.hprefix != '':
[146] Fix | Delete
if d[0:len(self.hprefix)] == self.hprefix:
[147] Fix | Delete
d = d[len(self.hprefix):].strip()
[148] Fix | Delete
self.header += d
[149] Fix | Delete
self.text.insert('end', d, (self.tags, self.chartags))
[150] Fix | Delete
[151] Fix | Delete
def handle_charref(self, name):
[152] Fix | Delete
if self.show:
[153] Fix | Delete
self.text.insert('end', unichr(int(name)))
[154] Fix | Delete
[155] Fix | Delete
[156] Fix | Delete
class HelpText(Text):
[157] Fix | Delete
"Display help.html."
[158] Fix | Delete
def __init__(self, parent, filename):
[159] Fix | Delete
"Configure tags and feed file to parser."
[160] Fix | Delete
uwide = idleConf.GetOption('main', 'EditorWindow', 'width', type='int')
[161] Fix | Delete
uhigh = idleConf.GetOption('main', 'EditorWindow', 'height', type='int')
[162] Fix | Delete
uhigh = 3 * uhigh // 4 # lines average 4/3 of editor line height
[163] Fix | Delete
Text.__init__(self, parent, wrap='word', highlightthickness=0,
[164] Fix | Delete
padx=5, borderwidth=0, width=uwide, height=uhigh)
[165] Fix | Delete
[166] Fix | Delete
normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica'])
[167] Fix | Delete
fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier'])
[168] Fix | Delete
self['font'] = (normalfont, 12)
[169] Fix | Delete
self.tag_configure('em', font=(normalfont, 12, 'italic'))
[170] Fix | Delete
self.tag_configure('h1', font=(normalfont, 20, 'bold'))
[171] Fix | Delete
self.tag_configure('h2', font=(normalfont, 18, 'bold'))
[172] Fix | Delete
self.tag_configure('h3', font=(normalfont, 15, 'bold'))
[173] Fix | Delete
self.tag_configure('pre', font=(fixedfont, 12), background='#f6f6ff')
[174] Fix | Delete
self.tag_configure('preblock', font=(fixedfont, 10), lmargin1=25,
[175] Fix | Delete
borderwidth=1, relief='solid', background='#eeffcc')
[176] Fix | Delete
self.tag_configure('l1', lmargin1=25, lmargin2=25)
[177] Fix | Delete
self.tag_configure('l2', lmargin1=50, lmargin2=50)
[178] Fix | Delete
self.tag_configure('l3', lmargin1=75, lmargin2=75)
[179] Fix | Delete
self.tag_configure('l4', lmargin1=100, lmargin2=100)
[180] Fix | Delete
[181] Fix | Delete
self.parser = HelpParser(self)
[182] Fix | Delete
with open(filename) as f:
[183] Fix | Delete
contents = f.read().decode(encoding='utf-8')
[184] Fix | Delete
self.parser.feed(contents)
[185] Fix | Delete
self['state'] = 'disabled'
[186] Fix | Delete
[187] Fix | Delete
def findfont(self, names):
[188] Fix | Delete
"Return name of first font family derived from names."
[189] Fix | Delete
for name in names:
[190] Fix | Delete
if name.lower() in (x.lower() for x in tkfont.names(root=self)):
[191] Fix | Delete
font = tkfont.Font(name=name, exists=True, root=self)
[192] Fix | Delete
return font.actual()['family']
[193] Fix | Delete
elif name.lower() in (x.lower()
[194] Fix | Delete
for x in tkfont.families(root=self)):
[195] Fix | Delete
return name
[196] Fix | Delete
[197] Fix | Delete
[198] Fix | Delete
class HelpFrame(Frame):
[199] Fix | Delete
"Display html text, scrollbar, and toc."
[200] Fix | Delete
def __init__(self, parent, filename):
[201] Fix | Delete
Frame.__init__(self, parent)
[202] Fix | Delete
text = HelpText(self, filename)
[203] Fix | Delete
self['background'] = text['background']
[204] Fix | Delete
scroll = Scrollbar(self, command=text.yview)
[205] Fix | Delete
text['yscrollcommand'] = scroll.set
[206] Fix | Delete
self.rowconfigure(0, weight=1)
[207] Fix | Delete
self.columnconfigure(1, weight=1) # text
[208] Fix | Delete
self.toc_menu(text).grid(column=0, row=0, sticky='nw')
[209] Fix | Delete
text.grid(column=1, row=0, sticky='nsew')
[210] Fix | Delete
scroll.grid(column=2, row=0, sticky='ns')
[211] Fix | Delete
[212] Fix | Delete
def toc_menu(self, text):
[213] Fix | Delete
"Create table of contents as drop-down menu."
[214] Fix | Delete
toc = Menubutton(self, text='TOC')
[215] Fix | Delete
drop = Menu(toc, tearoff=False)
[216] Fix | Delete
for lbl, dex in text.parser.toc:
[217] Fix | Delete
drop.add_command(label=lbl, command=lambda dex=dex:text.yview(dex))
[218] Fix | Delete
toc['menu'] = drop
[219] Fix | Delete
return toc
[220] Fix | Delete
[221] Fix | Delete
[222] Fix | Delete
class HelpWindow(Toplevel):
[223] Fix | Delete
"Display frame with rendered html."
[224] Fix | Delete
def __init__(self, parent, filename, title):
[225] Fix | Delete
Toplevel.__init__(self, parent)
[226] Fix | Delete
self.wm_title(title)
[227] Fix | Delete
self.protocol("WM_DELETE_WINDOW", self.destroy)
[228] Fix | Delete
HelpFrame(self, filename).grid(column=0, row=0, sticky='nsew')
[229] Fix | Delete
self.grid_columnconfigure(0, weight=1)
[230] Fix | Delete
self.grid_rowconfigure(0, weight=1)
[231] Fix | Delete
[232] Fix | Delete
[233] Fix | Delete
def copy_strip():
[234] Fix | Delete
"""Copy idle.html to idlelib/help.html, stripping trailing whitespace.
[235] Fix | Delete
[236] Fix | Delete
Files with trailing whitespace cannot be pushed to the hg cpython
[237] Fix | Delete
repository. For 3.x (on Windows), help.html is generated, after
[238] Fix | Delete
editing idle.rst in the earliest maintenance version, with
[239] Fix | Delete
sphinx-build -bhtml . build/html
[240] Fix | Delete
python_d.exe -c "from idlelib.help import copy_strip; copy_strip()"
[241] Fix | Delete
After refreshing TortoiseHG workshop to generate a diff,
[242] Fix | Delete
check both the diff and displayed text. Push the diff along with
[243] Fix | Delete
the idle.rst change and merge both into default (or an intermediate
[244] Fix | Delete
maintenance version).
[245] Fix | Delete
[246] Fix | Delete
When the 'earlist' version gets its final maintenance release,
[247] Fix | Delete
do an update as described above, without editing idle.rst, to
[248] Fix | Delete
rebase help.html on the next version of idle.rst. Do not worry
[249] Fix | Delete
about version changes as version is not displayed. Examine other
[250] Fix | Delete
changes and the result of Help -> IDLE Help.
[251] Fix | Delete
[252] Fix | Delete
If maintenance and default versions of idle.rst diverge, and
[253] Fix | Delete
merging does not go smoothly, then consider generating
[254] Fix | Delete
separate help.html files from separate idle.htmls.
[255] Fix | Delete
"""
[256] Fix | Delete
src = join(abspath(dirname(dirname(dirname(__file__)))),
[257] Fix | Delete
'Doc', 'build', 'html', 'library', 'idle.html')
[258] Fix | Delete
dst = join(abspath(dirname(__file__)), 'help.html')
[259] Fix | Delete
with open(src, 'r') as inn,\
[260] Fix | Delete
open(dst, 'w') as out:
[261] Fix | Delete
for line in inn:
[262] Fix | Delete
out.write(line.rstrip() + '\n')
[263] Fix | Delete
print('idle.html copied to help.html')
[264] Fix | Delete
[265] Fix | Delete
def show_idlehelp(parent):
[266] Fix | Delete
"Create HelpWindow; called from Idle Help event handler."
[267] Fix | Delete
filename = join(abspath(dirname(__file__)), 'help.html')
[268] Fix | Delete
if not isfile(filename):
[269] Fix | Delete
# try copy_strip, present message
[270] Fix | Delete
return
[271] Fix | Delete
HelpWindow(parent, filename, 'IDLE Help (%s)' % python_version())
[272] Fix | Delete
[273] Fix | Delete
if __name__ == '__main__':
[274] Fix | Delete
from idlelib.idle_test.htest import run
[275] Fix | Delete
run(show_idlehelp)
[276] Fix | Delete
[277] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function