Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38dc28a

Browse files
committed
Refactor process_script. Ensure auth comment exists.
1 parent 026991b commit 38dc28a

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

run.py

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,37 @@
185185
)
186186
)
187187

188+
# authentication
189+
authentication = (
190+
"{{comment}} Learn about API authentication here: "
191+
"{plotly_domain}/{{api_path}}/getting-started\n"
192+
"{{comment}} Find your api_key here: {plotly_domain}/settings/api\n"
193+
.format(plotly_domain="https://plot.ly")
194+
)
195+
196+
# define comment strings for each language
197+
comment = {
198+
"julia": "#",
199+
"matlab": "%",
200+
"python": "#",
201+
"matplotlib": '#',
202+
"r": "#",
203+
"ggplot2": "#",
204+
"node_js": "//",
205+
"plotly_js": "//"
206+
}
207+
208+
api_path = {
209+
"julia": "julia",
210+
"matlab": "matlab",
211+
"python": "python",
212+
"matplotlib": "python",
213+
"r": "r",
214+
"ggplot2": "r",
215+
"node_js": "nodejs",
216+
"plotly_js": "javascript-graphing-library"
217+
}
218+
188219

189220
def get_command():
190221
try:
@@ -696,34 +727,41 @@ def process_script_leaf(leaf, options, id_dict):
696727
raise plotly.exceptions.PlotlyError(
697728
"'{}' not found in '{}'".format(script_file, leaf['path'])
698729
)
699-
exec_lines = []
700-
found_sign_in = False
701-
for line in script.splitlines():
702-
if line[:6] == sign_in['execution'][language][:6]: # TODO, better way?
703-
exec_lines.append(sign_in['execution'][language])
704-
found_sign_in = True
705-
elif '>>>filename<<<' in line:
706-
exec_lines.append(line.replace('>>>filename<<<', leaf['id']))
707-
else:
708-
exec_lines.append(line)
709-
while exec_lines[-1] == '\n':
710-
exec_lines.pop()
711-
exec_string = '\n'.join(exec_lines)
712-
if not found_sign_in:
730+
731+
# find sign in line and raise error if DNE
732+
sign_in_lino = find_sign_in_line(script, language)
733+
if sign_in_lino < 0:
713734
raise Exception(
714735
"You need to have the first 6 characters of the following "
715736
"line in the script for this to work:"
716737
"\n{sign_in}\n\nThe offending file is here:"
717738
"\n{path}"
718739
.format(sign_in=sign_in['execution'][language], path=leaf['path'])
719740
)
741+
742+
# remove templated things from script file
743+
script = script.replace('>>>filename<<<', leaf['id'])
744+
script_lines = script.splitlines()
745+
script_lines.pop(sign_in_lino)
746+
747+
# save execution and exception files
748+
exec_lines = list(script_lines)
749+
exec_lines.insert(sign_in_lino, sign_in['execution'][language])
750+
exec_string = '\n'.join(exec_lines)
720751
save_code(exec_string, leaf, language, 'exception')
721-
code_string = exec_string.replace(sign_in['execution'][language],
722-
sign_in['documentation'][language])
723-
code_string = cgi.escape(code_string)
724-
code_path = save_code(code_string, leaf, language, 'documentation')
725-
leaf[language] = code_path
726752
save_code(exec_string, leaf, language, 'execution')
753+
754+
# save documentation file
755+
doc_lines = list(script_lines)
756+
auth = authentication.format(
757+
api_path=api_path[language], comment=comment[language]
758+
)
759+
doc_lines.insert(sign_in_lino, auth)
760+
code_string = '\n'.join(doc_lines)
761+
code_string = cgi.escape(code_string)
762+
leaf[language] = save_code(code_string, leaf, language, 'documentation')
763+
764+
# mark as complete!
727765
id_dict['complete'].add(leaf['id'])
728766

729767

0 commit comments

Comments
 (0)