-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
73 lines (61 loc) · 3.04 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from flask import Flask, request, render_template
import os
import json
# Define the paths to the config files
file_types_file = "config/file_types.json"
exclude_folders_file = "config/exclude_folders.json"
# Create an app instance with the name bingo
app = Flask("binGo")
# Define a function that recursively lists all files in a given folder that match the given file types and exclude the given folders
def list_files(folder, file_types, exclude_folders):
# Initialize an empty list to store the file paths
files = []
# Loop through each item in the folder
for item in os.listdir(folder):
# Get the full path of the item
path = os.path.join(folder, item)
# If the item is a file and has an extension that matches the file types, append it to the list
if os.path.isfile(path) and path.endswith(tuple(file_types)):
files.append(path)
# If the item is a directory and is not in the exclude folders, call the function recursively and extend the list
elif os.path.isdir(path) and item not in exclude_folders:
files.extend(list_files(path, file_types, exclude_folders))
# Return the list of files
return files
# Define a route that takes a folder address as a parameter
@app.route("/<path:folder>")
def show_files(folder):
# Get the absolute path of the folder
folder = os.path.abspath(folder)
# Check if the folder exists
if os.path.exists(folder) and os.path.isdir(folder):
# Read the file types file and parse it to a list
with open(file_types_file, "r") as f:
file_types = json.load(f)
# Read the exclude folders file and parse it to a list
with open(exclude_folders_file, "r") as f:
exclude_folders = json.load(f)
# Get the list of files in the folder and its subfolders that match the file types and exclude the folders
files = list_files(folder, file_types, exclude_folders)
# Initialize an empty list to store the file extensions and contents
files_data = []
# Loop through each file
for file in files:
# Get the relative path of the file
relative_path = os.path.relpath(file, folder)
# Open the file and read its content
with open(file, "r", encoding='utf-8-sig') as f:
content = f.read()
# Get the extension of the file
extension = os.path.splitext(file)[1][1:]
# Append the file relative path, extension and content to the list
files_data.append((relative_path, extension, content))
# Render the index template with the folder and files data
return render_template("index.html", folder=folder, files_data=files_data)
# If the folder does not exist, return an error message
else:
return "<H1>Invalid folder address. Please enter a valid folder address.</H1>"
# Return an error if route is blank
@app.route("/")
def error():
return "<H1>Please enter a folder address.</H1>"