Clean up and fix whitespace

This commit is contained in:
Joscha 2019-10-20 20:59:48 +00:00
parent 46b913941d
commit 189282a3f6
6 changed files with 21 additions and 20 deletions

View file

@ -2,12 +2,13 @@ import argparse
import logging import logging
from pathlib import Path from pathlib import Path
from typing import Any from typing import Any
from .config import * from .config import *
from .explore import *
from .known_files import * from .known_files import *
from .process import * from .process import *
from .util import *
from .explore import *
from .prompt import * from .prompt import *
from .util import *
#logging.basicConfig(level=logging.DEBUG, style="{", format="{levelname:>7}: {message}") #logging.basicConfig(level=logging.DEBUG, style="{", format="{levelname:>7}: {message}")
logging.basicConfig(level=logging.INFO, style="{", format="{levelname:>7}: {message}") logging.basicConfig(level=logging.INFO, style="{", format="{levelname:>7}: {message}")

View file

@ -5,7 +5,7 @@ managing local variables.
import copy import copy
import types import types
from typing import Dict, Any from typing import Any, Dict
__all__ = ["copy_local_variables"] __all__ = ["copy_local_variables"]

View file

@ -1,10 +1,10 @@
import logging
from dataclasses import dataclass from dataclasses import dataclass
from pathlib import Path from pathlib import Path
from typing import Dict, List, Optional from typing import Dict, List, Optional
import logging
from .util import *
from .colors import * from .colors import *
from .util import *
__all__ = ["FileInfo", "find_config_files"] __all__ = ["FileInfo", "find_config_files"]
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)

View file

@ -53,7 +53,7 @@ class Parser:
""" """
May raise: ParseException May raise: ParseException
""" """
self.statement_prefix = statement_prefix self.statement_prefix = statement_prefix
self.expression_prefix = expression_prefix self.expression_prefix = expression_prefix
self.expression_suffix = expression_suffix self.expression_suffix = expression_suffix
@ -97,7 +97,7 @@ class Line(ABC):
pass pass
return ActualLine(parser, text, line_number) return ActualLine(parser, text, line_number)
def __init__(self, parser: Parser, line_number: int) -> None: def __init__(self, parser: Parser, line_number: int) -> None:
self.parser = parser self.parser = parser
self.line_number = line_number self.line_number = line_number
@ -112,7 +112,7 @@ class Line(ABC):
def _parse_statement_noarg(self, text: str, statement_name: str) -> bool: def _parse_statement_noarg(self, text: str, statement_name: str) -> bool:
return text.strip() == f"{self.parser.statement_prefix} {statement_name}" return text.strip() == f"{self.parser.statement_prefix} {statement_name}"
class ActualLine(Line): class ActualLine(Line):
def __init__(self, parser: Parser, text: str, line_number: int) -> None: def __init__(self, parser: Parser, text: str, line_number: int) -> None:
""" """
@ -128,7 +128,7 @@ class ActualLine(Line):
argument is the text contained in the chunk and the second argument is the text contained in the chunk and the second
argument a boolean that indicates whether this chunk is a argument a boolean that indicates whether this chunk is a
python expression (or just plain text). python expression (or just plain text).
Because it simplifies the program logic, a chunk's text may Because it simplifies the program logic, a chunk's text may
also be the empty string. also be the empty string.
@ -173,7 +173,7 @@ class ActualLine(Line):
""" """
May raise: ExecuteException May raise: ExecuteException
""" """
if not chunk[1]: if not chunk[1]:
return chunk[0] return chunk[0]
@ -232,7 +232,7 @@ class Block:
""" """
May raise: ParseException May raise: ParseException
""" """
self._elements: List[Union[ActualLine, IfBlock]] = [] self._elements: List[Union[ActualLine, IfBlock]] = []
while lines_queue: while lines_queue:
@ -308,7 +308,7 @@ class IfBlock(Block):
if not isinstance(lines_queue[-1], EndifStatement): if not isinstance(lines_queue[-1], EndifStatement):
raise ParseException.on_line(lines_queue[-1], "Expected 'end' statement") raise ParseException.on_line(lines_queue[-1], "Expected 'end' statement")
lines_queue.pop() lines_queue.pop()
def evaluate(self, local_vars: Dict[str, Any]) -> List[str]: def evaluate(self, local_vars: Dict[str, Any]) -> List[str]:
for entry in self._sections: for entry in self._sections:
if entry[1] is None or safer_eval(entry[1], local_vars): if entry[1] is None or safer_eval(entry[1], local_vars):

View file

@ -37,7 +37,7 @@ class Processor:
raise LessCatastrophicError( raise LessCatastrophicError(
style_error("Could not load file ") + style_error("Could not load file ") +
style_path(path) + f": {e}") style_path(path) + f": {e}")
header, lines = split_header_and_rest(text) header, lines = split_header_and_rest(text)
try: try:
@ -46,7 +46,7 @@ class Processor:
raise LessCatastrophicError( raise LessCatastrophicError(
style_error("Could not parse header of file ") + style_error("Could not parse header of file ") +
style_path(path) + f": {e}") style_path(path) + f": {e}")
self._process_parseable(lines, config) self._process_parseable(lines, config)
def _process_file_with_header(self, path: Path, header_path: Path, config: Config) -> None: def _process_file_with_header(self, path: Path, header_path: Path, config: Config) -> None:

View file

@ -16,10 +16,10 @@ def copy_local_variables(local: Dict[str, Any]) -> Dict[str, Any]:
Attempts to deep-copy a set of local variables, but keeping Attempts to deep-copy a set of local variables, but keeping
modules at the top level alone, since they don't tend to deepcopy modules at the top level alone, since they don't tend to deepcopy
well. well.
May raise: Not sure at the moment May raise: Not sure at the moment
""" """
local_copy = {} local_copy = {}
for key, value in local.items(): for key, value in local.items():
@ -32,12 +32,12 @@ def copy_local_variables(local: Dict[str, Any]) -> Dict[str, Any]:
class ExecuteException(Exception): class ExecuteException(Exception):
pass pass
def safer_exec(code: str, local_vars: Dict[str, Any]) -> None: def safer_exec(code: str, local_vars: Dict[str, Any]) -> None:
""" """
May raise: ExecuteException May raise: ExecuteException
""" """
try: try:
exec(code, {}, local_vars) exec(code, {}, local_vars)
except Exception as e: except Exception as e:
@ -47,7 +47,7 @@ def safer_eval(code: str, local_vars: Dict[str, Any]) -> Any:
""" """
May raise: ExecuteException May raise: ExecuteException
""" """
try: try:
return eval(code, {}, local_vars) return eval(code, {}, local_vars)
except Exception as e: except Exception as e:
@ -60,7 +60,7 @@ def read_file(path: Path) -> str:
""" """
May raise: ReadFileException May raise: ReadFileException
""" """
try: try:
with open(path.expanduser()) as f: with open(path.expanduser()) as f:
return f.read() return f.read()