Browse Source

add split_file

刘凡 2 years ago
parent
commit
c101853a2f
3 changed files with 49 additions and 0 deletions
  1. 0 0
      utils/__init__.py
  2. 12 0
      utils/fileio.py
  3. 37 0
      utils/splitfile.py

+ 0 - 0
utils/__init__.py


+ 12 - 0
utils/fileio.py

@@ -0,0 +1,12 @@
+import os
+
+
+def walk_files(path, endpoint='.py'):
+    file_list = []
+    for root, dirs, files in os.walk(path):
+        for file in files:
+            file_path = os.path.join(root, file)
+            if file_path.endswith(endpoint):
+                file_list.append(file_path)
+
+    return file_list

+ 37 - 0
utils/splitfile.py

@@ -0,0 +1,37 @@
+import ast
+import os
+
+from utils.fileio import walk_files
+
+
+def write_content_to_file(content, file_path):
+    if not os.path.exists(file_path[:file_path.rindex("/")]):
+        os.makedirs(file_path[:file_path.rindex("/")])
+    with open(file_path, 'w') as file_out:
+        file_out.write(content)
+
+
+def split_file(file_dir, output_dir, endpoint=".py"):
+    if not os.path.exists(output_dir):
+        os.makedirs(output_dir)
+    for file_path in walk_files(file_dir, endpoint):
+        with open(file_path, 'r') as file:
+            content = file.read()
+        root = ast.parse(content)
+        file_id = 1
+        for node in root.body:
+            if isinstance(node, ast.FunctionDef):
+                new_file_name = file_path.replace(file_dir, output_dir).replace(".py", "_" + str(file_id) + ".py")
+                write_content_to_file(ast.get_source_segment(content, node), new_file_name)
+                file_id += 1
+            elif isinstance(node, ast.ClassDef):
+                for son in node.body:
+                    if isinstance(son, ast.FunctionDef):
+                        new_file_name = file_path.replace(file_dir, output_dir).replace(".py",
+                                                                                        "_" + str(file_id) + ".py")
+                        write_content_to_file(ast.get_source_segment(content, son), new_file_name)
+                        file_id += 1
+
+
+if __name__ == '__main__':
+    split_file("/Users/liufan/program/PYTHON/sap2nd/Data/archive", "../target/archive")