-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
141 lines (117 loc) · 4.82 KB
/
app.py
File metadata and controls
141 lines (117 loc) · 4.82 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
"""
SkautIS Contacts Converter - Web Application
Author: Lukas Tesar <lukastesar03@gmail.com>
"""
import os
import shutil
from flask import Flask, request, render_template, send_from_directory
from werkzeug.utils import secure_filename
from convert import convert
from git import Repo
app = Flask(__name__)
app.config["DEBUG"] = False
app.config["ENV"] = "production"
app.config.from_object("config")
ALLOWED_EXTENSIONS = set(["xlsx"])
def get_git_version(repo_path="."):
"""Get version info from git (nearest tag + short commit hash) and GitHub URL."""
try:
repo = Repo(repo_path)
hexsha = repo.head.commit.hexsha
hexsha_short = hexsha[:7]
# Try to get the nearest tag
try:
tag = repo.git.describe("--tags", "--abbrev=0")
except Exception:
tag = None
# Get GitHub URL from remote
github_url = None
try:
remote_url = repo.remote("origin").url
# Convert SSH URL to HTTPS
if remote_url.startswith("git@"):
# git@github.com:user/repo.git -> https://github.com/user/repo
remote_url = remote_url.replace(":", "/").replace("git@", "https://").replace(".git", "")
else:
remote_url = remote_url.replace(".git", "")
github_url = remote_url
except Exception:
pass
return tag, hexsha_short, hexsha, github_url
except Exception as e:
print(f"Error retrieving git info: {e}")
return None, None, None, None
def is_allowed_file(filename: str) -> bool:
"""Check if the uploaded file has an allowed extension."""
return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
def delete_output():
"""Delete all files in the output folder except .gitignore."""
folder = "static/output"
if os.path.exists(folder):
for file_name in os.listdir(folder):
if file_name != ".gitignore":
file_path = os.path.join(folder, file_name)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
def delete_input():
"""Delete all files in the input folder except .gitignore."""
folder = "static/input"
if os.path.exists(folder):
for file_name in os.listdir(folder):
if file_name != ".gitignore":
file_path = os.path.join(folder, file_name)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
@app.route("/", methods=["GET", "POST"])
def index():
"""Main route for uploading and converting files."""
delete_output()
version, hexsha_short, hexsha_full, github_url = get_git_version()
error = None
if request.method == "POST":
file = request.files.get("file")
if file and is_allowed_file(file.filename):
filename = secure_filename(file.filename)
input_path = os.path.join("static/input", filename)
file.save(input_path)
# Generate output filename (change extension to .csv)
base_name = os.path.splitext(filename)[0]
output_filename = f"contacts_{base_name}.csv"
output_path = os.path.join("static/output", output_filename)
try:
convert(input_path, output_path)
# Clean up input file
if os.path.exists(input_path):
os.remove(input_path)
return send_from_directory(
"static/output",
output_filename,
as_attachment=True,
download_name=output_filename,
)
except Exception as e:
error = f"Conversion failed: {str(e)}"
# Clean up on error
if os.path.exists(input_path):
os.remove(input_path)
else:
error = "Please upload a valid Excel file (.xlsx)"
return render_template(
"index.jinja", version=version, hexsha_short=hexsha_short, hexsha_full=hexsha_full, github_url=github_url, error=error
)
if __name__ == "__main__":
# Ensure directories exist
os.makedirs("static/input", exist_ok=True)
os.makedirs("static/output", exist_ok=True)
app.run(debug=False, host="0.0.0.0", port=5000)