1
0
forked from ScoDoc/ScoDoc
ScoDoc-Front/tools/fakeportal/fakeportal.py

166 lines
5.1 KiB
Python
Raw Normal View History

2021-08-31 23:31:36 +02:00
#!/usr/bin/env python3
2020-10-21 00:22:25 +02:00
"""Simple fake HTTP serveur
emulating "Apogee" Web service
"""
2020-10-21 22:55:54 +02:00
from pathlib import Path
2021-08-24 09:22:14 +02:00
from urllib.parse import parse_qs
from urllib.parse import urlparse
2020-10-21 00:22:25 +02:00
import http.server
2021-08-24 09:22:14 +02:00
import os
import random
import signal
2020-10-21 00:22:25 +02:00
import socketserver
2021-08-24 09:22:14 +02:00
import sys
import time
2020-10-21 00:22:25 +02:00
2021-08-31 23:31:36 +02:00
from gen_nomprenoms import nomprenom
script_dir = Path(os.path.abspath(__file__)).parent
2021-11-06 17:23:53 +01:00
os.chdir(script_dir)
# Les "photos" des étudiants
2021-11-06 17:23:53 +01:00
FAKE_FACES_PATHS = list((Path("faces").glob("*.jpg")))
2020-10-21 00:22:25 +02:00
# Etudiant avec tous les champs (USPN)
ETUD_TEMPLATE_FULL = open(script_dir / "etud_template.xml").read()
2020-10-21 00:22:25 +02:00
# Etudiant avec seulement les champs requis
ETUD_TEMPLATE_MINI = open(script_dir / "etud_minimal_template.xml").read()
2020-10-21 00:22:25 +02:00
ETUD_HEAD = """<?xml version="1.0" encoding="UTF-8"?>
<etudiants>"""
ETUD_TAIL = """</etudiants>
"""
def make_random_etud(nip, etape=None, annee=None, template=ETUD_TEMPLATE_FULL):
"""return XML for a student"""
random.seed(nip) # deterministic choice based on nip
gender = random.choice(("M", "F"))
nom, prenom = nomprenom(gender)
if not etape:
etape = random.choice(("V1RT", "V2RT", "V2RT2", ""))
if not annee:
annee = time.strftime("%Y") # current year
diplome = "VDRT"
data = template.format(
nip=nip,
gender=gender,
nom=nom,
prenom=prenom,
etape=etape,
diplome=diplome,
annee=annee,
2021-08-31 23:31:36 +02:00
ville_naissance=random.choice(("Paris", "Berlin", "Londres", "")),
code_dep_naissance=random.choice(("75", "99", "89")),
libelle_dep_naissance="nom département",
# nomlycee=
2020-10-21 00:22:25 +02:00
)
return data
def make_random_etape_etuds(etape, annee):
"""Liste d'etudiants d'une etape"""
random.seed(etape + annee)
nb = random.randint(0, 50)
2021-08-21 15:17:14 +02:00
print(f"generating {nb} students")
2020-10-21 00:22:25 +02:00
L = []
for i in range(nb):
if i % 2:
template = ETUD_TEMPLATE_MINI
else:
template = ETUD_TEMPLATE_FULL
nip = str(random.randint(10000000, 99999999)) # 8 digits
L.append(make_random_etud(nip, etape=etape, annee=annee, template=template))
return "\n".join(L)
def get_photo_filename(nip: str) -> str:
"""get an existing filename for a fake photo, found in faces/
Returns a path relative to the current working dir
"""
2020-10-21 22:55:54 +02:00
#
2021-11-06 17:23:53 +01:00
nb_faces = len(FAKE_FACES_PATHS)
if nb_faces == 0:
print("WARNING: aucun fichier image disponible !")
return ""
2021-11-06 17:23:53 +01:00
return FAKE_FACES_PATHS[hash(nip) % nb_faces]
2020-10-21 22:55:54 +02:00
2020-10-21 00:22:25 +02:00
class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def send_xml(self, data):
self.send_response(200)
self.send_header("Content-type", "text/xml;charset=UTF-8")
self.end_headers()
self.wfile.write(bytes(data, "utf8"))
def do_GET(self):
query_components = parse_qs(urlparse(self.path).query)
print(f"path={self.path}", file=sys.stderr)
print(query_components, file=sys.stderr)
if "etapes" in self.path.lower():
2021-09-03 17:09:55 +02:00
self.path = str(Path(script_dir / "etapes.xml").relative_to(Path.cwd()))
elif "scodocEtudiant" in self.path: # API v2
2020-10-21 00:22:25 +02:00
# 2 forms: nip=xxx or etape=eee&annee=aaa
if "nip" in query_components:
nip = query_components["nip"][0]
print(f"requesting nip={nip}")
data = ETUD_HEAD + make_random_etud(nip) + ETUD_TAIL
return self.send_xml(data)
elif "etape" in query_components:
etape = query_components["etape"][0]
2020-10-21 22:55:54 +02:00
print(f"requesting etape={etape}", file=sys.stderr)
2020-10-21 00:22:25 +02:00
if "annee" in query_components:
annee = query_components["annee"][0]
data = ETUD_HEAD + make_random_etape_etuds(etape, annee) + ETUD_TAIL
return self.send_xml(data)
else:
print(
f"Error 404: (missing annee) path={self.path}", file=sys.stderr
)
self.send_response(404)
return
else:
print(
f"Error 404: (missing nip or etape) path={self.path}",
file=sys.stderr,
)
self.send_response(404)
return
2021-12-13 10:20:16 +01:00
elif ("getPhoto" in self.path) or ("scodocPhoto" in self.path):
2020-10-21 22:55:54 +02:00
nip = query_components["nip"][0]
self.path = str(get_photo_filename(nip))
print(f"photo for nip={nip}: {self.path}")
2020-10-21 00:22:25 +02:00
else:
print(f"Error 404: path={self.path}")
self.send_response(404)
return
# Sending an '200 OK' response
self.send_response(200)
http.server.SimpleHTTPRequestHandler.do_GET(self)
return
PORT = 8678
def signal_handler(sig, frame):
print("You pressed Ctrl+C!")
raise SystemExit()
signal.signal(signal.SIGINT, signal_handler)
2020-10-21 00:22:25 +02:00
if __name__ == "__main__":
# Start the server
2020-11-26 10:23:52 +01:00
print(f"Server listening on port {PORT}...")
my_server = socketserver.TCPServer(("", PORT), MyHttpRequestHandler)
try:
my_server.serve_forever()
finally:
print("shutting down...")
2021-11-06 17:23:53 +01:00
my_server.shutdown()