Skip to content

Commit

Permalink
cli/_state: simplify code using pathlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun Persaud committed Oct 18, 2024
1 parent 57b5fbc commit fbc726a
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions src/pymcnp/cli/_state.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
import os
import sys
import inspect
from pathlib import Path
import sys
from typing import Final, Callable


DIR = os.getcwd() + '/.pymcnp/'
DIR = Path.getcwd() / '.pymcnp/'


def init():
if not os.path.isdir(DIR):
os.mkdir(DIR)
if not DIR.is_dir():
DIR.mkdir()

if not os.path.isfile(FileTable.PATH):
file = open(FileTable.PATH, 'w')
file.write('table = {}')
file.close()
if not FileTable.PATH.is_file():
FileTable.PATH.write_text('table = {}')

if not os.path.isfile(RunConfig.PATH):
file = open(RunConfig.PATH, 'w')
file.write(
if not RunConfig.PATH.is_file():
RunConfig.PATH.write_text(
"command = 'mcnp'\n\ndef prehook():\n return\n\ndef posthook():\n return\n"
)
file.close()


class FileTable:
DIR: Final[str] = os.getcwd() + '/.pymcnp/'
PATH: Final[str] = DIR + '_files.py'
DIR: Final[Path] = Path.cwd() / '.pymcnp'
PATH: Final[Path] = DIR / '_files.py'

def append(self, alias, path):
self = self._sync_up()
Expand Down Expand Up @@ -88,18 +84,16 @@ def _sync_down(self):

init()

file = open(FileTable.PATH, 'w')
file.write(f'table = {self._table.__repr__()}')
file.close()
FileTable.PATH.write_text(f'table = {self._table.__repr__()}')

def __iter__(self):
self = self._sync_up()
return iter(self._table.items())


class RunConfig:
DIR: Final[str] = os.getcwd() + '/.pymcnp/'
PATH: Final[str] = DIR + '_run.py'
DIR: Final[Path] = Path.cwd() / '.pymcnp'
PATH: Final[Path] = DIR / '_run.py'

def set_command(self, command: str):
self.command = command
Expand Down Expand Up @@ -151,11 +145,9 @@ def _sync_down(self):
prehook = 'def prehook():\n' + ''.join(inspect.getsourcelines(self.prehook)[0][1:])
posthook = 'def posthook():\n' + ''.join(inspect.getsourcelines(self.posthook)[0][1:])

file = open(RunConfig.PATH, 'w')
file.write(
RunConfig.PATH.write_text(
f'command = {self.command.__repr__()}\n' + '\n' + prehook + '\n' + posthook + '\n'
)
file.close()


table = FileTable()._sync_up()
Expand Down

0 comments on commit fbc726a

Please sign in to comment.