Skip to content

Instantly share code, notes, and snippets.

@akaszynski
Created May 24, 2021 04:46
Show Gist options
  • Save akaszynski/851288a158729475d57bedf6c899ba9a to your computer and use it in GitHub Desktop.
Save akaszynski/851288a158729475d57bedf6c899ba9a to your computer and use it in GitHub Desktop.
refactor mapdl_functions
import re
import shutil
from pathlib import Path
import os
from typing import Optional, Union
from ansys.mapdl.core.mapdl_types import *
def cleanup_method(src_meth):
"""cleanup a raw method"""
# split and deindent
lines = []
for line in src_meth.splitlines():
if line.startswith(' '):
line = line[4:]
lines.append(line)
# swap desc and apdl command
apdl_cmd_idx = None
for i, line in enumerate(lines):
if '"""APDL Command:' in line:
apdl_cmd_idx = i
if apdl_cmd_idx is not None:
apdl_cmd = lines[apdl_cmd_idx][7:]
desc = lines[apdl_cmd_idx + 2][4:]
lines[apdl_cmd_idx] = f' """{desc}'
lines[apdl_cmd_idx + 2] = f' {apdl_cmd}'
# convert to fstr
line = None
for i, line in enumerate(lines):
if 'command = ' in line:
break
if 'command = ' in line:
if 'f"' not in line and "f'" not in line:
if line is not None:
parms = re.findall(r'str\((.+?)\)', line)
for parm in parms:
line = line.replace('%s', '{%s}' % parm, 1)
line = line.replace('"', 'f"', 1)
line = line[:line.rfind('"') + 1]
lines[i] = line
# verify method is valid
meth_str = '\n'.join(lines)
try:
exec(meth_str)
except:
breakpoint()
return meth_str
src_meths = open('/home/alex/ansys/source/pymapdl/ansys/mapdl/core/mapdl_functions.py').read()
done_path = '/home/alex/ansys/source/pymapdl/ansys/mapdl/core/_commands'
docdir = '/home/alex/ansys/source/pymapdl/docs/source/mapdl_commands/'
# walk through all method autodocs
rst_files = []
for root, dirs, files in os.walk(docdir):
for filename in files:
if filename.endswith(".rst") and 'Mapdl' not in filename:
if filename == 'index.rst':
continue
rdc_pth = root.replace(docdir, '')
rst_files.append((filename, root, rdc_pth))
tgt_root_dir = '/tmp/refact'
if os.path.isdir(tgt_root_dir):
shutil.rmtree(tgt_root_dir)
os.mkdir(tgt_root_dir)
print_name = True
st = 27
n = 10
i = 0
# refact_mod = 'prep7'
# for src_file, src_path, mod_name in rst_files[st:st+n]:
for src_file, src_path, mod_name in rst_files:
# if mod_name != refact_mod:
# continue
tgt_file = src_file.replace('.rst', '')
# avoid name conflicts
if tgt_file == 'map':
tgt_file = 'map_cmd'
elif tgt_file == 'aux15':
tgt_file = 'aux15_'
elif tgt_file == 'aux3':
tgt_file = 'aux3_'
# must remap
if mod_name == 'prep7':
mod_name = 'preproc'
elif mod_name == 'aux12':
mod_name = 'aux12_'
elif mod_name == 'aux2':
mod_name = 'aux2_'
elif mod_name == 'post26':
mod_name = 'post26_'
# read in methods in docfile
src_rst = os.path.join(src_path, src_file)
# transfer over each src method
raw = open(src_rst).read()
methods = re.findall(r'Mapdl\.(.*)', raw)
# skip if target mod exists in actual path
final_mod = os.path.join(done_path, mod_name, tgt_file + '.py')
if os.path.isfile(final_mod):
if not print_name:
print(f'skipping {mod_name}.{tgt_file}')
continue
if not print_name:
print(f'parsing {mod_name}.{tgt_file}')
if mod_name:
mod_path = os.path.join(tgt_root_dir, mod_name)
if not os.path.isdir(mod_path):
os.mkdir(mod_path)
else:
mod_path = tgt_root_dir
mth_strs = []
for meth in methods:
# get method
st = src_meths.find(f'def {meth}(')
if print_name:
if mod_name:
print(f' {meth} = {mod_name}.{tgt_file}.{meth}')
else:
print(f' {meth} = {tgt_file}.{meth}')
if st < 0:
print(f'did not find {mod_name}.{tgt_file}.{meth}')
continue
ret = src_meths.find('return ', st)
en = src_meths.find('\n def ', ret)
raw_meth = src_meths[st:en]
# remove existing method
src_meths = src_meths.replace(raw_meth, '')
cln_meth = cleanup_method(raw_meth)
mth_strs.append(cln_meth)
tgt_mod = os.path.join(mod_path, tgt_file + '.py')
with open(tgt_mod, 'w') as fid:
fid.write(('\n'*3).join(mth_strs))
with open(os.path.join(tgt_root_dir, 'mapdl_functions.py'), 'w') as fid:
fid.write(src_meths)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment