diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md index 46c1f24f..77fd90ba 100644 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ b/.github/ISSUE_TEMPLATE/bug_report.md @@ -2,36 +2,78 @@ name: Bug report about: Create a report to help us improve title: '' -labels: bug +labels: bugs assignees: '' --- - + **Describe the bug** + A clear and concise description of what the bug is. **To Reproduce** -Steps to reproduce the behavior: -1. Go to '...' -2. Click on '....' -3. Scroll down to '....' -4. See error -**Expected behavior** -A clear and concise description of what you expected to happen. +A short code snippet of what you have ran. Please change or remove any specific values or anything that can't be public. For example: + +``` python +problem = montepy.read_input("foo.imcnp") +``` + +**Error Message (if any)** + +If an error message was printed please include the entire stacktrace. If it includes any specific values please change or remove them. For example: + +``` python +In [6]: problem.cells.append(copy.deepcopy(cell)) +--------------------------------------------------------------------------- +NumberConflictError Traceback (most recent call last) +Cell In[6], line 1 +----> 1 problem.cells.append(copy.deepcopy(cell)) + +File ~/dev/montepy/montepy/numbered_object_collection.py:202, in NumberedObjectCollection.append(self, obj) + 200 raise TypeError(f"object being appended must be of type: {self._obj_class}") + 201 if obj.number in self.numbers: +--> 202 raise NumberConflictError( + 203 ( + 204 "There was a numbering conflict when attempting to add " + 205 f"{obj} to {type(self)}. Conflict was with {self[obj.number]}" + 206 ) + 207 ) + 208 else: + 209 self.__num_cache[obj.number] = obj -**Screenshots** -If applicable, add screenshots to help explain your problem. +NumberConflictError: There was a numbering conflict when attempting to add CELL: 3, mat: 3, DENS: 1.0 g/cm3 to . Conflict was with CELL: 3, mat: 3, DENS: 1.0 g/cm3 +``` + +**MCNP input file snippet** + +If applicable, please include a small section of the input file you were working on. If it includes any specific values please change or remove them. For example: + +``` +1 1 20 + -1000 $ dollar comment + imp:n,p=1 U=350 trcl=5 + +C surfaces +1000 SO 1 + +C data +C materials +C UO2 5 atpt enriched +m1 92235.80c 5 & +92238.80c 95 +``` + +**Version** -**Desktop (please complete the following information):** - - OS: [e.g. iOS] - Version [e.g. 0.2.5] **Additional context** + Add any other context about the problem here. diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md new file mode 100644 index 00000000..34bc845b --- /dev/null +++ b/.github/pull_request_template.md @@ -0,0 +1,21 @@ + + +# Description + +Please include a summary of the change and which issue is fixed if applicable. Please also include relevant motivation and context. + +Fixes # (issue) + +# Checklist + +- [ ] I have performed a self-review of my own code +- [ ] I have made corresponding changes to the documentation (if applicable) +- [ ] I have added tests that prove my fix is effective or that my feature works (if applicable) + diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index e75139c7..c4fc82f8 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -4,6 +4,7 @@ on: push: branches: [main] + jobs: last-minute-test: runs-on: ubuntu-latest @@ -13,13 +14,12 @@ jobs: uses: actions/setup-python@v4 with: python-version: 3.8 - - run: pip install --user -r requirements/dev.txt + - run: pip install . montepy[develop] - run: python -m pytest build-pages: environment: name: github-pages - url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest steps: - name: Configure git @@ -30,13 +30,67 @@ jobs: - uses: actions/setup-python@v4 with: python-version: 3.8 - - run: pip install --user montepy[doc] + - run: pip install --user . montepy[doc] - run: cd doc && make html - uses: actions/configure-pages@v4 - uses: actions/upload-pages-artifact@v3 with: name: deploy-pages path: doc/build/html/ + + build-packages: + name: Build, sign, and release packages on github + runs-on: ubuntu-latest + needs: [last-minute-test] + permissions: + contents: write # IMPORTANT: mandatory for making GitHub Releases + id-token: write # IMPORTANT: mandatory for sigstore + env: + GH_TOKEN: ${{ github.token }} + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true + - name: set up python 3.8 + uses: actions/setup-python@v4 + with: + python-version: 3.8 + - run: pip install . montepy[build] + - name: GitHub Actions Create Tag + id: tag_version + uses: mathieudutour/github-tag-action@v6.1 + with: + github_token: ${{ secrets.GITHUB_TOKEN }} + # ensure tags are up to date + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + fetch-tags: true + - run: python -m build . + - name: Sign the dists with Sigstore + uses: sigstore/gh-action-sigstore-python@v2.1.1 + with: + inputs: >- + ./dist/*.tar.gz + ./dist/*.whl + - name: Create a GitHub release + uses: ncipollo/release-action@v1 + with: + tag: ${{ steps.tag_version.outputs.new_tag }} + name: Release ${{ steps.tag_version.outputs.new_tag }} + body: ${{ steps.tag_version.outputs.changelog }} + draft: true + - run: >- + gh release upload + '${{ steps.tag_version.outputs.new_tag }}' dist/** + --repo '${{ github.repository }}' + - uses: actions/upload-artifact@v4 + with: + name: build + path: | + dist/*.tar.gz + dist/*.whl deploy-pages: permissions: @@ -65,18 +119,16 @@ jobs: environment: name: test-pypi url: https://test.pypi.org/p/montepy # Replace with your PyPI project name - needs: [deploy-pages] + needs: [deploy-pages, build-packages] permissions: contents: read id-token: write runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v4 + - uses: actions/download-artifact@v4 with: - python-version: 3.8 - - run: python -m pip install build - - run: python -m build --sdist --wheel + name: build + path: dist/ - name: Publish distribution 📦 to PyPI uses: pypa/gh-action-pypi-publish@release/v1 with: @@ -86,20 +138,19 @@ jobs: environment: name: pypi url: https://pypi.org/p/montepy # Replace with your PyPI project name - needs: [deploy-pages, deploy-test-pypi] + needs: [deploy-pages, deploy-test-pypi, build-packages] permissions: contents: read id-token: write runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-python@v4 + - uses: actions/download-artifact@v4 with: - python-version: 3.8 - - run: python -m pip install build - - run: python -m build --sdist --wheel + name: build + path: dist/ - name: Publish distribution 📦 to PyPI uses: pypa/gh-action-pypi-publish@release/v1 + diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d635a22b..10507e64 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,9 @@ -name: Test package +name: CI testing -on: [push] +on: + pull_request: + push: + branches: [develop, main, alpha-test] jobs: build: @@ -16,15 +19,20 @@ jobs: with: python-version: ${{ matrix.python-version }} - run: pip install --upgrade pip build - - run: pip install -r requirements/common.txt + - run: pip install build - run: python -m build --sdist --wheel - run: pip install . - run: pip uninstall -y montepy - run: pip install --user dist/*.whl + # run scripts + - run: change_to_ascii -h - run: pip uninstall -y montepy - run: pip install --user dist/*.tar.gz - - run: pip install --user montepy[test] - - run: pip install --user montepy[doc] + - run: pip install --user . montepy[test] + - run: pip install --user . montepy[doc] + - run: pip install --user . montepy[format] + - run: pip install --user . montepy[build] + - run: pip install --user . montepy[develop] - run: pip freeze - name: Upload build artifacts uses: actions/upload-artifact@v3 @@ -34,6 +42,7 @@ jobs: test: runs-on: ubuntu-latest + permissions: write-all strategy: matrix: python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"] @@ -44,20 +53,35 @@ jobs: uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - - run: pip install --user -r requirements/dev.txt + - run: pip install --user . montepy[test] - run: coverage run -m pytest --junitxml=test_report.xml - run: coverage report - run: coverage xml - name: Upload test report - uses: actions/upload-artifact@v3 + if: ${{ matrix.python-version == '3.9' }} + uses: actions/upload-artifact@v4 with: name: test path: test_report.xml - name: Upload coverage report - uses: actions/upload-artifact@v3 + if: ${{ matrix.python-version == '3.9' }} + uses: actions/upload-artifact@v4 with: name: coverage path: coverage.xml + - name: Test Reporter + if: ${{ matrix.python-version == '3.9' }} + uses: dorny/test-reporter@v1.7.0 + with: + name: CI-test-report + path: test_report.xml + reporter: java-junit + - name: Coveralls GitHub Action + if: ${{ matrix.python-version == '3.9' }} + uses: coverallsapp/github-action@v2.2.3 + with: + file: coverage.xml + doc-test: runs-on: ubuntu-latest @@ -68,13 +92,17 @@ jobs: uses: actions/setup-python@v4 with: python-version: 3.8 - - run: pip install montepy[doc] + - run: pip install . montepy[doc] - run: sphinx-build doc/source/ doc/build/ -W --keep-going -E - run: sphinx-build -b html doc/source/ doc/build/html - uses: actions/upload-artifact@v3 with: name: website path: doc/build/html + - name: Test for missing API documentation + run: | + cd doc/source + python _test_for_missing_docs.py format-test: runs-on: ubuntu-latest @@ -85,7 +113,7 @@ jobs: uses: actions/setup-python@v4 with: python-version: 3.8 - - run: pip install --user -r requirements/dev.txt + - run: pip install . montepy[format] - run: black --check montepy/ tests/ diff --git a/.gitignore b/.gitignore index 18762426..990257f6 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ doc/build/* .coverage .idea/ .ipynb_checkpoints/ +montepy/_version.py diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml deleted file mode 100644 index d486b16a..00000000 --- a/.gitlab-ci.yml +++ /dev/null @@ -1,167 +0,0 @@ -image: python:latest - -include: - - templates/install.yml - - templates/test.yml - -variables: - PYTHON_VER: "3.8" - CONDA_BASE_NAME: experiment_analysis_montepy - -default: - artifacts: - expire_in: 7 day - -after_script: - - export PATH=$(echo "$PATH" | sed -e 's/:\/data\/gitlab-runner\/.conda\/envs\/experiment_analysis_mcnpy\/$//') - - echo $PATH - - conda deactivate - -.job_template: &install-boiler - stage: build - extends: .install - when: delayed - start_in: 1 minutes - variables: - PYTHON_VER: "3.8" - -install-3.8: - stage: build - extends: .install - variables: - PYTHON_VER: "3.8" - -install-3.9: - <<: *install-boiler - variables: - PYTHON_VER: "3.9" - - -install-3.10: - <<: *install-boiler - variables: - PYTHON_VER: "3.10" - -install-3.11: - <<: *install-boiler - variables: - PYTHON_VER: "3.11" - -install-3.12: - <<: *install-boiler - variables: - PYTHON_VER: "3.12" - -coverage_unit_test: - stage: test - coverage: '/TOTAL.+ ([0-9\.]{1,5}%)/' - script: - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - echo $PATH - - echo "Executing unit tests" - - which python - - pip freeze - - coverage run -m pytest --junitxml=test_report.xml - - coverage report - - coverage xml - artifacts: - reports: - junit: test_report.xml - coverage_report: - coverage_format: cobertura - path: coverage.xml - needs: ["install-3.8"] - -# skip 3.8 because of coverage test - -.job_template: &test_config - stage: test - extends: .test_no_cover - -unit_test-3.9: - <<: *test_config - variables: - PYTHON_VER: "3.9" - needs: ["coverage_unit_test","install-3.9"] - -unit_test-3.10: - <<: *test_config - variables: - PYTHON_VER: "3.10" - needs: ["coverage_unit_test","install-3.10"] - -unit_test-3.11: - <<: *test_config - variables: - PYTHON_VER: "3.11" - needs: ["coverage_unit_test","install-3.11"] - -unit_test-3.12: - <<: *test_config - variables: - PYTHON_VER: "3.12" - needs: ["coverage_unit_test","install-3.12"] - -doc_test: - stage: test - script: - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - echo "Executing Sphinx build to detect syntax errors" - - sphinx-build doc/source/ doc/build/ -W --keep-going -E - - sphinx-build -b html doc/source/ doc/build/html - - mv doc/build/html html - needs: ["install-3.8"] - artifacts: - expose_as: 'Updated Documentation' - paths: - - html - expire_in: 1 day - -format_test: - stage: test - script: - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - echo "Testing if anything is unformatted" - - echo "If error occurs changes were not formatted using black" - - black --check montepy/ tests/ - needs: ["install-3.8"] - - - -pages: - stage: deploy - script: - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - echo "Building documentation with Sphinx." - - cd doc/ - - make html - - cd .. - - mv doc/build/html public - - python -m build --sdist --wheel - - mv dist/ public/dist - artifacts: - paths: - - public - only: - - master - - main - - doc - - -packaging: - stage: deploy - script: - - rm dist/* - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - pip install build twine - - python -m build --sdist --wheel - - TWINE_PASSWORD=${CI_JOB_TOKEN} TWINE_USERNAME=gitlab-ci-token python -m twine upload --repository-url ${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/pypi dist/* - - only: - - master - - main diff --git a/README.md b/README.md index 8eee6eff..860b3edb 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,11 @@ MontePY: a cute snek on a red over white circle -A python library to read, edit, and write MCNP input files. +[![license](https://img.shields.io/github/license/idaholab/MontePy.svg)](https://github.com/idaholab/MontePy/blob/develop/LICENSE) +[![Coverage Status](https://coveralls.io/repos/github/idaholab/MontePy/badge.svg?branch=develop)](https://coveralls.io/github/idaholab/MontePy?branch=develop) +[![PyPI version](https://badge.fury.io/py/montepy.svg)](https://badge.fury.io/py/montepy) + +MontePy is a python library to read, edit, and write MCNP input files. ## Installing @@ -23,7 +27,7 @@ There is also a developer's guide covering the design and approach of MontePy, a * Can parse the following surfaces exactly P(X|Y|Z), C(X|Y|Z), C/(X|Y|Z) (I mean it can do PX, and PY, etc.) * Can read in all other inputs but not understand them * Can write out full MCNP problem even if it doesn't fully understand an input. -* Can write out the MCNP problem verbatim, and try to match +* Can write out the MCNP problem verbatim, and try to match the original user formatting. * Can quickly access cells, surfaces, and materials by their numbers. For example: `cell = problem.cells[105]`. * Can quickly update cell importances. For example `cell.importance.neutron = 2.0`. * Has over 240 test cases right now diff --git a/doc/source/_test_for_missing_docs.py b/doc/source/_test_for_missing_docs.py new file mode 100644 index 00000000..f0dbf9a7 --- /dev/null +++ b/doc/source/_test_for_missing_docs.py @@ -0,0 +1,34 @@ +import glob +import os +import sys +import warnings + +ignored = {"__pycache__", "_version.py", "__main__.py", "_cell_data_control.py"} + +base = os.path.join("..", "..") + + +def crawl_path(rel_path): + missing = False + for f in os.listdir(os.path.join(base, rel_path)): + f_name = os.path.join(rel_path, f) + if f in ignored: + continue + if os.path.isdir(os.path.join(base, f_name)): + crawl_path(f_name) + elif os.path.isfile(os.path.join(base, f_name)) and ".py" in f: + if f == "__init__.py": + path = f_name.replace("/", ".").replace(".__init__.py", ".rst") + else: + path = f_name.replace("/", ".").replace(".py", ".rst") + if not os.path.exists(os.path.join("api", path)): + missing = True + warnings.warn( + f"Missing sphinx documentation for {os.path.join(rel_path, f)}" + ) + return missing + + +missing = crawl_path("montepy") +if missing: + sys.exit(314) diff --git a/doc/source/api/montepy.data_inputs.rst b/doc/source/api/montepy.data_inputs.rst index e4248fb2..96bdae1e 100644 --- a/doc/source/api/montepy.data_inputs.rst +++ b/doc/source/api/montepy.data_inputs.rst @@ -26,6 +26,8 @@ Submodules montepy.data_inputs.material_component montepy.data_inputs.mode montepy.data_inputs.thermal_scattering + montepy.data_inputs.tally + montepy.data_inputs.tally_multiplier montepy.data_inputs.transform montepy.data_inputs.universe_input montepy.data_inputs.volume diff --git a/doc/source/api/montepy.data_inputs.tally.rst b/doc/source/api/montepy.data_inputs.tally.rst new file mode 100644 index 00000000..dc16f381 --- /dev/null +++ b/doc/source/api/montepy.data_inputs.tally.rst @@ -0,0 +1,8 @@ +montepy.data\_inputs.tally module +================================= + + +.. automodule:: montepy.data_inputs.tally + :members: + :undoc-members: + :show-inheritance: diff --git a/doc/source/api/montepy.data_inputs.tally_multiplier.rst b/doc/source/api/montepy.data_inputs.tally_multiplier.rst new file mode 100644 index 00000000..f38e1552 --- /dev/null +++ b/doc/source/api/montepy.data_inputs.tally_multiplier.rst @@ -0,0 +1,8 @@ +montepy.data\_inputs.tally_multiplier module +============================================ + + +.. automodule:: montepy.data_inputs.tally_multiplier + :members: + :undoc-members: + :show-inheritance: diff --git a/doc/source/api/montepy.input_parser.input_file.rst b/doc/source/api/montepy.input_parser.input_file.rst new file mode 100644 index 00000000..44ebfc41 --- /dev/null +++ b/doc/source/api/montepy.input_parser.input_file.rst @@ -0,0 +1,10 @@ +montepy.input\_parser.input\_file module +======================================== + + + +.. automodule:: montepy.input_parser.input_file + :members: + :undoc-members: + :show-inheritance: + :private-members: _convert_to_int, _convert_to_enum diff --git a/doc/source/api/montepy.input_parser.rst b/doc/source/api/montepy.input_parser.rst index 5ffd32e2..fc837495 100644 --- a/doc/source/api/montepy.input_parser.rst +++ b/doc/source/api/montepy.input_parser.rst @@ -16,6 +16,7 @@ Submodules montepy.input_parser.block_type montepy.input_parser.cell_parser montepy.input_parser.data_parser + montepy.input_parser.input_file montepy.input_parser.input_reader montepy.input_parser.input_syntax_reader montepy.input_parser.mcnp_input @@ -24,5 +25,6 @@ Submodules montepy.input_parser.shortcuts montepy.input_parser.surface_parser montepy.input_parser.syntax_node + montepy.input_parser.tally_parser montepy.input_parser.thermal_parser montepy.input_parser.tokens diff --git a/doc/source/api/montepy.input_parser.tally_parser.rst b/doc/source/api/montepy.input_parser.tally_parser.rst new file mode 100644 index 00000000..eaf525b2 --- /dev/null +++ b/doc/source/api/montepy.input_parser.tally_parser.rst @@ -0,0 +1,8 @@ +montepy.input\_parser.tally\_parser module +========================================== + + +.. automodule:: montepy.input_parser.tally_parser + :members: + :undoc-members: + :show-inheritance: diff --git a/doc/source/developing.rst b/doc/source/developing.rst index c6ec53a2..5d9c2485 100644 --- a/doc/source/developing.rst +++ b/doc/source/developing.rst @@ -77,19 +77,40 @@ Setting up and Typical Development Workflow Deploy Process ^^^^^^^^^^^^^^ -MontePy currently does not use a continuous deploy process. -Rather changes are staged on the ``develop`` branch prior to a release. +MontePy currently does not use a continuous deploy (CD) process. +Changes are staged on the ``develop`` branch prior to a release. Both ``develop`` and ``main`` are protected branches. -``main`` should only be used for releases. +``main`` is only be used for releases. If someone clones ``main`` they will get the most recent official release. Only a select few core-developers are allowed to approve a merge to ``main`` and therefore a new release. -``develop`` should be production quality code that has been approved for release, +``develop`` is for production quality code that has been approved for release, but is waiting on the next release. So all new features and bug fixes must first be merged onto ``develop``. The expectation is that features once merged onto ``develop`` are stable, well tested, well documented, and well-formatted. +Automated Versioning +^^^^^^^^^^^^^^^^^^^^ + +As part of the CD process a new version number is created. +The `GitHub action `_ that does this goes through the following process: + +1. Finds the last release version as git tags. +2. Analyzes all commit messages since then to determine if this is a Major, Minor, or Patch release. +3. Creates a tag with the apropriately incremented new release version. + +This means that git commit messages needs to convey the appropriate level of information. +The library uses `angular's commit message conventions `_. +This convention will not be enforced for all commits, +but will be for all merge commits from Pull Requests. + +Additional References: + +1. `github action `_ +1. `angular's commit message conventions `_ +1. `Semantic versioning standard `_ + Merge Checklist ^^^^^^^^^^^^^^^ diff --git a/doc/source/faq.rst b/doc/source/faq.rst new file mode 100644 index 00000000..b075d03b --- /dev/null +++ b/doc/source/faq.rst @@ -0,0 +1,70 @@ +Frequently Asked Questions +========================== + +Or more likely Frequent Error Debugging. + +Encoding Errors: UnicodeDecodeError +----------------------------------- + +If you received the error below while opening a file in MontePy, +there is like a non-ASCII character in your input file. +You can read more about :ref:`Character Encoding here `. + +To solve this problem you can: + +1. Try another encoding such as ``'utf8'`` or ``'cp1252'``. Pass it as an argument to :func:`~montepy.input_parser.input_reader.read_input`. +2. Remove all non-ASCII characters with :ref:`the change_to_ascii utility ` + +.. code-block:: python + + --------------------------------------------------------------------------- + UnicodeDecodeError Traceback (most recent call last) + Cell In[2], line 1 + ----> 1 problem = montepy.read_input("tests/inputs/bad_encoding.imcnp") + + File ~/dev/montepy/montepy/input_parser/input_reader.py:35, in read_input(input_file, mcnp_version, encoding) + 33 problem = mcnp_problem.MCNP_Problem(input_file) + 34 problem.mcnp_version = mcnp_version + ---> 35 problem.parse_input(encoding=encoding) + 36 return problem + + File ~/dev/montepy/montepy/mcnp_problem.py:262, in MCNP_Problem.parse_input(self, check_input, encoding) + 253 OBJ_MATCHER = { + 254 block_type.BlockType.CELL: (Cell, self._cells), + 255 block_type.BlockType.SURFACE: ( + (...) + 259 block_type.BlockType.DATA: (parse_data, self._data_inputs), + 260 } + 261 try: + --> 262 for i, input in enumerate( + 263 input_syntax_reader.read_input_syntax( + 264 self._input_file, self.mcnp_version, encoding=encoding + 265 ) + 266 ): + 267 self._original_inputs.append(input) + 268 if i == 0 and isinstance(input, mcnp_input.Message): + + File ~/dev/montepy/montepy/input_parser/input_syntax_reader.py:48, in read_input_syntax(input_file, mcnp_version, encoding) + 46 reading_queue = deque() + 47 with input_file.open("r", encoding=encoding) as fh: + ---> 48 yield from read_front_matters(fh, mcnp_version) + 49 yield from read_data(fh, mcnp_version) + + File ~/dev/montepy/montepy/input_parser/input_syntax_reader.py:79, in read_front_matters(fh, mcnp_version) + 77 lines = [] + 78 raw_lines = [] + ---> 79 for i, line in enumerate(fh): + 80 if i == 0 and line.upper().startswith("MESSAGE:"): + 81 is_in_message_block = True + + File ~/dev/montepy/montepy/input_parser/input_file.py:95, in MCNP_InputFile.__iter__(self) + 94 def __iter__(self): + ---> 95 for lineno, line in enumerate(self._fh): + 96 self._lineno = lineno + 1 + 97 yield line + + File ~/mambaforge/lib/python3.10/encodings/ascii.py:26, in IncrementalDecoder.decode(self, input, final) + 25 def decode(self, input, final=False): + ---> 26 return codecs.ascii_decode(input, self.errors)[0] + + UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 159: ordinal not in range(128) diff --git a/doc/source/index.rst b/doc/source/index.rst index be171a76..31077195 100644 --- a/doc/source/index.rst +++ b/doc/source/index.rst @@ -12,12 +12,18 @@ Welcome to MontePy's documentation! starting + utilities + tricks + faq + developing api/modules + publications + See Also ======== diff --git a/doc/source/publications.rst b/doc/source/publications.rst new file mode 100644 index 00000000..c8820728 --- /dev/null +++ b/doc/source/publications.rst @@ -0,0 +1,15 @@ +Publications +============ + +Permanent Link to Software +-------------------------- + +* `DOI:10.11578/dc.20240115.1 `_ + + +Journals and Conference Papers +------------------------------ + +Currently we don't have any articles to show here. +If you have an interesting paper that uses MontePy, +open an issue and we may add it here. diff --git a/doc/source/utilities.rst b/doc/source/utilities.rst new file mode 100644 index 00000000..4d42e3ad --- /dev/null +++ b/doc/source/utilities.rst @@ -0,0 +1,115 @@ +Utility Scripts +=============== + +Package Level Execution Options +------------------------------- +.. code-block:: console + + usage: montepy [-h] [-c [input_file ...]] + + Tool for editing and working with MCNP input files. + + options: + -h, --help show this help message and exit + -c [input_file ...], --check [input_file ...] + Check the given input file(s) for errors. Accepts globs, and multiple arguments. + +Checking Input Files for Errors +------------------------------- +MontePy can be used to check for errors that it will check for. +MontePy will check for: + +* general syntax errors +* syntax errors for all MCNP Objects supported (e.g., cells, surfaces, materials, etc.) +* Bad references to other object when the object referring to another object is supported. +* Bad mode options + +It will print all errors it found in the input to the terminal. + +To use this run: + +.. code-block:: console + + python -m montepy -c [files] + +.. _convert_ascii: + +Converting Encoding to ASCII +---------------------------- + +.. _ascii_command: + +Command Line Options +++++++++++++++++++++ +.. code-block:: console + + usage: Change_to_ascii [-h] [-d | -w] in_file out_file + + Change the encoding of a file to strict ASCII. Everything not compliant will be removed. + + positional arguments: + in_file The input file to convert + out_file The input file to convert + + options: + -h, --help show this help message and exit + -d, --delete Delete any non-ascii characters. This is the default. + -w, --whitespace Replace non-ascii characters with a space. + + +.. _encoding_background: + +Background +++++++++++ +`Character encoding `_ is the process of representing all characters as numbers, +so they may be used by a computer. +It is the bane of almost all programmers. + +The `American Standard Code for Information Interchange (ASCII) `_ is one of the oldest, +and simplest encoding standards. +It uses one byte per character, +and only goes from 0 – 127. +This has some issues, being very American-centric, +and also only allowing 128 characters, +52 of them being the English alphabet. +One solution to this was `"Extended ASCII" `_, +which used the final bit, and allowed the encoding system +to include 0 – 255. +There isn't one "Extended ASCII", +but one of the most popular encodings is Windows CP-1252. +This isn't great. + +The most commonly used encoding now is `UTF-8 `_, or "unicode". +UTF-8 can support almost any printable character in any language, including emojis. +The complexity is that each character is a variable-length of bytes. +This means that older software, like fortran, may get confused by it. + +As far as I can tell MCNP does not document what encoding it uses. +ASCII is the most conservative bet, +so MontePy by default tries to read input files in strict ASCII. + +Dealing with Encoding Issues +++++++++++++++++++++++++++++ + +You are likely here because you got an error message something like this: + +>>> montepy.read_input("example.imcnp") +UnicodeDecodeError Traceback (most recent call last) + +UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 1132: ordinal not in range(128) + +You can either change the encoding used by :func:`~montepy.input_parser.input_reader.read_input`, +or just force the entire file to be strictly ASCII. + +MontePY offers the ``change_to_ascii`` script. +The options are listed above: :ref:`ascii_command`. +For any non-ASCII character it will either remove +the character or replace it with a space (``' '``). +It defaults to deleting. +To replace it with a space instead use ``-w``. +Otherwise the arguments are the input file to correct, +and the path to write the output file to. + +.. code-block:: console + + change_to_ascii -w unicode_input.imcnp ascii_input.imcnp diff --git a/montepy/__init__.py b/montepy/__init__.py index ac195ceb..93cf0e4f 100644 --- a/montepy/__init__.py +++ b/montepy/__init__.py @@ -23,7 +23,19 @@ from montepy.universe import Universe import sys -__version__ = "0.2.5" + +try: + from . import _version + + __version__ = _version.version +except ImportError: + try: + from setuptools_scm import get_version + + __version__ = get_version() + except ImportError: + __version__ = "Undefined" + # enable deprecated warnings for users if not sys.warnoptions: diff --git a/montepy/__main__.py b/montepy/__main__.py index ae39fb05..5141ea4b 100644 --- a/montepy/__main__.py +++ b/montepy/__main__.py @@ -14,7 +14,7 @@ """ -def define_args(args): +def define_args(args=None): """ Sets and parses the command line arguments. @@ -34,6 +34,12 @@ def define_args(args): help="Check the given input file(s) for errors. Accepts globs, and multiple arguments.", metavar="input_file", ) + parser.add_argument( + "-v", + "--version", + action="store_true", + help="Print the version number", + ) args = parser.parse_args(args) return args @@ -58,9 +64,11 @@ def main(): # pragma: no cover """ The main function """ - args = define_args(sys.argv[1:]) - if "check" in args: + args = define_args() + if args.check: check_inputs(args.check) + if args.version: + print(montepy.__version__) if __name__ == "__main__": # pragma: no cover diff --git a/montepy/_scripts/__init__.py b/montepy/_scripts/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/montepy/_scripts/change_to_ascii.py b/montepy/_scripts/change_to_ascii.py new file mode 100644 index 00000000..0f056d7e --- /dev/null +++ b/montepy/_scripts/change_to_ascii.py @@ -0,0 +1,87 @@ +import argparse +import sys + + +def define_args(args): + """ + Parses the arguments from the command line. + + :param args: the arguments from the command line. + :type args: list + :returns: the parsed arguments (with argparse) + :rtype: argparse.Namespace + """ + parser = argparse.ArgumentParser( + prog="Change_to_ascii", + description="Change the encoding of a file to strict ASCII. Everything not compliant will be removed.", + ) + group = parser.add_mutually_exclusive_group() + group.add_argument( + "-d", + "--delete", + dest="delete", + action="store_true", + help="Delete any non-ascii characters. This is the default.", + ) + group.add_argument( + "-w", + "--whitespace", + dest="whitespace", + action="store_true", + help="Replace non-ascii characters with a space.", + ) + parser.add_argument("in_file", nargs=1, help="The input file to convert") + parser.add_argument("out_file", nargs=1, help="The input file to convert") + args = parser.parse_args(args) + return args + + +def strip_characters(args): + """ + Strips non-ascii characters from the input file, and writes out the output file. + + :param args: the parsed command line arguments. + :type args: argparse.Namespace + """ + if args.whitespace: + replacer = " " + elif args.delete: + replacer = "" + # default to delete + else: + replacer = "" + with open(args.in_file[0], "rb") as in_fh, open(args.out_file[0], "wb") as out_fh: + for line in in_fh: + utf8_line = line.decode(encoding="utf8", errors="replace") + utf8_line = utf8_line.replace("�", replacer) + + try: + out_fh.write(utf8_line.encode(encoding="ascii", errors="strict")) + except UnicodeError as e: + new_line = [] + # find the bad characters character by character + for char in utf8_line: + if ord(char) > 128: + new_line.append(replacer) + else: + new_line.append(char) + out_fh.write( + "".join(new_line).encode(encoding="ascii", errors="strict") + ) + + +def main(args=None): + """ + Main runner function. + + :param args: The arguments passed from the command line. + :type args: list + """ + if args is None: + args = sys.argv[1:] + args = define_args(args) + strip_characters(args) + + +if __name__ == "__main__": + main(sys.argv[1:]) diff --git a/montepy/constants.py b/montepy/constants.py index 60bac6ec..3bf4d8b3 100644 --- a/montepy/constants.py +++ b/montepy/constants.py @@ -40,6 +40,13 @@ How many spaces a tab is expand to. """ +ASCII_CEILING = 127 +""" +The maximum allowed code point allowed by ASCII. + +Source: `Wikipedia `_ +""" + def get_max_line_length(mcnp_version=DEFAULT_VERSION): """ diff --git a/montepy/data_inputs/fill.py b/montepy/data_inputs/fill.py index cd18f1fa..82e6d5d6 100644 --- a/montepy/data_inputs/fill.py +++ b/montepy/data_inputs/fill.py @@ -525,9 +525,11 @@ def _value_node_generator(): payload.append(self.universes[i][j][k].number) else: payload = [ - self.universe.number - if self.universe is not None - else self.old_universe_number + ( + self.universe.number + if self.universe is not None + else self.old_universe_number + ) ] try: start_transform = new_vals.index("(") diff --git a/montepy/data_inputs/material.py b/montepy/data_inputs/material.py index 022c20bc..44516538 100644 --- a/montepy/data_inputs/material.py +++ b/montepy/data_inputs/material.py @@ -116,6 +116,21 @@ def cells(self): if cell.material == self: yield cell + def format_for_mcnp_input(self, mcnp_version): + """ + Creates a string representation of this MCNP_Object that can be + written to file. + + :param mcnp_version: The tuple for the MCNP version that must be exported to. + :type mcnp_version: tuple + :return: a list of strings for the lines that this input will occupy. + :rtype: list + """ + lines = super().format_for_mcnp_input(mcnp_version) + if self.thermal_scattering is not None: + lines += self.thermal_scattering.format_for_mcnp_input(mcnp_version) + return lines + def add_thermal_scattering(self, law): """ Adds thermal scattering law to the material diff --git a/montepy/input_parser/cell_parser.py b/montepy/input_parser/cell_parser.py index 567c9963..8585a649 100644 --- a/montepy/input_parser/cell_parser.py +++ b/montepy/input_parser/cell_parser.py @@ -75,7 +75,7 @@ def union(self, p): def geometry_expr(self, p): left = p.geometry_expr right = p.geometry_term - nodes = {"left": left.nodes, "operator": p.union, "right": right.nodes} + nodes = {"left": left, "operator": p.union, "right": right} return syntax_node.GeometryTree("union", nodes, ":", left, right) @_("geometry_term") @@ -112,7 +112,7 @@ def geometry_term(self, p): for node in node_iter: new_tree = syntax_node.GeometryTree( "intersection", - {"left": left, "operator": None, "right": node}, + {"left": left, "operator": syntax_node.PaddingNode(), "right": node}, "*", left, node, @@ -142,7 +142,10 @@ def geometry_factor(self, p): @_("COMPLEMENT geometry_factory") def geometry_factor(self, p): - nodes = {"operator": p.COMPLEMENT, "left": p.geometry_factory} + nodes = { + "operator": syntax_node.PaddingNode(p.COMPLEMENT), + "left": p.geometry_factory, + } return syntax_node.GeometryTree( "complement", nodes, diff --git a/montepy/input_parser/input_file.py b/montepy/input_parser/input_file.py index c69bf8af..0ef736ce 100644 --- a/montepy/input_parser/input_file.py +++ b/montepy/input_parser/input_file.py @@ -1,5 +1,6 @@ # Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved. import itertools as it +from montepy.constants import ASCII_CEILING from montepy.utilities import * @@ -20,6 +21,8 @@ def __init__(self, path, parent_file=None): self._path = path self._parent_file = parent_file self._lineno = 1 + self._replace_with_space = False + self._mode = None self._fh = None @make_prop_pointer("_path") @@ -57,7 +60,7 @@ def lineno(self): """ pass - def open(self, mode): + def open(self, mode, encoding="ascii", replace=True): """ Opens the underlying file, and returns self. @@ -65,11 +68,29 @@ def open(self, mode): For this reason, a ``close`` functional is intentionally not provided. + + .. Note:: + For different encoding schemes see the available list + `here `_. + + CP1252 is commonly referred to as "extended-ASCII". + You may have success with this encoding for working with special characters. + :param mode: the mode to open the file in :type mode: str + :param encoding: The encoding scheme to use. If replace is true, this is ignored, and changed to ASCII + :type encoding: str + :param replace: replace all non-ASCII characters with a space (0x20) + :type replace: bool :returns: self """ - self._fh = open(self.path, mode, encoding="ascii") + if "r" in mode: + if replace: + self._replace_with_space = True + mode = "rb" + encoding = None + self._mode = mode + self._fh = open(self.path, mode, encoding=encoding) return self def __enter__(self): @@ -84,12 +105,23 @@ def __exit__(self, exc_type, exc_val, exc_tb): def __iter__(self): for lineno, line in enumerate(self._fh): self._lineno = lineno + 1 + if self._mode == "rb" and self._replace_with_space: + line = self._clean_line(line) yield line + @staticmethod + def _clean_line(line): + new_line = bytes([code if code < ASCII_CEILING else ord(" ") for code in line]) + line = new_line.decode("ascii") + line = line.replace("\r\n", "\n").replace("\r", "\n") + return line + def read(self, size=-1): """ """ if self._fh: ret = self._fh.read(size) + if self._mode == "rb" and self._replace_with_space: + ret = self._clean_line(ret) self._lineno += ret.count("\n") return ret @@ -97,6 +129,8 @@ def readline(self, size=-1): """ """ if self._fh: ret = self._fh.readline(size) + if self._mode == "rb" and self._replace_with_space: + ret = self._clean_line(ret) self._lineno += ret.count("\n") return ret diff --git a/montepy/input_parser/input_reader.py b/montepy/input_parser/input_reader.py index 3d32245b..400d5e1f 100644 --- a/montepy/input_parser/input_reader.py +++ b/montepy/input_parser/input_reader.py @@ -3,17 +3,20 @@ from montepy.constants import DEFAULT_VERSION -def read_input(input_file, mcnp_version=DEFAULT_VERSION): +def read_input(input_file, mcnp_version=DEFAULT_VERSION, replace=True): """ Reads the specified MCNP Input file. The MCNP version must be a three component tuple e.g., (6, 2, 0) and (5, 1, 60). + :param input_file: the path to the input file to read. :type input_file: str :param mcnp_version: The version of MCNP that the input is intended for. :type mcnp_version: tuple :returns: The MCNP_Problem instance representing this file. + :param replace: replace all non-ASCII characters with a space (0x20) + :type replace: bool :rtype: MCNP_Problem :raises UnsupportedFeature: If an input format is used that MontePy does not support. :raises MalformedInputError: If an input has a broken syntax. @@ -23,5 +26,5 @@ def read_input(input_file, mcnp_version=DEFAULT_VERSION): """ problem = mcnp_problem.MCNP_Problem(input_file) problem.mcnp_version = mcnp_version - problem.parse_input() + problem.parse_input(replace=replace) return problem diff --git a/montepy/input_parser/input_syntax_reader.py b/montepy/input_parser/input_syntax_reader.py index 540164a0..e6cf1b9e 100644 --- a/montepy/input_parser/input_syntax_reader.py +++ b/montepy/input_parser/input_syntax_reader.py @@ -15,7 +15,7 @@ reading_queue = [] -def read_input_syntax(input_file, mcnp_version=DEFAULT_VERSION): +def read_input_syntax(input_file, mcnp_version=DEFAULT_VERSION, replace=True): """ Creates a generator function to return a new MCNP input for every new one that is encountered. @@ -30,12 +30,14 @@ def read_input_syntax(input_file, mcnp_version=DEFAULT_VERSION): :type input_file: MCNP_InputFile :param mcnp_version: The version of MCNP that the input is intended for. :type mcnp_version: tuple + :param replace: replace all non-ASCII characters with a space (0x20) + :type replace: bool :returns: a generator of MCNP_Object objects :rtype: generator """ global reading_queue reading_queue = deque() - with input_file.open("r") as fh: + with input_file.open("r", replace=replace) as fh: yield from read_front_matters(fh, mcnp_version) yield from read_data(fh, mcnp_version) @@ -186,7 +188,7 @@ def flush_input(): ): yield from flush_input() # die if it is a vertical syntax format - if "#" in line[0:BLANK_SPACE_CONTINUE]: + if "#" in line[0:BLANK_SPACE_CONTINUE] and not line_is_comment: raise errors.UnsupportedFeature("Vertical Input format is not allowed") # cut line down to allowed length old_line = line diff --git a/montepy/input_parser/syntax_node.py b/montepy/input_parser/syntax_node.py index b6c1b6d1..496a7280 100644 --- a/montepy/input_parser/syntax_node.py +++ b/montepy/input_parser/syntax_node.py @@ -222,6 +222,7 @@ class GeometryTree(SyntaxNodeBase): def __init__(self, name, tokens, op, left, right=None): super().__init__(name) + assert all(list(map(lambda v: isinstance(v, SyntaxNodeBase), tokens.values()))) self._nodes = tokens self._operator = Operator(op) self._left_side = left @@ -241,19 +242,8 @@ def format(self): @property def comments(self): - for key, node in self.nodes.items(): - if isinstance(node, SyntaxNodeBase): - yield from node.comments - elif isinstance(node, dict): - yield from GeometryTree._recurse_comments(node) - - @staticmethod - def _recurse_comments(tree): - for node in tree.values(): - if isinstance(node, SyntaxNodeBase): - yield from node.comments - elif isinstance(node, dict): - yield from GeometryTree._recurse_comments(node) + for node in self.nodes.values(): + yield from node.comments @property def left(self): diff --git a/montepy/mcnp_problem.py b/montepy/mcnp_problem.py index 7e4d8f3e..16b8426e 100644 --- a/montepy/mcnp_problem.py +++ b/montepy/mcnp_problem.py @@ -232,12 +232,14 @@ def transforms(self): """ return self._transforms - def parse_input(self, check_input=False): + def parse_input(self, check_input=False, replace=True): """ Semantically parses the MCNP file provided to the constructor. :param check_input: If true, will try to find all errors with input and collect them as warnings to log. :type check_input: bool + :param replace: replace all non-ASCII characters with a space (0x20) + :type replace: bool """ trailing_comment = None last_obj = None @@ -252,7 +254,7 @@ def parse_input(self, check_input=False): try: for i, input in enumerate( input_syntax_reader.read_input_syntax( - self._input_file, self.mcnp_version + self._input_file, self.mcnp_version, replace=replace ) ): self._original_inputs.append(input) diff --git a/pyproject.toml b/pyproject.toml index 6dd48dad..9fab1856 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,13 +30,25 @@ classifiers = [ ] dependencies = [ - "numpy", + "numpy>=1.18", "sly==0.5" ] [project.optional-dependencies] -test = ["coverage", "pytest"] +test = ["coverage[toml]>=6.3.2", "pytest"] doc = ["sphinx", "sphinxcontrib-apidoc", "sphinx_rtd_theme"] +format = ["black>=23.3.0"] +build = [ + "build", + "setuptools_scm>=8", +] +develop = [ + "montepy[test]", + "montepy[doc]", + "montepy[format]", + "montepy[build]", +] + [project.urls] Homepage = "https://idaholab.github.io/MontePy/index.html" @@ -44,18 +56,21 @@ Repository = "https://github.com/idaholab/montepy.git" Documentation = "https://idaholab.github.io/MontePy/index.html" "Bug Tracker" = "https://github.com/idaholab/MontePy/issues" +[project.scripts] +"change_to_ascii" = "montepy._scripts.change_to_ascii:main" + [build-system] -requires = ["setuptools >= 61.0.0"] +requires = ["setuptools >= 64.0.0", "setuptools_scm>=8"] build-backend = "setuptools.build_meta" +[tool.setuptools_scm] +version_file = "montepy/_version.py" + [tool.setuptools.packages.find] include = ["montepy*"] -[tool.setuptools.dynamic] -version = {attr = "montepy.__version__"} - [tool.coverage.run] -source = ["montepy"] +omit = ["montepy/_version.py","tests/*"] [tool.coverage.report] precision = 2 @@ -69,3 +84,5 @@ exclude_also = [ [tool.pytest.ini_options] minversion = "6.0" junit_logging = "all" +junit_family="xunit2" +filterwarnings="error" diff --git a/pytest.ini b/pytest.ini deleted file mode 100644 index 731f942b..00000000 --- a/pytest.ini +++ /dev/null @@ -1,3 +0,0 @@ -[pytest] -junit_family=xunit2 -filterwarnings=error diff --git a/requirements/common.txt b/requirements/common.txt deleted file mode 100644 index 97e31a82..00000000 --- a/requirements/common.txt +++ /dev/null @@ -1,2 +0,0 @@ -numpy >= 1.18 -sly >= 0.5.0 diff --git a/requirements/dev.txt b/requirements/dev.txt deleted file mode 100644 index c73727e3..00000000 --- a/requirements/dev.txt +++ /dev/null @@ -1,8 +0,0 @@ --r common.txt -build -sphinx~=4.3 -sphinxcontrib-apidoc -sphinx_rtd_theme -coverage[toml]>=6.3.2 -pytest -black==23.3.0 diff --git a/templates/install.yml b/templates/install.yml deleted file mode 100644 index 8ccea7bb..00000000 --- a/templates/install.yml +++ /dev/null @@ -1,37 +0,0 @@ -variables: - PYTHON_VER: "3.8" - CONDA_BASE_NAME: experiment_analysis_montepy - -.install: - script: - - if [[ -d dist ]]; then rm -r dist ; fi; - - if [[ `conda env list` == *"$CONDA_BASE_NAME-$PYTHON_VER"* ]]; then - - else - - conda create -n $CONDA_BASE_NAME-$PYTHON_VER -y python==$PYTHON_VER - - fi - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - echo "Installing dependencies" - - which python - - which pip - - pip uninstall -y montepy - # make alternate package to confuse the builder. - - if [[ -d fake_py ]]; then mkdir fake_py; fi; - - if [[ -f fake_py/__init__.py ]]; then - - touch fake_py/__init__.py - - fi - # install base requirements first - - pip install -r requirements/common.txt - # needed to build. Users shouldn't be doing this on their own. - # Want to make sure it can be installed without dev stuff. - - pip install build - - python -m build --sdist --wheel - - pip install . - - pip uninstall -y montepy - - pip install --user dist/*.whl - # test the extras installation - - pip install --user montepy[test] - - pip install --user montepy[doc] - - pip freeze - artifacts: - paths: - - dist/ diff --git a/templates/test.yml b/templates/test.yml deleted file mode 100644 index 5284fd9b..00000000 --- a/templates/test.yml +++ /dev/null @@ -1,15 +0,0 @@ -variables: - PYTHON_VER: "3.8" - CONDA_BASE_NAME: experiment_analysis_montepy - -.test_no_cover: - script: - - conda activate $CONDA_BASE_NAME-$PYTHON_VER - - export PATH="~/.local/bin/:/data/gitlab-runner/.conda/envs/$CONDA_BASE_NAME-$PYTHON_VER/bin:$PATH" - - which python - - pip freeze - - python -m pytest --junitxml=test_report.xml - artifacts: - reports: - junit: test_report.xml - diff --git a/tests/constants.py b/tests/constants.py index 27875fa8..7398f7e4 100644 --- a/tests/constants.py +++ b/tests/constants.py @@ -21,4 +21,11 @@ "testReadRec2.imcnp", "testReadRec3.imcnp", "testReadTarget.imcnp", + "bad_encoding.imcnp", + "unicode.imcnp", +} + +BAD_ENCODING_FILES = { + "bad_encoding.imcnp", + "unicode.imcnp", } diff --git a/tests/inputs/bad_encoding.imcnp b/tests/inputs/bad_encoding.imcnp new file mode 100644 index 00000000..7094afc7 --- /dev/null +++ b/tests/inputs/bad_encoding.imcnp @@ -0,0 +1,52 @@ +MESSAGE: this is a message +it should show up at the beginning +foo +Note: this file in encoded in "extended ascii": "CP-1252" + +MCNP Test Model for MOAA +C cells +c +1 1 20 + -1000 $ dollar comment + imp:n,p=1 U=350 trcl=5 +2 2 8 + -1005 + imp:n=1 + imp:p=0.5 +3 3 -1 + 1000 1005 -1010 + imp:n,p=1 +99 0 + 1010 + imp:n,p=0 +5 0 + #99 + imp:n,p=3 fill=350 (1 0 0 ) +c foo end comment + +C surfaces +1000 SO 1 +1005 RCC 0 1.5 -0.5 0 0 1 0.25 +1010 SO 3 + +C data +C materials +C UO2 5 atpt enriched +m1 92235.80c 5 & +92238.80c 95 +C Iron +m2 26054.80c 5.85 + 26056.80c 91.75 + 26057.80c 2.12 + 26058.80c 0.28 +C water +C foo +m3 1001.80c 2 + 8016.80c 1 +MT3 lwtr.23t h-zr.20t h/zr.28t +C execution +ksrc 0 0 0 +kcode 100000 1.000 50 1050 +phys:p j 1 2j 1 +mode n p +vol NO 2J 1 1.5 J diff --git a/tests/inputs/test.imcnp b/tests/inputs/test.imcnp index 4d9b70e3..fd0d4cbe 100644 --- a/tests/inputs/test.imcnp +++ b/tests/inputs/test.imcnp @@ -4,6 +4,7 @@ foo MCNP Test Model for MOAA C cells +c # hidden vertical Do not touch c 1 1 20 -1000 $ dollar comment diff --git a/tests/inputs/unicode.imcnp b/tests/inputs/unicode.imcnp new file mode 100644 index 00000000..7b7f09ba --- /dev/null +++ b/tests/inputs/unicode.imcnp @@ -0,0 +1,52 @@ +MESSAGE: this is a message +it should show up at the beginning +foo + +MCNP Test Model for MOAA +C cells 🔴⚪🐍 MontePy is great +c +1 1 20 + -1000 $ dollar comment + imp:n,p=1 U=350 trcl=5 +2 2 8 + -1005 + imp:n=1 + imp:p=0.5 +3 3 -1 + 1000 1005 -1010 + imp:n,p=1 +99 0 + 1010 + imp:n,p=0 +5 0 + #99 + imp:n,p=3 fill=350 (1 0 0 ) +c foo end comment + +C surfaces +1000 SO 1 +1005 RCC 0 1.5 -0.5 0 0 1 0.25 +1010 SO 3 + +C data +C materials +C UO2 5 atpt enriched +m1 92235.80c 5 & +92238.80c 95 +C Iron +m2 26054.80c 5.85 + 26056.80c 91.75 + 26057.80c 2.12 + 26058.80c 0.28 +C water +C foo +m3 1001.80c 2 + 8016.80c 1 +MT3 lwtr.23t h-zr.20t h/zr.28t +C execution +ksrc 0 0 0 +kcode 100000 1.000 50 1050 +phys:p j 1 2j 1 +mode n p +vol NO 2J 1 1.5 J + diff --git a/tests/test_edge_cases.py b/tests/test_edge_cases.py index 9285e4e7..fabcf147 100644 --- a/tests/test_edge_cases.py +++ b/tests/test_edge_cases.py @@ -176,3 +176,4 @@ def test_geometry_comments(self): cell = montepy.Cell(input) # this step caused an error for #163 cell.comments + cell._tree.format() diff --git a/tests/test_geometry.py b/tests/test_geometry.py index 32c0a3ff..661e474f 100644 --- a/tests/test_geometry.py +++ b/tests/test_geometry.py @@ -10,7 +10,7 @@ class TestHalfSpaceUnit(TestCase): def test_init(self): surface = montepy.surfaces.CylinderOnAxis() - node = montepy.input_parser.syntax_node.GeometryTree("hi", [], "*", " ", " ") + node = montepy.input_parser.syntax_node.GeometryTree("hi", {}, "*", " ", " ") half_space = HalfSpace(+surface, Operator.UNION, -surface, node) self.assertIs(half_space.operator, Operator.UNION) self.assertEqual(half_space.left, +surface) diff --git a/tests/test_integration.py b/tests/test_integration.py index a43836b5..c4ba5afb 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -138,6 +138,10 @@ def test_write_to_file(self): for i, data in enumerate(self.simple_problem.data_inputs): if isinstance(data, material.Material): self.assertEqual(data.number, test_problem.data_inputs[i].number) + if data.thermal_scattering is not None: + assert ( + test_problem.data_inputs[i].thermal_scattering is not None + ) elif isinstance(data, volume.Volume): self.assertEqual(str(data), str(test_problem.data_inputs[i])) else: @@ -342,6 +346,7 @@ def test_cell_card_pass_through(self): # test input pass-through answer = [ "C cells", + "c # hidden vertical Do not touch", "c", "1 1 20", " -1000 $ dollar comment", @@ -354,20 +359,20 @@ def test_cell_card_pass_through(self): cell = new_prob.cells[1] output = cell.format_for_mcnp_input((6, 2, 0)) print(output) - self.assertEqual(int(output[3].split("$")[0]), -5) + self.assertEqual(int(output[4].split("$")[0]), -5) # test mass density printer cell.mass_density = 10.0 with self.assertWarns(LineExpansionWarning): output = cell.format_for_mcnp_input((6, 2, 0)) print(output) - self.assertAlmostEqual(float(output[2].split()[2]), -10) + self.assertAlmostEqual(float(output[3].split()[2]), -10) # ensure that surface number updated # Test material number change new_prob = copy.deepcopy(problem) new_prob.materials[1].number = 5 cell = new_prob.cells[1] output = cell.format_for_mcnp_input((6, 2, 0)) - self.assertEqual(int(output[2].split()[1]), 5) + self.assertEqual(int(output[3].split()[1]), 5) def test_thermal_scattering_pass_through(self): problem = copy.deepcopy(self.simple_problem) @@ -1031,3 +1036,12 @@ def test_expansion_warning_crash(self): os.remove(out) except FileNotFoundError: pass + + def test_alternate_encoding(self): + with self.assertRaises(UnicodeDecodeError): + problem = montepy.read_input( + os.path.join("tests", "inputs", "bad_encoding.imcnp"), replace=False + ) + problem = montepy.read_input( + os.path.join("tests", "inputs", "bad_encoding.imcnp"), replace=True + ) diff --git a/tests/test_scripts.py b/tests/test_scripts.py new file mode 100644 index 00000000..aa68de76 --- /dev/null +++ b/tests/test_scripts.py @@ -0,0 +1,88 @@ +import itertools +from unittest import TestCase +from tests import constants +import os +import subprocess + + +class TestChangeAsciiScript(TestCase): + @classmethod + def setUpClass(cls): + new_files = {} + for input_file in constants.BAD_ENCODING_FILES: + new_files[input_file] = {} + for flag in {"-w", "-d"}: + new_file = f"{input_file}{flag}.imcnp" + cls.run_script( + [flag, os.path.join("tests", "inputs", input_file), new_file] + ) + new_files[input_file][flag] = new_file + cls.files = new_files + + @classmethod + def tearDownClass(cls): + for group in cls.files.values(): + for file_name in group.values(): + try: + os.remove(file_name) + except FileNotFoundError: + pass + + @staticmethod + def run_script(args): + return subprocess.run( + ["python", os.path.join("montepy", "_scripts", "change_to_ascii.py")] + args + ) + + def test_delete_bad(self): + for in_file in self.files: + with open(os.path.join("tests", "inputs", in_file), "rb") as in_fh, open( + self.files[in_file]["-d"], "rb" + ) as out_fh: + for in_line, out_line in zip(in_fh, out_fh): + try: + in_line.decode("ascii") + self.assertEqual(in_line, out_line) + except UnicodeError: + new_line = [] + for char in in_line: + if char <= 128: + new_line.append(chr(char)) + self.assertEqual("".join(new_line), out_line.decode("ascii")) + + def test_whitespace_bad(self): + for in_file in self.files: + with open(os.path.join("tests", "inputs", in_file), "rb") as in_fh, open( + self.files[in_file]["-w"], "rb" + ) as out_fh: + for in_line, out_line in zip(in_fh, out_fh): + try: + in_line.decode("ascii") + self.assertEqual(in_line, out_line) + except UnicodeError: + new_line = [] + try: + # try to change to utf-8 + for char in in_line.decode(): + if ord(char) <= 128: + new_line.append(char) + else: + new_line.append(" ") + except UnicodeError: + # try to go bit by bit + for char in in_line: + if char <= 128: + new_line.append(chr(char)) + else: + new_line.append(" ") + self.assertEqual("".join(new_line), out_line.decode("ascii")) + + def test_bad_arguments(self): + ret_code = self.run_script( + [ + "-w", + "-d", + os.path.join("tests", "inputs", "bad_encode.imcnp", "foo.imcnp"), + ] + ) + self.assertNotEqual(ret_code.returncode, 0)