Commits
Darrell Schiebel authored 4093db8bb01
1 - | import os |
2 - | import datetime |
3 - | import webbrowser |
4 - | import subprocess |
5 - | import sys |
6 - | import xml.etree.cElementTree as ET |
7 1 | import urllib2 |
8 - | from urlparse import urlparse |
2 + | import re |
3 + | import webbrowser |
9 4 | |
10 5 | class __doc(object): |
11 6 | "command-line Plone help" |
12 7 | |
13 8 | def __init__( self ): |
14 - | self.local_toc = None |
15 - | self.remote_toc = None |
16 - | version = "casa-%d.%d.%d" % tuple(cu.version( )[:3]) |
17 - | self.remote_source_url = "https://casa.nrao.edu/casadocs/%s" % version |
18 - | self.remote_source_url_components = urlparse(self.remote_source_url) |
19 - | self.remote_toc_url = 'https://%s/PloneResource/%s/toc.xml' % (self.remote_source_url_components[1],version) |
20 9 | |
21 - | self.local_toc_url = None if casa['dirs']['doc'] is None else casa['dirs']['doc'] + '/casa.nrao.edu/casadocs/toc.xml' |
22 - | self.local_start_path = "usingcasa/starting-casa.html" |
23 - | self.local_base = "/casadocs/%s/" % version |
10 + | version = "%d.%d.%d" % tuple(cu.version( )[:3]) |
24 11 | |
25 - | def __welcome( self, welcome="\nOpening packaged documentation.\n" ): |
26 - | if welcome is not None: |
27 - | print welcome |
28 - | print "The most recent version of all CASA documentation is available online from:" |
29 - | print "\thttps://casa.nrao.edu/casadocs/\n" |
12 + | self.task_url = "https://casa.nrao.edu/PloneResource/casa-" + version + "/taskXml/" |
13 + | self.tool_url = "https://casa.nrao.edu/PloneResource/casa-" + version + "/toolXml/" |
14 + | self.toc_url = "https://casa.nrao.edu/casadocs/casa-" + version + "/global-task-list" |
15 + | self.start_url = "https://casa.nrao.edu/casadocs/casa-" + version |
30 16 | |
31 - | def __call__( self, sec=None ): |
32 - | "open browser with documentation, try \"doc('toc')\"" |
17 + | self.task_prefix = "https://casa.nrao.edu/casadocs/casa-" + version + "/global-task-list/task_" |
18 + | self.tool_prefix = "https://casa.nrao.edu/casadocs/casa-" + version + "/global-tool-list/tool_" |
33 19 | |
34 - | ## for now, access to the plone site is turned off |
35 - | remote=False |
36 - | def show_toc( toc_dict ): |
37 - | width = max(len(key) for key in toc_dict.keys( ))+3 |
38 - | for i in sorted(toc_dict.iterkeys( )): |
39 - | if "external" in toc_dict[i]['visibility']: |
40 - | print "".join([i.ljust(width),toc_dict[i]['desc'].replace('\n','')]) |
20 + | self.tasklist = [ ] |
21 + | self.toollist = [ ] |
41 22 | |
42 - | def entry_to_dict(acc,e): |
43 - | if e.tag == 'entry': |
44 - | acc[e.find('key').text] = { |
45 - | 'desc': e.find('desc').text, |
46 - | 'type': e.find('type').text, |
47 - | 'visibility': e.find('visibility').text, |
48 - | 'path': e.find('path').text } |
49 - | return acc |
23 + | def __call__( self, topic=None ): |
24 + | "open browser with documentation, try \"doc('toc')\"" |
50 25 | |
51 - | if remote: |
52 - | if sec is None: |
53 - | return webbrowser.open("https://casa.nrao.edu/casadocs/") |
54 - | else: |
55 - | if self.remote_toc is None: |
56 - | self.remote_toc = reduce( entry_to_dict, ET.ElementTree(file=urllib2.urlopen(self.remote_toc_url)).getroot( ).getchildren( ), { } ) |
57 - | if sec == 'toc': |
58 - | show_toc(self.remote_toc) |
59 - | elif self.remote_toc.has_key(sec): |
60 - | return webbrowser.open("https://casa.nrao.edu/casadocs/stable/" + self.remote_toc[sec]['path']) |
61 - | else: |
62 - | print "Sorry '%s' is not a recognized section..." % sec |
63 - | print "------------------------------------------------------------------------------" |
64 - | show_toc(self.remote_toc) |
26 + | if len(self.tasklist) == 0: |
27 + | try: |
28 + | self.tasklist = re.findall("\w+.xml", urllib2.urlopen(self.task_url).read().decode()) |
29 + | except: |
30 + | self.tasklist = [ ] |
31 + | |
32 + | if len(self.toollist) == 0: |
33 + | try: |
34 + | self.toollist = re.findall("\w+.xml", urllib2.urlopen(self.tool_url).read().decode()) |
35 + | except: |
36 + | self.toollist = [ ] |
37 + | |
38 + | if type(topic) != str or topic == "toc": |
39 + | webbrowser.open_new_tab(self.toc_url) |
40 + | elif topic == "start": |
41 + | webbrowser.open_new_tab(self.start_url) |
42 + | elif topic+'.xml' in self.tasklist: |
43 + | webbrowser.open_new_tab(self.task_prefix+topic+"/parameters") |
44 + | elif topic+'.xml' in (toollist): |
45 + | webbrowser.open_new_tab(self.tool_prefix+topic+"/methods") |
65 46 | else: |
66 - | path = casa['dirs']['doc'] + "/casa.nrao.edu" |
67 - | if sec is None: |
68 - | homepage = "%s%s.html" % (path,self.remote_source_url_components[2]) |
69 - | if os.path.exists(path): |
70 - | self.__welcome( ) |
71 - | return webbrowser.open("file://" + homepage) |
72 - | else: |
73 - | print "local documentation tree not found..." |
74 - | self.__welcome(None) |
75 - | return False |
76 - | else: |
77 - | if self.local_toc is None: |
78 - | if self.local_toc_url is not None: |
79 - | self.local_toc = reduce( entry_to_dict, ET.ElementTree(file=urllib2.urlopen("file://" + self.local_toc_url)).getroot( ).getchildren( ), { } ) |
80 - | else: |
81 - | print "local documentation tree not found..." |
82 - | self.__welcome(None) |
83 - | return False |
84 - | if sec == 'toc': |
85 - | show_toc(self.local_toc) |
86 - | elif sec == 'start': |
87 - | self.__welcome( ) |
88 - | return webbrowser.open("file://" + path + self.local_base + self.local_start_path) |
89 - | elif self.local_toc.has_key(sec): |
90 - | self.__welcome( ) |
91 - | return webbrowser.open("file://" + path + self.local_base + self.local_toc[sec]['path']) |
92 - | else: |
93 - | self.__welcome(None) |
94 - | print "Sorry '%s' is not a recognized section..." % sec |
95 - | print "------------------------------------------------------------------------------" |
96 - | show_toc(self.local_toc) |
97 - | |
98 - | |
99 - | def fetch( self ): |
100 - | if casa['dirs']['doc'] is None: |
101 - | print "casa['dirs']['doc'] has not been set..." |
102 - | return False |
103 - | if not os.path.exists(casa['dirs']['doc']): |
104 - | print ("directory %s does not exist..." % casa['dirs']['doc']) |
105 - | return False |
106 - | |
107 - | ## rename existing directory |
108 - | path = casa['dirs']['doc'] + "/casa.nrao.edu" |
109 - | if os.path.exists(casa['dirs']['doc'] + "/casa.nrao.edu"): |
110 - | now = datetime.datetime.now( ).isoformat('-') |
111 - | os.rename(path, path + "." + now) |
112 - | |
113 - | print " source: %s" % self.remote_source_url |
114 - | print " table of contents: %s" % self.remote_toc_url |
115 - | print " download point: %s" % path |
116 - | print "--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---" |
117 - | print "this will take some time..." |
118 - | print "relax..." |
119 - | print "do not hit ^C ..." |
120 - | print "do not expect output..." |
121 - | |
122 - | wgetcmd = "wget" |
123 - | |
124 - | if sys.platform == "darwin": |
125 - | wgetcmd = casa['dirs']['root'] + "/Resources/wget" |
126 - | |
127 - | tree = subprocess.call( [ wgetcmd, "--no-parent", "--no-check-certificate", "--html-extension", "--convert-links", "--recursive", |
128 - | "--level=inf", "--page-requisites", "-e", "robots=off", "--wait=0", "--quota=inf", "--reject", |
129 - | '*_form,RSS,*login*,logged_in,*logout*,logged_out,createObject*,select_default_page,selectViewTemplate*,object_cut,object_copy,object_rename,delete_confirmation,content_status_*,addtoFavorites,pdf.html,print.html', |
130 - | "--exclude-directories='search,*com_mailto*'", "--directory-prefix=" + casa['dirs']['doc'], |
131 - | "--convert-links", self.remote_source_url], stderr=subprocess.STDOUT, stdout=open(os.devnull,"w") ) |
132 - | toc = subprocess.call( [ wgetcmd, self.remote_toc_url, "-O", self.local_toc_url], stderr=subprocess.STDOUT, stdout=open(os.devnull,"w") ) |
133 - | if self.remote_source_url_components[1] != 'casa.nrao.edu': |
134 - | orig = os.getcwd() |
135 - | os.chdir(casa['dirs']['doc']) |
136 - | if os.path.exists(self.remote_source_url_components[1]): |
137 - | if os.path.exists('casa.nrao.edu'): |
138 - | os.remove('casa.nrao.edu') |
139 - | os.symlink(self.remote_source_url_components[1],'casa.nrao.edu') |
140 - | else: |
141 - | print "warning, could not find mirror (%s/%s)" % (casa['dirs']['doc'],self.remote_source_url_components[1]) |
142 - | os.chdir(orig) |
143 - | return (tree, toc) |
144 - | |
47 + | webbrowser.open_new_tab(self.toc_url) |
145 48 | |
146 49 | doc = __doc( ) |