Skip to content

Commit

Permalink
Merge pull request #658 from idaholab/188-do-not-warn-about-comments-…
Browse files Browse the repository at this point in the history
…that-go-over-the-line-length

Do not warn about comments that go over the line length
  • Loading branch information
tjlaboss authored Feb 8, 2025
2 parents 8072131 + bf92f6b commit 6e5a313
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 25 deletions.
1 change: 1 addition & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Fixes # (issue number)

- [ ] I have performed a self-review of my own code.
- [ ] The code follows the standards outlined in the [development documentation](https://idaholab.github.io/MontePy/developing.html).
- [ ] I have formatted my code with `black` version 25.
- [ ] I have added tests that prove my fix is effective or that my feature works (if applicable).

---
Expand Down
2 changes: 2 additions & 0 deletions doc/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ MontePy Changelog
* Added ability to parse all MCNP objects from a string (:issue:`88`).
* Added function: :func:`~montepy.mcnp_problem.MCNP_Problem.parse` to parse arbitrary MCNP object (:issue:`88`).
* An error is now raised when typos in object attributes are used, e.g., ``cell.nubmer`` (:issue:`508`).
* Warnings are no longer raised for comments that exceed the maximum line lengths (:issue:`188`).
* Particle type exceptions are now warnings, not errors (:issue:`381`).


**Bugs Fixed**

* Made it so that a material created from scratch can be written to file (:issue:`512`).
Expand Down
1 change: 1 addition & 0 deletions montepy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
"""MontePy is a library for reading, editing, and writing MCNP input files.
Expand Down
9 changes: 8 additions & 1 deletion montepy/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
import re

from montepy.errors import UnsupportedFeature

"""
Expand All @@ -25,6 +27,11 @@
Number of spaces in a new line before it's considered a continuation.
"""

COMMENT_FINDER = re.compile(rf"\s{{0,{BLANK_SPACE_CONTINUE - 1}}}c", re.IGNORECASE)
"""
A regular expression for finding the start of a ``c`` style comment.
"""

LINE_LENGTH = {
(5, 1, 60): 80,
(6, 1, 0): 80,
Expand Down
28 changes: 18 additions & 10 deletions montepy/input_parser/input_syntax_reader.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
from .block_type import BlockType
# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
from collections import deque
from .. import errors
import itertools
import io
import re
import os
import warnings

from montepy.constants import *
from montepy.errors import *
from montepy.input_parser.block_type import BlockType
from montepy.input_parser.input_file import MCNP_InputFile
from montepy.input_parser.mcnp_input import Input, Message, ReadInput, Title
from montepy.input_parser.read_parser import ReadParser
from montepy.utilities import is_comment
import os
import warnings


reading_queue = []

Expand Down Expand Up @@ -174,15 +176,21 @@ def flush_input():
yield from flush_input()
# die if it is a vertical syntax format
if "#" in line[0:BLANK_SPACE_CONTINUE] and not line_is_comment:
raise errors.UnsupportedFeature("Vertical Input format is not allowed")
raise UnsupportedFeature("Vertical Input format is not allowed")
# cut line down to allowed length
old_line = line
line = line[:line_length]
if len(old_line) != len(line):
warnings.warn(
f"The line: {old_line} exceeded the allowed line length of: {line_length} for MCNP {mcnp_version}",
errors.LineOverRunWarning,
)
if len(line.split("$")[0]) >= line_length and not COMMENT_FINDER.match(
line
):
warnings.warn(
f"The line: {old_line} exceeded the allowed line length of: {line_length} for MCNP {mcnp_version}",
LineOverRunWarning,
)
# if extra length is a comment keep it long
else:
line = old_line
if line.endswith(" &\n"):
continue_input = True
else:
Expand Down
34 changes: 22 additions & 12 deletions montepy/mcnp_object.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024, Battelle Energy Alliance, LLC All Rights Reserved.
# Copyright 2024-2025, Battelle Energy Alliance, LLC All Rights Reserved.
from __future__ import annotations
from abc import ABC, ABCMeta, abstractmethod
import copy
Expand All @@ -14,6 +14,7 @@
from montepy.errors import *
from montepy.constants import (
BLANK_SPACE_CONTINUE,
COMMENT_FINDER,
get_max_line_length,
rel_tol,
abs_tol,
Expand Down Expand Up @@ -324,17 +325,26 @@ def wrap_string_for_mcnp(
for line in strings:
buffer = wrapper.wrap(line)
if len(buffer) > 1:
warning = LineExpansionWarning(
f"The line exceeded the maximum length allowed by MCNP, and was split. The line was:\n{line}"
)
warning.cause = "line"
warning.og_value = line
warning.new_value = buffer
warnings.warn(
warning,
LineExpansionWarning,
stacklevel=2,
)
# don't warn for comments, nor line wrap
# this order assumes that comment overruns are rare
if COMMENT_FINDER.match(line):
buffer = [line]
elif "$" in line:
parts = line.split("$")
buffer = wrapper.wrap(parts[0])
buffer[-1] = "$".join([buffer[-1]] + parts[1:])
else:
warning = LineExpansionWarning(
f"The line exceeded the maximum length allowed by MCNP, and was split. The line was:\n{line}"
)
warning.cause = "line"
warning.og_value = line
warning.new_value = buffer
warnings.warn(
warning,
LineExpansionWarning,
stacklevel=2,
)
# lazy final guard against extra lines
if suppress_blank_end:
buffer = [s for s in buffer if s.strip()]
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ doc = [
"sphinx_autodoc_typehints",
"autodocsumm",
]
format = ["black>=23.3.0"]
format = ["black~=25.1"]
build = [
"build",
"setuptools>=64.0.0",
Expand Down
2 changes: 1 addition & 1 deletion tests/inputs/test.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ C Iron
m2 26054.80c 5.85
plib= 80p
26056.80c 91.75
26057.80c 2.12
26057.80c 2.12 $ very very very very very very very very very very very very very very long line that exceeds line limit
26058.80c 0.28 $ trailing comment shouldn't move #458.
C water
C foo
Expand Down
1 change: 1 addition & 0 deletions tests/inputs/test_universe.imcnp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ c
c foo end comment

C surfaces
c this comment is a ver very very very very very very very very very long line in a comment that shouldn't raise a warning but maybe?
1000 SO 1
1005 RCC 0 1.5 -0.5 0 0 1 0.25
1010 SO 3
Expand Down

0 comments on commit 6e5a313

Please sign in to comment.