Coverage for /home/mattis/projects/websites/dighl/edictor/src/edictor/server.py: 78%

1 from http.server import SimpleHTTPRequestHandler

2

3 from edictor.util import (

4 DATA, get_distinct, get_columns,

5 check, configuration,

6 file_type, file_name, file_handler, triples, download,

7 update, serve_base, new_id, modifications, alignments,

8 cognates, patterns, quit

9 )

10

11 CONF = configuration()

12

13

14 class Handler(SimpleHTTPRequestHandler):

15 """

16 Modified basic class for handling requests in our local server.

17 """

18

19 def do_POST(s):

20 """

21 Do a POST request.

22

23 Note:

24

25 This GIST gave me the tip on how to proceed with POST data.

26

27 https://gist.github.com/scimad/ae0196afc0bade2ae39d604225084507

28 """

29 content_length = int(s.headers['Content-Length'])

30 post_data_bytes = s.rfile.read(content_length)

31

32 ft = file_type(s.path)

33 fn = file_name(s.path)

34

35 if ft in DATA:

36 file_handler(s, ft, fn)

37 return

38

39 fn = file_name(s.path)

40

41 if fn == "/triples/triples.py":

42 triples(s, post_data_bytes, "POST", CONF)

43 if fn == "/download.py":

44 download(s, post_data_bytes)

45 if fn == "/check.py":

46 check(s)

47 if fn == "/triples/update.py":

48 update(s, post_data_bytes, "POST", CONF)

49 if fn == "/triples/new_id.py":

50 new_id(s, post_data_bytes, "POST", CONF)

51 if fn == "/triples/modifications.py":

52 modifications(s, post_data_bytes, "POST", CONF)

53 if fn == "/alignments.py":

54 alignments(s, post_data_bytes, "POST")

55 if fn == "/cognates.py":

56 cognates(s, post_data_bytes, "POST")

57 if fn == "/patterns.py":

58 patterns(s, post_data_bytes, "POST")

59 if fn == "/quit.py":

60 quit(s)

61

62 def do_GET(s):

63 """

64 Do a GET request.

65 """

66

67 ft = file_type(s.path)

68 fn = file_name(s.path)

69

70 if fn == "/":

71 serve_base(s, CONF)

72

73 if ft in DATA:

74 file_handler(s, ft, fn)

75 return

76

77 if fn == "/triples/triples.py":

78 triples(s, s.path, "GET", CONF)

79 if fn == "/triples/update.py":

80 update(s, s.path, "GET", CONF)

81 if fn == "/triples/new_id.py":

82 new_id(s, s.path, "GET", CONF)

83 if fn == "/triples/modifications.py":

84 modifications(s, s.path, "GET", CONF)

85 if fn == "/quit.py":

86 quit(s)