-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_simple.py
More file actions
206 lines (167 loc) · 6.44 KB
/
app_simple.py
File metadata and controls
206 lines (167 loc) · 6.44 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Servidor Flask simplificado e independiente para SHW Reader
Este archivo puede ser compilado con PyInstaller para crear un ejecutable independiente
"""
import os
import sys
import zipfile
import json
import tempfile
import uuid
from pathlib import Path
# Importar Flask y dependencias
try:
from flask import Flask, render_template, request, jsonify, send_file, send_from_directory
from werkzeug.utils import secure_filename
except ImportError as e:
print(f"Error: {e}")
print("Flask no está instalado. Instale Flask con: pip install flask")
sys.exit(1)
# Configuración
app = Flask(__name__, template_folder='templates', static_folder='static')
app.secret_key = 'shw_reader_secret_key_2024'
# Variables globales
UPLOAD_FOLDER = 'uploads'
ALLOWED_EXTENSIONS = {'shw'}
current_data = None
current_filename = None
# Crear directorio de uploads si no existe
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
def allowed_file(filename):
"""Verificar si el archivo tiene una extensión permitida"""
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def process_shw_file(filepath):
"""Procesar archivo SHW y extraer datos"""
try:
channels = []
with zipfile.ZipFile(filepath, 'r') as zip_file:
file_list = zip_file.namelist()
# Buscar archivo de datos principal
for file_name in file_list:
if file_name.endswith('.xml'):
try:
content = zip_file.read(file_name).decode('utf-8', errors='ignore')
# Aquí iría el parser XML específico para SHW
# Por ahora, devolvemos datos de ejemplo
channels = [
{
'id': i + 1,
'name': f'Channel {i + 1}',
'frequency': f'{500 + i}.000',
'group': 'A',
'type': 'Wireless'
}
for i in range(10)
]
break
except Exception as e:
print(f"Error procesando {file_name}: {e}")
continue
return {
'success': True,
'channels': channels,
'total': len(channels),
'filename': os.path.basename(filepath)
}
except Exception as e:
return {
'success': False,
'error': f'Error procesando archivo SHW: {str(e)}'
}
@app.route('/')
def index():
"""Página principal"""
return render_template('index.html')
@app.route('/upload', methods=['POST'])
def upload_file():
"""Subir y procesar archivo SHW"""
global current_data, current_filename
if 'file' not in request.files:
return jsonify({'success': False, 'error': 'No se seleccionó archivo'})
file = request.files['file']
if file.filename == '':
return jsonify({'success': False, 'error': 'No se seleccionó archivo'})
if file and file.filename and allowed_file(file.filename):
filename = secure_filename(file.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(filepath)
# Procesar archivo
result = process_shw_file(filepath)
if result['success']:
current_data = result
current_filename = filename
return jsonify(result)
return jsonify({'success': False, 'error': 'Tipo de archivo no válido'})
@app.route('/data')
def get_data():
"""Obtener datos actuales"""
global current_data
if current_data:
return jsonify(current_data)
return jsonify({'success': False, 'error': 'No hay datos cargados'})
@app.route('/export/<format>')
def export_data(format):
"""Exportar datos en diferentes formatos"""
global current_data, current_filename
if not current_data or not current_data.get('success'):
return jsonify({'success': False, 'error': 'No hay datos para exportar'})
try:
if format == 'csv':
return export_csv()
elif format == 'json':
return export_json()
else:
return jsonify({'success': False, 'error': 'Formato no soportado'})
except Exception as e:
return jsonify({'success': False, 'error': str(e)})
def export_csv():
"""Exportar a CSV"""
import csv
import io
if not current_data or 'channels' not in current_data:
return jsonify({'success': False, 'error': 'No hay datos para exportar'})
output = io.StringIO()
writer = csv.writer(output)
# Escribir encabezados
writer.writerow(['ID', 'Name', 'Frequency', 'Group', 'Type'])
# Escribir datos
for channel in current_data['channels']:
writer.writerow([
channel.get('id', ''),
channel.get('name', ''),
channel.get('frequency', ''),
channel.get('group', ''),
channel.get('type', '')
])
# Crear archivo temporal
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.csv', delete=False)
temp_file.write(output.getvalue())
temp_file.close()
return send_file(temp_file.name, as_attachment=True, download_name=f'{current_filename}_export.csv')
def export_json():
"""Exportar a JSON"""
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False)
json.dump(current_data, temp_file, indent=2)
temp_file.close()
return send_file(temp_file.name, as_attachment=True, download_name=f'{current_filename}_export.json')
@app.route('/static/<path:filename>')
def static_files(filename):
"""Servir archivos estáticos"""
return send_from_directory('static', filename)
def main():
"""Función principal"""
print("=== SHW Reader Backend Server ===")
print("Iniciando servidor Flask...")
print("URL: http://127.0.0.1:5001")
print("Presiona Ctrl+C para detener el servidor")
try:
app.run(host='127.0.0.1', port=5001, debug=False, threaded=True)
except KeyboardInterrupt:
print("\nServidor detenido por el usuario")
except Exception as e:
print(f"Error en el servidor: {e}")
if __name__ == '__main__':
main()