|
23 | 23 |
|
24 | 24 | input_file = sys.argv[1]
|
25 | 25 | print(f"reading {input_file}")
|
26 |
| -with open(input_file) as fpin: |
| 26 | +with open(input_file, encoding="utf-8") as fpin: |
27 | 27 | text = fpin.read()
|
28 | 28 |
|
29 | 29 | # Compute output file path.
|
|
40 | 40 | nbook = v3.reads_py("")
|
41 | 41 | nbook = v4.upgrade(nbook) # Upgrade v3 to v4
|
42 | 42 |
|
| 43 | +METADATA = {"language_info": {"name": "python"}} |
| 44 | +nbook["metadata"] = METADATA |
| 45 | + |
43 | 46 | print("Adding copyright cell...")
|
44 |
| -google = "##### Copyright 2024 Google LLC." |
45 |
| -nbook["cells"].append(v4.new_markdown_cell(source=google, id="google")) |
| 47 | +GOOGLE = "##### Copyright 2024 Google LLC." |
| 48 | +nbook["cells"].append(v4.new_markdown_cell(source=GOOGLE, id="google")) |
46 | 49 |
|
47 | 50 | print("Adding license cell...")
|
48 |
| -apache = """Licensed under the Apache License, Version 2.0 (the "License"); |
| 51 | +APACHE = """Licensed under the Apache License, Version 2.0 (the "License"); |
49 | 52 | you may not use this file except in compliance with the License.
|
50 | 53 | You may obtain a copy of the License at
|
51 | 54 |
|
|
57 | 60 | See the License for the specific language governing permissions and
|
58 | 61 | limitations under the License.
|
59 | 62 | """
|
60 |
| -nbook["cells"].append(v4.new_markdown_cell(source=apache, id="apache")) |
| 63 | +nbook["cells"].append(v4.new_markdown_cell(source=APACHE, id="apache")) |
61 | 64 |
|
62 | 65 | print("Adding Title cell...")
|
63 | 66 | basename = "# " + os.path.basename(input_file).replace(".py", "")
|
64 | 67 | nbook["cells"].append(v4.new_markdown_cell(source=basename, id="basename"))
|
65 | 68 |
|
66 | 69 | print("Adding link cell...")
|
67 |
| -github_logo = ( |
| 70 | +GITHUB_LOGO = ( |
68 | 71 | "https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png"
|
69 | 72 | )
|
70 |
| -github_path = "https://github.com/google/or-tools/blob/main/" + input_file |
| 73 | +GITHUB_PATH = "https://github.com/google/or-tools/blob/main/" + input_file |
71 | 74 |
|
72 |
| -colab_path = ( |
| 75 | +COLAB_PATH = ( |
73 | 76 | "https://colab.research.google.com/github/google/or-tools/blob/main/" + output_file
|
74 | 77 | )
|
75 |
| -colab_logo = ( |
| 78 | +COLAB_LOGO = ( |
76 | 79 | "https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png"
|
77 | 80 | )
|
78 | 81 | link = f"""<table align=\"left\">
|
79 | 82 | <td>
|
80 |
| -<a href=\"{colab_path}\"><img src=\"{colab_logo}\"/>Run in Google Colab</a> |
| 83 | +<a href=\"{COLAB_PATH}\"><img src=\"{COLAB_LOGO}\"/>Run in Google Colab</a> |
81 | 84 | </td>
|
82 | 85 | <td>
|
83 |
| -<a href=\"{github_path}\"><img src=\"{github_logo}\"/>View source on GitHub</a> |
| 86 | +<a href=\"{GITHUB_PATH}\"><img src=\"{GITHUB_LOGO}\"/>View source on GitHub</a> |
84 | 87 | </td>
|
85 | 88 | </table>"""
|
86 | 89 | nbook["cells"].append(v4.new_markdown_cell(source=link, id="link"))
|
87 | 90 |
|
88 | 91 | print("Adding ortools install cell...")
|
89 |
| -install_doc = ( |
| 92 | +INSTALL_DOC = ( |
90 | 93 | "First, you must install "
|
91 | 94 | "[ortools](https://pypi.org/project/ortools/) package in this "
|
92 | 95 | "colab."
|
93 | 96 | )
|
94 |
| -nbook["cells"].append(v4.new_markdown_cell(source=install_doc, id="doc")) |
95 |
| -install_cmd = "%pip install ortools" |
96 |
| -nbook["cells"].append(v4.new_code_cell(source=install_cmd, id="install")) |
| 97 | +nbook["cells"].append(v4.new_markdown_cell(source=INSTALL_DOC, id="doc")) |
| 98 | +INSTALL_CMD = "%pip install ortools" |
| 99 | +nbook["cells"].append(v4.new_code_cell(source=INSTALL_CMD, id="install")) |
97 | 100 |
|
98 | 101 | print("Adding code cell...")
|
99 | 102 | all_blocks = ast.parse(text).body
|
|
102 | 105 | line_start[0] = 0
|
103 | 106 | lines = text.split("\n")
|
104 | 107 |
|
105 |
| -full_text = "" |
| 108 | +FULL_TEXT = "" |
106 | 109 | for idx, (c_block, s, e) in enumerate(
|
107 | 110 | zip(all_blocks, line_start, line_start[1:] + [len(lines)])
|
108 | 111 | ):
|
|
148 | 151 | and c_block.names[0].name == "flags"
|
149 | 152 | ):
|
150 | 153 | print(f"Rewrite import {c_block.module}.{c_block.names[0].name}...")
|
151 |
| - full_text += "from ortools.sat.colab import flags\n" |
| 154 | + FULL_TEXT += "from ortools.sat.colab import flags\n" |
152 | 155 | # Unwrap __main__ function
|
153 | 156 | elif isinstance(c_block, ast.If) and c_block.test.comparators[0].s == "__main__":
|
154 | 157 | print("Unwrapping main function...")
|
|
173 | 176 | filtered_lines = [
|
174 | 177 | re.sub(r"app.run\((.*)\)$", r"\1()", s) for s in filtered_lines
|
175 | 178 | ]
|
176 |
| - full_text += "\n".join(filtered_lines) + "\n" |
| 179 | + FULL_TEXT += "\n".join(filtered_lines) + "\n" |
177 | 180 | # Others
|
178 | 181 | else:
|
179 | 182 | print("Appending block...")
|
|
186 | 189 | filtered_lines = list(
|
187 | 190 | filter(lambda l: not re.search(r"# \[END .*\]$", l), filtered_lines)
|
188 | 191 | )
|
189 |
| - full_text += "\n".join(filtered_lines) + "\n" |
| 192 | + FULL_TEXT += "\n".join(filtered_lines) + "\n" |
190 | 193 |
|
191 |
| -nbook["cells"].append(v4.new_code_cell(source=full_text, id="code")) |
| 194 | +nbook["cells"].append( |
| 195 | + v4.new_code_cell(source=FULL_TEXT, id="code") |
| 196 | +) |
192 | 197 |
|
193 | 198 | jsonform = v4.writes(nbook) + "\n"
|
194 | 199 |
|
195 | 200 | print(f"writing {output_file}")
|
196 |
| -with open(output_file, "w") as fpout: |
| 201 | +with open(output_file, mode="w", encoding="utf-8") as fpout: |
197 | 202 | fpout.write(jsonform)
|
0 commit comments