Skip to content

Commit 6f6ab01

Browse files
authored
feat: cleaning code (#440)
1 parent 0c4d6dd commit 6f6ab01

File tree

12 files changed

+91
-42
lines changed

12 files changed

+91
-42
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ repos:
2828
- --max-line-length=100
2929

3030
- repo: https://github.com/pre-commit/pre-commit-hooks
31-
rev: v4.4.0
31+
rev: v5.0.0
3232
hooks:
3333
- id: check-merge-conflict
3434
- id: debug-statements

src/pyconverter/xml2py/ast_tree.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,16 +2118,26 @@ def args(self):
21182118
for item in self.raw_args:
21192119
arg = to_py_arg_name(str(item))
21202120

2121-
# simply check if we can use this as a valid Python kwarg
2122-
try:
2123-
exec(f"{arg} = 1.0")
2124-
except SyntaxError:
2121+
if "blank" in arg or arg == "":
2122+
# if the argument is not a valid Python identifier, then skip it
21252123
arg = ""
2124+
args.append(arg)
21262125

2127-
if "blank" in arg:
2128-
arg = ""
2126+
elif " or " in arg:
2127+
arg = arg.split(" or ")[0].strip()
2128+
args.append(arg)
2129+
2130+
elif arg in ["...", ". . ."]:
2131+
# Elipsis, needs to be skipped
2132+
pass
21292133

2130-
args.append(arg)
2134+
elif arg.isidentifier() is False:
2135+
raise ValueError(
2136+
f"Invalid argument '{arg}' in refname element: {self._element.tag}"
2137+
)
2138+
2139+
else:
2140+
args.append(arg)
21312141

21322142
# rename duplicate arguments
21332143
if len(args) != len(set(args)):
@@ -3115,13 +3125,13 @@ def arg_desc(self) -> List[Argument]:
31153125
if len(arguments.py_arg_names) != len(arguments.initial_args):
31163126
# This function needs a special treatment
31173127
if arg_file.exists():
3118-
with open(arg_file, "r") as f:
3128+
with open(arg_file, "r", encoding="utf-8") as f:
31193129
for line in f:
31203130
pass
31213131
last_line = line
31223132
else:
31233133
last_line = ""
3124-
with open(arg_file, "a") as f:
3134+
with open(arg_file, "a", encoding="utf-8") as f:
31253135
if last_line != f"py_arg_name : {arguments.py_arg_names}\n":
31263136
f.write("--------------------------------------------------\n")
31273137
f.write(f"{self.py_name}: {self.group}\n")

src/pyconverter/xml2py/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def version():
160160
"-r",
161161
"--run-pre-commit",
162162
type=click.BOOL,
163+
default=False,
164+
is_flag=True,
163165
help="Whether to run the pre-commit hook on the autogenerated package source code.",
164166
)
165167
@click.option(

src/pyconverter/xml2py/custom_functions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_docstring_lists(filename: str) -> Tuple[list[str], list[str], list[str],
5252
List containing the library import section.
5353
"""
5454
lines = None
55-
with open(filename, "r") as pyfile:
55+
with open(filename, "r", encoding="utf-8") as pyfile:
5656
lines = pyfile.readlines()
5757
bool_def = False
5858
end_def = False

src/pyconverter/xml2py/download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ def download(c: ContentFile, out: Path) -> None:
3535
https://github.com/Nordgaren/Github-Folder-Downloader
3636
3737
"""
38-
r = requests.get(c.download_url)
38+
r = requests.get(c.download_url, timeout=10)
3939
output_path = out / c.path
4040
output_path.parent.mkdir(parents=True, exist_ok=True)
41-
with open(output_path, "wb") as f:
41+
with open(output_path, "wb", encoding="utf-8") as f:
4242
print(f"downloading {c.path} to {out}")
4343
f.write(r.content)
4444

src/pyconverter/xml2py/formatter.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,32 @@
2323
"""This module contains the functions to format the generated docstrings with
2424
`Black <black_>`_."""
2525

26-
import os
26+
from pathlib import Path
27+
28+
# Subprocess is needed to run pre-commit hooks.
29+
import subprocess
2730

2831

2932
def run_pre_commit(package_path) -> None:
3033
"""Run `pre-commit <pre-commit_>`_ on the autogenerated package."""
3134
output = 1
3235
cur_run = 0
3336
max_run = 10
37+
pre_commit_file = package_path / Path(".pre-commit-config.yaml")
38+
if not pre_commit_file.exists():
39+
raise FileNotFoundError(f"Pre-commit configuration file not found at {pre_commit_file}.")
3440
while cur_run < max_run and output != 0:
3541
cur_run += 1
36-
output = os.system(
37-
f"pre-commit run --all-files --config {package_path}/.pre-commit-config.yaml"
38-
)
42+
output = subprocess.run(
43+
[
44+
"pre-commit",
45+
"run",
46+
"--all-files",
47+
"--config",
48+
str(pre_commit_file),
49+
],
50+
capture_output=True,
51+
).returncode
3952
if output != 0:
4053
raise RuntimeError("Pre-commit failed.")
4154
else:

src/pyconverter/xml2py/load_xml_doc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434

3535
def link_replacer(file, terms, docu_global, links, base_url, fcache):
36-
with open(file, "r") as fid:
36+
with open(file, "r", encoding="utf-8") as fid:
3737
text = fid.read()
3838
matches = re.findall(r"ENTITY([\S\s]*?)(?=\n)", text)
3939
for match in matches:
@@ -158,7 +158,7 @@ def load_docu_global(term_path: Path) -> dict:
158158
docu_ent = term_path / "glb" / "docu_global.ent"
159159

160160
docu_global = {}
161-
with open(docu_ent, "r") as fid:
161+
with open(docu_ent, "r", encoding="utf-8") as fid:
162162
lines = fid.read().splitlines()
163163

164164
# have to write our own interperter here since this is non-standard lxml
@@ -230,7 +230,7 @@ def load_terms(
230230

231231
variable_path = term_path / "glb" / variable_file
232232
if variable_path.is_file():
233-
with open(variable_path, "r") as fid:
233+
with open(variable_path, "r", encoding="utf-8") as fid:
234234
lines = fid.read().splitlines()
235235

236236
# have to write our own interperter here since this is non-standard lxml
@@ -251,7 +251,7 @@ def load_terms(
251251

252252
global_terms_path = term_path / "glb" / global_terms_file
253253
if global_terms_path.is_file():
254-
with open(global_terms_path, "r") as fid:
254+
with open(global_terms_path, "r", encoding="utf-8") as fid:
255255
lines = fid.read().splitlines()
256256

257257
for line in lines:
@@ -302,7 +302,7 @@ def load_terms(
302302
if ent_dir.is_dir():
303303
isoams_dat = list(ent_dir.glob("*.ent"))
304304
for filename in isoams_dat:
305-
with open(filename, "r") as fid:
305+
with open(filename, "r", encoding="utf-8") as fid:
306306
lines = fid.read().splitlines()
307307
# have to write our own interperter here since this is non-standard lxml
308308
for line in lines:
@@ -319,7 +319,7 @@ def load_terms(
319319
# load group code
320320
group_code_terms_path = term_path / group_code_file
321321
if group_code_terms_path.is_file():
322-
with open(group_code_terms_path, "r") as fid:
322+
with open(group_code_terms_path, "r", encoding="utf-8") as fid:
323323
lines = fid.read().splitlines()
324324

325325
for line in lines:

src/pyconverter/xml2py/utils/utils.py

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def parse_yaml(yaml_path: Path) -> dict:
4646
Dictionary with the content of the YAML file.
4747
"""
4848
if yaml_path.is_file():
49-
with open(yaml_path, "r") as file:
49+
with open(yaml_path, "r", encoding="utf-8") as file:
5050
data = yaml.safe_load(file)
5151
return data
5252
else:
@@ -228,7 +228,7 @@ def import_handler(
228228
String before the function definition.
229229
"""
230230

231-
content = open(filename, "r").read()
231+
content = open(filename, "r", encoding="utf-8").read()
232232
list_imports = list(filter(None, str_before_def.split("\n")))
233233
for import_line in list_imports:
234234
if import_line in content:
@@ -237,11 +237,11 @@ def import_handler(
237237

238238
if len(list_imports) > 0:
239239
str_before_def = "\n".join(list_imports) + "\n\n"
240-
with open(filename, "r+") as f:
240+
with open(filename, "r+", encoding="utf-8") as f:
241241
f.seek(0, 0)
242242
f.write(str_before_def + content + additional_content)
243243
else:
244-
with open(filename, "a") as f:
244+
with open(filename, "a", encoding="utf-8") as f:
245245
f.write(additional_content)
246246

247247

@@ -300,3 +300,25 @@ def get_refentry(filename: Path) -> list:
300300
"""
301301
root = fromstring(open(filename, "rb").read())
302302
return list(root.iterfind(".//refentry"))
303+
304+
305+
def is_valid_method(method: str) -> bool:
306+
"""Check if a method is valid Python code.
307+
308+
Parameters
309+
----------
310+
method: str
311+
The method source code as a string.
312+
It needs to be already indented for class context.
313+
314+
Returns
315+
-------
316+
bool
317+
``True`` if the method is valid Python code, ``False`` otherwise.
318+
"""
319+
dummy_class = f"class Dummy:\n{method}"
320+
try:
321+
compile(dummy_class, "<string>", "exec")
322+
return True
323+
except SyntaxError as e:
324+
return False

src/pyconverter/xml2py/writer.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import logging
2424
from pathlib import Path
25+
import py_compile
2526
import shutil
2627
from typing import Tuple, Union
2728

@@ -38,6 +39,7 @@
3839
get_library_path,
3940
get_refentry,
4041
import_handler,
42+
is_valid_method,
4143
)
4244
import regex as re
4345
from tqdm import tqdm
@@ -236,7 +238,7 @@ def write_global__init__file(library_path: Path, config_path: Path) -> None:
236238

237239
init_path = init_folder / "__init__.py"
238240

239-
with open(init_path, "w") as fid:
241+
with open(init_path, "w", encoding="utf-8") as fid:
240242
fid.write(f"from .{initial_imports} import (\n")
241243
for dir in library_path.iterdir():
242244
if dir.is_dir():
@@ -265,7 +267,7 @@ def write__init__file(library_path: Path) -> None:
265267
if dir.is_dir():
266268
listdir = list(dir.iterdir())
267269
if len(listdir) > 0:
268-
with open(dir / "__init__.py", "w") as fid:
270+
with open(dir / "__init__.py", "w", encoding="utf-8") as fid:
269271
fid.write(f"from . import (\n")
270272
for file in listdir:
271273
if file.name.endswith(".py"):
@@ -415,15 +417,15 @@ def write_source(
415417
python_name = name_map[initial_command_name]
416418
path = library_path / f"{python_name}.py"
417419
python_method = command_obj.to_python(custom_functions, comment_command_dict, indent="")
418-
try:
419-
exec(python_method)
420+
# Check the Python method is valid before writing it to the file
421+
if is_valid_method(python_method):
420422
with open(path, "w", encoding="utf-8") as fid:
421423
fid.write(f"{python_method}\n")
422-
except Exception as e:
423-
raise RuntimeError(f"Failed to execute {python_name}.py") from e
424-
424+
else:
425+
logging.warning(
426+
f"Invalid Python method for {initial_command_name}: {python_method}"
427+
)
425428
else:
426-
import subprocess
427429

428430
package_structure = {}
429431
all_commands = []
@@ -490,7 +492,7 @@ def write_source(
490492
for class_name, _ in package_structure[module_name].items():
491493
file_path = library_path / module_name / f"{class_name}.py"
492494
try:
493-
subprocess.run(["python", str(file_path)])
495+
py_compile.compile(str(file_path))
494496
except Exception as e:
495497
raise RuntimeError(
496498
f"Failed to execute '{python_method}' from '{file_path}'."
@@ -553,7 +555,7 @@ def write_docs(
553555

554556
# Write the main doc file
555557
doc_src = doc_package_path / "docs.rst"
556-
with open(doc_src, "w") as fid:
558+
with open(doc_src, "w", encoding="utf-8") as fid:
557559
fid.write(doc_src_content)
558560

559561
if package_structure is not None:
@@ -588,7 +590,7 @@ def write_docs(
588590
module_folder = doc_package_path / module_folder_name
589591
module_folder.mkdir(parents=True, exist_ok=True)
590592
module_file = module_folder / "index.rst"
591-
with open(module_file, "w") as fid:
593+
with open(module_file, "w", encoding="utf-8") as fid:
592594
fid.write(module_content)
593595

594596
for class_file_name, (class_name, method_list) in class_map.items():
@@ -616,7 +618,7 @@ def write_docs(
616618

617619
# Write the class file
618620
class_file = module_folder / f"{class_file_name}.rst"
619-
with open(class_file, "w") as fid:
621+
with open(class_file, "w", encoding="utf-8") as fid:
620622
fid.write(class_content)
621623

622624
return doc_src

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def directory_path(ghdir):
5050
if os.environ.get("ON_CI", "").lower() == "true":
5151
directory_path = Path(ghdir) / "mapdl-cmd-doc"
5252
else:
53-
directory_path = Path.cwd().parent / "mapdl-cmd-doc"
53+
directory_path = Path.cwd().parent.parent / "ansys-internal" / "mapdl-cmd-doc"
5454
directory_path = directory_path.resolve()
5555
return directory_path
5656

0 commit comments

Comments
 (0)