Skip to content

Commit

Permalink
Fix: find edition when config only stata_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
hugetim committed Oct 2, 2023
1 parent a6f2396 commit a4c68fc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 70 deletions.
102 changes: 65 additions & 37 deletions nbs/01_config.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,16 @@
"outputs": [],
"source": [
"#| export\n",
"def _win_find_path():\n",
"def _win_find_path(_dir=None):\n",
" import winreg\n",
" dirs = [r'C:\\Program Files\\Stata19',\n",
" r'C:\\Program Files\\Stata18',\n",
" r'C:\\Program Files\\Stata17']\n",
" for _dir in dirs:\n",
" path = Path(_dir)\n",
" if _dir is None:\n",
" dirs = [r'C:\\Program Files\\Stata19',\n",
" r'C:\\Program Files\\Stata18',\n",
" r'C:\\Program Files\\Stata17']\n",
" else:\n",
" dirs = [_dir]\n",
" for this_dir in dirs:\n",
" path = Path(this_dir)\n",
" if os.path.exists(path):\n",
" executables = [exe for exe in path.glob(\"Stata*.exe\") if exe not in set(path.glob(\"Stata*_old.exe\"))]\n",
" if executables:\n",
Expand Down Expand Up @@ -141,15 +144,17 @@
"outputs": [],
"source": [
"#| export\n",
"def _mac_find_path():\n",
"def _mac_find_path(_dir=None):\n",
" \"\"\"\n",
" Attempt to find Stata path on macOS when not on user's PATH.\n",
" Modified from stata_kernel's original to only location \"Applications/Stata\". \n",
"\n",
" Returns:\n",
" (str): Path to Stata. Empty string if not found.\n",
" \"\"\"\n",
" path = Path('/Applications/Stata')\n",
" if _dir is None:\n",
" _dir = '/Applications/Stata'\n",
" path = Path(_dir)\n",
" if not os.path.exists(path):\n",
" return ''\n",
" else:\n",
Expand Down Expand Up @@ -229,15 +234,15 @@
"outputs": [],
"source": [
"#| export\n",
"def _find_path():\n",
"def _find_path(_dir=None):\n",
" if os.getenv('CONTINUOUS_INTEGRATION'):\n",
" print('WARNING: Running as CI; Stata path not set correctly')\n",
" return 'stata'\n",
" path = ''\n",
" if platform.system() == 'Windows':\n",
" path = _win_find_path()\n",
" path = _win_find_path(_dir)\n",
" elif platform.system() == 'Darwin':\n",
" path = _mac_find_path()\n",
" path = _mac_find_path(_dir)\n",
" return path if path else _other_find_path()"
]
},
Expand Down Expand Up @@ -322,6 +327,42 @@
" find_dir_edition('')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a9230eae-f81c-430d-bfcd-7e2ff7e4d384",
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"def find_edition(stata_dir):\n",
" stata_path = _find_path(stata_dir)\n",
" stata_exe = str(os.path.basename(stata_path)).lower()\n",
" return _edition(stata_exe)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f17e3f6d-197a-4818-93c0-aca2be1223b3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'mp'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_eq(find_edition(''), \"be\")\n",
"find_edition('C:\\\\Program Files\\\\Stata18')"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -343,7 +384,7 @@
"output_type": "stream",
"text": [
"('C:\\\\Program Files\\\\Stata18', 'mp')\n",
"Elapsed time: 0.0004 seconds\n"
"Elapsed time: 0.0005 seconds\n"
]
}
],
Expand Down Expand Up @@ -381,7 +422,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Elapsed time: 0.0029 seconds\n"
"Elapsed time: 0.0028 seconds\n"
]
}
],
Expand Down Expand Up @@ -427,10 +468,11 @@
" \"\"\"\n",
" We modify stata_setup to make splash screen optional\n",
" \"\"\"\n",
" if stata_dir is None or edition is None:\n",
" path_found, edition_found = find_dir_edition()\n",
" stata_dir = path_found if stata_dir is None else stata_dir\n",
" if stata_dir is None:\n",
" stata_dir, edition_found = find_dir_edition()\n",
" edition = edition_found if edition is None else edition\n",
" elif edition is None:\n",
" edition = find_edition(stata_dir)\n",
" set_pystata_path(stata_dir)\n",
" import pystata\n",
" try:\n",
Expand Down Expand Up @@ -464,7 +506,7 @@
" graphic display True\n",
" graphic size width = default, height = default\n",
" graphic format svg\n",
"Elapsed time: 0.4793 seconds\n"
"Elapsed time: 1.3332 seconds\n"
]
}
],
Expand Down Expand Up @@ -629,7 +671,7 @@
"#| export\n",
"class Config:\n",
" env = {'stata_dir': None,\n",
" 'edition': 'be',\n",
" 'edition': None,\n",
" 'graph_format': 'png',\n",
" 'graph_width': '5.5in',\n",
" 'graph_height': '4in',\n",
Expand All @@ -638,6 +680,7 @@
" 'missing': '.',\n",
" }\n",
" valid_values_of = dict(\n",
" edition={None, 'mp', 'se', 'be'},\n",
" graph_format={'pystata', 'svg', 'png', 'pdf'},\n",
" echo={'True', 'False', 'None'},\n",
" splash={'True', 'False'},\n",
Expand All @@ -661,13 +704,6 @@
" self._update_backup_graph_size()\n",
" self.config_path = None\n",
" self._process_config_file()\n",
" if self.env['stata_dir'] is None or self.env['edition'] is None:\n",
" try: \n",
" stata_dir, stata_ed = find_dir_edition() \n",
" except OSError:\n",
" pass\n",
" else:\n",
" self.env.update({'stata_dir': stata_dir, 'edition': stata_ed})\n",
"\n",
" def _update_backup_graph_size(self):\n",
" self.backup_graph_size = {key: self.env[key] for key in {'graph_width', 'graph_height'}}\n",
Expand Down Expand Up @@ -707,7 +743,7 @@
" env.pop(key)\n",
" elif key in self.valid_values_of and env[key] not in self.valid_values_of[key]:\n",
" self.errors.append(\n",
" f\" '{key}' configuration invalid. \"\n",
" f\" '{key}' configuration invalid: '{env[key]}' is not a valid value. \"\n",
" f\"Reverting to: {key} = {self.env[key]}\"\n",
" )\n",
" env.pop(key)\n",
Expand Down Expand Up @@ -745,19 +781,11 @@
"id": "d9f66a87",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[31mConfiguration error in C:\\Users\\tjhuegerich\\.nbstata.conf:\n",
" No section: 'nbstata'\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"{'stata_dir': 'C:\\\\Program Files\\\\Stata18',\n",
" 'edition': 'mp',\n",
" 'edition': 'se',\n",
" 'graph_format': 'png',\n",
" 'graph_width': '5.5in',\n",
" 'graph_height': '4in',\n",
Expand Down Expand Up @@ -974,7 +1002,7 @@
"data": {
"text/plain": [
"{'stata_dir': 'C:\\\\Program Files\\\\Stata18',\n",
" 'edition': 'mp',\n",
" 'edition': 'se',\n",
" 'graph_format': 'png',\n",
" 'graph_width': '3',\n",
" 'graph_height': '4in',\n",
Expand Down Expand Up @@ -1011,7 +1039,7 @@
"data": {
"text/plain": [
"{'stata_dir': 'C:\\\\Program Files\\\\Stata18',\n",
" 'edition': 'mp',\n",
" 'edition': 'se',\n",
" 'graph_format': 'png',\n",
" 'graph_width': '3',\n",
" 'graph_height': '4in',\n",
Expand Down
1 change: 1 addition & 0 deletions nbstata/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
'nbstata.config._set_graph_size': ('config.html#_set_graph_size', 'nbstata/config.py'),
'nbstata.config._win_find_path': ('config.html#_win_find_path', 'nbstata/config.py'),
'nbstata.config.find_dir_edition': ('config.html#find_dir_edition', 'nbstata/config.py'),
'nbstata.config.find_edition': ('config.html#find_edition', 'nbstata/config.py'),
'nbstata.config.launch_stata': ('config.html#launch_stata', 'nbstata/config.py'),
'nbstata.config.set_graph_format': ('config.html#set_graph_format', 'nbstata/config.py'),
'nbstata.config.set_pystata_path': ('config.html#set_pystata_path', 'nbstata/config.py')},
Expand Down
Loading

0 comments on commit a4c68fc

Please sign in to comment.