← back to Exo
format format.py
cb217b7b774543f123111e7b8d65ffe7d5acd792 · 2024-07-27 19:57:18 -0700 · Alex Cheema
Files touched
Diff
commit cb217b7b774543f123111e7b8d65ffe7d5acd792
Author: Alex Cheema <alexcheema123@gmail.com>
Date: Sat Jul 27 19:57:18 2024 -0700
format format.py
---
format.py | 145 ++++++++++++++++++++++++++++++++------------------------------
1 file changed, 75 insertions(+), 70 deletions(-)
diff --git a/format.py b/format.py
index fcf2fd54..7586bbf2 100644
--- a/format.py
+++ b/format.py
@@ -10,96 +10,101 @@ PLACEHOLDER = "###DEBUG_PLACEHOLDER###"
# Add ignore patterns here
IGNORE_PATTERNS = [
- '.venv/*',
- 'setup.py',
- '*helpers.py',
- '*node_service_pb2.py',
- '*node_service_pb2_grpc.py',
+ '.venv/*',
+ 'setup.py',
+ '*helpers.py',
+ '*node_service_pb2.py',
+ '*node_service_pb2_grpc.py',
]
+
def should_ignore(file_path):
- for pattern in IGNORE_PATTERNS:
- if fnmatch.fnmatch(file_path, pattern):
- return True
- return False
+ for pattern in IGNORE_PATTERNS:
+ if fnmatch.fnmatch(file_path, pattern):
+ return True
+ return False
+
def preserve_debug_lines(content):
- def replace(match):
- indent, line = match.groups()
- return f"{indent}{PLACEHOLDER}{line.strip()}"
- return DEBUG_PATTERN.sub(replace, content)
+ def replace(match):
+ indent, line = match.groups()
+ return f"{indent}{PLACEHOLDER}{line.strip()}"
+
+ return DEBUG_PATTERN.sub(replace, content)
+
def restore_debug_lines(content):
- return re.sub(f"^(\\s*){PLACEHOLDER}(.+)$", r"\1\2", content, flags=re.MULTILINE)
+ return re.sub(f"^(\\s*){PLACEHOLDER}(.+)$", r"\1\2", content, flags=re.MULTILINE)
+
def adjust_indentation(content):
- lines = content.split('\n')
- adjusted_lines = []
- for line in lines:
- if line.strip() and not line.startswith(PLACEHOLDER):
- indent = len(line) - len(line.lstrip())
- new_indent = ' ' * (indent // 2)
- adjusted_lines.append(new_indent + line.lstrip())
- else:
- adjusted_lines.append(line)
- return '\n'.join(adjusted_lines)
+ lines = content.split('\n')
+ adjusted_lines = []
+ for line in lines:
+ if line.strip() and not line.startswith(PLACEHOLDER):
+ indent = len(line) - len(line.lstrip())
+ new_indent = ' ' * (indent // 2)
+ adjusted_lines.append(new_indent + line.lstrip())
+ else:
+ adjusted_lines.append(line)
+ return '\n'.join(adjusted_lines)
+
def process_file(file_path, process_func):
- with open(file_path, 'r') as file:
- content = file.read()
+ with open(file_path, 'r') as file:
+ content = file.read()
- modified_content = process_func(content)
+ modified_content = process_func(content)
- if content != modified_content:
- with open(file_path, 'w') as file:
- file.write(modified_content)
+ if content != modified_content:
+ with open(file_path, 'w') as file:
+ file.write(modified_content)
-def run_black(target):
- # Convert ignore patterns to Black's --extend-exclude format
- exclude_patterns = '|'.join(f'({pattern.replace("*", ".*")})' for pattern in IGNORE_PATTERNS)
- command = [
- "black",
- "--line-length", "200",
- "--extend-exclude", exclude_patterns,
- target
- ]
- subprocess.run(command, check=True)
-def format_files(target):
- if os.path.isfile(target):
- files = [target] if not should_ignore(target) else []
- elif os.path.isdir(target):
- files = []
- for root, _, filenames in os.walk(target):
- for filename in filenames:
- if filename.endswith('.py'):
- file_path = os.path.join(root, filename)
- if not should_ignore(file_path):
- files.append(file_path)
- else:
- print(f"Error: {target} is not a valid file or directory")
- return
+def run_black(target):
+ # Convert ignore patterns to Black's --extend-exclude format
+ exclude_patterns = '|'.join(f'({pattern.replace("*", ".*")})' for pattern in IGNORE_PATTERNS)
+ command = ["black", "--line-length", "200", "--extend-exclude", exclude_patterns, target]
+ subprocess.run(command, check=True)
- # Preserve debug lines
- for file in files:
- process_file(file, preserve_debug_lines)
- # Run Black
- run_black(target)
+def format_files(target):
+ if os.path.isfile(target):
+ files = [target] if not should_ignore(target) else []
+ elif os.path.isdir(target):
+ files = []
+ for root, _, filenames in os.walk(target):
+ for filename in filenames:
+ if filename.endswith('.py'):
+ file_path = os.path.join(root, filename)
+ if not should_ignore(file_path):
+ files.append(file_path)
+ else:
+ print(f"Error: {target} is not a valid file or directory")
+ return
+
+ # Preserve debug lines
+ for file in files:
+ process_file(file, preserve_debug_lines)
+
+ # Run Black
+ run_black(target)
+
+ # Adjust indentation and restore debug lines
+ for file in files:
+ process_file(file, adjust_indentation)
+ process_file(file, restore_debug_lines)
- # Adjust indentation and restore debug lines
- for file in files:
- process_file(file, adjust_indentation)
- process_file(file, restore_debug_lines)
def main():
- if len(sys.argv) < 2:
- print("Usage: python format.py <directory_or_file>")
- sys.exit(1)
+ if len(sys.argv) < 2:
+ print("Usage: python format.py <directory_or_file>")
+ sys.exit(1)
+
+ target = sys.argv[1]
+ format_files(target)
+ print("Formatting completed.")
- target = sys.argv[1]
- format_files(target)
- print("Formatting completed.")
if __name__ == "__main__":
- main()
\ No newline at end of file
+ main()
← 4cb36a7f increase max line length to 200
·
back to Exo
·
update deepseek sanitize to shard layers first before handle a6bb8ddf →