Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from simplejson import dumps, loads, load
- from requests import get, put
- from bs4 import BeautifulSoup
- from html_table_parser import parser_functions as p
- from jinja2 import Template
- from base64 import b64decode
- class Dich:
- global template_body = {
- "id":"{}".format(str(page_id)), "type":"page",
- "title":"{}".format(title),
- "space":{"key":"{}".format(space)},
- "body":
- {"storage":
- {"value":"<table class=\"wrapped\"><colgroup><col /></colgroup><colgroup><col /></colgroup><colgroup><col /></colgroup><colgroup><col /></colgroup><colgroup><col /></colgroup><colgroup><col /></colgroup><tbody><tr><th>123</th><th>323</th><th>Severity</th><th>Credibility</th><th>Relevance</th><th>444</th></tr><tr><td>KillChain</td><td>DIB - 2-level chain</td><td>8</td><td>8</td><td>10</td><td>11</td></tr><tr><td>KillChain</td><td>DIB - 3-level chain</td><td>9</td><td>9</td><td>10</td><td>4</td></tr><tr><td>KillChain</td><td>DIB - 4-level chain</td><td>10</td><td>10</td><td>10</td><td>4</td></tr><tr><td>KillChain</td><td>DIB - 5-level chain</td><td>10</td><td>10</td><td>10</td><td>5</td></tr><tr><td>KillChain</td><td>DIB - 6-level chain</td><td>10</td><td>10</td><td>10</td><td>5</td></tr><tr><td>KillChain</td><td>DIB - 2-level chain by User</td><td>8</td><td>8</td><td>10</td><td>2</td></tr><tr><td>KillChain</td><td>DIB - 3-level chain by User</td><td>9</td><td>9</td><td>10</td><td>3</td></tr><tr><td>KillChain</td><td>DIB - 4-level chain by User</td><td>10</td><td>10</td><td>10</td><td>3</td></tr><tr><td>KillChain</td><td>DIB - 5-level chain by User</td><td>10</td><td>10</td><td>10</td><td>3</td></tr></tbody></table>",
- "representation":"storage"
- }
- },
- "version":{"number":""}}
- def read_config(config):
- with open(config) as params:
- data = load(params)
- page_id = data["page"]["page_id"]
- space = data["page"]["space"]
- title = data["page"]["title"]
- user = data["auth"]["username"]
- password = (b64decode(data["auth"]["password"])).decode("utf-8")
- return(page_id,space,title,user,password)
- def get_body(page_id=page_id, user=user, password=password):
- body = get('https://virgo.ftc.ru/rest/api/content/{}?expand=body.storage'.format(page_id),\
- auth=(user, password))
- json_body = loads(body.content)
- template = json_body['body']['storage']['value']
- soup = BeautifulSoup(template, "lxml")
- table_raw = soup.find('table')
- table_2d = p.make2d(table_raw)
- headers = table_2d[0]
- del table_2d[0]
- collumns = table_2d
- return(headers, collumns)
- #получение версии модификации
- def get_version(page_id=page_id, user=user, password=password):
- version = get('https://virgo.ftc.ru/rest/api/content/{}/history'.format(page_id),\
- auth=(user, password))
- json_version = loads(version.content)
- current_version = json_version["lastUpdated"]["number"]
- return(current_version)
- #отправка данных
- def put_data(data, page_id=page_id, user=user, password=password):
- send = put('https://virgo.ftc.ru/rest/api/content/{}/'.format(page_id),\
- headers=({'Content-Type':'application/json'}),\
- data=dumps(data), auth=(user, password))
- print('answer from server: ' + str(send))
- #обновление шаблона
- def update_html_template(headers, collumns):
- html = open('app/template.html').read()
- template = Template(html)
- count_headers = len(headers)
- data = { 'colgroup': count_headers, 'results': headers, 'collumns':collumns}
- new_html = template.render(data)
- new_html = new_html.replace('\n\n', '').replace('\n', '')
- return(new_html)
- def get_contest():
- headers = get_body()[0]
- collumns = get_body()[1]
- result = []
- for i in collumns:
- tmp = dict(zip(headers, i))
- result.append(tmp)
- print(result)
- def new_header(new):
- headers = get_body()[0]
- collumns = get_body()[1]
- version = int(get_version()) + 1
- headers.append(new)
- new_html = update_html_template(headers, collumns)
- new_data = template_body
- new_data["body"]["storage"]["value"] = new_html
- new_data["version"]["number"] = version
- put_data(new_data)
- def rm_header(old):
- headers = get_body()[0]
- collumns = get_body()[1]
- version = get_version()) + 1
- headers.remove(old)
- new_html = update_html_template(headers, collumns)
- new_data = template_body
- new_data["body"]["storage"]["value"] = new_html
- new_data["version"]["number"] = version
- put_data(new_data)
- def update_header(old, new):
- headers = get_body()[0]
- collumns =g et_body()[1]
- version = int(get_version()) + 1
- index = headers.index(old)
- headers[index] = new
- new_html = update_html_template(headers, collumns)
- new_data = template_body
- new_data["body"]["storage"]["value"] = new_html
- new_data["version"]["number"] = version
- put_data(new_data)
- def update_rule(old, new):
- headers = get_body()[0]
- collumns = get_body()[1]
- version = int(get_version()) + 1
- for collumn in collumns:
- if old in collumn:
- index = collumn.index(old)
- collumn[index] = new
- new_html = update_html_template(headers, collumns)
- new_data = template_body
- new_data["body"]["storage"]["value"] = new_html
- new_data["version"]["number"] = version
- put_data(new_data)
- def update_collumn(rule, header, new):
- headers = get_body()[0]
- collumns = get_body()[1]
- version = int(get_version()) + 1
- count = 0
- for i in headers:
- if header != i:
- count += 1
- else:
- break
- for collumn in collumns:
- if rule in collumn:
- collumn[count] = new
- new_html = update_html_template(headers, collumns)
- new_data = template_body
- new_data["body"]["storage"]["value"] = new_html
- new_data["version"]["number"] = version
- put_data(new_data)
- def find(filters):
- headers = get_body()[0]
- collumns = get_body()[1]
- version = int(get_version()) + 1
- result = []
- for i in collumns:
- sl = dict(zip(headers, i))
- tmp = dict(filters.items() & sl.items())
- if tmp.keys() == filters.keys():
- result.append(i)
- print(result)
- def set_function(tmp):
- key = tmp[0]
- position = tmp[1]
- if key == 'update':
- if position == 'header':
- update_header(tmp[3], tmp[2])
- elif position == 'rule':
- update_rule(tmp[3], tmp[2])
- elif position == 'collumn':
- update_collumn(tmp[5], tmp[4], tmp[2])
- if key == 'rm':
- if position == 'header':
- rm_header(tmp[3])
- if key == 'new':
- if position == 'header':
- new_header(tmp[2])
- if key == 'find':
- find(tmp[6])
- if key == 'get_all':
- get_contest()
- def return_param(data):
- key = data["key"] #0
- position = data.get("params").get("position") #1
- new = data.get("params").get("new") #2
- old = data.get("params").get("old") #3
- header = data.get("params").get("header") #4
- rule = data.get("params").get("rule") #5
- filter = data.get("params").get("filter") #6
- tmp = [key, position, new, old, header, rule, filter]
- set_function(tmp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement