# -*- coding:utf-8 -*-
# server to get a file from selected range
# Get parameters (every param can have n after, f.ex file1=)
# file = URL for file to get
# start = regexp that much match to start printing (default = first line)
# startcnt= int: how many times the start must match before start (default=1)
# startn = int: how many lines to move print forward or backward from start-point (default = 0)
# end = regexp to stop printing (default = last line)
# endcnt = int: how many times the end must match before end (default=1)
# endn = int: how many lines to move end forward or backward from end-point (default = 0)
# linefmt = format for line number, f.exe linefmt={0:03d}%20 (default = "")
# maxn = max number of lines to print (default=10000)
# lastn = last linenumber to print (default=1000000)
# url = if 1, use same url as last file (default=)
# include = after this file is printied, print this text, \n is new line (default="")
# replace = replace every line that match this, by the by-parameter (default="")
# by = by what text is the replace replaced (\n is new line), (default="")
#
# Examples
# ?file=http://example.org/Hello.java&start=main&end=} -> print from main to first }
# ?file=http://example.org/Hello.java -> print whole file
# ?file=http://example.org/Hello.java&start=startn=1&endn=-1 -> print file except first and last line
# ?file=http://example.org/Hello.java&start=main&end=. -> print only the first line where is main
# ?file=http://example.org/Hello.java&start=main&end=.&endn=1
# -> print only the first line where is main and next line
#
import http.server
import socketserver
import time
import math
import sys
sys.path.insert(0, '/py') # /py on mountattu docker kontissa /opt/tim/timApp/modules/py -hakemistoon
from fileParams import * # noqa
PORT = 5000
csstatic = '/service/timApp/modules/cs/static/'
regx_hm = re.compile(r"[ ,/;:.hm]")
def timestr_2_sec2(value):
# compare to timestr_2_sec, this is slower
if not value:
return 0
if isinstance(value, int):
return value
s = "0 0 0 " + str(value).replace("s", "") # loppu s unohdetaan muodosta 1h3m2s
s = regx_hm.sub(" ", s)
s = s.strip()
sc = s.split(" ")
n = len(sc)
h = int(sc[n - 3])
m = int(sc[n - 2])
s = int(sc[n - 1])
return h * 3600.0 + m * 60.0 + s * 1.0
def take_last_number(s, i):
n = 0
k = 1
c = '0'
while i >= 0: # pass non numbers
c = s[i]
if '0' <= c <= '9':
break
i -= 1
while i >= 0:
n += int(c) * k
k *= 10
i -= 1
if i < 0:
break
c = s[i]
if c < '0' or '9' < c:
break
return n, i
def timestr_2_sec(value):
# Tämän ratkaisun vaikutus: 100 000 small videota alku ja loppuajalla
# start: 53
# end: 59 1.2 s
# end: "59" 1.27
# end: "23:45:59" 1.5 s
# timestr_2_sec2 1.8 s
if not value:
return 0
if isinstance(value, int):
return value
st = str(value)
if st.isdigit():
return int(st)
i = len(st) - 1
s, i = take_last_number(st, i)
m, i = take_last_number(st, i)
h, i = take_last_number(st, i)
return h * 3600.0 + m * 60.0 + s * 1.0
def sec_2_timestr(t: float):
# tt = time.gmtime(t) jne on todella hidas
if not t:
return ""
h = math.floor(t / 3600)
t = (t - h * 3600)
m = math.floor(t / 60)
s = int((t - m * 60))
if not h:
h = ""
else:
h = str(h) + "h"
if not h and not m:
m = ""
else:
m = str(m) + "m"
s = str(s) + "s"
return h + m + s
def get_image_md(query):
"""Muodostaa kuvan näyttämiseksi tarvittavan MD-koodin.
:param query: pyynnön paramterit
:return: kuvan md-jono
"""
url = get_clean_param(query, "file", "")
w = get_clean_param(query, "width", "")
h = get_clean_param(query, "height", "")
if w:
w = 'width=' + w + ' '
if h:
h = 'height=' + h + ' '
header, footer = get_surrounding_md_headers2(query, "pluginHeader", None)
result = header + "\n\n" + "![{0}]({1}){{{2}{3}}}".format(footer, url, w, h)
return result
def get_image_html(query):
"""Muodostaa kuvan näyttämiseksi tarvittavan HTML-koodin.
:param query: pyynnön paramterit
:return: kuvan html-jono
"""
url = get_clean_param(query, "file", "")
w = get_clean_param(query, "width", "")
h = get_clean_param(query, "height", "")
if w:
w = 'width="' + w + '" '
if h:
h = 'height="' + h + '" '
header, footer = get_surrounding_headers2(query)
result = header + '' + footer
if is_user_lazy(query):
return add_lazy(result) + header + footer
return NOLAZY + result
def replace_time_params(query, htmlstr):
start = timestr_2_sec(get_param(query, "start", ""))
end = timestr_2_sec(get_param(query, "end", ""))
startt = sec_2_timestr(start)
if startt:
startt = ", " + startt
s = htmlstr.replace("{{startt}}", startt)
duration = sec_2_timestr(end - start)
if duration != "":
duration = "(" + duration + ") "
s = s.replace("{{duration}}", duration)
return s
def small__and_list_html(query, duration_template):
s = replace_template_param(query, '{{stem}}', "stem")
di = replace_template_param(query, ' {{videoname}}', "videoname")
if di:
dur = replace_time_params(query, duration_template)
icon = replace_template_param(
query, '
',
"videoicon", "/csstatic/video_small.png")
s += ' ' + icon + di + dur + ''
di = replace_template_param(query, ' {{doctext}}', "doctext")
if di:
icon = replace_template_param(
query, '
', "docicon", "/csstatic/book.png")
s += ' ' + icon + di + ''
return s
def small_and_list_md(query, duration_template):
s = replace_template_param(query, '{{stem}}', "stem")
di = replace_template_param(query, ' {{videoname}}', "videoname")
if di:
dur = replace_time_params(query, duration_template)
icon = replace_template_param(
query, ' \\includegraphics[width=0.1in]{{{{videoicon}}}}',
"videoicon", csstatic + "video_small.png")
s += '' + icon + di + ' ' + dur + ''
di = replace_template_param(query, ' {{doctext}}', "doctext")
if di:
icon = replace_template_param(
query, '\\includegraphics[width=0.1in]{{{{docicon}}}}', "docicon", csstatic + "book.png")
s += ' ' + icon + di + ''
return s
def small_video_html(query):
# Kokeiltu myös listoilla, ei mitattavasti parempi
s = '
' + small__and_list_html(query, "{{duration}}") + '
' s += replace_template_param(query, '{{stem}}
', "stem") s += '' s += get_file_to_output(query, show_html) if show_html: s += '
' + fn + ''
s = NOLAZY + get_surrounding_headers(query, s) # TODO: korjaa tähän mahdollisuus lazyyyn, oletus on ei.
return s
'''
def keep_running():
return True
def run_while_true(server_class=http.server.HTTPServer,
handler_class=http.server.BaseHTTPRequestHandler):
"""
This assumes that keep_running() is a function of no arguments which
is tested initially and after each request. If its return value
is true, the server continues.
"""
server_address = ('', PORT)
httpd = server_class(server_address, handler_class)
while keep_running():
httpd.handle_request()
if __name__ == '__main__':
run_while_true(handler_class=TIMShowFileServer)
print("svn-plugin waiting for requests...")
'''
# Kun debuggaa Windowsissa, pitää vaihtaa ThreadingMixIn
# Jos ajaa Linuxissa ThreadingMixIn, niin chdir vaihtaa kaikkien hakemistoa?
# Ongelmaa korjattu siten, että kaikki run-kommennot saavat prgpathin käyttöönsä
# if __debug__:
# if True:
class ThreadedHTTPServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
"""Handle requests in a separate thread."""
print("Debug mode/ThreadingMixIn")
#
# else:
# class ThreadedHTTPServer(socketserver.ForkingMixIn, http.server.HTTPServer):
# """Handle requests in a separate thread."""
# print("Normal mode/ForkingMixIn")
if __name__ == '__main__':
server = ThreadedHTTPServer(('', PORT), TIMShowFileServer)
print('Starting ShowFileServer, use