Skip to content

Commit

Permalink
Did hacky lazy encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
MicahGale committed Jan 22, 2024
1 parent 0746fb1 commit 94b3096
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
7 changes: 7 additions & 0 deletions montepy/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://en.wikipedia.org/wiki/ASCII>`_
"""


def get_max_line_length(mcnp_version=DEFAULT_VERSION):
"""
Expand Down
24 changes: 23 additions & 1 deletion montepy/input_parser/input_file.py
Original file line number Diff line number Diff line change
@@ -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 *


Expand All @@ -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")
Expand Down Expand Up @@ -57,7 +60,7 @@ def lineno(self):
"""
pass

def open(self, mode, encoding="ascii"):
def open(self, mode, encoding="ascii", replace=False):
"""
Opens the underlying file, and returns self.
Expand All @@ -79,6 +82,12 @@ def open(self, mode, encoding="ascii"):
:type encoding: str
:returns: self
"""
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

Expand All @@ -94,19 +103,32 @@ 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

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

Expand Down

0 comments on commit 94b3096

Please sign in to comment.