1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
Forwarded: https://github.com/BlueBrain/nmodl/pull/1166
--- a/nmodl/ast.py
+++ b/nmodl/ast.py
@@ -1,5 +1,14 @@
+import getpass
+import json
+import os
+import tempfile
+import webbrowser
+from importlib.resources import files
+from shutil import copytree
+
+from ._nmodl import to_json
from ._nmodl.ast import * # noqa
-from pkg_resources import *
+
def view(nmodl_ast):
"""Visualize given NMODL AST in web browser
@@ -14,29 +23,20 @@
Returns:
None
"""
- from ._nmodl import to_json
- from distutils.dir_util import copy_tree
- import getpass
- import json
- import os
- import tempfile
- import webbrowser
-
- resource = "ext/viz"
- if resource_exists(__name__, resource) and resource_isdir(__name__, resource):
- installed_viz_tool = resource_filename(__name__, resource)
- else:
+
+ path = files("nmodl") / "ext/viz"
+ if not path.is_dir():
raise FileNotFoundError("Could not find sample mod files")
work_dir = os.path.join(tempfile.gettempdir(), getpass.getuser(), "nmodl")
# first copy necessary files to temp work directory
- copy_tree(installed_viz_tool, work_dir)
+ copytree(path, work_dir, dirs_exist_ok=True)
# prepare json data
- with open(os.path.join(work_dir, 'ast.js'), 'w') as outfile:
- json_data = json.loads(to_json(nmodl_ast, True, True, True))
- outfile.write('var astRoot = %s;' % json.dumps(json_data))
+ json_data = json.loads(to_json(nmodl_ast, True, True, True))
+ with open(os.path.join(work_dir, "ast.js"), "w", encoding="utf-8") as outfile:
+ outfile.write(f"var astRoot = {json.dumps(json_data)};")
# open browser with ast
url = 'file://' + os.path.join(work_dir, "index.html")
--- a/nmodl/dsl.py
+++ b/nmodl/dsl.py
@@ -1,5 +1,5 @@
import os.path as osp
-from pkg_resources import *
+from importlib.resources import files
from ._nmodl import *
@@ -13,8 +13,9 @@
Returns:
List of available examples
"""
- if resource_exists(__name__, RESOURCE_DIR) and resource_isdir(__name__, RESOURCE_DIR):
- return resource_listdir(__name__, RESOURCE_DIR)
+ path = files("nmodl") / RESOURCE_DIR
+ if path.exists() and path.is_dir():
+ return [result.name for result in path.glob("*.mod")]
else:
raise FileNotFoundError("Could not find sample directory")
@@ -31,8 +32,8 @@
Returns:
List of available examples
"""
- resource = osp.join(RESOURCE_DIR, example)
- if resource_exists(__name__, resource):
- return resource_string(__name__, resource)
+ path = files("nmodl") / RESOURCE_DIR / example
+ if path.exists():
+ return path.read_text()
else:
raise FileNotFoundError("Could not find sample mod files")
--- a/pywheel/shim/_binwrapper.py
+++ b/pywheel/shim/_binwrapper.py
@@ -7,27 +7,17 @@
import os
import sys
import stat
-from pkg_resources import working_set
+from importlib.metadata import metadata, PackageNotFoundError
+from importlib.resources import files
from find_libpython import find_libpython
def _config_exe(exe_name):
"""Sets the environment to run the real executable (returned)"""
- package_name = "nmodl"
-
- if package_name not in working_set.by_key:
- print ("INFO : Using nmodl-nightly Package (Developer Version)")
- package_name = 'nmodl-nightly'
-
- assert (
- package_name in working_set.by_key
- ), "NMODL package not found! Verify PYTHONPATH"
-
- NMODL_PREFIX = os.path.join(working_set.by_key[package_name].location, "nmodl")
- NMODL_HOME = os.path.join(NMODL_PREFIX, ".data")
- NMODL_BIN = os.path.join(NMODL_HOME, "bin")
- NMODL_LIB = os.path.join(NMODL_HOME, "lib")
+ NMODL_PREFIX = files("nmodl")
+ NMODL_HOME = NMODL_PREFIX / ".data"
+ NMODL_BIN = NMODL_HOME / "bin"
# add pywrapper path to environment
if sys.platform == "darwin":
@@ -39,10 +29,12 @@
os.environ["NMODL_PYLIB"] = find_libpython()
# add nmodl home to environment (i.e. necessary for nrnunits.lib)
- os.environ["NMODLHOME"] = NMODL_HOME
+ os.environ["NMODLHOME"] = str(NMODL_HOME)
# set PYTHONPATH for embedded python to properly find the nmodl module
- os.environ["PYTHONPATH"] = working_set.by_key[package_name].location + ':' + os.environ.get("PYTHONPATH", "")
+ os.environ["PYTHONPATH"] = (
+ str(NMODL_PREFIX.parent) + ":" + os.environ.get("PYTHONPATH", "")
+ )
return os.path.join(NMODL_BIN, exe_name)
--- /dev/null
+++ b/test/unit/pybind/test_examples.py
@@ -0,0 +1,22 @@
+from json import loads
+from pathlib import Path
+
+from nmodl import dsl
+
+
+def test_example():
+ """
+ Test for the Python API from example
+ """
+
+ examples = dsl.list_examples()
+
+ # ordering may be off so we use a set
+ assert set(examples) == {"exp2syn.mod", "expsyn.mod", "hh.mod", "passive.mod"}
+
+ driver = dsl.NmodlDriver()
+ for example in examples:
+ nmodl_string = dsl.load_example(example)
+ # make sure we can parse the string
+ assert driver.parse_string(nmodl_string)
+
|