From e74e82bb5bb290e2f8a256a1baec03ff3232dbc1 Mon Sep 17 00:00:00 2001 From: Charles Staats III Date: Sat, 28 Dec 2024 17:47:51 -0800 Subject: [PATCH] Fix pylint errors. --- asy-list.py | 6 ++-- findsym.py | 88 +++++++++++++++++++++++----------------------------- keywords.py | 9 +++--- opsymbols.py | 5 +-- 4 files changed, 49 insertions(+), 59 deletions(-) diff --git a/asy-list.py b/asy-list.py index c7271e1a5..252364301 100644 --- a/asy-list.py +++ b/asy-list.py @@ -13,7 +13,7 @@ import textwrap # Open the file 'asy-keywords.el' for writing. -with open("asy-keywords.el", "w") as keywords: +with open("asy-keywords.el", "w", encoding="ascii") as keywords: # Write header information to 'asy-keywords.el'. # This includes comments and the definition of 'asy-keywords-version' using a @@ -39,7 +39,7 @@ def add(keyword): keywords.write("(defvar asy-keyword-name '(\n") # Open the file 'camp.l' for reading. - with open("camp.l", "r") as camp: + with open("camp.l", "r", encoding="ascii") as camp: # Read lines from 'camp.l' until reaching a line that contains only '%%'. for line in camp: @@ -60,7 +60,7 @@ def add(keyword): keywords.write(match.group(1) + " ") # Open an input file specified in the command-line arguments. - with open(sys.argv[1], "r") as asylist: + with open(sys.argv[1], "r", encoding="ascii") as asylist: # Lists to store types, functions, and variables found in the file. types = [] # List to hold data types. diff --git a/findsym.py b/findsym.py index 01ce6cb5d..dbc6c409e 100644 --- a/findsym.py +++ b/findsym.py @@ -26,59 +26,49 @@ _, outname, *innames = sys.argv # Attempt to open the output file in write mode. -header = open(outname, "w") +with open(outname, "w", encoding="ascii") as header: -# Write a predefined header comment block to the output file. -header.write( - textwrap.dedent( - """\ - /***** - * This file is automatically generated by findsym.py - * Changes will be overwritten. - *****/ + # Write a predefined header comment block to the output file. + header.write( + textwrap.dedent( + """\ + /***** + * This file is automatically generated by findsym.py + * Changes will be overwritten. + *****/ - // If the ADDSYMBOL macro is not already defined, define it with the default - // purpose of referring to an external pre-translated symbol, such that - // SYM(name) also refers to that symbol. - #ifndef ADDSYMBOL - #define ADDSYMBOL(name) extern sym::symbol PRETRANSLATED_SYMBOL_##name - #define SYM(name) PRETRANSLATED_SYMBOL_##name - #endif + // If the ADDSYMBOL macro is not already defined, define it with the default + // purpose of referring to an external pre-translated symbol, such that + // SYM(name) also refers to that symbol. + #ifndef ADDSYMBOL + #define ADDSYMBOL(name) extern sym::symbol PRETRANSLATED_SYMBOL_##name + #define SYM(name) PRETRANSLATED_SYMBOL_##name + #endif - """ + """ + ) ) -) + # Initialize an empty set to store unique symbols. + symbols = set() -# Define a function named 'add' that takes a symbol name as an argument. -def add(symbol): - # Concatenate the ADDSYMBOL macro with the provided symbol name and a - # newline character, then write it to the header file. - header.write(f"ADDSYMBOL({symbol});\n") + # Iterate over each remaining command-line argument, which should be the names + # of C++ source files to process. + for inname in innames: + # Attempt to open the current input file in read mode. + with open(inname, "r", encoding="ascii") as infile: + # Read the input file line by line. + for line in infile: + # Use a regular expression to find all occurrences of the SYM macro + # with a valid symbol name inside parentheses. + matches = re.findall(r"SYM\(([_A-Za-z][_A-Za-z0-9]*)\)", line) + for match in matches: + # Add each matched symbol name to the symbols set. + symbols.add(match) - -# Initialize an empty set to store unique symbols. -symbols = set() - -# Iterate over each remaining command-line argument, which should be the names -# of C++ source files to process. -for inname in innames: - # Attempt to open the current input file in read mode. - with open(inname, "r") as infile: - # Read the input file line by line. - for line in infile: - # Use a regular expression to find all occurrences of the SYM macro - # with a valid symbol name inside parentheses. - matches = re.findall(r"SYM\(([_A-Za-z][_A-Za-z0-9]*)\)", line) - for match in matches: - # Add each matched symbol name to the symbols set. - symbols.add(match) - -# After collecting all unique symbols, iterate over the sorted list of symbol -# names. -for symbol in sorted(symbols): - # Call the 'add' function to write its declaration to the header file. - add(symbol) - -# Close the output file to finalize and save the output. -header.close() + # After collecting all unique symbols, iterate over the sorted list of symbol + # names. + for symbol in sorted(symbols): + # Concatenate the ADDSYMBOL macro with the provided symbol name and a + # newline character, then write it to the header file. + header.write(f"ADDSYMBOL({symbol});\n") diff --git a/keywords.py b/keywords.py index 96966284d..834b7807e 100644 --- a/keywords.py +++ b/keywords.py @@ -9,7 +9,6 @@ import argparse import re -import sys import textwrap from typing import List @@ -26,7 +25,7 @@ # Extra keywords to add that aren't automatically extracted, currently none. extrawords: List[str] = [] -with open(output_keywords_file, "w") as keywords: +with open(output_keywords_file, "w", encoding="ascii") as keywords: keywords.write( textwrap.dedent( @@ -40,13 +39,13 @@ ) ) - def add(word): + def add(word): # pylint: disable=redefined-outer-name keywords.write(f"ADD({word});\n") for word in extrawords: add(word) - with open(camplfile) as camp: + with open(camplfile, encoding="ascii") as camp: # Search for the %% separator, after which the definitions start. for line in camp: if re.search(r"^%%\s*$", line): @@ -61,7 +60,7 @@ def add(word): add(match.group(1)) # Grab the special commands from the interactive prompt. - with open(process_file) as process: + with open(process_file, encoding="ascii") as process: for line in process: match = re.search( r"^\s*ADDCOMMAND\(\s*([A-Za-z_][A-Za-z0-9_]*),", diff --git a/opsymbols.py b/opsymbols.py index 61870913b..bd04a203b 100644 --- a/opsymbols.py +++ b/opsymbols.py @@ -17,8 +17,9 @@ args = parser.parse_args() # Open output file and write header -with open(args.output, "w") as header: +with open(args.output, "w", encoding="ascii") as header: header.write( + # pylint: disable=line-too-long textwrap.dedent( """\ /***** @@ -43,7 +44,7 @@ def add(symbol, name): header.write(f'OPSYMBOL("{symbol}", {name});\n') # Open and process campfile - with open(args.campfile, "r") as lexer: + with open(args.campfile, "r", encoding="ascii") as lexer: for line in lexer: match = re.search(r'^"(\S+)"\s*{\s*DEFSYMBOL\((\w+)\);', line) if match: